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 "libavutil/avassert.h"
00037 #include "mathops.h"
00038 #include "config.h"
00039
00040 typedef struct PutBitContext {
00041 uint32_t bit_buf;
00042 int bit_left;
00043 uint8_t *buf, *buf_ptr, *buf_end;
00044 int size_in_bits;
00045 } PutBitContext;
00046
00053 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
00054 {
00055 if(buffer_size < 0) {
00056 buffer_size = 0;
00057 buffer = NULL;
00058 }
00059
00060 s->size_in_bits= 8*buffer_size;
00061 s->buf = buffer;
00062 s->buf_end = s->buf + buffer_size;
00063 s->buf_ptr = s->buf;
00064 s->bit_left=32;
00065 s->bit_buf=0;
00066 }
00067
00071 static inline int put_bits_count(PutBitContext *s)
00072 {
00073 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
00074 }
00075
00079 static inline void flush_put_bits(PutBitContext *s)
00080 {
00081 #ifndef BITSTREAM_WRITER_LE
00082 if (s->bit_left < 32)
00083 s->bit_buf<<= s->bit_left;
00084 #endif
00085 while (s->bit_left < 32) {
00086
00087 #ifdef BITSTREAM_WRITER_LE
00088 *s->buf_ptr++=s->bit_buf;
00089 s->bit_buf>>=8;
00090 #else
00091 *s->buf_ptr++=s->bit_buf >> 24;
00092 s->bit_buf<<=8;
00093 #endif
00094 s->bit_left+=8;
00095 }
00096 s->bit_left=32;
00097 s->bit_buf=0;
00098 }
00099
00100 #ifdef BITSTREAM_WRITER_LE
00101 #define avpriv_align_put_bits align_put_bits_unsupported_here
00102 #define avpriv_put_string ff_put_string_unsupported_here
00103 #define avpriv_copy_bits avpriv_copy_bits_unsupported_here
00104 #else
00105
00108 void avpriv_align_put_bits(PutBitContext *s);
00109
00115 void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string);
00116
00122 void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
00123 #endif
00124
00129 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
00130 {
00131 unsigned int bit_buf;
00132 int bit_left;
00133
00134
00135 av_assert2(n <= 31 && value < (1U << n));
00136
00137 bit_buf = s->bit_buf;
00138 bit_left = s->bit_left;
00139
00140
00141
00142 #ifdef BITSTREAM_WRITER_LE
00143 bit_buf |= value << (32 - bit_left);
00144 if (n >= bit_left) {
00145 av_assert2(s->buf_ptr+3<s->buf_end);
00146 AV_WL32(s->buf_ptr, bit_buf);
00147 s->buf_ptr+=4;
00148 bit_buf = (bit_left==32)?0:value >> bit_left;
00149 bit_left+=32;
00150 }
00151 bit_left-=n;
00152 #else
00153 if (n < bit_left) {
00154 bit_buf = (bit_buf<<n) | value;
00155 bit_left-=n;
00156 } else {
00157 bit_buf<<=bit_left;
00158 bit_buf |= value >> (n - bit_left);
00159 av_assert2(s->buf_ptr+3<s->buf_end);
00160 AV_WB32(s->buf_ptr, bit_buf);
00161
00162 s->buf_ptr+=4;
00163 bit_left+=32 - n;
00164 bit_buf = value;
00165 }
00166 #endif
00167
00168 s->bit_buf = bit_buf;
00169 s->bit_left = bit_left;
00170 }
00171
00172 static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
00173 {
00174 av_assert2(n >= 0 && n <= 31);
00175
00176 put_bits(pb, n, value & ((1<<n)-1));
00177 }
00178
00182 static void av_unused put_bits32(PutBitContext *s, uint32_t value)
00183 {
00184 int lo = value & 0xffff;
00185 int hi = value >> 16;
00186 #ifdef BITSTREAM_WRITER_LE
00187 put_bits(s, 16, lo);
00188 put_bits(s, 16, hi);
00189 #else
00190 put_bits(s, 16, hi);
00191 put_bits(s, 16, lo);
00192 #endif
00193 }
00194
00199 static inline uint8_t* put_bits_ptr(PutBitContext *s)
00200 {
00201 return s->buf_ptr;
00202 }
00203
00208 static inline void skip_put_bytes(PutBitContext *s, int n)
00209 {
00210 av_assert2((put_bits_count(s)&7)==0);
00211 av_assert2(s->bit_left==32);
00212 s->buf_ptr += n;
00213 }
00214
00220 static inline void skip_put_bits(PutBitContext *s, int n)
00221 {
00222 s->bit_left -= n;
00223 s->buf_ptr-= 4*(s->bit_left>>5);
00224 s->bit_left &= 31;
00225 }
00226
00232 static inline void set_put_bits_buffer_size(PutBitContext *s, int size)
00233 {
00234 s->buf_end= s->buf + size;
00235 }
00236
00237 #endif