]> git.sesse.net Git - ffmpeg/blob - libavcodec/put_bits.h
b91e0f8f5ab7ccf9739b3bdfb1e74f7b59dc28d4
[ffmpeg] / libavcodec / put_bits.h
1 /*
2  * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * bitstream writer API
24  */
25
26 #ifndef AVCODEC_PUT_BITS_H
27 #define AVCODEC_PUT_BITS_H
28
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include "libavutil/bswap.h"
33 #include "libavutil/common.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/log.h"
36 #include "mathops.h"
37 #include "config.h"
38
39 //#define ALT_BITSTREAM_WRITER
40 //#define ALIGNED_BITSTREAM_WRITER
41
42 /* buf and buf_end must be present and used by every alternative writer. */
43 typedef struct PutBitContext {
44 #ifdef ALT_BITSTREAM_WRITER
45     uint8_t *buf, *buf_end;
46     int index;
47 #else
48     uint32_t bit_buf;
49     int bit_left;
50     uint8_t *buf, *buf_ptr, *buf_end;
51 #endif
52     int size_in_bits;
53 } PutBitContext;
54
55 /**
56  * Initialize the PutBitContext s.
57  *
58  * @param buffer the buffer where to put bits
59  * @param buffer_size the size in bytes of buffer
60  */
61 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
62 {
63     if(buffer_size < 0) {
64         buffer_size = 0;
65         buffer = NULL;
66     }
67
68     s->size_in_bits= 8*buffer_size;
69     s->buf = buffer;
70     s->buf_end = s->buf + buffer_size;
71 #ifdef ALT_BITSTREAM_WRITER
72     s->index=0;
73     AV_ZERO32(s->buf);
74 #else
75     s->buf_ptr = s->buf;
76     s->bit_left=32;
77     s->bit_buf=0;
78 #endif
79 }
80
81 /**
82  * @return the total number of bits written to the bitstream.
83  */
84 static inline int put_bits_count(PutBitContext *s)
85 {
86 #ifdef ALT_BITSTREAM_WRITER
87     return s->index;
88 #else
89     return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
90 #endif
91 }
92
93 /**
94  * Pad the end of the output stream with zeros.
95  */
96 static inline void flush_put_bits(PutBitContext *s)
97 {
98 #ifdef ALT_BITSTREAM_WRITER
99     align_put_bits(s);
100 #else
101 #ifndef BITSTREAM_WRITER_LE
102     s->bit_buf<<= s->bit_left;
103 #endif
104     while (s->bit_left < 32) {
105         /* XXX: should test end of buffer */
106 #ifdef BITSTREAM_WRITER_LE
107         *s->buf_ptr++=s->bit_buf;
108         s->bit_buf>>=8;
109 #else
110         *s->buf_ptr++=s->bit_buf >> 24;
111         s->bit_buf<<=8;
112 #endif
113         s->bit_left+=8;
114     }
115     s->bit_left=32;
116     s->bit_buf=0;
117 #endif
118 }
119
120 #if defined(ALT_BITSTREAM_WRITER) || defined(BITSTREAM_WRITER_LE)
121 #define align_put_bits align_put_bits_unsupported_here
122 #define ff_put_string ff_put_string_unsupported_here
123 #define ff_copy_bits ff_copy_bits_unsupported_here
124 #else
125 /**
126  * Pad the bitstream with zeros up to the next byte boundary.
127  */
128 void align_put_bits(PutBitContext *s);
129
130 /**
131  * Put the string string in the bitstream.
132  *
133  * @param terminate_string 0-terminates the written string if value is 1
134  */
135 void ff_put_string(PutBitContext *pb, const char *string, int terminate_string);
136
137 /**
138  * Copy the content of src to the bitstream.
139  *
140  * @param length the number of bits of src to copy
141  */
142 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
143 #endif
144
145 /**
146  * Write up to 31 bits into a bitstream.
147  * Use put_bits32 to write 32 bits.
148  */
149 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
150 #ifndef ALT_BITSTREAM_WRITER
151 {
152     unsigned int bit_buf;
153     int bit_left;
154
155     //    printf("put_bits=%d %x\n", n, value);
156     assert(n <= 31 && value < (1U << n));
157
158     bit_buf = s->bit_buf;
159     bit_left = s->bit_left;
160
161     //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
162     /* XXX: optimize */
163 #ifdef BITSTREAM_WRITER_LE
164     bit_buf |= value << (32 - bit_left);
165     if (n >= bit_left) {
166         AV_WL32(s->buf_ptr, bit_buf);
167         s->buf_ptr+=4;
168         bit_buf = (bit_left==32)?0:value >> bit_left;
169         bit_left+=32;
170     }
171     bit_left-=n;
172 #else
173     if (n < bit_left) {
174         bit_buf = (bit_buf<<n) | value;
175         bit_left-=n;
176     } else {
177         bit_buf<<=bit_left;
178         bit_buf |= value >> (n - bit_left);
179         AV_WB32(s->buf_ptr, bit_buf);
180         //printf("bitbuf = %08x\n", bit_buf);
181         s->buf_ptr+=4;
182         bit_left+=32 - n;
183         bit_buf = value;
184     }
185 #endif
186
187     s->bit_buf = bit_buf;
188     s->bit_left = bit_left;
189 }
190 #else  /* ALT_BITSTREAM_WRITER defined */
191 {
192 #    ifdef ALIGNED_BITSTREAM_WRITER
193 #        if ARCH_X86
194     __asm__ volatile(
195         "movl %0, %%ecx                 \n\t"
196         "xorl %%eax, %%eax              \n\t"
197         "shrdl %%cl, %1, %%eax          \n\t"
198         "shrl %%cl, %1                  \n\t"
199         "movl %0, %%ecx                 \n\t"
200         "shrl $3, %%ecx                 \n\t"
201         "andl $0xFFFFFFFC, %%ecx        \n\t"
202         "bswapl %1                      \n\t"
203         "orl %1, (%2, %%ecx)            \n\t"
204         "bswapl %%eax                   \n\t"
205         "addl %3, %0                    \n\t"
206         "movl %%eax, 4(%2, %%ecx)       \n\t"
207         : "=&r" (s->index), "=&r" (value)
208         : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
209         : "%eax", "%ecx"
210     );
211 #        else
212     int index= s->index;
213     uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
214
215     value<<= 32-n;
216
217     ptr[0] |= av_be2ne32(value>>(index&31));
218     ptr[1]  = av_be2ne32(value<<(32-(index&31)));
219 //if(n>24) printf("%d %d\n", n, value);
220     index+= n;
221     s->index= index;
222 #        endif
223 #    else //ALIGNED_BITSTREAM_WRITER
224 #        if ARCH_X86
225     __asm__ volatile(
226         "movl $7, %%ecx                 \n\t"
227         "andl %0, %%ecx                 \n\t"
228         "addl %3, %%ecx                 \n\t"
229         "negl %%ecx                     \n\t"
230         "shll %%cl, %1                  \n\t"
231         "bswapl %1                      \n\t"
232         "movl %0, %%ecx                 \n\t"
233         "shrl $3, %%ecx                 \n\t"
234         "orl %1, (%%ecx, %2)            \n\t"
235         "addl %3, %0                    \n\t"
236         "movl $0, 4(%%ecx, %2)          \n\t"
237         : "=&r" (s->index), "=&r" (value)
238         : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
239         : "%ecx"
240     );
241 #        else
242     int index= s->index;
243     uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
244
245     ptr[0] |= av_be2ne32(value<<(32-n-(index&7) ));
246     ptr[1] = 0;
247 //if(n>24) printf("%d %d\n", n, value);
248     index+= n;
249     s->index= index;
250 #        endif
251 #    endif //!ALIGNED_BITSTREAM_WRITER
252 }
253 #endif
254
255 static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
256 {
257     assert(n >= 0 && n <= 31);
258
259     put_bits(pb, n, value & ((1<<n)-1));
260 }
261
262 /**
263  * Write exactly 32 bits into a bitstream.
264  */
265 static void av_unused put_bits32(PutBitContext *s, uint32_t value)
266 {
267     int lo = value & 0xffff;
268     int hi = value >> 16;
269 #ifdef BITSTREAM_WRITER_LE
270     put_bits(s, 16, lo);
271     put_bits(s, 16, hi);
272 #else
273     put_bits(s, 16, hi);
274     put_bits(s, 16, lo);
275 #endif
276 }
277
278 /**
279  * Return the pointer to the byte where the bitstream writer will put
280  * the next bit.
281  */
282 static inline uint8_t* put_bits_ptr(PutBitContext *s)
283 {
284 #ifdef ALT_BITSTREAM_WRITER
285         return s->buf + (s->index>>3);
286 #else
287         return s->buf_ptr;
288 #endif
289 }
290
291 /**
292  * Skip the given number of bytes.
293  * PutBitContext must be flushed & aligned to a byte boundary before calling this.
294  */
295 static inline void skip_put_bytes(PutBitContext *s, int n)
296 {
297         assert((put_bits_count(s)&7)==0);
298 #ifdef ALT_BITSTREAM_WRITER
299         FIXME may need some cleaning of the buffer
300         s->index += n<<3;
301 #else
302         assert(s->bit_left==32);
303         s->buf_ptr += n;
304 #endif
305 }
306
307 /**
308  * Skip the given number of bits.
309  * Must only be used if the actual values in the bitstream do not matter.
310  * If n is 0 the behavior is undefined.
311  */
312 static inline void skip_put_bits(PutBitContext *s, int n)
313 {
314 #ifdef ALT_BITSTREAM_WRITER
315     s->index += n;
316 #else
317     s->bit_left -= n;
318     s->buf_ptr-= 4*(s->bit_left>>5);
319     s->bit_left &= 31;
320 #endif
321 }
322
323 /**
324  * Change the end of the buffer.
325  *
326  * @param size the new size in bytes of the buffer where to put bits
327  */
328 static inline void set_put_bits_buffer_size(PutBitContext *s, int size)
329 {
330     s->buf_end= s->buf + size;
331 }
332
333 #endif /* AVCODEC_PUT_BITS_H */