]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - src/patches/flac-1.2.1-bitreader.patch
udev: remove /var/run mount from initskript.
[people/teissler/ipfire-2.x.git] / src / patches / flac-1.2.1-bitreader.patch
CommitLineData
946a2afe
MT
1Index: src/libFLAC/bitreader.c
2===================================================================
3RCS file: /cvsroot/flac/flac/src/libFLAC/bitreader.c,v
4retrieving revision 1.15
5diff -u -r1.15 bitreader.c
6--- src/libFLAC/bitreader.c 28 Feb 2008 05:34:26 -0000 1.15
7+++ src/libFLAC/bitreader.c 14 Mar 2008 11:07:07 -0000
8@@ -69,13 +69,12 @@
9 #endif
10 /* counts the # of zero MSBs in a word */
11 #define COUNT_ZERO_MSBS(word) ( \
12- (word) <= 0xffff ? \
13- ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
14- ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
15+ word > 0xffffff ? byte_to_unary_table[(word) >> 24] : \
16+ !word ? 32 : \
17+ word > 0xffff ? byte_to_unary_table[word >> 16] + 8 : \
18+ word > 0xff ? byte_to_unary_table[(word) >> 8] + 16 : \
19+ byte_to_unary_table[word] + 24 \
20 )
21-/* this alternate might be slightly faster on some systems/compilers: */
22-#define COUNT_ZERO_MSBS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) )
23-
24
25 /*
26 * This should be at least twice as large as the largest number of words
27Index: src/libFLAC/bitreader.c
28===================================================================
29RCS file: /cvsroot/flac/flac/src/libFLAC/bitreader.c,v
30retrieving revision 1.15
31diff -u -r1.15 bitreader.c
32--- src/libFLAC/bitreader.c 28 Feb 2008 05:34:26 -0000 1.15
33+++ src/libFLAC/bitreader.c 14 Mar 2008 13:19:46 -0000
34@@ -149,6 +148,7 @@
35 FLAC__CPUInfo cpu_info;
36 };
37
38+#if FLAC__BYTES_PER_WORD == 4 && FLAC__CPU_IA32
39 #ifdef _MSC_VER
40 /* OPT: an MSVC built-in would be better */
41 static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
42@@ -173,6 +173,15 @@
43 done1:
44 }
45 }
46+#elif __GNUC__
47+static void local_swap32_block_(FLAC__uint32 *start, FLAC__uint32 len)
48+{
49+ FLAC__uint32 *end;
50+
51+ for(end = start + len; start < end; start++)
52+ asm ("bswap %0" : "=r"(*start) : "0"(*start));
53+}
54+#endif
55 #endif
56
57 static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
58@@ -263,7 +272,7 @@
59 #if WORDS_BIGENDIAN
60 #else
61 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
62-# if defined(_MSC_VER) && (FLAC__BYTES_PER_WORD == 4)
63+# if FLAC__CPU_IA32 && (__GNUC__ || defined(_MSC_VER)) && FLAC__BYTES_PER_WORD == 4
64 if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia32.bswap) {
65 start = br->words;
66 local_swap32_block_(br->buffer + start, end - start);
67Index: src/libFLAC/bitreader.c
68===================================================================
69RCS file: /cvsroot/flac/flac/src/libFLAC/bitreader.c,v
70retrieving revision 1.15
71diff -u -r1.15 bitreader.c
72--- src/libFLAC/bitreader.c 28 Feb 2008 05:34:26 -0000 1.15
73+++ src/libFLAC/bitreader.c 17 Mar 2008 15:42:57 -0000
74@@ -803,379 +812,144 @@
75 }
76
77 /* this is by far the most heavily used reader call. it ain't pretty but it's fast */
78-/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
79 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
80-/* OPT: possibly faster version for use with MSVC */
81-#ifdef _MSC_VER
82 {
83- unsigned i;
84- unsigned uval = 0;
85- unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
86-
87 /* try and get br->consumed_words and br->consumed_bits into register;
88 * must remember to flush them back to *br before calling other
89- * bitwriter functions that use them, and before returning */
90- register unsigned cwords;
91- register unsigned cbits;
92+ * bitreader functions that use them, and before returning */
93+ unsigned cwords, words, lsbs, msbs, x, y;
94+ unsigned ucbits; /* keep track of the number of unconsumed bits in word */
95+ brword b;
96+ int *val, *end;
97
98 FLAC__ASSERT(0 != br);
99 FLAC__ASSERT(0 != br->buffer);
100 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
101 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
102 FLAC__ASSERT(parameter < 32);
103- /* the above two asserts also guarantee that the binary part never straddles more that 2 words, so we don't have to loop to read it */
104-
105- if(nvals == 0)
106- return true;
107-
108- cbits = br->consumed_bits;
109- cwords = br->consumed_words;
110+ /* the above two asserts also guarantee that the binary part never straddles more than 2 words, so we don't have to loop to read it */
111
112- while(1) {
113+ val = vals;
114+ end = vals + nvals;
115
116- /* read unary part */
117- while(1) {
118- while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
119- brword b = br->buffer[cwords] << cbits;
120- if(b) {
121-#if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
122- __asm {
123- bsr eax, b
124- not eax
125- and eax, 31
126- mov i, eax
127- }
128-#else
129- i = COUNT_ZERO_MSBS(b);
130-#endif
131- uval += i;
132- bits = parameter;
133- i++;
134- cbits += i;
135- if(cbits == FLAC__BITS_PER_WORD) {
136- crc16_update_word_(br, br->buffer[cwords]);
137- cwords++;
138- cbits = 0;
139- }
140- goto break1;
141- }
142- else {
143- uval += FLAC__BITS_PER_WORD - cbits;
144- crc16_update_word_(br, br->buffer[cwords]);
145- cwords++;
146- cbits = 0;
147- /* didn't find stop bit yet, have to keep going... */
148- }
149- }
150- /* at this point we've eaten up all the whole words; have to try
151- * reading through any tail bytes before calling the read callback.
152- * this is a repeat of the above logic adjusted for the fact we
153- * don't have a whole word. note though if the client is feeding
154- * us data a byte at a time (unlikely), br->consumed_bits may not
155- * be zero.
156- */
157- if(br->bytes) {
158- const unsigned end = br->bytes * 8;
159- brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
160- if(b) {
161- i = COUNT_ZERO_MSBS(b);
162- uval += i;
163- bits = parameter;
164- i++;
165- cbits += i;
166- FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
167- goto break1;
168- }
169- else {
170- uval += end - cbits;
171- cbits += end;
172- FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
173- /* didn't find stop bit yet, have to keep going... */
174- }
175- }
176- /* flush registers and read; bitreader_read_from_client_() does
177- * not touch br->consumed_bits at all but we still need to set
178- * it in case it fails and we have to return false.
179- */
180- br->consumed_bits = cbits;
181- br->consumed_words = cwords;
182- if(!bitreader_read_from_client_(br))
183+ if(parameter == 0) {
184+ while(val < end) {
185+ /* read the unary MSBs and end bit */
186+ if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
187 return false;
188- cwords = br->consumed_words;
189- }
190-break1:
191- /* read binary part */
192- FLAC__ASSERT(cwords <= br->words);
193-
194- if(bits) {
195- while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
196- /* flush registers and read; bitreader_read_from_client_() does
197- * not touch br->consumed_bits at all but we still need to set
198- * it in case it fails and we have to return false.
199- */
200- br->consumed_bits = cbits;
201- br->consumed_words = cwords;
202- if(!bitreader_read_from_client_(br))
203- return false;
204- cwords = br->consumed_words;
205- }
206- if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
207- if(cbits) {
208- /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
209- const unsigned n = FLAC__BITS_PER_WORD - cbits;
210- const brword word = br->buffer[cwords];
211- if(bits < n) {
212- uval <<= bits;
213- uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
214- cbits += bits;
215- goto break2;
216- }
217- uval <<= n;
218- uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
219- bits -= n;
220- crc16_update_word_(br, word);
221- cwords++;
222- cbits = 0;
223- if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
224- uval <<= bits;
225- uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
226- cbits = bits;
227- }
228- goto break2;
229- }
230- else {
231- FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
232- uval <<= bits;
233- uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
234- cbits = bits;
235- goto break2;
236- }
237- }
238- else {
239- /* in this case we're starting our read at a partial tail word;
240- * the reader has guaranteed that we have at least 'bits' bits
241- * available to read, which makes this case simpler.
242- */
243- uval <<= bits;
244- if(cbits) {
245- /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
246- FLAC__ASSERT(cbits + bits <= br->bytes*8);
247- uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
248- cbits += bits;
249- goto break2;
250- }
251- else {
252- uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
253- cbits += bits;
254- goto break2;
255- }
256- }
257- }
258-break2:
259- /* compose the value */
260- *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
261
262- /* are we done? */
263- --nvals;
264- if(nvals == 0) {
265- br->consumed_bits = cbits;
266- br->consumed_words = cwords;
267- return true;
268+ *val++ = (int)(msbs >> 1) ^ -(int)(msbs & 1);
269 }
270
271- uval = 0;
272- ++vals;
273-
274+ return true;
275 }
276-}
277-#else
278-{
279- unsigned i;
280- unsigned uval = 0;
281
282- /* try and get br->consumed_words and br->consumed_bits into register;
283- * must remember to flush them back to *br before calling other
284- * bitwriter functions that use them, and before returning */
285- register unsigned cwords;
286- register unsigned cbits;
287- unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
288+ FLAC__ASSERT(parameter > 0);
289
290- FLAC__ASSERT(0 != br);
291- FLAC__ASSERT(0 != br->buffer);
292- /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
293- FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
294- FLAC__ASSERT(parameter < 32);
295- /* the above two asserts also guarantee that the binary part never straddles more than 2 words, so we don't have to loop to read it */
296+ cwords = br->consumed_words;
297+ words = br->words;
298
299- if(nvals == 0)
300- return true;
301+ /* if we've not consumed up to a partial tail word... */
302+ if(cwords >= words) {
303+ x = 0;
304+ goto process_tail;
305+ }
306+
307+ ucbits = FLAC__BITS_PER_WORD - br->consumed_bits;
308+ b = br->buffer[cwords] << br->consumed_bits; /* keep unconsumed bits aligned to left */
309+
310+ while(val < end) {
311+ /* read the unary MSBs and end bit */
312+ x = y = COUNT_ZERO_MSBS(b);
313+ if(x == FLAC__BITS_PER_WORD) {
314+ x = ucbits;
315+ do {
316+ /* didn't find stop bit yet, have to keep going... */
317+ crc16_update_word_(br, br->buffer[cwords++]);
318+ if (cwords >= words)
319+ goto incomplete_msbs;
320+ b = br->buffer[cwords];
321+ y = COUNT_ZERO_MSBS(b);
322+ x += y;
323+ } while(y == FLAC__BITS_PER_WORD);
324+ }
325+ b <<= y;
326+ b <<= 1; /* account for stop bit */
327+ ucbits = (ucbits - x - 1) % FLAC__BITS_PER_WORD;
328+ msbs = x;
329+
330+ /* read the binary LSBs */
331+ x = b >> (FLAC__BITS_PER_WORD - parameter);
332+ if(parameter <= ucbits) {
333+ ucbits -= parameter;
334+ b <<= parameter;
335+ } else {
336+ /* there are still bits left to read, they will all be in the next word */
337+ crc16_update_word_(br, br->buffer[cwords++]);
338+ if (cwords >= words)
339+ goto incomplete_lsbs;
340+ b = br->buffer[cwords];
341+ ucbits += FLAC__BITS_PER_WORD - parameter;
342+ x |= b >> ucbits;
343+ b <<= FLAC__BITS_PER_WORD - ucbits;
344+ }
345+ lsbs = x;
346
347- cbits = br->consumed_bits;
348- cwords = br->consumed_words;
349- ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
350+ /* compose the value */
351+ x = (msbs << parameter) | lsbs;
352+ *val++ = (int)(x >> 1) ^ -(int)(x & 1);
353
354- while(1) {
355+ continue;
356
357- /* read unary part */
358- while(1) {
359- while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
360- brword b = br->buffer[cwords] << cbits;
361- if(b) {
362-#if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
363- asm volatile (
364- "bsrl %1, %0;"
365- "notl %0;"
366- "andl $31, %0;"
367- : "=r"(i)
368- : "r"(b)
369- );
370-#else
371- i = COUNT_ZERO_MSBS(b);
372-#endif
373- uval += i;
374- cbits += i;
375- cbits++; /* skip over stop bit */
376- if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
377- crc16_update_word_(br, br->buffer[cwords]);
378- cwords++;
379- cbits = 0;
380- }
381- goto break1;
382- }
383- else {
384- uval += FLAC__BITS_PER_WORD - cbits;
385- crc16_update_word_(br, br->buffer[cwords]);
386- cwords++;
387- cbits = 0;
388- /* didn't find stop bit yet, have to keep going... */
389- }
390- }
391- /* at this point we've eaten up all the whole words; have to try
392- * reading through any tail bytes before calling the read callback.
393- * this is a repeat of the above logic adjusted for the fact we
394- * don't have a whole word. note though if the client is feeding
395- * us data a byte at a time (unlikely), br->consumed_bits may not
396- * be zero.
397- */
398- if(br->bytes) {
399- const unsigned end = br->bytes * 8;
400- brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
401- if(b) {
402- i = COUNT_ZERO_MSBS(b);
403- uval += i;
404- cbits += i;
405- cbits++; /* skip over stop bit */
406- FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
407- goto break1;
408- }
409- else {
410- uval += end - cbits;
411- cbits += end;
412- FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
413- /* didn't find stop bit yet, have to keep going... */
414- }
415+ /* at this point we've eaten up all the whole words */
416+process_tail:
417+ do {
418+ if(0) {
419+incomplete_msbs:
420+ br->consumed_bits = 0;
421+ br->consumed_words = cwords;
422 }
423- /* flush registers and read; bitreader_read_from_client_() does
424- * not touch br->consumed_bits at all but we still need to set
425- * it in case it fails and we have to return false.
426- */
427- br->consumed_bits = cbits;
428- br->consumed_words = cwords;
429- if(!bitreader_read_from_client_(br))
430+
431+ /* read the unary MSBs and end bit */
432+ if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
433 return false;
434- cwords = br->consumed_words;
435- ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
436- /* + uval to offset our count by the # of unary bits already
437- * consumed before the read, because we will add these back
438- * in all at once at break1
439- */
440- }
441-break1:
442- ucbits -= uval;
443- ucbits--; /* account for stop bit */
444-
445- /* read binary part */
446- FLAC__ASSERT(cwords <= br->words);
447-
448- if(parameter) {
449- while(ucbits < parameter) {
450- /* flush registers and read; bitreader_read_from_client_() does
451- * not touch br->consumed_bits at all but we still need to set
452- * it in case it fails and we have to return false.
453- */
454- br->consumed_bits = cbits;
455+ msbs += x;
456+ x = ucbits = 0;
457+
458+ if(0) {
459+incomplete_lsbs:
460+ br->consumed_bits = 0;
461 br->consumed_words = cwords;
462- if(!bitreader_read_from_client_(br))
463- return false;
464- cwords = br->consumed_words;
465- ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
466- }
467- if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
468- if(cbits) {
469- /* this also works when consumed_bits==0, it's just slower than necessary for that case */
470- const unsigned n = FLAC__BITS_PER_WORD - cbits;
471- const brword word = br->buffer[cwords];
472- if(parameter < n) {
473- uval <<= parameter;
474- uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
475- cbits += parameter;
476- }
477- else {
478- uval <<= n;
479- uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
480- crc16_update_word_(br, word);
481- cwords++;
482- cbits = parameter - n;
483- if(cbits) { /* parameter > n, i.e. if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
484- uval <<= cbits;
485- uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
486- }
487- }
488- }
489- else {
490- cbits = parameter;
491- uval <<= parameter;
492- uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
493- }
494 }
495- else {
496- /* in this case we're starting our read at a partial tail word;
497- * the reader has guaranteed that we have at least 'parameter'
498- * bits available to read, which makes this case simpler.
499- */
500- uval <<= parameter;
501- if(cbits) {
502- /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
503- FLAC__ASSERT(cbits + parameter <= br->bytes*8);
504- uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
505- cbits += parameter;
506- }
507- else {
508- cbits = parameter;
509- uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
510- }
511- }
512- }
513
514- ucbits -= parameter;
515-
516- /* compose the value */
517- *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
518+ /* read the binary LSBs */
519+ if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter - ucbits))
520+ return false;
521+ lsbs = x | lsbs;
522
523- /* are we done? */
524- --nvals;
525- if(nvals == 0) {
526- br->consumed_bits = cbits;
527- br->consumed_words = cwords;
528- return true;
529- }
530+ /* compose the value */
531+ x = (msbs << parameter) | lsbs;
532+ *val++ = (int)(x >> 1) ^ -(int)(x & 1);
533+ x = 0;
534
535- uval = 0;
536- ++vals;
537+ cwords = br->consumed_words;
538+ words = br->words;
539+ ucbits = FLAC__BITS_PER_WORD - br->consumed_bits;
540+ b = br->buffer[cwords] << br->consumed_bits;
541+ } while(cwords >= words && val < end);
542+ }
543
544+ if(ucbits == 0 && cwords < words) {
545+ /* don't leave the head word with no unconsumed bits */
546+ crc16_update_word_(br, br->buffer[cwords++]);
547+ ucbits = FLAC__BITS_PER_WORD;
548 }
549+
550+ br->consumed_bits = FLAC__BITS_PER_WORD - ucbits;
551+ br->consumed_words = cwords;
552+
553+ return true;
554 }
555-#endif
556
557 #if 0 /* UNUSED */
558 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)