Revision d8cf5aea libavcodec/common.c

View differences:

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

Also available in: Unified diff