flac_bitreader.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - 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. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * 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. */
  31. #if HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include <stdlib.h> /* for malloc() */
  35. #include <string.h> /* for memcpy(), memset() */
  36. #ifdef _WIN32
  37. #include <winsock.h> /* for ntohl() */
  38. #elif defined macintosh
  39. #include <machine/endian.h> /* for ntohl() */
  40. #else
  41. #include <netinet/in.h> /* for ntohl() */
  42. #endif
  43. #include "flac_private_bitmath.h"
  44. #include "flac_private_bitreader.h"
  45. #include "flac_private_crc.h"
  46. #include "flac_FLAC_assert.h"
  47. /* Things should be fastest when this matches the machine word size */
  48. /* WATCHOUT: if you change this you must also change the following #defines down to COUNT_ZERO_MSBS below to match */
  49. /* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */
  50. /* also, some sections currently only have fast versions for 4 or 8 bytes per word */
  51. typedef FLAC__uint32 brword;
  52. #define FLAC__BYTES_PER_WORD 4
  53. #define FLAC__BITS_PER_WORD 32
  54. #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
  55. /* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */
  56. #if WORDS_BIGENDIAN
  57. #define SWAP_BE_WORD_TO_HOST(x) (x)
  58. #else
  59. #ifdef _MSC_VER
  60. #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
  61. #else
  62. #define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
  63. #endif
  64. #endif
  65. /* counts the # of zero MSBs in a word */
  66. #define COUNT_ZERO_MSBS(word) ( \
  67. (word) <= 0xffff ? \
  68. ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
  69. ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
  70. )
  71. /* this alternate might be slightly faster on some systems/compilers: */
  72. #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])) )
  73. /*
  74. * This should be at least twice as large as the largest number of words
  75. * required to represent any 'number' (in any encoding) you are going to
  76. * read. With FLAC this is on the order of maybe a few hundred bits.
  77. * If the buffer is smaller than that, the decoder won't be able to read
  78. * in a whole number that is in a variable length encoding (e.g. Rice).
  79. * But to be practical it should be at least 1K bytes.
  80. *
  81. * Increase this number to decrease the number of read callbacks, at the
  82. * expense of using more memory. Or decrease for the reverse effect,
  83. * keeping in mind the limit from the first paragraph. The optimal size
  84. * also depends on the CPU cache size and other factors; some twiddling
  85. * may be necessary to squeeze out the best performance.
  86. */
  87. static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
  88. static const unsigned char byte_to_unary_table[] = {
  89. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  90. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  91. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  92. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  93. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  94. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  95. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  96. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  97. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  98. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  99. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  100. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  101. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  102. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  103. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  104. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  105. };
  106. #ifdef min
  107. #undef min
  108. #endif
  109. #define min(x,y) ((x)<(y)?(x):(y))
  110. #ifdef max
  111. #undef max
  112. #endif
  113. #define max(x,y) ((x)>(y)?(x):(y))
  114. /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
  115. #ifdef _MSC_VER
  116. #define FLAC__U64L(x) x
  117. #else
  118. #define FLAC__U64L(x) x##LLU
  119. #endif
  120. #ifndef FLaC__INLINE
  121. #define FLaC__INLINE
  122. #endif
  123. /* WATCHOUT: assembly routines rely on the order in which these fields are declared */
  124. struct FLAC__BitReader {
  125. /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
  126. /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
  127. brword *buffer;
  128. unsigned capacity; /* in words */
  129. unsigned words; /* # of completed words in buffer */
  130. unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
  131. unsigned consumed_words; /* #words ... */
  132. unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
  133. unsigned read_crc16; /* the running frame CRC */
  134. unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
  135. FLAC__BitReaderReadCallback read_callback;
  136. void *client_data;
  137. FLAC__CPUInfo cpu_info;
  138. };
  139. #ifdef _MSC_VER
  140. /* OPT: an MSVC built-in would be better */
  141. static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
  142. {
  143. x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
  144. return (x>>16) | (x<<16);
  145. }
  146. static void local_swap32_block_(FLAC__uint32 *start, FLAC__uint32 len)
  147. {
  148. __asm {
  149. mov edx, start
  150. mov ecx, len
  151. test ecx, ecx
  152. loop1:
  153. jz done1
  154. mov eax, [edx]
  155. bswap eax
  156. mov [edx], eax
  157. add edx, 4
  158. dec ecx
  159. jmp short loop1
  160. done1:
  161. }
  162. }
  163. #endif
  164. static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
  165. {
  166. register unsigned crc = br->read_crc16;
  167. #if FLAC__BYTES_PER_WORD == 4
  168. switch(br->crc16_align) {
  169. case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
  170. case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
  171. case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
  172. case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
  173. }
  174. #elif FLAC__BYTES_PER_WORD == 8
  175. switch(br->crc16_align) {
  176. case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
  177. case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
  178. case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
  179. case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
  180. case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
  181. case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
  182. case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
  183. case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
  184. }
  185. #else
  186. for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
  187. crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
  188. br->read_crc16 = crc;
  189. #endif
  190. br->crc16_align = 0;
  191. }
  192. /* would be static except it needs to be called by asm routines */
  193. FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
  194. {
  195. unsigned start, end;
  196. size_t bytes;
  197. FLAC__byte *target;
  198. /* first shift the unconsumed buffer data toward the front as much as possible */
  199. if(br->consumed_words > 0) {
  200. start = br->consumed_words;
  201. end = br->words + (br->bytes? 1:0);
  202. memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
  203. br->words -= start;
  204. br->consumed_words = 0;
  205. }
  206. /*
  207. * set the target for reading, taking into account word alignment and endianness
  208. */
  209. bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
  210. if(bytes == 0)
  211. return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
  212. target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
  213. /* before reading, if the existing reader looks like this (say brword is 32 bits wide)
  214. * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
  215. * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
  216. * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
  217. * ^^-------target, bytes=3
  218. * on LE machines, have to byteswap the odd tail word so nothing is
  219. * overwritten:
  220. */
  221. #if WORDS_BIGENDIAN
  222. #else
  223. if(br->bytes)
  224. br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
  225. #endif
  226. /* now it looks like:
  227. * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
  228. * buffer[BE]: 11 22 33 44 55 ?? ?? ??
  229. * buffer[LE]: 44 33 22 11 55 ?? ?? ??
  230. * ^^-------target, bytes=3
  231. */
  232. /* read in the data; note that the callback may return a smaller number of bytes */
  233. if(!br->read_callback(target, &bytes, br->client_data))
  234. return false;
  235. /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
  236. * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
  237. * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
  238. * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
  239. * now have to byteswap on LE machines:
  240. */
  241. #if WORDS_BIGENDIAN
  242. #else
  243. end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
  244. # if defined(_MSC_VER) && (FLAC__BYTES_PER_WORD == 4)
  245. if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia32.bswap) {
  246. start = br->words;
  247. local_swap32_block_(br->buffer + start, end - start);
  248. }
  249. else
  250. # endif
  251. for(start = br->words; start < end; start++)
  252. br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
  253. #endif
  254. /* now it looks like:
  255. * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
  256. * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
  257. * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
  258. * finally we'll update the reader values:
  259. */
  260. end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
  261. br->words = end / FLAC__BYTES_PER_WORD;
  262. br->bytes = end % FLAC__BYTES_PER_WORD;
  263. return true;
  264. }
  265. /***********************************************************************
  266. *
  267. * Class constructor/destructor
  268. *
  269. ***********************************************************************/
  270. FLAC__BitReader *FLAC__bitreader_new(void)
  271. {
  272. FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
  273. /* calloc() implies:
  274. memset(br, 0, sizeof(FLAC__BitReader));
  275. br->buffer = 0;
  276. br->capacity = 0;
  277. br->words = br->bytes = 0;
  278. br->consumed_words = br->consumed_bits = 0;
  279. br->read_callback = 0;
  280. br->client_data = 0;
  281. */
  282. return br;
  283. }
  284. void FLAC__bitreader_delete(FLAC__BitReader *br)
  285. {
  286. FLAC__ASSERT(0 != br);
  287. FLAC__bitreader_free(br);
  288. free(br);
  289. }
  290. /***********************************************************************
  291. *
  292. * Public class methods
  293. *
  294. ***********************************************************************/
  295. FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
  296. {
  297. FLAC__ASSERT(0 != br);
  298. br->words = br->bytes = 0;
  299. br->consumed_words = br->consumed_bits = 0;
  300. br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
  301. br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
  302. if(br->buffer == 0)
  303. return false;
  304. br->read_callback = rcb;
  305. br->client_data = cd;
  306. br->cpu_info = cpu;
  307. return true;
  308. }
  309. void FLAC__bitreader_free(FLAC__BitReader *br)
  310. {
  311. FLAC__ASSERT(0 != br);
  312. if(0 != br->buffer)
  313. free(br->buffer);
  314. br->buffer = 0;
  315. br->capacity = 0;
  316. br->words = br->bytes = 0;
  317. br->consumed_words = br->consumed_bits = 0;
  318. br->read_callback = 0;
  319. br->client_data = 0;
  320. }
  321. FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
  322. {
  323. br->words = br->bytes = 0;
  324. br->consumed_words = br->consumed_bits = 0;
  325. return true;
  326. }
  327. void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
  328. {
  329. unsigned i, j;
  330. if(br == 0) {
  331. fprintf(out, "bitreader is NULL\n");
  332. }
  333. else {
  334. fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits);
  335. for(i = 0; i < br->words; i++) {
  336. fprintf(out, "%08X: ", i);
  337. for(j = 0; j < FLAC__BITS_PER_WORD; j++)
  338. if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
  339. fprintf(out, ".");
  340. else
  341. fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
  342. fprintf(out, "\n");
  343. }
  344. if(br->bytes > 0) {
  345. fprintf(out, "%08X: ", i);
  346. for(j = 0; j < br->bytes*8; j++)
  347. if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
  348. fprintf(out, ".");
  349. else
  350. fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
  351. fprintf(out, "\n");
  352. }
  353. }
  354. }
  355. void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
  356. {
  357. FLAC__ASSERT(0 != br);
  358. FLAC__ASSERT(0 != br->buffer);
  359. FLAC__ASSERT((br->consumed_bits & 7) == 0);
  360. br->read_crc16 = (unsigned)seed;
  361. br->crc16_align = br->consumed_bits;
  362. }
  363. FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
  364. {
  365. FLAC__ASSERT(0 != br);
  366. FLAC__ASSERT(0 != br->buffer);
  367. FLAC__ASSERT((br->consumed_bits & 7) == 0);
  368. FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
  369. /* CRC any tail bytes in a partially-consumed word */
  370. if(br->consumed_bits) {
  371. const brword tail = br->buffer[br->consumed_words];
  372. for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
  373. br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
  374. }
  375. return br->read_crc16;
  376. }
  377. FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
  378. {
  379. return ((br->consumed_bits & 7) == 0);
  380. }
  381. FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
  382. {
  383. return 8 - (br->consumed_bits & 7);
  384. }
  385. FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
  386. {
  387. return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
  388. }
  389. FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
  390. {
  391. FLAC__ASSERT(0 != br);
  392. FLAC__ASSERT(0 != br->buffer);
  393. FLAC__ASSERT(bits <= 32);
  394. FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
  395. FLAC__ASSERT(br->consumed_words <= br->words);
  396. /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
  397. FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
  398. if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
  399. *val = 0;
  400. return true;
  401. }
  402. while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
  403. if(!bitreader_read_from_client_(br))
  404. return false;
  405. }
  406. if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
  407. /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
  408. if(br->consumed_bits) {
  409. /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
  410. const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
  411. const brword word = br->buffer[br->consumed_words];
  412. if(bits < n) {
  413. *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
  414. br->consumed_bits += bits;
  415. return true;
  416. }
  417. *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
  418. bits -= n;
  419. crc16_update_word_(br, word);
  420. br->consumed_words++;
  421. br->consumed_bits = 0;
  422. 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 */
  423. *val <<= bits;
  424. *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
  425. br->consumed_bits = bits;
  426. }
  427. return true;
  428. }
  429. else {
  430. const brword word = br->buffer[br->consumed_words];
  431. if(bits < FLAC__BITS_PER_WORD) {
  432. *val = word >> (FLAC__BITS_PER_WORD-bits);
  433. br->consumed_bits = bits;
  434. return true;
  435. }
  436. /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
  437. *val = word;
  438. crc16_update_word_(br, word);
  439. br->consumed_words++;
  440. return true;
  441. }
  442. }
  443. else {
  444. /* in this case we're starting our read at a partial tail word;
  445. * the reader has guaranteed that we have at least 'bits' bits
  446. * available to read, which makes this case simpler.
  447. */
  448. /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
  449. if(br->consumed_bits) {
  450. /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
  451. FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
  452. *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
  453. br->consumed_bits += bits;
  454. return true;
  455. }
  456. else {
  457. *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
  458. br->consumed_bits += bits;
  459. return true;
  460. }
  461. }
  462. }
  463. FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
  464. {
  465. /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
  466. if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
  467. return false;
  468. /* sign-extend: */
  469. *val <<= (32-bits);
  470. *val >>= (32-bits);
  471. return true;
  472. }
  473. FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
  474. {
  475. FLAC__uint32 hi, lo;
  476. if(bits > 32) {
  477. if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
  478. return false;
  479. if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
  480. return false;
  481. *val = hi;
  482. *val <<= 32;
  483. *val |= lo;
  484. }
  485. else {
  486. if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
  487. return false;
  488. *val = lo;
  489. }
  490. return true;
  491. }
  492. FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
  493. {
  494. FLAC__uint32 x8, x32 = 0;
  495. /* this doesn't need to be that fast as currently it is only used for vorbis comments */
  496. if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
  497. return false;
  498. if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
  499. return false;
  500. x32 |= (x8 << 8);
  501. if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
  502. return false;
  503. x32 |= (x8 << 16);
  504. if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
  505. return false;
  506. x32 |= (x8 << 24);
  507. *val = x32;
  508. return true;
  509. }
  510. FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
  511. {
  512. /*
  513. * OPT: a faster implementation is possible but probably not that useful
  514. * since this is only called a couple of times in the metadata readers.
  515. */
  516. FLAC__ASSERT(0 != br);
  517. FLAC__ASSERT(0 != br->buffer);
  518. if(bits > 0) {
  519. const unsigned n = br->consumed_bits & 7;
  520. unsigned m;
  521. FLAC__uint32 x;
  522. if(n != 0) {
  523. m = min(8-n, bits);
  524. if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
  525. return false;
  526. bits -= m;
  527. }
  528. m = bits / 8;
  529. if(m > 0) {
  530. if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
  531. return false;
  532. bits %= 8;
  533. }
  534. if(bits > 0) {
  535. if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
  536. return false;
  537. }
  538. }
  539. return true;
  540. }
  541. FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
  542. {
  543. FLAC__uint32 x;
  544. FLAC__ASSERT(0 != br);
  545. FLAC__ASSERT(0 != br->buffer);
  546. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
  547. /* step 1: skip over partial head word to get word aligned */
  548. while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
  549. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  550. return false;
  551. nvals--;
  552. }
  553. if(0 == nvals)
  554. return true;
  555. /* step 2: skip whole words in chunks */
  556. while(nvals >= FLAC__BYTES_PER_WORD) {
  557. if(br->consumed_words < br->words) {
  558. br->consumed_words++;
  559. nvals -= FLAC__BYTES_PER_WORD;
  560. }
  561. else if(!bitreader_read_from_client_(br))
  562. return false;
  563. }
  564. /* step 3: skip any remainder from partial tail bytes */
  565. while(nvals) {
  566. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  567. return false;
  568. nvals--;
  569. }
  570. return true;
  571. }
  572. FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
  573. {
  574. FLAC__uint32 x;
  575. FLAC__ASSERT(0 != br);
  576. FLAC__ASSERT(0 != br->buffer);
  577. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
  578. /* step 1: read from partial head word to get word aligned */
  579. while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
  580. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  581. return false;
  582. *val++ = (FLAC__byte)x;
  583. nvals--;
  584. }
  585. if(0 == nvals)
  586. return true;
  587. /* step 2: read whole words in chunks */
  588. while(nvals >= FLAC__BYTES_PER_WORD) {
  589. if(br->consumed_words < br->words) {
  590. const brword word = br->buffer[br->consumed_words++];
  591. #if FLAC__BYTES_PER_WORD == 4
  592. val[0] = (FLAC__byte)(word >> 24);
  593. val[1] = (FLAC__byte)(word >> 16);
  594. val[2] = (FLAC__byte)(word >> 8);
  595. val[3] = (FLAC__byte)word;
  596. #elif FLAC__BYTES_PER_WORD == 8
  597. val[0] = (FLAC__byte)(word >> 56);
  598. val[1] = (FLAC__byte)(word >> 48);
  599. val[2] = (FLAC__byte)(word >> 40);
  600. val[3] = (FLAC__byte)(word >> 32);
  601. val[4] = (FLAC__byte)(word >> 24);
  602. val[5] = (FLAC__byte)(word >> 16);
  603. val[6] = (FLAC__byte)(word >> 8);
  604. val[7] = (FLAC__byte)word;
  605. #else
  606. for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
  607. val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
  608. #endif
  609. val += FLAC__BYTES_PER_WORD;
  610. nvals -= FLAC__BYTES_PER_WORD;
  611. }
  612. else if(!bitreader_read_from_client_(br))
  613. return false;
  614. }
  615. /* step 3: read any remainder from partial tail bytes */
  616. while(nvals) {
  617. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  618. return false;
  619. *val++ = (FLAC__byte)x;
  620. nvals--;
  621. }
  622. return true;
  623. }
  624. FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
  625. #if 0 /* slow but readable version */
  626. {
  627. unsigned bit;
  628. FLAC__ASSERT(0 != br);
  629. FLAC__ASSERT(0 != br->buffer);
  630. *val = 0;
  631. while(1) {
  632. if(!FLAC__bitreader_read_bit(br, &bit))
  633. return false;
  634. if(bit)
  635. break;
  636. else
  637. *val++;
  638. }
  639. return true;
  640. }
  641. #else
  642. {
  643. unsigned i;
  644. FLAC__ASSERT(0 != br);
  645. FLAC__ASSERT(0 != br->buffer);
  646. *val = 0;
  647. while(1) {
  648. while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
  649. brword b = br->buffer[br->consumed_words] << br->consumed_bits;
  650. if(b) {
  651. i = COUNT_ZERO_MSBS(b);
  652. *val += i;
  653. i++;
  654. br->consumed_bits += i;
  655. if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
  656. crc16_update_word_(br, br->buffer[br->consumed_words]);
  657. br->consumed_words++;
  658. br->consumed_bits = 0;
  659. }
  660. return true;
  661. }
  662. else {
  663. *val += FLAC__BITS_PER_WORD - br->consumed_bits;
  664. crc16_update_word_(br, br->buffer[br->consumed_words]);
  665. br->consumed_words++;
  666. br->consumed_bits = 0;
  667. /* didn't find stop bit yet, have to keep going... */
  668. }
  669. }
  670. /* at this point we've eaten up all the whole words; have to try
  671. * reading through any tail bytes before calling the read callback.
  672. * this is a repeat of the above logic adjusted for the fact we
  673. * don't have a whole word. note though if the client is feeding
  674. * us data a byte at a time (unlikely), br->consumed_bits may not
  675. * be zero.
  676. */
  677. if(br->bytes) {
  678. const unsigned end = br->bytes * 8;
  679. brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
  680. if(b) {
  681. i = COUNT_ZERO_MSBS(b);
  682. *val += i;
  683. i++;
  684. br->consumed_bits += i;
  685. FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
  686. return true;
  687. }
  688. else {
  689. *val += end - br->consumed_bits;
  690. br->consumed_bits += end;
  691. FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
  692. /* didn't find stop bit yet, have to keep going... */
  693. }
  694. }
  695. if(!bitreader_read_from_client_(br))
  696. return false;
  697. }
  698. }
  699. #endif
  700. FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
  701. {
  702. FLAC__uint32 lsbs = 0;
  703. unsigned msbs = 0;
  704. unsigned uval;
  705. FLAC__ASSERT(0 != br);
  706. FLAC__ASSERT(0 != br->buffer);
  707. FLAC__ASSERT(parameter <= 31);
  708. /* read the unary MSBs and end bit */
  709. if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
  710. return false;
  711. /* read the binary LSBs */
  712. if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
  713. return false;
  714. /* compose the value */
  715. uval = (msbs << parameter) | lsbs;
  716. if(uval & 1)
  717. *val = -((int)(uval >> 1)) - 1;
  718. else
  719. *val = (int)(uval >> 1);
  720. return true;
  721. }
  722. /* this is by far the most heavily used reader call. it ain't pretty but it's fast */
  723. /* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
  724. FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
  725. /* OPT: possibly faster version for use with MSVC */
  726. #ifdef _MSC_VER
  727. {
  728. unsigned i;
  729. unsigned uval = 0;
  730. unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
  731. /* try and get br->consumed_words and br->consumed_bits into register;
  732. * must remember to flush them back to *br before calling other
  733. * bitwriter functions that use them, and before returning */
  734. register unsigned cwords;
  735. register unsigned cbits;
  736. FLAC__ASSERT(0 != br);
  737. FLAC__ASSERT(0 != br->buffer);
  738. /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
  739. FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
  740. FLAC__ASSERT(parameter < 32);
  741. /* 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 */
  742. if(nvals == 0)
  743. return true;
  744. cbits = br->consumed_bits;
  745. cwords = br->consumed_words;
  746. while(1) {
  747. /* read unary part */
  748. while(1) {
  749. while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
  750. brword b = br->buffer[cwords] << cbits;
  751. if(b) {
  752. #if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
  753. __asm {
  754. bsr eax, b
  755. not eax
  756. and eax, 31
  757. mov i, eax
  758. }
  759. #else
  760. i = COUNT_ZERO_MSBS(b);
  761. #endif
  762. uval += i;
  763. bits = parameter;
  764. i++;
  765. cbits += i;
  766. if(cbits == FLAC__BITS_PER_WORD) {
  767. crc16_update_word_(br, br->buffer[cwords]);
  768. cwords++;
  769. cbits = 0;
  770. }
  771. goto break1;
  772. }
  773. else {
  774. uval += FLAC__BITS_PER_WORD - cbits;
  775. crc16_update_word_(br, br->buffer[cwords]);
  776. cwords++;
  777. cbits = 0;
  778. /* didn't find stop bit yet, have to keep going... */
  779. }
  780. }
  781. /* at this point we've eaten up all the whole words; have to try
  782. * reading through any tail bytes before calling the read callback.
  783. * this is a repeat of the above logic adjusted for the fact we
  784. * don't have a whole word. note though if the client is feeding
  785. * us data a byte at a time (unlikely), br->consumed_bits may not
  786. * be zero.
  787. */
  788. if(br->bytes) {
  789. const unsigned end = br->bytes * 8;
  790. brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
  791. if(b) {
  792. i = COUNT_ZERO_MSBS(b);
  793. uval += i;
  794. bits = parameter;
  795. i++;
  796. cbits += i;
  797. FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
  798. goto break1;
  799. }
  800. else {
  801. uval += end - cbits;
  802. cbits += end;
  803. FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
  804. /* didn't find stop bit yet, have to keep going... */
  805. }
  806. }
  807. /* flush registers and read; bitreader_read_from_client_() does
  808. * not touch br->consumed_bits at all but we still need to set
  809. * it in case it fails and we have to return false.
  810. */
  811. br->consumed_bits = cbits;
  812. br->consumed_words = cwords;
  813. if(!bitreader_read_from_client_(br))
  814. return false;
  815. cwords = br->consumed_words;
  816. }
  817. break1:
  818. /* read binary part */
  819. FLAC__ASSERT(cwords <= br->words);
  820. if(bits) {
  821. while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
  822. /* flush registers and read; bitreader_read_from_client_() does
  823. * not touch br->consumed_bits at all but we still need to set
  824. * it in case it fails and we have to return false.
  825. */
  826. br->consumed_bits = cbits;
  827. br->consumed_words = cwords;
  828. if(!bitreader_read_from_client_(br))
  829. return false;
  830. cwords = br->consumed_words;
  831. }
  832. if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
  833. if(cbits) {
  834. /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
  835. const unsigned n = FLAC__BITS_PER_WORD - cbits;
  836. const brword word = br->buffer[cwords];
  837. if(bits < n) {
  838. uval <<= bits;
  839. uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
  840. cbits += bits;
  841. goto break2;
  842. }
  843. uval <<= n;
  844. uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
  845. bits -= n;
  846. crc16_update_word_(br, word);
  847. cwords++;
  848. cbits = 0;
  849. 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 */
  850. uval <<= bits;
  851. uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
  852. cbits = bits;
  853. }
  854. goto break2;
  855. }
  856. else {
  857. FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
  858. uval <<= bits;
  859. uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
  860. cbits = bits;
  861. goto break2;
  862. }
  863. }
  864. else {
  865. /* in this case we're starting our read at a partial tail word;
  866. * the reader has guaranteed that we have at least 'bits' bits
  867. * available to read, which makes this case simpler.
  868. */
  869. uval <<= bits;
  870. if(cbits) {
  871. /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
  872. FLAC__ASSERT(cbits + bits <= br->bytes*8);
  873. uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
  874. cbits += bits;
  875. goto break2;
  876. }
  877. else {
  878. uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
  879. cbits += bits;
  880. goto break2;
  881. }
  882. }
  883. }
  884. break2:
  885. /* compose the value */
  886. *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
  887. /* are we done? */
  888. --nvals;
  889. if(nvals == 0) {
  890. br->consumed_bits = cbits;
  891. br->consumed_words = cwords;
  892. return true;
  893. }
  894. uval = 0;
  895. ++vals;
  896. }
  897. }
  898. #else
  899. {
  900. unsigned i;
  901. unsigned uval = 0;
  902. /* try and get br->consumed_words and br->consumed_bits into register;
  903. * must remember to flush them back to *br before calling other
  904. * bitwriter functions that use them, and before returning */
  905. register unsigned cwords;
  906. register unsigned cbits;
  907. unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
  908. FLAC__ASSERT(0 != br);
  909. FLAC__ASSERT(0 != br->buffer);
  910. /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
  911. FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
  912. FLAC__ASSERT(parameter < 32);
  913. /* 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 */
  914. if(nvals == 0)
  915. return true;
  916. cbits = br->consumed_bits;
  917. cwords = br->consumed_words;
  918. ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
  919. while(1) {
  920. /* read unary part */
  921. while(1) {
  922. while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
  923. brword b = br->buffer[cwords] << cbits;
  924. if(b) {
  925. #if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
  926. asm volatile (
  927. "bsrl %1, %0;"
  928. "notl %0;"
  929. "andl $31, %0;"
  930. : "=r"(i)
  931. : "r"(b)
  932. );
  933. #else
  934. i = COUNT_ZERO_MSBS(b);
  935. #endif
  936. uval += i;
  937. cbits += i;
  938. cbits++; /* skip over stop bit */
  939. if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
  940. crc16_update_word_(br, br->buffer[cwords]);
  941. cwords++;
  942. cbits = 0;
  943. }
  944. goto break1;
  945. }
  946. else {
  947. uval += FLAC__BITS_PER_WORD - cbits;
  948. crc16_update_word_(br, br->buffer[cwords]);
  949. cwords++;
  950. cbits = 0;
  951. /* didn't find stop bit yet, have to keep going... */
  952. }
  953. }
  954. /* at this point we've eaten up all the whole words; have to try
  955. * reading through any tail bytes before calling the read callback.
  956. * this is a repeat of the above logic adjusted for the fact we
  957. * don't have a whole word. note though if the client is feeding
  958. * us data a byte at a time (unlikely), br->consumed_bits may not
  959. * be zero.
  960. */
  961. if(br->bytes) {
  962. const unsigned end = br->bytes * 8;
  963. brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
  964. if(b) {
  965. i = COUNT_ZERO_MSBS(b);
  966. uval += i;
  967. cbits += i;
  968. cbits++; /* skip over stop bit */
  969. FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
  970. goto break1;
  971. }
  972. else {
  973. uval += end - cbits;
  974. cbits += end;
  975. FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
  976. /* didn't find stop bit yet, have to keep going... */
  977. }
  978. }
  979. /* flush registers and read; bitreader_read_from_client_() does
  980. * not touch br->consumed_bits at all but we still need to set
  981. * it in case it fails and we have to return false.
  982. */
  983. br->consumed_bits = cbits;
  984. br->consumed_words = cwords;
  985. if(!bitreader_read_from_client_(br))
  986. return false;
  987. cwords = br->consumed_words;
  988. ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
  989. /* + uval to offset our count by the # of unary bits already
  990. * consumed before the read, because we will add these back
  991. * in all at once at break1
  992. */
  993. }
  994. break1:
  995. ucbits -= uval;
  996. ucbits--; /* account for stop bit */
  997. /* read binary part */
  998. FLAC__ASSERT(cwords <= br->words);
  999. if(parameter) {
  1000. while(ucbits < parameter) {
  1001. /* flush registers and read; bitreader_read_from_client_() does
  1002. * not touch br->consumed_bits at all but we still need to set
  1003. * it in case it fails and we have to return false.
  1004. */
  1005. br->consumed_bits = cbits;
  1006. br->consumed_words = cwords;
  1007. if(!bitreader_read_from_client_(br))
  1008. return false;
  1009. cwords = br->consumed_words;
  1010. ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
  1011. }
  1012. if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
  1013. if(cbits) {
  1014. /* this also works when consumed_bits==0, it's just slower than necessary for that case */
  1015. const unsigned n = FLAC__BITS_PER_WORD - cbits;
  1016. const brword word = br->buffer[cwords];
  1017. if(parameter < n) {
  1018. uval <<= parameter;
  1019. uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
  1020. cbits += parameter;
  1021. }
  1022. else {
  1023. uval <<= n;
  1024. uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
  1025. crc16_update_word_(br, word);
  1026. cwords++;
  1027. cbits = parameter - n;
  1028. 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 */
  1029. uval <<= cbits;
  1030. uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
  1031. }
  1032. }
  1033. }
  1034. else {
  1035. cbits = parameter;
  1036. uval <<= parameter;
  1037. uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
  1038. }
  1039. }
  1040. else {
  1041. /* in this case we're starting our read at a partial tail word;
  1042. * the reader has guaranteed that we have at least 'parameter'
  1043. * bits available to read, which makes this case simpler.
  1044. */
  1045. uval <<= parameter;
  1046. if(cbits) {
  1047. /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
  1048. FLAC__ASSERT(cbits + parameter <= br->bytes*8);
  1049. uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
  1050. cbits += parameter;
  1051. }
  1052. else {
  1053. cbits = parameter;
  1054. uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
  1055. }
  1056. }
  1057. }
  1058. ucbits -= parameter;
  1059. /* compose the value */
  1060. *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
  1061. /* are we done? */
  1062. --nvals;
  1063. if(nvals == 0) {
  1064. br->consumed_bits = cbits;
  1065. br->consumed_words = cwords;
  1066. return true;
  1067. }
  1068. uval = 0;
  1069. ++vals;
  1070. }
  1071. }
  1072. #endif
  1073. #if 0 /* UNUSED */
  1074. FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
  1075. {
  1076. FLAC__uint32 lsbs = 0, msbs = 0;
  1077. unsigned bit, uval, k;
  1078. FLAC__ASSERT(0 != br);
  1079. FLAC__ASSERT(0 != br->buffer);
  1080. k = FLAC__bitmath_ilog2(parameter);
  1081. /* read the unary MSBs and end bit */
  1082. if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
  1083. return false;
  1084. /* read the binary LSBs */
  1085. if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
  1086. return false;
  1087. if(parameter == 1u<<k) {
  1088. /* compose the value */
  1089. uval = (msbs << k) | lsbs;
  1090. }
  1091. else {
  1092. unsigned d = (1 << (k+1)) - parameter;
  1093. if(lsbs >= d) {
  1094. if(!FLAC__bitreader_read_bit(br, &bit))
  1095. return false;
  1096. lsbs <<= 1;
  1097. lsbs |= bit;
  1098. lsbs -= d;
  1099. }
  1100. /* compose the value */
  1101. uval = msbs * parameter + lsbs;
  1102. }
  1103. /* unfold unsigned to signed */
  1104. if(uval & 1)
  1105. *val = -((int)(uval >> 1)) - 1;
  1106. else
  1107. *val = (int)(uval >> 1);
  1108. return true;
  1109. }
  1110. FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
  1111. {
  1112. FLAC__uint32 lsbs, msbs = 0;
  1113. unsigned bit, k;
  1114. FLAC__ASSERT(0 != br);
  1115. FLAC__ASSERT(0 != br->buffer);
  1116. k = FLAC__bitmath_ilog2(parameter);
  1117. /* read the unary MSBs and end bit */
  1118. if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
  1119. return false;
  1120. /* read the binary LSBs */
  1121. if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
  1122. return false;
  1123. if(parameter == 1u<<k) {
  1124. /* compose the value */
  1125. *val = (msbs << k) | lsbs;
  1126. }
  1127. else {
  1128. unsigned d = (1 << (k+1)) - parameter;
  1129. if(lsbs >= d) {
  1130. if(!FLAC__bitreader_read_bit(br, &bit))
  1131. return false;
  1132. lsbs <<= 1;
  1133. lsbs |= bit;
  1134. lsbs -= d;
  1135. }
  1136. /* compose the value */
  1137. *val = msbs * parameter + lsbs;
  1138. }
  1139. return true;
  1140. }
  1141. #endif /* UNUSED */
  1142. /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
  1143. FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
  1144. {
  1145. FLAC__uint32 v = 0;
  1146. FLAC__uint32 x;
  1147. unsigned i;
  1148. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  1149. return false;
  1150. if(raw)
  1151. raw[(*rawlen)++] = (FLAC__byte)x;
  1152. if(!(x & 0x80)) { /* 0xxxxxxx */
  1153. v = x;
  1154. i = 0;
  1155. }
  1156. else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
  1157. v = x & 0x1F;
  1158. i = 1;
  1159. }
  1160. else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
  1161. v = x & 0x0F;
  1162. i = 2;
  1163. }
  1164. else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
  1165. v = x & 0x07;
  1166. i = 3;
  1167. }
  1168. else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
  1169. v = x & 0x03;
  1170. i = 4;
  1171. }
  1172. else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
  1173. v = x & 0x01;
  1174. i = 5;
  1175. }
  1176. else {
  1177. *val = 0xffffffff;
  1178. return true;
  1179. }
  1180. for( ; i; i--) {
  1181. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  1182. return false;
  1183. if(raw)
  1184. raw[(*rawlen)++] = (FLAC__byte)x;
  1185. if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
  1186. *val = 0xffffffff;
  1187. return true;
  1188. }
  1189. v <<= 6;
  1190. v |= (x & 0x3F);
  1191. }
  1192. *val = v;
  1193. return true;
  1194. }
  1195. /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
  1196. FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
  1197. {
  1198. FLAC__uint64 v = 0;
  1199. FLAC__uint32 x;
  1200. unsigned i;
  1201. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  1202. return false;
  1203. if(raw)
  1204. raw[(*rawlen)++] = (FLAC__byte)x;
  1205. if(!(x & 0x80)) { /* 0xxxxxxx */
  1206. v = x;
  1207. i = 0;
  1208. }
  1209. else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
  1210. v = x & 0x1F;
  1211. i = 1;
  1212. }
  1213. else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
  1214. v = x & 0x0F;
  1215. i = 2;
  1216. }
  1217. else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
  1218. v = x & 0x07;
  1219. i = 3;
  1220. }
  1221. else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
  1222. v = x & 0x03;
  1223. i = 4;
  1224. }
  1225. else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
  1226. v = x & 0x01;
  1227. i = 5;
  1228. }
  1229. else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
  1230. v = 0;
  1231. i = 6;
  1232. }
  1233. else {
  1234. *val = FLAC__U64L(0xffffffffffffffff);
  1235. return true;
  1236. }
  1237. for( ; i; i--) {
  1238. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  1239. return false;
  1240. if(raw)
  1241. raw[(*rawlen)++] = (FLAC__byte)x;
  1242. if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
  1243. *val = FLAC__U64L(0xffffffffffffffff);
  1244. return true;
  1245. }
  1246. v <<= 6;
  1247. v |= (x & 0x3F);
  1248. }
  1249. *val = v;
  1250. return true;
  1251. }