Revision b2755007
libavcodec/ac3enc.c | ||
---|---|---|
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 { |
libavcodec/adpcm.c | ||
---|---|---|
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 |
/** |
libavcodec/alacenc.c | ||
---|---|---|
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" |
libavcodec/asv1.c | ||
---|---|---|
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 |
|
libavcodec/bitstream.c | ||
---|---|---|
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, |
libavcodec/bitstream.h | ||
---|---|---|
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 | 72 |
# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) |
75 | 73 |
#endif |
76 | 74 |
|
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 | 75 |
/* bit input */ |
178 | 76 |
/* buffer, buffer_end and size_in_bits must be present and used by every reader */ |
179 | 77 |
typedef struct GetBitContext { |
... | ... | |
207 | 105 |
uint8_t run; |
208 | 106 |
} RL_VLC_ELEM; |
209 | 107 |
|
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 | 108 |
/* Bitstream reader API docs: |
383 | 109 |
name |
384 | 110 |
arbitrary name which is used as prefix for the internal variables |
libavcodec/cabac.h | ||
---|---|---|
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> |
libavcodec/dca.c | ||
---|---|---|
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" |
libavcodec/dv.c | ||
---|---|---|
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 |
|
libavcodec/faxcompr.c | ||
---|---|---|
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 |
libavcodec/ffv1.c | ||
---|---|---|
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" |
libavcodec/flashsvenc.c | ||
---|---|---|
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 |
|
libavcodec/g726.c | ||
---|---|---|
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. |
libavcodec/gif.c | ||
---|---|---|
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 |
libavcodec/golomb.h | ||
---|---|---|
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 |
|
libavcodec/huffyuv.c | ||
---|---|---|
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 |
libavcodec/lclenc.c | ||
---|---|---|
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 |
libavcodec/lzwenc.c | ||
---|---|---|
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 |
libavcodec/mjpeg.h | ||
---|---|---|
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 */ |
libavcodec/mpegaudioenc.c | ||
---|---|---|
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 |
libavcodec/mpegvideo.h | ||
---|---|---|
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" |
libavcodec/nellymoserenc.c | ||
---|---|---|
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 |
libavcodec/put_bits.h | ||
---|---|---|
1 |
/* |
|
2 |
* copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> |
|
3 |
* |
|
4 |
* This file is part of FFmpeg. |
|
5 |
* |
|
6 |
* FFmpeg 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 |
* FFmpeg 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 FFmpeg; 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 libavcodec/put_bits.h |
|
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 |
|
|
38 |
//#define ALT_BITSTREAM_WRITER |
|
39 |
//#define ALIGNED_BITSTREAM_WRITER |
|
40 |
|
|
41 |
/* buf and buf_end must be present and used by every alternative writer. */ |
|
42 |
typedef struct PutBitContext { |
|
43 |
#ifdef ALT_BITSTREAM_WRITER |
|
44 |
uint8_t *buf, *buf_end; |
|
45 |
int index; |
|
46 |
#else |
|
47 |
uint32_t bit_buf; |
|
48 |
int bit_left; |
|
49 |
uint8_t *buf, *buf_ptr, *buf_end; |
|
50 |
#endif |
|
51 |
int size_in_bits; |
|
52 |
} PutBitContext; |
|
53 |
|
|
54 |
/** |
|
55 |
* Initializes the PutBitContext \p s. |
|
56 |
* |
|
57 |
* @param buffer the buffer where to put bits |
|
58 |
* @param buffer_size the size in bytes of \p buffer |
|
59 |
*/ |
|
60 |
static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size) |
|
61 |
{ |
|
62 |
if(buffer_size < 0) { |
|
63 |
buffer_size = 0; |
|
64 |
buffer = NULL; |
|
65 |
} |
|
66 |
|
|
67 |
s->size_in_bits= 8*buffer_size; |
|
68 |
s->buf = buffer; |
|
69 |
s->buf_end = s->buf + buffer_size; |
|
70 |
#ifdef ALT_BITSTREAM_WRITER |
|
71 |
s->index=0; |
|
72 |
((uint32_t*)(s->buf))[0]=0; |
|
73 |
// memset(buffer, 0, buffer_size); |
|
74 |
#else |
|
75 |
s->buf_ptr = s->buf; |
|
76 |
s->bit_left=32; |
|
77 |
s->bit_buf=0; |
|
78 |
#endif |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* Returns 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 |
* Pads 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 |
/** |
|
121 |
* Pads the bitstream with zeros up to the next byte boundary. |
|
122 |
*/ |
|
123 |
void align_put_bits(PutBitContext *s); |
|
124 |
|
|
125 |
/** |
|
126 |
* Puts the string \p s in the bitstream. |
|
127 |
* |
|
128 |
* @param terminate_string 0-terminates the written string if value is 1 |
|
129 |
*/ |
|
130 |
void ff_put_string(PutBitContext * pbc, const char *s, int terminate_string); |
|
131 |
|
|
132 |
/** |
|
133 |
* Copies the content of \p src to the bitstream. |
|
134 |
* |
|
135 |
* @param length the number of bits of \p src to copy |
|
136 |
*/ |
|
137 |
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length); |
|
138 |
|
|
139 |
static inline void put_bits(PutBitContext *s, int n, unsigned int value) |
|
140 |
#ifndef ALT_BITSTREAM_WRITER |
|
141 |
{ |
|
142 |
unsigned int bit_buf; |
|
143 |
int bit_left; |
|
144 |
|
|
145 |
// printf("put_bits=%d %x\n", n, value); |
|
146 |
assert(n == 32 || value < (1U << n)); |
|
147 |
|
|
148 |
bit_buf = s->bit_buf; |
|
149 |
bit_left = s->bit_left; |
|
150 |
|
|
151 |
// printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); |
|
152 |
/* XXX: optimize */ |
|
153 |
#ifdef BITSTREAM_WRITER_LE |
|
154 |
bit_buf |= value << (32 - bit_left); |
|
155 |
if (n >= bit_left) { |
|
156 |
#if !HAVE_FAST_UNALIGNED |
|
157 |
if (3 & (intptr_t) s->buf_ptr) { |
|
158 |
AV_WL32(s->buf_ptr, bit_buf); |
|
159 |
} else |
|
160 |
#endif |
|
161 |
*(uint32_t *)s->buf_ptr = le2me_32(bit_buf); |
|
162 |
s->buf_ptr+=4; |
|
163 |
bit_buf = (bit_left==32)?0:value >> bit_left; |
|
164 |
bit_left+=32; |
|
165 |
} |
|
166 |
bit_left-=n; |
|
167 |
#else |
|
168 |
if (n < bit_left) { |
|
169 |
bit_buf = (bit_buf<<n) | value; |
|
170 |
bit_left-=n; |
|
171 |
} else { |
|
172 |
bit_buf<<=bit_left; |
|
173 |
bit_buf |= value >> (n - bit_left); |
|
174 |
#if !HAVE_FAST_UNALIGNED |
|
175 |
if (3 & (intptr_t) s->buf_ptr) { |
|
176 |
AV_WB32(s->buf_ptr, bit_buf); |
|
177 |
} else |
|
178 |
#endif |
|
179 |
*(uint32_t *)s->buf_ptr = be2me_32(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] |= be2me_32(value>>(index&31)); |
|
218 |
ptr[1] = be2me_32(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] |= be2me_32(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 bits, int32_t val) |
|
256 |
{ |
|
257 |
assert(bits >= 0 && bits <= 31); |
|
258 |
|
|
259 |
put_bits(pb, bits, val & ((1<<bits)-1)); |
|
260 |
} |
|
261 |
|
|
262 |
|
|
263 |
static inline uint8_t* pbBufPtr(PutBitContext *s) |
|
264 |
{ |
|
265 |
#ifdef ALT_BITSTREAM_WRITER |
|
266 |
return s->buf + (s->index>>3); |
|
267 |
#else |
|
268 |
return s->buf_ptr; |
|
269 |
#endif |
|
270 |
} |
|
271 |
|
|
272 |
/** |
|
273 |
* Skips the given number of bytes. |
|
274 |
* PutBitContext must be flushed & aligned to a byte boundary before calling this. |
|
275 |
*/ |
|
276 |
static inline void skip_put_bytes(PutBitContext *s, int n){ |
|
277 |
assert((put_bits_count(s)&7)==0); |
|
278 |
#ifdef ALT_BITSTREAM_WRITER |
|
279 |
FIXME may need some cleaning of the buffer |
|
280 |
s->index += n<<3; |
|
281 |
#else |
|
282 |
assert(s->bit_left==32); |
|
283 |
s->buf_ptr += n; |
|
284 |
#endif |
|
285 |
} |
|
286 |
|
|
287 |
/** |
|
288 |
* Skips the given number of bits. |
|
289 |
* Must only be used if the actual values in the bitstream do not matter. |
|
290 |
* If \p n is 0 the behavior is undefined. |
|
291 |
*/ |
|
292 |
static inline void skip_put_bits(PutBitContext *s, int n){ |
|
293 |
#ifdef ALT_BITSTREAM_WRITER |
|
294 |
s->index += n; |
|
295 |
#else |
|
296 |
s->bit_left -= n; |
|
297 |
s->buf_ptr-= s->bit_left>>5; |
|
298 |
s->bit_left &= 31; |
|
299 |
#endif |
|
300 |
} |
|
301 |
|
|
302 |
/** |
|
303 |
* Changes the end of the buffer. |
|
304 |
* |
|
305 |
* @param size the new size in bytes of the buffer where to put bits |
|
306 |
*/ |
|
307 |
static inline void set_put_bits_buffer_size(PutBitContext *s, int size){ |
|
308 |
s->buf_end= s->buf + size; |
|
309 |
} |
|
310 |
|
|
311 |
#endif /* AVCODEC_PUT_BITS_H */ |
libavcodec/vorbis_enc.c | ||
---|---|---|
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> |
libavcodec/wma.h | ||
---|---|---|
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 */ |
libavformat/adtsenc.c | ||
---|---|---|
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 |
|
libavformat/gif.c | ||
---|---|---|
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 |
libavformat/movenc.c | ||
---|---|---|
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> |
libavformat/mpegenc.c | ||
---|---|---|
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 |
|
libavformat/swfenc.c | ||
---|---|---|
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 |
|
Also available in: Unified diff