Browse code

Split bitstream.h, put the bitstream writer stuff in the new file put_bits.h.

Originally committed as revision 18461 to svn://svn.ffmpeg.org/ffmpeg/trunk

Stefano Sabatini authored on 2009/04/12 17:35:26
Showing 30 changed files
... ...
@@ -27,7 +27,8 @@
27 27
 //#define DEBUG_BITALLOC
28 28
 #include "libavutil/crc.h"
29 29
 #include "avcodec.h"
30
-#include "bitstream.h"
30
+#include "bitstream.h" // for ff_reverse
31
+#include "put_bits.h"
31 32
 #include "ac3.h"
32 33
 
33 34
 typedef struct AC3EncodeContext {
... ...
@@ -20,6 +20,7 @@
20 20
  */
21 21
 #include "avcodec.h"
22 22
 #include "bitstream.h"
23
+#include "put_bits.h"
23 24
 #include "bytestream.h"
24 25
 
25 26
 /**
... ...
@@ -21,6 +21,7 @@
21 21
 
22 22
 #include "avcodec.h"
23 23
 #include "bitstream.h"
24
+#include "put_bits.h"
24 25
 #include "dsputil.h"
25 26
 #include "lpc.h"
26 27
 #include "mathops.h"
... ...
@@ -26,6 +26,7 @@
26 26
 
27 27
 #include "avcodec.h"
28 28
 #include "bitstream.h"
29
+#include "put_bits.h"
29 30
 #include "dsputil.h"
30 31
 #include "mpeg12data.h"
31 32
 
... ...
@@ -29,6 +29,7 @@
29 29
 
30 30
 #include "avcodec.h"
31 31
 #include "bitstream.h"
32
+#include "put_bits.h"
32 33
 
33 34
 const uint8_t ff_log2_run[32]={
34 35
  0, 0, 0, 0, 1, 1, 1, 1,
... ...
@@ -39,8 +39,6 @@
39 39
 #   define ALT_BITSTREAM_READER
40 40
 #endif
41 41
 
42
-//#define ALT_BITSTREAM_WRITER
43
-//#define ALIGNED_BITSTREAM_WRITER
44 42
 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
45 43
 #   if ARCH_ARM
46 44
 #       define A32_BITSTREAM_READER
... ...
@@ -74,106 +72,6 @@ static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
74 74
 #    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
75 75
 #endif
76 76
 
77
-/* bit output */
78
-
79
-/* buf and buf_end must be present and used by every alternative writer. */
80
-typedef struct PutBitContext {
81
-#ifdef ALT_BITSTREAM_WRITER
82
-    uint8_t *buf, *buf_end;
83
-    int index;
84
-#else
85
-    uint32_t bit_buf;
86
-    int bit_left;
87
-    uint8_t *buf, *buf_ptr, *buf_end;
88
-#endif
89
-    int size_in_bits;
90
-} PutBitContext;
91
-
92
-/**
93
- * Initializes the PutBitContext \p s.
94
- *
95
- * @param buffer the buffer where to put bits
96
- * @param buffer_size the size in bytes of \p buffer
97
- */
98
-static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
99
-{
100
-    if(buffer_size < 0) {
101
-        buffer_size = 0;
102
-        buffer = NULL;
103
-    }
104
-
105
-    s->size_in_bits= 8*buffer_size;
106
-    s->buf = buffer;
107
-    s->buf_end = s->buf + buffer_size;
108
-#ifdef ALT_BITSTREAM_WRITER
109
-    s->index=0;
110
-    ((uint32_t*)(s->buf))[0]=0;
111
-//    memset(buffer, 0, buffer_size);
112
-#else
113
-    s->buf_ptr = s->buf;
114
-    s->bit_left=32;
115
-    s->bit_buf=0;
116
-#endif
117
-}
118
-
119
-/**
120
- * Returns the total number of bits written to the bitstream.
121
- */
122
-static inline int put_bits_count(PutBitContext *s)
123
-{
124
-#ifdef ALT_BITSTREAM_WRITER
125
-    return s->index;
126
-#else
127
-    return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
128
-#endif
129
-}
130
-
131
-/**
132
- * Pads the end of the output stream with zeros.
133
- */
134
-static inline void flush_put_bits(PutBitContext *s)
135
-{
136
-#ifdef ALT_BITSTREAM_WRITER
137
-    align_put_bits(s);
138
-#else
139
-#ifndef BITSTREAM_WRITER_LE
140
-    s->bit_buf<<= s->bit_left;
141
-#endif
142
-    while (s->bit_left < 32) {
143
-        /* XXX: should test end of buffer */
144
-#ifdef BITSTREAM_WRITER_LE
145
-        *s->buf_ptr++=s->bit_buf;
146
-        s->bit_buf>>=8;
147
-#else
148
-        *s->buf_ptr++=s->bit_buf >> 24;
149
-        s->bit_buf<<=8;
150
-#endif
151
-        s->bit_left+=8;
152
-    }
153
-    s->bit_left=32;
154
-    s->bit_buf=0;
155
-#endif
156
-}
157
-
158
-/**
159
- * Pads the bitstream with zeros up to the next byte boundary.
160
- */
161
-void align_put_bits(PutBitContext *s);
162
-
163
-/**
164
- * Puts the string \p s in the bitstream.
165
- *
166
- * @param terminate_string 0-terminates the written string if value is 1
167
- */
168
-void ff_put_string(PutBitContext * pbc, const char *s, int terminate_string);
169
-
170
-/**
171
- * Copies the content of \p src to the bitstream.
172
- *
173
- * @param length the number of bits of \p src to copy
174
- */
175
-void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
176
-
177 77
 /* bit input */
178 78
 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
179 79
 typedef struct GetBitContext {
... ...
@@ -207,178 +105,6 @@ typedef struct RL_VLC_ELEM {
207 207
     uint8_t run;
208 208
 } RL_VLC_ELEM;
209 209
 
210
-static inline void put_bits(PutBitContext *s, int n, unsigned int value)
211
-#ifndef ALT_BITSTREAM_WRITER
212
-{
213
-    unsigned int bit_buf;
214
-    int bit_left;
215
-
216
-    //    printf("put_bits=%d %x\n", n, value);
217
-    assert(n == 32 || value < (1U << n));
218
-
219
-    bit_buf = s->bit_buf;
220
-    bit_left = s->bit_left;
221
-
222
-    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
223
-    /* XXX: optimize */
224
-#ifdef BITSTREAM_WRITER_LE
225
-    bit_buf |= value << (32 - bit_left);
226
-    if (n >= bit_left) {
227
-#if !HAVE_FAST_UNALIGNED
228
-        if (3 & (intptr_t) s->buf_ptr) {
229
-            AV_WL32(s->buf_ptr, bit_buf);
230
-        } else
231
-#endif
232
-        *(uint32_t *)s->buf_ptr = le2me_32(bit_buf);
233
-        s->buf_ptr+=4;
234
-        bit_buf = (bit_left==32)?0:value >> bit_left;
235
-        bit_left+=32;
236
-    }
237
-    bit_left-=n;
238
-#else
239
-    if (n < bit_left) {
240
-        bit_buf = (bit_buf<<n) | value;
241
-        bit_left-=n;
242
-    } else {
243
-        bit_buf<<=bit_left;
244
-        bit_buf |= value >> (n - bit_left);
245
-#if !HAVE_FAST_UNALIGNED
246
-        if (3 & (intptr_t) s->buf_ptr) {
247
-            AV_WB32(s->buf_ptr, bit_buf);
248
-        } else
249
-#endif
250
-        *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
251
-        //printf("bitbuf = %08x\n", bit_buf);
252
-        s->buf_ptr+=4;
253
-        bit_left+=32 - n;
254
-        bit_buf = value;
255
-    }
256
-#endif
257
-
258
-    s->bit_buf = bit_buf;
259
-    s->bit_left = bit_left;
260
-}
261
-#else  /* ALT_BITSTREAM_WRITER defined */
262
-{
263
-#    ifdef ALIGNED_BITSTREAM_WRITER
264
-#        if ARCH_X86
265
-    __asm__ volatile(
266
-        "movl %0, %%ecx                 \n\t"
267
-        "xorl %%eax, %%eax              \n\t"
268
-        "shrdl %%cl, %1, %%eax          \n\t"
269
-        "shrl %%cl, %1                  \n\t"
270
-        "movl %0, %%ecx                 \n\t"
271
-        "shrl $3, %%ecx                 \n\t"
272
-        "andl $0xFFFFFFFC, %%ecx        \n\t"
273
-        "bswapl %1                      \n\t"
274
-        "orl %1, (%2, %%ecx)            \n\t"
275
-        "bswapl %%eax                   \n\t"
276
-        "addl %3, %0                    \n\t"
277
-        "movl %%eax, 4(%2, %%ecx)       \n\t"
278
-        : "=&r" (s->index), "=&r" (value)
279
-        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
280
-        : "%eax", "%ecx"
281
-    );
282
-#        else
283
-    int index= s->index;
284
-    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
285
-
286
-    value<<= 32-n;
287
-
288
-    ptr[0] |= be2me_32(value>>(index&31));
289
-    ptr[1]  = be2me_32(value<<(32-(index&31)));
290
-//if(n>24) printf("%d %d\n", n, value);
291
-    index+= n;
292
-    s->index= index;
293
-#        endif
294
-#    else //ALIGNED_BITSTREAM_WRITER
295
-#        if ARCH_X86
296
-    __asm__ volatile(
297
-        "movl $7, %%ecx                 \n\t"
298
-        "andl %0, %%ecx                 \n\t"
299
-        "addl %3, %%ecx                 \n\t"
300
-        "negl %%ecx                     \n\t"
301
-        "shll %%cl, %1                  \n\t"
302
-        "bswapl %1                      \n\t"
303
-        "movl %0, %%ecx                 \n\t"
304
-        "shrl $3, %%ecx                 \n\t"
305
-        "orl %1, (%%ecx, %2)            \n\t"
306
-        "addl %3, %0                    \n\t"
307
-        "movl $0, 4(%%ecx, %2)          \n\t"
308
-        : "=&r" (s->index), "=&r" (value)
309
-        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
310
-        : "%ecx"
311
-    );
312
-#        else
313
-    int index= s->index;
314
-    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
315
-
316
-    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
317
-    ptr[1] = 0;
318
-//if(n>24) printf("%d %d\n", n, value);
319
-    index+= n;
320
-    s->index= index;
321
-#        endif
322
-#    endif //!ALIGNED_BITSTREAM_WRITER
323
-}
324
-#endif
325
-
326
-static inline void put_sbits(PutBitContext *pb, int bits, int32_t val)
327
-{
328
-    assert(bits >= 0 && bits <= 31);
329
-
330
-    put_bits(pb, bits, val & ((1<<bits)-1));
331
-}
332
-
333
-
334
-static inline uint8_t* pbBufPtr(PutBitContext *s)
335
-{
336
-#ifdef ALT_BITSTREAM_WRITER
337
-        return s->buf + (s->index>>3);
338
-#else
339
-        return s->buf_ptr;
340
-#endif
341
-}
342
-
343
-/**
344
- * Skips the given number of bytes.
345
- * PutBitContext must be flushed & aligned to a byte boundary before calling this.
346
- */
347
-static inline void skip_put_bytes(PutBitContext *s, int n){
348
-        assert((put_bits_count(s)&7)==0);
349
-#ifdef ALT_BITSTREAM_WRITER
350
-        FIXME may need some cleaning of the buffer
351
-        s->index += n<<3;
352
-#else
353
-        assert(s->bit_left==32);
354
-        s->buf_ptr += n;
355
-#endif
356
-}
357
-
358
-/**
359
- * Skips the given number of bits.
360
- * Must only be used if the actual values in the bitstream do not matter.
361
- * If \p n is 0 the behavior is undefined.
362
- */
363
-static inline void skip_put_bits(PutBitContext *s, int n){
364
-#ifdef ALT_BITSTREAM_WRITER
365
-    s->index += n;
366
-#else
367
-    s->bit_left -= n;
368
-    s->buf_ptr-= s->bit_left>>5;
369
-    s->bit_left &= 31;
370
-#endif
371
-}
372
-
373
-/**
374
- * Changes the end of the buffer.
375
- *
376
- * @param size the new size in bytes of the buffer where to put bits
377
- */
378
-static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
379
-    s->buf_end= s->buf + size;
380
-}
381
-
382 210
 /* Bitstream reader API docs:
383 211
 name
384 212
     arbitrary name which is used as prefix for the internal variables
... ...
@@ -27,7 +27,7 @@
27 27
 #ifndef AVCODEC_CABAC_H
28 28
 #define AVCODEC_CABAC_H
29 29
 
30
-#include "bitstream.h"
30
+#include "put_bits.h"
31 31
 
32 32
 //#undef NDEBUG
33 33
 #include <assert.h>
... ...
@@ -33,6 +33,7 @@
33 33
 #include "avcodec.h"
34 34
 #include "dsputil.h"
35 35
 #include "bitstream.h"
36
+#include "put_bits.h"
36 37
 #include "dcadata.h"
37 38
 #include "dcahuff.h"
38 39
 #include "dca.h"
... ...
@@ -41,6 +41,7 @@
41 41
 #include "avcodec.h"
42 42
 #include "dsputil.h"
43 43
 #include "bitstream.h"
44
+#include "put_bits.h"
44 45
 #include "simple_idct.h"
45 46
 #include "dvdata.h"
46 47
 
... ...
@@ -26,6 +26,7 @@
26 26
  */
27 27
 #include "avcodec.h"
28 28
 #include "bitstream.h"
29
+#include "put_bits.h"
29 30
 #include "faxcompr.h"
30 31
 
31 32
 #define CCITT_SYMS 104
... ...
@@ -27,6 +27,7 @@
27 27
 
28 28
 #include "avcodec.h"
29 29
 #include "bitstream.h"
30
+#include "put_bits.h"
30 31
 #include "dsputil.h"
31 32
 #include "rangecoder.h"
32 33
 #include "golomb.h"
... ...
@@ -59,7 +59,7 @@
59 59
 #include <zlib.h>
60 60
 
61 61
 #include "avcodec.h"
62
-#include "bitstream.h"
62
+#include "put_bits.h"
63 63
 #include "bytestream.h"
64 64
 
65 65
 
... ...
@@ -24,6 +24,7 @@
24 24
 #include <limits.h>
25 25
 #include "avcodec.h"
26 26
 #include "bitstream.h"
27
+#include "put_bits.h"
27 28
 
28 29
 /**
29 30
  * G.726 11bit float.
... ...
@@ -48,7 +48,7 @@
48 48
 /* at least they don't use PDP_ENDIAN :) */
49 49
 #define BITSTREAM_WRITER_LE
50 50
 
51
-#include "bitstream.h"
51
+#include "put_bits.h"
52 52
 
53 53
 /* bitstream minipacket size */
54 54
 #define GIF_CHUNKS 100
... ...
@@ -32,6 +32,7 @@
32 32
 
33 33
 #include <stdint.h>
34 34
 #include "bitstream.h"
35
+#include "put_bits.h"
35 36
 
36 37
 #define INVALID_VLC           0x80000000
37 38
 
... ...
@@ -30,6 +30,7 @@
30 30
 
31 31
 #include "avcodec.h"
32 32
 #include "bitstream.h"
33
+#include "put_bits.h"
33 34
 #include "dsputil.h"
34 35
 
35 36
 #define VLC_BITS 11
... ...
@@ -42,7 +42,7 @@
42 42
 #include <stdlib.h>
43 43
 
44 44
 #include "avcodec.h"
45
-#include "bitstream.h"
45
+#include "put_bits.h"
46 46
 #include "lcl.h"
47 47
 
48 48
 #if CONFIG_ZLIB
... ...
@@ -26,7 +26,7 @@
26 26
  */
27 27
 
28 28
 #include "avcodec.h"
29
-#include "bitstream.h"
29
+#include "put_bits.h"
30 30
 #include "lzw.h"
31 31
 
32 32
 #define LZW_MAXBITS 12
... ...
@@ -34,7 +34,7 @@
34 34
 #define AVCODEC_MJPEG_H
35 35
 
36 36
 #include "avcodec.h"
37
-#include "bitstream.h"
37
+#include "put_bits.h"
38 38
 
39 39
 
40 40
 /* JPEG marker codes */
... ...
@@ -25,7 +25,7 @@
25 25
  */
26 26
 
27 27
 #include "avcodec.h"
28
-#include "bitstream.h"
28
+#include "put_bits.h"
29 29
 
30 30
 #undef  CONFIG_MPEGAUDIO_HP
31 31
 #define CONFIG_MPEGAUDIO_HP 0
... ...
@@ -30,6 +30,7 @@
30 30
 
31 31
 #include "dsputil.h"
32 32
 #include "bitstream.h"
33
+#include "put_bits.h"
33 34
 #include "ratecontrol.h"
34 35
 #include "parser.h"
35 36
 #include "mpeg12data.h"
... ...
@@ -40,7 +40,7 @@
40 40
 #include "dsputil.h"
41 41
 
42 42
 #define BITSTREAM_WRITER_LE
43
-#include "bitstream.h"
43
+#include "put_bits.h"
44 44
 
45 45
 #define POW_TABLE_SIZE (1<<11)
46 46
 #define POW_TABLE_OFFSET 3
47 47
new file mode 100644
... ...
@@ -0,0 +1,311 @@
0
+/*
1
+ * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file libavcodec/put_bits.h
22
+ * bitstream writer API
23
+ */
24
+
25
+#ifndef AVCODEC_PUT_BITS_H
26
+#define AVCODEC_PUT_BITS_H
27
+
28
+#include <stdint.h>
29
+#include <stdlib.h>
30
+#include <assert.h>
31
+#include "libavutil/bswap.h"
32
+#include "libavutil/common.h"
33
+#include "libavutil/intreadwrite.h"
34
+#include "libavutil/log.h"
35
+#include "mathops.h"
36
+
37
+//#define ALT_BITSTREAM_WRITER
38
+//#define ALIGNED_BITSTREAM_WRITER
39
+
40
+/* buf and buf_end must be present and used by every alternative writer. */
41
+typedef struct PutBitContext {
42
+#ifdef ALT_BITSTREAM_WRITER
43
+    uint8_t *buf, *buf_end;
44
+    int index;
45
+#else
46
+    uint32_t bit_buf;
47
+    int bit_left;
48
+    uint8_t *buf, *buf_ptr, *buf_end;
49
+#endif
50
+    int size_in_bits;
51
+} PutBitContext;
52
+
53
+/**
54
+ * Initializes the PutBitContext \p s.
55
+ *
56
+ * @param buffer the buffer where to put bits
57
+ * @param buffer_size the size in bytes of \p buffer
58
+ */
59
+static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
60
+{
61
+    if(buffer_size < 0) {
62
+        buffer_size = 0;
63
+        buffer = NULL;
64
+    }
65
+
66
+    s->size_in_bits= 8*buffer_size;
67
+    s->buf = buffer;
68
+    s->buf_end = s->buf + buffer_size;
69
+#ifdef ALT_BITSTREAM_WRITER
70
+    s->index=0;
71
+    ((uint32_t*)(s->buf))[0]=0;
72
+//    memset(buffer, 0, buffer_size);
73
+#else
74
+    s->buf_ptr = s->buf;
75
+    s->bit_left=32;
76
+    s->bit_buf=0;
77
+#endif
78
+}
79
+
80
+/**
81
+ * Returns the total number of bits written to the bitstream.
82
+ */
83
+static inline int put_bits_count(PutBitContext *s)
84
+{
85
+#ifdef ALT_BITSTREAM_WRITER
86
+    return s->index;
87
+#else
88
+    return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
89
+#endif
90
+}
91
+
92
+/**
93
+ * Pads the end of the output stream with zeros.
94
+ */
95
+static inline void flush_put_bits(PutBitContext *s)
96
+{
97
+#ifdef ALT_BITSTREAM_WRITER
98
+    align_put_bits(s);
99
+#else
100
+#ifndef BITSTREAM_WRITER_LE
101
+    s->bit_buf<<= s->bit_left;
102
+#endif
103
+    while (s->bit_left < 32) {
104
+        /* XXX: should test end of buffer */
105
+#ifdef BITSTREAM_WRITER_LE
106
+        *s->buf_ptr++=s->bit_buf;
107
+        s->bit_buf>>=8;
108
+#else
109
+        *s->buf_ptr++=s->bit_buf >> 24;
110
+        s->bit_buf<<=8;
111
+#endif
112
+        s->bit_left+=8;
113
+    }
114
+    s->bit_left=32;
115
+    s->bit_buf=0;
116
+#endif
117
+}
118
+
119
+/**
120
+ * Pads the bitstream with zeros up to the next byte boundary.
121
+ */
122
+void align_put_bits(PutBitContext *s);
123
+
124
+/**
125
+ * Puts the string \p s in the bitstream.
126
+ *
127
+ * @param terminate_string 0-terminates the written string if value is 1
128
+ */
129
+void ff_put_string(PutBitContext * pbc, const char *s, int terminate_string);
130
+
131
+/**
132
+ * Copies the content of \p src to the bitstream.
133
+ *
134
+ * @param length the number of bits of \p src to copy
135
+ */
136
+void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
137
+
138
+static inline void put_bits(PutBitContext *s, int n, unsigned int value)
139
+#ifndef ALT_BITSTREAM_WRITER
140
+{
141
+    unsigned int bit_buf;
142
+    int bit_left;
143
+
144
+    //    printf("put_bits=%d %x\n", n, value);
145
+    assert(n == 32 || value < (1U << n));
146
+
147
+    bit_buf = s->bit_buf;
148
+    bit_left = s->bit_left;
149
+
150
+    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
151
+    /* XXX: optimize */
152
+#ifdef BITSTREAM_WRITER_LE
153
+    bit_buf |= value << (32 - bit_left);
154
+    if (n >= bit_left) {
155
+#if !HAVE_FAST_UNALIGNED
156
+        if (3 & (intptr_t) s->buf_ptr) {
157
+            AV_WL32(s->buf_ptr, bit_buf);
158
+        } else
159
+#endif
160
+        *(uint32_t *)s->buf_ptr = le2me_32(bit_buf);
161
+        s->buf_ptr+=4;
162
+        bit_buf = (bit_left==32)?0:value >> bit_left;
163
+        bit_left+=32;
164
+    }
165
+    bit_left-=n;
166
+#else
167
+    if (n < bit_left) {
168
+        bit_buf = (bit_buf<<n) | value;
169
+        bit_left-=n;
170
+    } else {
171
+        bit_buf<<=bit_left;
172
+        bit_buf |= value >> (n - bit_left);
173
+#if !HAVE_FAST_UNALIGNED
174
+        if (3 & (intptr_t) s->buf_ptr) {
175
+            AV_WB32(s->buf_ptr, bit_buf);
176
+        } else
177
+#endif
178
+        *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
179
+        //printf("bitbuf = %08x\n", bit_buf);
180
+        s->buf_ptr+=4;
181
+        bit_left+=32 - n;
182
+        bit_buf = value;
183
+    }
184
+#endif
185
+
186
+    s->bit_buf = bit_buf;
187
+    s->bit_left = bit_left;
188
+}
189
+#else  /* ALT_BITSTREAM_WRITER defined */
190
+{
191
+#    ifdef ALIGNED_BITSTREAM_WRITER
192
+#        if ARCH_X86
193
+    __asm__ volatile(
194
+        "movl %0, %%ecx                 \n\t"
195
+        "xorl %%eax, %%eax              \n\t"
196
+        "shrdl %%cl, %1, %%eax          \n\t"
197
+        "shrl %%cl, %1                  \n\t"
198
+        "movl %0, %%ecx                 \n\t"
199
+        "shrl $3, %%ecx                 \n\t"
200
+        "andl $0xFFFFFFFC, %%ecx        \n\t"
201
+        "bswapl %1                      \n\t"
202
+        "orl %1, (%2, %%ecx)            \n\t"
203
+        "bswapl %%eax                   \n\t"
204
+        "addl %3, %0                    \n\t"
205
+        "movl %%eax, 4(%2, %%ecx)       \n\t"
206
+        : "=&r" (s->index), "=&r" (value)
207
+        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
208
+        : "%eax", "%ecx"
209
+    );
210
+#        else
211
+    int index= s->index;
212
+    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
213
+
214
+    value<<= 32-n;
215
+
216
+    ptr[0] |= be2me_32(value>>(index&31));
217
+    ptr[1]  = be2me_32(value<<(32-(index&31)));
218
+//if(n>24) printf("%d %d\n", n, value);
219
+    index+= n;
220
+    s->index= index;
221
+#        endif
222
+#    else //ALIGNED_BITSTREAM_WRITER
223
+#        if ARCH_X86
224
+    __asm__ volatile(
225
+        "movl $7, %%ecx                 \n\t"
226
+        "andl %0, %%ecx                 \n\t"
227
+        "addl %3, %%ecx                 \n\t"
228
+        "negl %%ecx                     \n\t"
229
+        "shll %%cl, %1                  \n\t"
230
+        "bswapl %1                      \n\t"
231
+        "movl %0, %%ecx                 \n\t"
232
+        "shrl $3, %%ecx                 \n\t"
233
+        "orl %1, (%%ecx, %2)            \n\t"
234
+        "addl %3, %0                    \n\t"
235
+        "movl $0, 4(%%ecx, %2)          \n\t"
236
+        : "=&r" (s->index), "=&r" (value)
237
+        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
238
+        : "%ecx"
239
+    );
240
+#        else
241
+    int index= s->index;
242
+    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
243
+
244
+    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
245
+    ptr[1] = 0;
246
+//if(n>24) printf("%d %d\n", n, value);
247
+    index+= n;
248
+    s->index= index;
249
+#        endif
250
+#    endif //!ALIGNED_BITSTREAM_WRITER
251
+}
252
+#endif
253
+
254
+static inline void put_sbits(PutBitContext *pb, int bits, int32_t val)
255
+{
256
+    assert(bits >= 0 && bits <= 31);
257
+
258
+    put_bits(pb, bits, val & ((1<<bits)-1));
259
+}
260
+
261
+
262
+static inline uint8_t* pbBufPtr(PutBitContext *s)
263
+{
264
+#ifdef ALT_BITSTREAM_WRITER
265
+        return s->buf + (s->index>>3);
266
+#else
267
+        return s->buf_ptr;
268
+#endif
269
+}
270
+
271
+/**
272
+ * Skips the given number of bytes.
273
+ * PutBitContext must be flushed & aligned to a byte boundary before calling this.
274
+ */
275
+static inline void skip_put_bytes(PutBitContext *s, int n){
276
+        assert((put_bits_count(s)&7)==0);
277
+#ifdef ALT_BITSTREAM_WRITER
278
+        FIXME may need some cleaning of the buffer
279
+        s->index += n<<3;
280
+#else
281
+        assert(s->bit_left==32);
282
+        s->buf_ptr += n;
283
+#endif
284
+}
285
+
286
+/**
287
+ * Skips the given number of bits.
288
+ * Must only be used if the actual values in the bitstream do not matter.
289
+ * If \p n is 0 the behavior is undefined.
290
+ */
291
+static inline void skip_put_bits(PutBitContext *s, int n){
292
+#ifdef ALT_BITSTREAM_WRITER
293
+    s->index += n;
294
+#else
295
+    s->bit_left -= n;
296
+    s->buf_ptr-= s->bit_left>>5;
297
+    s->bit_left &= 31;
298
+#endif
299
+}
300
+
301
+/**
302
+ * Changes the end of the buffer.
303
+ *
304
+ * @param size the new size in bytes of the buffer where to put bits
305
+ */
306
+static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
307
+    s->buf_end= s->buf + size;
308
+}
309
+
310
+#endif /* AVCODEC_PUT_BITS_H */
... ...
@@ -31,7 +31,7 @@
31 31
 #include "vorbis_enc_data.h"
32 32
 
33 33
 #define BITSTREAM_WRITER_LE
34
-#include "bitstream.h"
34
+#include "put_bits.h"
35 35
 
36 36
 #undef NDEBUG
37 37
 #include <assert.h>
... ...
@@ -23,6 +23,7 @@
23 23
 #define AVCODEC_WMA_H
24 24
 
25 25
 #include "bitstream.h"
26
+#include "put_bits.h"
26 27
 #include "dsputil.h"
27 28
 
28 29
 /* size of blocks */
... ...
@@ -21,6 +21,7 @@
21 21
  */
22 22
 
23 23
 #include "libavcodec/bitstream.h"
24
+#include "libavcodec/put_bits.h"
24 25
 #include "libavcodec/internal.h"
25 26
 #include "avformat.h"
26 27
 
... ...
@@ -45,7 +45,7 @@
45 45
 /* at least they don't use PDP_ENDIAN :) */
46 46
 #define BITSTREAM_WRITER_LE
47 47
 
48
-#include "libavcodec/bitstream.h"
48
+#include "libavcodec/put_bits.h"
49 49
 
50 50
 /* bitstream minipacket size */
51 51
 #define GIF_CHUNKS 100
... ...
@@ -27,6 +27,7 @@
27 27
 #include "isom.h"
28 28
 #include "avc.h"
29 29
 #include "libavcodec/bitstream.h"
30
+#include "libavcodec/put_bits.h"
30 31
 
31 32
 #undef NDEBUG
32 33
 #include <assert.h>
... ...
@@ -20,7 +20,7 @@
20 20
  */
21 21
 
22 22
 #include "libavutil/fifo.h"
23
-#include "libavcodec/bitstream.h"
23
+#include "libavcodec/put_bits.h"
24 24
 #include "avformat.h"
25 25
 #include "mpeg.h"
26 26
 
... ...
@@ -20,7 +20,7 @@
20 20
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 21
  */
22 22
 
23
-#include "libavcodec/bitstream.h"
23
+#include "libavcodec/put_bits.h"
24 24
 #include "avformat.h"
25 25
 #include "swf.h"
26 26