Revision d8cf5aea

View differences:

ffserver.c
36 36
#include <ctype.h>
37 37
#include <signal.h>
38 38

  
39
#include "bswap.h" // needed for the bitstream writer in common.h which is included in avformat.h
39 40
#include "avformat.h"
40 41

  
41 42
/* maximum number of simultaneous HTTP connections */
libavcodec/common.c
29 29
    s->buf = buffer;
30 30
    s->buf_end = s->buf + buffer_size;
31 31
    s->data_out_size = 0;
32
#ifdef ALT_BITSTREAM_WRITER
33
    s->index=0;
34
    ((uint32_t*)(s->buf))[0]=0;
35
//    memset(buffer, 0, buffer_size);
36 32
    if(write_data!=NULL) 
37 33
    {
38 34
    	fprintf(stderr, "write Data callback is not supported\n");
39 35
    }
36
#ifdef ALT_BITSTREAM_WRITER
37
    s->index=0;
38
    ((uint32_t*)(s->buf))[0]=0;
39
//    memset(buffer, 0, buffer_size);
40 40
#else
41
    s->write_data = write_data;
42
    s->opaque = opaque;
43 41
    s->buf_ptr = s->buf;
44
    s->bit_cnt=0;
42
    s->bit_left=32;
45 43
    s->bit_buf=0;
46 44
#endif
47 45
}
48 46

  
49
#ifndef ALT_BITSTREAM_WRITER
50
static void flush_buffer(PutBitContext *s)
51
{
52
    int size;
53
    if (s->write_data) {
54
        size = s->buf_ptr - s->buf;
55
        if (size > 0)
56
            s->write_data(s->opaque, s->buf, size);
57
        s->buf_ptr = s->buf;
58
        s->data_out_size += size;
59
    }
60
}
61

  
62
void put_bits(PutBitContext *s, int n, unsigned int value)
63
{
64
    unsigned int bit_buf;
65
    int bit_cnt;
66

  
67
#ifdef STATS
68
    st_out_bit_counts[st_current_index] += n;
69
#endif
70
    //    printf("put_bits=%d %x\n", n, value);
71
    assert(n == 32 || value < (1U << n));
72

  
73
    bit_buf = s->bit_buf;
74
    bit_cnt = s->bit_cnt;
75

  
76
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
77
    /* XXX: optimize */
78
    if (n < (32-bit_cnt)) {
79
        bit_buf |= value << (32 - n - bit_cnt);
80
        bit_cnt+=n;
81
    } else {
82
        bit_buf |= value >> (n + bit_cnt - 32);
83
        *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
84
        //printf("bitbuf = %08x\n", bit_buf);
85
        s->buf_ptr+=4;
86
        if (s->buf_ptr >= s->buf_end)
87
            flush_buffer(s);
88
        bit_cnt=bit_cnt + n - 32;
89
        if (bit_cnt == 0) {
90
            bit_buf = 0;
91
        } else {
92
            bit_buf = value << (32 - bit_cnt);
93
        }
94
    }
95
    
96
    s->bit_buf = bit_buf;
97
    s->bit_cnt = bit_cnt;
98
}
99
#endif
100

  
101 47
/* return the number of bits output */
102 48
INT64 get_bit_count(PutBitContext *s)
103 49
{
104 50
#ifdef ALT_BITSTREAM_WRITER
105 51
    return s->data_out_size * 8 + s->index;
106 52
#else
107
    return (s->buf_ptr - s->buf + s->data_out_size) * 8 + (INT64)s->bit_cnt;
53
    return (s->buf_ptr - s->buf + s->data_out_size) * 8 + 32 - (INT64)s->bit_left;
108 54
#endif
109 55
}
110 56

  
......
113 59
#ifdef ALT_BITSTREAM_WRITER
114 60
    put_bits(s,(  - s->index) & 7,0);
115 61
#else
116
    put_bits(s,(8 - s->bit_cnt) & 7,0);
62
    put_bits(s,s->bit_left & 7,0);
117 63
#endif
118 64
}
119 65

  
......
123 69
#ifdef ALT_BITSTREAM_WRITER
124 70
    align_put_bits(s);
125 71
#else
126
    while (s->bit_cnt > 0) {
72
    s->bit_buf<<= s->bit_left;
73
    while (s->bit_left < 32) {
127 74
        /* XXX: should test end of buffer */
128 75
        *s->buf_ptr++=s->bit_buf >> 24;
129 76
        s->bit_buf<<=8;
130
        s->bit_cnt-=8;
77
        s->bit_left+=8;
131 78
    }
132
    flush_buffer(s);
133
    s->bit_cnt=0;
79
    s->bit_left=32;
134 80
    s->bit_buf=0;
135 81
#endif
136 82
}
137 83

  
138
#ifndef ALT_BITSTREAM_WRITER
139
/* for jpeg : escape 0xff with 0x00 after it */
140
void jput_bits(PutBitContext *s, int n, unsigned int value)
141
{
142
    unsigned int bit_buf, b;
143
    int bit_cnt, i;
144
    
145
    assert(n == 32 || value < (1U << n));
146

  
147
    bit_buf = s->bit_buf;
148
    bit_cnt = s->bit_cnt;
149

  
150
    //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
151
    /* XXX: optimize */
152
    if (n < (32-bit_cnt)) {
153
        bit_buf |= value << (32 - n - bit_cnt);
154
        bit_cnt+=n;
155
    } else {
156
        bit_buf |= value >> (n + bit_cnt - 32);
157
        /* handle escape */
158
        for(i=0;i<4;i++) {
159
            b = (bit_buf >> 24);
160
            *(s->buf_ptr++) = b;
161
            if (b == 0xff)
162
                *(s->buf_ptr++) = 0;
163
            bit_buf <<= 8;
164
        }
165
        /* we flush the buffer sooner to handle worst case */
166
        if (s->buf_ptr >= (s->buf_end - 8))
167
            flush_buffer(s);
168

  
169
        bit_cnt=bit_cnt + n - 32;
170
        if (bit_cnt == 0) {
171
            bit_buf = 0;
172
        } else {
173
            bit_buf = value << (32 - bit_cnt);
174
        }
175
    }
176
    
177
    s->bit_buf = bit_buf;
178
    s->bit_cnt = bit_cnt;
179
}
180
#endif
181

  
182 84
/* pad the end of the output stream with zeros */
183 85
#ifndef ALT_BITSTREAM_WRITER
184 86
void jflush_put_bits(PutBitContext *s)
185 87
{
186 88
    unsigned int b;
187
    s->bit_buf |= ~1U >> s->bit_cnt; /* set all the unused bits to one */
89
    s->bit_buf<<= s->bit_left;
90
    s->bit_buf |= ~1U >> (32 - s->bit_left); /* set all the unused bits to one */
188 91

  
189
    while (s->bit_cnt > 0) {
92
    while (s->bit_left < 32) {
190 93
        b = s->bit_buf >> 24;
191 94
        *s->buf_ptr++ = b;
192 95
        if (b == 0xff)
193 96
            *s->buf_ptr++ = 0;
194 97
        s->bit_buf<<=8;
195
        s->bit_cnt-=8;
98
        s->bit_left+=8;
196 99
    }
197
    flush_buffer(s);
198
    s->bit_cnt=0;
100
    s->bit_left=32;
199 101
    s->bit_buf=0;
200 102
}
201 103
#else
libavcodec/common.h
168 168
    int index;
169 169
#else
170 170
    UINT32 bit_buf;
171
    int bit_cnt;
171
    int bit_left;
172 172
    UINT8 *buf, *buf_ptr, *buf_end;
173
    void *opaque;
174
    WriteDataFunc write_data;
175 173
#endif
176 174
    INT64 data_out_size; /* in bytes */
177 175
} PutBitContext;
......
181 179
                   void *opaque,
182 180
                   void (*write_data)(void *, UINT8 *, int));
183 181

  
184
#ifndef ALT_BITSTREAM_WRITER
185
void put_bits(PutBitContext *s, int n, unsigned int value);
186
#endif
187

  
188 182
INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
189 183
void align_put_bits(PutBitContext *s);
190 184
void flush_put_bits(PutBitContext *s);
191 185

  
192 186
/* jpeg specific put_bits */
193
#ifndef ALT_BITSTREAM_WRITER
194
void jput_bits(PutBitContext *s, int n, unsigned int value);
195
#endif
196 187
void jflush_put_bits(PutBitContext *s);
197 188

  
198 189
/* bit input */
......
238 229
#endif
239 230
#endif //!ARCH_X86
240 231

  
232
#ifndef ALT_BITSTREAM_WRITER
233
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
234
{
235
    unsigned int bit_buf;
236
    int bit_left;
237

  
238
#ifdef STATS
239
    st_out_bit_counts[st_current_index] += n;
240
#endif
241
    //    printf("put_bits=%d %x\n", n, value);
242
    assert(n == 32 || value < (1U << n));
243

  
244
    bit_buf = s->bit_buf;
245
    bit_left = s->bit_left;
246

  
247
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
248
    /* XXX: optimize */
249
    if (n < bit_left) {
250
        bit_buf = (bit_buf<<n) | value;
251
        bit_left-=n;
252
    } else {
253
	bit_buf<<=bit_left;
254
        bit_buf |= value >> (n - bit_left);
255
        *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
256
        //printf("bitbuf = %08x\n", bit_buf);
257
        s->buf_ptr+=4;
258
	bit_left+=32 - n;
259
        bit_buf = value;
260
    }
261

  
262
    s->bit_buf = bit_buf;
263
    s->bit_left = bit_left;
264
}
265
#endif
266

  
267

  
241 268
#ifdef ALT_BITSTREAM_WRITER
242 269
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
243 270
{
......
304 331
}
305 332
#endif
306 333

  
334
#ifndef ALT_BITSTREAM_WRITER
335
/* for jpeg : escape 0xff with 0x00 after it */
336
static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
337
{
338
    unsigned int bit_buf, b;
339
    int bit_left, i;
340
    
341
    assert(n == 32 || value < (1U << n));
342

  
343
    bit_buf = s->bit_buf;
344
    bit_left = s->bit_left;
345

  
346
    //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
347
    /* XXX: optimize */
348
    if (n < bit_left) {
349
        bit_buf = (bit_buf<<n) | value;
350
        bit_left-=n;
351
    } else {
352
	bit_buf<<=bit_left;
353
        bit_buf |= value >> (n - bit_left);
354
        /* handle escape */
355
        for(i=0;i<4;i++) {
356
            b = (bit_buf >> 24);
357
            *(s->buf_ptr++) = b;
358
            if (b == 0xff)
359
                *(s->buf_ptr++) = 0;
360
            bit_buf <<= 8;
361
        }
362

  
363
	bit_left+= 32 - n;
364
        bit_buf = value;
365
    }
366
    
367
    s->bit_buf = bit_buf;
368
    s->bit_left = bit_left;
369
}
370
#endif
371

  
372

  
307 373
#ifdef ALT_BITSTREAM_WRITER
308 374
static inline void jput_bits(PutBitContext *s, int n, int value)
309 375
{

Also available in: Unified diff