Originally committed as revision 18461 to svn://svn.ffmpeg.org/ffmpeg/trunk
| ... | ... |
@@ -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 |
| 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 */ |