Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(543)

Side by Side Diff: src/libFLAC/bitwriter.c

Issue 1961133002: Update FLAC to 1.3.1 (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/flac.git@master
Patch Set: build config tweaks for Windows Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/libFLAC/bitreader.c ('k') | src/libFLAC/cpu.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* libFLAC - Free Lossless Audio Codec library 1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson 2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2014 Xiph.Org Foundation
3 * 4 *
4 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
6 * are met: 7 * are met:
7 * 8 *
8 * - Redistributions of source code must retain the above copyright 9 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
10 * 11 *
11 * - Redistributions in binary form must reproduce the above copyright 12 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
14 * 15 *
15 * - Neither the name of the Xiph.org Foundation nor the names of its 16 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from 17 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission. 18 * this software without specific prior written permission.
18 * 19 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 31 */
31 32
32 #if HAVE_CONFIG_H 33 #ifdef HAVE_CONFIG_H
33 # include <config.h> 34 # include <config.h>
34 #endif 35 #endif
35 36
36 #include <stdlib.h> /* for malloc() */ 37 #include <stdlib.h>
37 #include <string.h> /* for memcpy(), memset() */ 38 #include <string.h>
38 #ifdef _MSC_VER
39 #include <winsock.h> /* for ntohl() */
40 #elif defined FLAC__SYS_DARWIN
41 #include <machine/endian.h> /* for ntohl() */
42 #elif defined __MINGW32__
43 #include <winsock.h> /* for ntohl() */
44 #else
45 #include <netinet/in.h> /* for ntohl() */
46 #endif
47 #if 0 /* UNUSED */
48 #include "private/bitmath.h"
49 #endif
50 #include "private/bitwriter.h" 39 #include "private/bitwriter.h"
51 #include "private/crc.h" 40 #include "private/crc.h"
41 #include "private/macros.h"
52 #include "FLAC/assert.h" 42 #include "FLAC/assert.h"
53 #include "share/alloc.h" 43 #include "share/alloc.h"
44 #include "share/compat.h"
45 #include "share/endswap.h"
54 46
55 /* Things should be fastest when this matches the machine word size */ 47 /* Things should be fastest when this matches the machine word size */
56 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */ 48 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
57 /* WATCHOUT: there are a few places where the code will not work unless bwword i s >= 32 bits wide */ 49 /* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */
58 typedef FLAC__uint32 bwword;
59 #define FLAC__BYTES_PER_WORD 4 50 #define FLAC__BYTES_PER_WORD 4
60 #define FLAC__BITS_PER_WORD 32 51 #define FLAC__BITS_PER_WORD 32
61 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff) 52 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
62 /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */ 53 /* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) i f necessary to match host byte order */
63 #if WORDS_BIGENDIAN 54 #if WORDS_BIGENDIAN
64 #define SWAP_BE_WORD_TO_HOST(x) (x) 55 #define SWAP_BE_WORD_TO_HOST(x) (x)
65 #else 56 #else
66 #ifdef _MSC_VER 57 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
67 #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
68 #else
69 #define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
70 #endif
71 #endif 58 #endif
72 59
73 /* 60 /*
74 * The default capacity here doesn't matter too much. The buffer always grows 61 * The default capacity here doesn't matter too much. The buffer always grows
75 * to hold whatever is written to it. Usually the encoder will stop adding at 62 * to hold whatever is written to it. Usually the encoder will stop adding at
76 * a frame or metadata block, then write that out and clear the buffer for the 63 * a frame or metadata block, then write that out and clear the buffer for the
77 * next one. 64 * next one.
78 */ 65 */
79 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword) ; /* size in words */ 66 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(uint32_ t); /* size in words */
80 /* When growing, increment 4K at a time */ 67 /* When growing, increment 4K at a time */
81 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword) ; /* size in words */ 68 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(uint32_ t); /* size in words */
82 69
83 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD) 70 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
84 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits) 71 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
85 72
86 #ifdef min
87 #undef min
88 #endif
89 #define min(x,y) ((x)<(y)?(x):(y))
90
91 /* adjust for compilers that can't understand using LLU suffix for uint64_t lite rals */
92 #ifdef _MSC_VER
93 #define FLAC__U64L(x) x
94 #else
95 #define FLAC__U64L(x) x##LLU
96 #endif
97
98 #ifndef FLaC__INLINE
99 #define FLaC__INLINE
100 #endif
101
102 struct FLAC__BitWriter { 73 struct FLAC__BitWriter {
103 » bwword *buffer; 74 » uint32_t *buffer;
104 » bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */ 75 » uint32_t accum; /* accumulator; bits are right-justified; when full, acc um is appended to buffer */
105 unsigned capacity; /* capacity of buffer in words */ 76 unsigned capacity; /* capacity of buffer in words */
106 unsigned words; /* # of complete words in buffer */ 77 unsigned words; /* # of complete words in buffer */
107 unsigned bits; /* # of used bits in accum */ 78 unsigned bits; /* # of used bits in accum */
108 }; 79 };
109 80
110 #ifdef _MSC_VER 81 /* * WATCHOUT: The current implementation only grows the buffer. */
111 /* OPT: an MSVC built-in would be better */ 82 #ifndef __SUNPRO_C
112 static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x) 83 static
113 {
114 » x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
115 » return (x>>16) | (x<<16);
116 }
117 #endif 84 #endif
118 85 FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
119 /* * WATCHOUT: The current implementation only grows the buffer. */
120 static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
121 { 86 {
122 unsigned new_capacity; 87 unsigned new_capacity;
123 » bwword *new_buffer; 88 » uint32_t *new_buffer;
124 89
125 FLAC__ASSERT(0 != bw); 90 FLAC__ASSERT(0 != bw);
126 FLAC__ASSERT(0 != bw->buffer); 91 FLAC__ASSERT(0 != bw->buffer);
127 92
128 /* calculate total words needed to store 'bits_to_add' additional bits * / 93 /* calculate total words needed to store 'bits_to_add' additional bits * /
129 new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WOR D - 1) / FLAC__BITS_PER_WORD); 94 new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WOR D - 1) / FLAC__BITS_PER_WORD);
130 95
131 /* it's possible (due to pessimism in the growth estimation that 96 /* it's possible (due to pessimism in the growth estimation that
132 * leads to this call) that we don't actually need to grow 97 * leads to this call) that we don't actually need to grow
133 */ 98 */
134 if(bw->capacity >= new_capacity) 99 if(bw->capacity >= new_capacity)
135 return true; 100 return true;
136 101
137 /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INC REMENT */ 102 /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INC REMENT */
138 if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT) 103 if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
139 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capaci ty - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT); 104 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capaci ty - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
140 /* make sure we got everything right */ 105 /* make sure we got everything right */
141 FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAUL T_INCREMENT); 106 FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAUL T_INCREMENT);
142 FLAC__ASSERT(new_capacity > bw->capacity); 107 FLAC__ASSERT(new_capacity > bw->capacity);
143 FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC __BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD)); 108 FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC __BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
144 109
145 » new_buffer = (bwword*)safe_realloc_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity); 110 » new_buffer = safe_realloc_mul_2op_(bw->buffer, sizeof(uint32_t), /*times */new_capacity);
146 if(new_buffer == 0) 111 if(new_buffer == 0)
147 return false; 112 return false;
148 bw->buffer = new_buffer; 113 bw->buffer = new_buffer;
149 bw->capacity = new_capacity; 114 bw->capacity = new_capacity;
150 return true; 115 return true;
151 } 116 }
152 117
153 118
154 /*********************************************************************** 119 /***********************************************************************
155 * 120 *
156 * Class constructor/destructor 121 * Class constructor/destructor
157 * 122 *
158 ***********************************************************************/ 123 ***********************************************************************/
159 124
160 FLAC__BitWriter *FLAC__bitwriter_new(void) 125 FLAC__BitWriter *FLAC__bitwriter_new(void)
161 { 126 {
162 » FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter )); 127 » FLAC__BitWriter *bw = calloc(1, sizeof(FLAC__BitWriter));
163 /* note that calloc() sets all members to 0 for us */ 128 /* note that calloc() sets all members to 0 for us */
164 return bw; 129 return bw;
165 } 130 }
166 131
167 void FLAC__bitwriter_delete(FLAC__BitWriter *bw) 132 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
168 { 133 {
169 FLAC__ASSERT(0 != bw); 134 FLAC__ASSERT(0 != bw);
170 135
171 FLAC__bitwriter_free(bw); 136 FLAC__bitwriter_free(bw);
172 free(bw); 137 free(bw);
173 } 138 }
174 139
175 /*********************************************************************** 140 /***********************************************************************
176 * 141 *
177 * Public class methods 142 * Public class methods
178 * 143 *
179 ***********************************************************************/ 144 ***********************************************************************/
180 145
181 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw) 146 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
182 { 147 {
183 FLAC__ASSERT(0 != bw); 148 FLAC__ASSERT(0 != bw);
184 149
185 bw->words = bw->bits = 0; 150 bw->words = bw->bits = 0;
186 bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY; 151 bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
187 » bw->buffer = (bwword*)malloc(sizeof(bwword) * bw->capacity); 152 » bw->buffer = malloc(sizeof(uint32_t) * bw->capacity);
188 if(bw->buffer == 0) 153 if(bw->buffer == 0)
189 return false; 154 return false;
190 155
191 return true; 156 return true;
192 } 157 }
193 158
194 void FLAC__bitwriter_free(FLAC__BitWriter *bw) 159 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
195 { 160 {
196 FLAC__ASSERT(0 != bw); 161 FLAC__ASSERT(0 != bw);
197 162
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 } 257 }
293 258
294 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw) 259 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
295 { 260 {
296 /* nothing to do. in the future, strict checking of a 'writer-is-in- 261 /* nothing to do. in the future, strict checking of a 'writer-is-in-
297 * get-mode' flag could be added everywhere and then cleared here 262 * get-mode' flag could be added everywhere and then cleared here
298 */ 263 */
299 (void)bw; 264 (void)bw;
300 } 265 }
301 266
302 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsign ed bits) 267 inline FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bit s)
303 { 268 {
304 unsigned n; 269 unsigned n;
305 270
306 FLAC__ASSERT(0 != bw); 271 FLAC__ASSERT(0 != bw);
307 FLAC__ASSERT(0 != bw->buffer); 272 FLAC__ASSERT(0 != bw->buffer);
308 273
309 if(bits == 0) 274 if(bits == 0)
310 return true; 275 return true;
311 /* slightly pessimistic size check but faster than "<= bw->words + (bw-> bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */ 276 /* slightly pessimistic size check but faster than "<= bw->words + (bw-> bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
312 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits)) 277 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
313 return false; 278 return false;
314 /* first part gets to word alignment */ 279 /* first part gets to word alignment */
315 if(bw->bits) { 280 if(bw->bits) {
316 » » n = min(FLAC__BITS_PER_WORD - bw->bits, bits); 281 » » n = flac_min(FLAC__BITS_PER_WORD - bw->bits, bits);
317 bw->accum <<= n; 282 bw->accum <<= n;
318 bits -= n; 283 bits -= n;
319 bw->bits += n; 284 bw->bits += n;
320 if(bw->bits == FLAC__BITS_PER_WORD) { 285 if(bw->bits == FLAC__BITS_PER_WORD) {
321 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum ); 286 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum );
322 bw->bits = 0; 287 bw->bits = 0;
323 } 288 }
324 else 289 else
325 return true; 290 return true;
326 } 291 }
327 /* do whole words */ 292 /* do whole words */
328 while(bits >= FLAC__BITS_PER_WORD) { 293 while(bits >= FLAC__BITS_PER_WORD) {
329 bw->buffer[bw->words++] = 0; 294 bw->buffer[bw->words++] = 0;
330 bits -= FLAC__BITS_PER_WORD; 295 bits -= FLAC__BITS_PER_WORD;
331 } 296 }
332 /* do any leftovers */ 297 /* do any leftovers */
333 if(bits > 0) { 298 if(bits > 0) {
334 bw->accum = 0; 299 bw->accum = 0;
335 bw->bits = bits; 300 bw->bits = bits;
336 } 301 }
337 return true; 302 return true;
338 } 303 }
339 304
340 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FL AC__uint32 val, unsigned bits) 305 inline FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__ui nt32 val, unsigned bits)
341 { 306 {
342 register unsigned left; 307 register unsigned left;
343 308
344 /* WATCHOUT: code does not work with <32bit words; we can make things mu ch faster with this assertion */ 309 /* WATCHOUT: code does not work with <32bit words; we can make things mu ch faster with this assertion */
345 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); 310 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
346 311
347 FLAC__ASSERT(0 != bw); 312 FLAC__ASSERT(0 != bw);
348 FLAC__ASSERT(0 != bw->buffer); 313 FLAC__ASSERT(0 != bw->buffer);
349 314
350 FLAC__ASSERT(bits <= 32); 315 FLAC__ASSERT(bits <= 32);
(...skipping 18 matching lines...) Expand all
369 } 334 }
370 else { 335 else {
371 bw->accum = val; 336 bw->accum = val;
372 bw->bits = 0; 337 bw->bits = 0;
373 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val); 338 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
374 } 339 }
375 340
376 return true; 341 return true;
377 } 342 }
378 343
379 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLA C__int32 val, unsigned bits) 344 inline FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int 32 val, unsigned bits)
380 { 345 {
381 /* zero-out unused bits */ 346 /* zero-out unused bits */
382 if(bits < 32) 347 if(bits < 32)
383 val &= (~(0xffffffff << bits)); 348 val &= (~(0xffffffff << bits));
384 349
385 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits); 350 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
386 } 351 }
387 352
388 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FL AC__uint64 val, unsigned bits) 353 inline FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__ui nt64 val, unsigned bits)
389 { 354 {
390 /* this could be a little faster but it's not used for much */ 355 /* this could be a little faster but it's not used for much */
391 if(bits > 32) { 356 if(bits > 32) {
392 return 357 return
393 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val> >32), bits-32) && 358 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val> >32), bits-32) &&
394 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32); 359 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
395 } 360 }
396 else 361 else
397 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, b its); 362 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, b its);
398 } 363 }
399 364
400 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__Bit Writer *bw, FLAC__uint32 val) 365 inline FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
401 { 366 {
402 /* this doesn't need to be that fast as currently it is only used for vo rbis comments */ 367 /* this doesn't need to be that fast as currently it is only used for vo rbis comments */
403 368
404 if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8)) 369 if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
405 return false; 370 return false;
406 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8)) 371 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
407 return false; 372 return false;
408 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8)) 373 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
409 return false; 374 return false;
410 if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8)) 375 if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
411 return false; 376 return false;
412 377
413 return true; 378 return true;
414 } 379 }
415 380
416 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, co nst FLAC__byte vals[], unsigned nvals) 381 inline FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FL AC__byte vals[], unsigned nvals)
417 { 382 {
418 unsigned i; 383 unsigned i;
419 384
420 /* this could be faster but currently we don't need it to be since it's only used for writing metadata */ 385 /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
421 for(i = 0; i < nvals; i++) { 386 for(i = 0; i < nvals; i++) {
422 if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]) , 8)) 387 if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]) , 8))
423 return false; 388 return false;
424 } 389 }
425 390
426 return true; 391 return true;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 { 507 {
543 const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|= mask1 to set the stop bit above it... */ 508 const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|= mask1 to set the stop bit above it... */
544 const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ... then mask off the bits above the stop bit with val&=mask2*/ 509 const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ... then mask off the bits above the stop bit with val&=mask2*/
545 FLAC__uint32 uval; 510 FLAC__uint32 uval;
546 unsigned left; 511 unsigned left;
547 const unsigned lsbits = 1 + parameter; 512 const unsigned lsbits = 1 + parameter;
548 unsigned msbits; 513 unsigned msbits;
549 514
550 FLAC__ASSERT(0 != bw); 515 FLAC__ASSERT(0 != bw);
551 FLAC__ASSERT(0 != bw->buffer); 516 FLAC__ASSERT(0 != bw->buffer);
552 » FLAC__ASSERT(parameter < 8*sizeof(bwword)-1); 517 » FLAC__ASSERT(parameter < 8*sizeof(uint32_t)-1);
553 /* WATCHOUT: code does not work with <32bit words; we can make things mu ch faster with this assertion */ 518 /* WATCHOUT: code does not work with <32bit words; we can make things mu ch faster with this assertion */
554 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); 519 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
555 520
556 while(nvals) { 521 while(nvals) {
557 /* fold signed to unsigned; actual formula is: negative(v)? -2v- 1 : 2v */ 522 /* fold signed to unsigned; actual formula is: negative(v)? -2v- 1 : 2v */
558 uval = (*vals<<1) ^ (*vals>>31); 523 uval = (*vals<<1) ^ (*vals>>31);
559 524
560 msbits = uval >> parameter; 525 msbits = uval >> parameter;
561 526
562 #if 0 /* OPT: can remove this special case if it doesn't make up for the extra c ompare (doesn't make a statistically significant difference with msvc or gcc/x86 ) */ 527 » » if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current uint32_t */
563 » » if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD ) { /* i.e. if the whole thing fits in the current bwword */ 528 » » » /* ^^^ if bw->bits is 0 then we may have filled the buff er and have no free uint32_t to work in */
564 » » » /* ^^^ if bw->bits is 0 then we may have filled the buff er and have no free bwword to work in */
565 bw->bits = bw->bits + msbits + lsbits; 529 bw->bits = bw->bits + msbits + lsbits;
566 uval |= mask1; /* set stop bit */ 530 uval |= mask1; /* set stop bit */
567 uval &= mask2; /* mask off unused top bits */ 531 uval &= mask2; /* mask off unused top bits */
568 /* NOT: bw->accum <<= msbits + lsbits because msbits+lsb its could be 32, then the shift would be a NOP */
569 bw->accum <<= msbits;
570 bw->accum <<= lsbits;
571 bw->accum |= uval;
572 if(bw->bits == FLAC__BITS_PER_WORD) {
573 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(b w->accum);
574 bw->bits = 0;
575 /* burying the capacity check down here means we have to grow the buffer a little if there are more vals to do */
576 if(bw->capacity <= bw->words && nvals > 1 && !bi twriter_grow_(bw, 1)) {
577 FLAC__ASSERT(bw->capacity == bw->words);
578 return false;
579 }
580 }
581 }
582 else {
583 #elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much dif ference for gcc-4 */
584 if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
585 /* ^^^ if bw->bits is 0 then we may have filled the buff er and have no free bwword to work in */
586 bw->bits = bw->bits + msbits + lsbits;
587 uval |= mask1; /* set stop bit */
588 uval &= mask2; /* mask off unused top bits */
589 bw->accum <<= msbits + lsbits; 532 bw->accum <<= msbits + lsbits;
590 bw->accum |= uval; 533 bw->accum |= uval;
591 } 534 }
592 else { 535 else {
593 #endif
594 /* slightly pessimistic size check but faster than "<= b w->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" * / 536 /* slightly pessimistic size check but faster than "<= b w->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" * /
595 /* OPT: pessimism may cause flurry of false calls to gro w_ which eat up all savings before it */ 537 /* OPT: pessimism may cause flurry of false calls to gro w_ which eat up all savings before it */
596 » » » if(bw->capacity <= bw->words + bw->bits + msbits + 1/*ls bits always fit in 1 bwword*/ && !bitwriter_grow_(bw, msbits+lsbits)) 538 » » » if(bw->capacity <= bw->words + bw->bits + msbits + 1/*ls bits always fit in 1 uint32_t*/ && !bitwriter_grow_(bw, msbits+lsbits))
597 return false; 539 return false;
598 540
599 if(msbits) { 541 if(msbits) {
600 /* first part gets to word alignment */ 542 /* first part gets to word alignment */
601 if(bw->bits) { 543 if(bw->bits) {
602 left = FLAC__BITS_PER_WORD - bw->bits; 544 left = FLAC__BITS_PER_WORD - bw->bits;
603 if(msbits < left) { 545 if(msbits < left) {
604 bw->accum <<= msbits; 546 bw->accum <<= msbits;
605 bw->bits += msbits; 547 bw->bits += msbits;
606 goto break1; 548 goto break1;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 * be > lsbits (because of previous assertions) so it would have 580 * be > lsbits (because of previous assertions) so it would have
639 * triggered the (lsbits<left) case above. 581 * triggered the (lsbits<left) case above.
640 */ 582 */
641 FLAC__ASSERT(bw->bits); 583 FLAC__ASSERT(bw->bits);
642 FLAC__ASSERT(left < FLAC__BITS_PER_WORD); 584 FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
643 bw->accum <<= left; 585 bw->accum <<= left;
644 bw->accum |= uval >> (bw->bits = lsbits - left); 586 bw->accum |= uval >> (bw->bits = lsbits - left);
645 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(b w->accum); 587 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(b w->accum);
646 bw->accum = uval; 588 bw->accum = uval;
647 } 589 }
648 #if 1
649 } 590 }
650 #endif
651 vals++; 591 vals++;
652 nvals--; 592 nvals--;
653 } 593 }
654 return true; 594 return true;
655 } 595 }
656 596
657 #if 0 /* UNUSED */ 597 #if 0 /* UNUSED */
658 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, uns igned parameter) 598 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, uns igned parameter)
659 { 599 {
660 unsigned total_bits, msbs, uval; 600 unsigned total_bits, msbs, uval;
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 } 820 }
881 821
882 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw) 822 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
883 { 823 {
884 /* 0-pad to byte boundary */ 824 /* 0-pad to byte boundary */
885 if(bw->bits & 7u) 825 if(bw->bits & 7u)
886 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u)); 826 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
887 else 827 else
888 return true; 828 return true;
889 } 829 }
830
831 /* These functions are declared inline in this file but are also callable as
832 * externs from elsewhere.
833 * According to the C99 spec, section 6.7.4, simply providing a function
834 * prototype in a header file without 'inline' and making the function inline
835 * in this file should be sufficient.
836 * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To
837 * fix that we add extern declarations here.
838 */
839 extern FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bit s);
840 extern FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int 32 val, unsigned bits);
841 extern FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__ui nt64 val, unsigned bits);
842 extern FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val);
843 extern FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FL AC__byte vals[], unsigned nvals);
OLDNEW
« no previous file with comments | « src/libFLAC/bitreader.c ('k') | src/libFLAC/cpu.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698