bitreader: Add inlined and unchecked versions of the important functions

API: gst_bit_reader_skip_unchecked
API: gst_bit_reader_skip_to_byte_unchecked
API: gst_bit_reader_get_bits_uint16_unchecked
API: gst_bit_reader_get_bits_uint32_unchecked
API: gst_bit_reader_get_bits_uint64_unchecked
API: gst_bit_reader_get_bits_uint8_unchecked
API: gst_bit_reader_peek_bits_uint16_unchecked
API: gst_bit_reader_peek_bits_uint32_unchecked
API: gst_bit_reader_peek_bits_uint64_unchecked
API: gst_bit_reader_peek_bits_uint8_unchecked

This alone makes flacparse about 3 times faster.
This commit is contained in:
Sebastian Dröge 2010-10-03 15:27:37 +02:00
parent 10c18151d2
commit dd762eb49f
6 changed files with 401 additions and 79 deletions

View file

@ -377,6 +377,19 @@ gst_bit_reader_peek_bits_uint32
gst_bit_reader_peek_bits_uint64 gst_bit_reader_peek_bits_uint64
gst_bit_reader_peek_bits_uint8 gst_bit_reader_peek_bits_uint8
gst_bit_reader_skip_unchecked
gst_bit_reader_skip_to_byte_unchecked
gst_bit_reader_get_bits_uint16_unchecked
gst_bit_reader_get_bits_uint32_unchecked
gst_bit_reader_get_bits_uint64_unchecked
gst_bit_reader_get_bits_uint8_unchecked
gst_bit_reader_peek_bits_uint16_unchecked
gst_bit_reader_peek_bits_uint32_unchecked
gst_bit_reader_peek_bits_uint64_unchecked
gst_bit_reader_peek_bits_uint8_unchecked
<SUBSECTION Private> <SUBSECTION Private>
GST_BIT_READER GST_BIT_READER
</SECTION> </SECTION>

View file

@ -36,7 +36,8 @@ libgstbase_@GST_MAJORMINOR@include_HEADERS = \
gstdataqueue.h gstdataqueue.h
noinst_HEADERS = \ noinst_HEADERS = \
gstbytereader-docs.h gstbytereader-docs.h \
gstbitreader-docs.h
CLEANFILES = *.gcno *.gcda *.gcov CLEANFILES = *.gcno *.gcda *.gcov

View file

@ -0,0 +1,165 @@
/* GStreamer bit reader dummy header for gtk-doc
* Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* This header is not installed, it just contains stuff for gtk-doc to parse,
* in particular docs and some dummy function declarations for the static
* inline functions we generate via macros in gstbitreader.h.
*/
#error "This header should never be included in code, it is only for gtk-doc"
/**
* gst_bit_reader_skip_unchecked:
* @reader: a #GstBitReader instance
* @nbits: the number of bits to skip
*
* Skips @nbits bits of the #GstBitReader instance without checking if there
* are enough bits available in the bit reader.
*
* Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
*
* Since: 0.10.31
*/
void gst_bit_reader_skip_unchecked (GstBitReader * reader, guint nbits);
/**
* gst_bit_reader_skip_to_byte_unchecked:
* @reader: a #GstBitReader instance
*
* Skips until the next byte without checking if there are enough bits
* available in the bit reader.
*
* Returns: %TRUE if successful, %FALSE otherwise.
*
* Since: 0.10.31
*/
void gst_bit_reader_skip_to_byte_unchecked (GstBitReader * reader);
/**
* gst_bit_reader_get_bits_uint8_unchecked:
* @reader: a #GstBitReader instance
* @nbits: number of bits to read
*
* Read @nbits bits into @val and update the current position without
* checking if there are enough bits available in the bit reader.
*
* Returns: unsigned 8 bit integer with the bits.
*
* Since: 0.10.31
*/
guint8 gst_bit_reader_peek_bits_uint8_unchecked (const GstBitReader *reader, guint nbits);
/**
* gst_bit_reader_peek_bits_uint8_unchecked:
* @reader: a #GstBitReader instance
* @nbits: number of bits to read
*
* Read @nbits bits into @val but keep the current position without
* checking if there are enough bits available in the bit reader
*
* Returns: unsigned 8 bit integer with the bits.
*
* Since: 0.10.31
*/
guint8 gst_bit_reader_get_bits_uint8_unchecked (GstBitReader *reader, guint nbits);
/**
* gst_bit_reader_get_bits_uint16_unchecked:
* @reader: a #GstBitReader instance
* @nbits: number of bits to read
*
* Read @nbits bits into @val and update the current position without
* checking if there are enough bits available in the bit reader.
*
* Returns: unsigned 16 bit integer with the bits.
*
* Since: 0.10.31
*/
guint16 gst_bit_reader_peek_bits_uint16_unchecked (const GstBitReader *reader, guint nbits);
/**
* gst_bit_reader_peek_bits_uint16_unchecked:
* @reader: a #GstBitReader instance
* @nbits: number of bits to read
*
* Read @nbits bits into @val but keep the current position without
* checking if there are enough bits available in the bit reader
*
* Returns: unsigned 16 bit integer with the bits.
*
* Since: 0.10.31
*/
guint16 gst_bit_reader_get_bits_uint16_unchecked (GstBitReader *reader, guint nbits);
/**
* gst_bit_reader_get_bits_uint32_unchecked:
* @reader: a #GstBitReader instance
* @nbits: number of bits to read
*
* Read @nbits bits into @val and update the current position without
* checking if there are enough bits available in the bit reader.
*
* Returns: unsigned 32 bit integer with the bits.
*
* Since: 0.10.31
*/
guint32 gst_bit_reader_peek_bits_uint32_unchecked (const GstBitReader *reader, guint nbits);
/**
* gst_bit_reader_peek_bits_uint32_unchecked:
* @reader: a #GstBitReader instance
* @nbits: number of bits to read
*
* Read @nbits bits into @val but keep the current position without
* checking if there are enough bits available in the bit reader
*
* Returns: unsigned 32 bit integer with the bits.
*
* Since: 0.10.31
*/
guint32 gst_bit_reader_get_bits_uint32_unchecked (GstBitReader *reader, guint nbits);
/**
* gst_bit_reader_get_bits_uint64_unchecked:
* @reader: a #GstBitReader instance
* @nbits: number of bits to read
*
* Read @nbits bits into @val and update the current position without
* checking if there are enough bits available in the bit reader.
*
* Returns: unsigned 64 bit integer with the bits.
*
* Since: 0.10.31
*/
guint64 gst_bit_reader_peek_bits_uint64_unchecked (const GstBitReader *reader, guint nbits);
/**
* gst_bit_reader_peek_bits_uint16_unchecked:
* @reader: a #GstBitReader instance
* @nbits: number of bits to read
*
* Read @nbits bits into @val but keep the current position without
* checking if there are enough bits available in the bit reader
*
* Returns: unsigned 64 bit integer with the bits.
*
* Since: 0.10.31
*/
guint64 gst_bit_reader_get_bits_uint64_unchecked (GstBitReader *reader, guint nbits);

View file

@ -22,6 +22,7 @@
#include "config.h" #include "config.h"
#endif #endif
#define GST_BIT_READER_DISABLE_INLINES
#include "gstbitreader.h" #include "gstbitreader.h"
#include <string.h> #include <string.h>
@ -174,9 +175,7 @@ gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
guint guint
gst_bit_reader_get_pos (const GstBitReader * reader) gst_bit_reader_get_pos (const GstBitReader * reader)
{ {
g_return_val_if_fail (reader != NULL, 0); return _gst_bit_reader_get_pos_inline (reader);
return reader->byte * 8 + reader->bit;
} }
/** /**
@ -192,9 +191,7 @@ gst_bit_reader_get_pos (const GstBitReader * reader)
guint guint
gst_bit_reader_get_remaining (const GstBitReader * reader) gst_bit_reader_get_remaining (const GstBitReader * reader)
{ {
g_return_val_if_fail (reader != NULL, 0); return _gst_bit_reader_get_remaining_inline (reader);
return reader->size * 8 - (reader->byte * 8 + reader->bit);
} }
/** /**
@ -210,9 +207,7 @@ gst_bit_reader_get_remaining (const GstBitReader * reader)
guint guint
gst_bit_reader_get_size (const GstBitReader * reader) gst_bit_reader_get_size (const GstBitReader * reader)
{ {
g_return_val_if_fail (reader != NULL, 0); return _gst_bit_reader_get_size_inline (reader);
return reader->size * 8;
} }
/** /**
@ -229,16 +224,7 @@ gst_bit_reader_get_size (const GstBitReader * reader)
gboolean gboolean
gst_bit_reader_skip (GstBitReader * reader, guint nbits) gst_bit_reader_skip (GstBitReader * reader, guint nbits)
{ {
g_return_val_if_fail (reader != NULL, FALSE); return _gst_bit_reader_skip_inline (reader, nbits);
if (gst_bit_reader_get_remaining (reader) < nbits)
return FALSE;
reader->bit += nbits;
reader->byte += reader->bit / 8;
reader->bit = reader->bit % 8;
return TRUE;
} }
/** /**
@ -254,17 +240,7 @@ gst_bit_reader_skip (GstBitReader * reader, guint nbits)
gboolean gboolean
gst_bit_reader_skip_to_byte (GstBitReader * reader) gst_bit_reader_skip_to_byte (GstBitReader * reader)
{ {
g_return_val_if_fail (reader != NULL, FALSE); return _gst_bit_reader_skip_to_byte_inline (reader);
if (reader->byte > reader->size)
return FALSE;
if (reader->bit) {
reader->bit = 0;
reader->byte++;
}
return TRUE;
} }
/** /**
@ -375,55 +351,13 @@ gst_bit_reader_skip_to_byte (GstBitReader * reader)
gboolean \ gboolean \
gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \ gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
{ \ { \
guint##bits ret = 0; \ return _gst_bit_reader_peek_bits_uint##bits##_inline (reader, val, nbits); \
const guint8 *data; \
guint byte, bit; \
\
g_return_val_if_fail (reader != NULL, FALSE); \
g_return_val_if_fail (val != NULL, FALSE); \
g_return_val_if_fail (nbits <= bits, FALSE); \
\
data = reader->data; \
byte = reader->byte; \
bit = reader->bit; \
\
if (byte * 8 + bit + nbits > reader->size * 8) \
return FALSE; \
\
while (nbits > 0) { \
guint toread = MIN (nbits, 8 - bit); \
\
ret <<= toread; \
ret |= (data[byte] & (0xff >> bit)) >> (8 - toread - bit); \
\
bit += toread; \
if (bit >= 8) { \
byte++; \
bit = 0; \
} \
nbits -= toread; \
} \
\
*val = ret; \
return TRUE; \
} \ } \
\ \
gboolean \ gboolean \
gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \ gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
{ \ { \
gboolean ret; \ return _gst_bit_reader_get_bits_uint##bits##_inline (reader, val, nbits); \
\
ret = gst_bit_reader_peek_bits_uint##bits (reader, val, nbits); \
\
if (ret) { \
reader->bit += nbits; \
if (reader->bit >= 8) { \
reader->byte += reader->bit / 8; \
reader->bit = reader->bit % 8; \
} \
} \
\
return ret; \
} }
GST_BIT_READER_READ_BITS (8); GST_BIT_READER_READ_BITS (8);

View file

@ -100,6 +100,215 @@ gboolean gst_bit_reader_peek_bits_uint64 (const GstBitReader *reader, guint64 *v
*/ */
#define GST_BIT_READER_INIT_FROM_BUFFER(buffer) {GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer), 0, 0} #define GST_BIT_READER_INIT_FROM_BUFFER(buffer) {GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer), 0, 0}
/* Unchecked variants */
static inline void
gst_bit_reader_skip_unchecked (GstBitReader * reader, guint nbits)
{
reader->bit += nbits;
reader->byte += reader->bit / 8;
reader->bit = reader->bit % 8;
}
static inline void
gst_bit_reader_skip_to_byte_unchecked (GstBitReader * reader)
{
if (reader->bit) {
reader->bit = 0;
reader->byte++;
}
}
#define __GST_BIT_READER_READ_BITS_UNCHECKED(bits) \
static inline guint##bits \
gst_bit_reader_peek_bits_uint##bits##_unchecked (const GstBitReader *reader, guint nbits) \
{ \
guint##bits ret = 0; \
const guint8 *data; \
guint byte, bit; \
\
data = reader->data; \
byte = reader->byte; \
bit = reader->bit; \
\
while (nbits > 0) { \
guint toread = MIN (nbits, 8 - bit); \
\
ret <<= toread; \
ret |= (data[byte] & (0xff >> bit)) >> (8 - toread - bit); \
\
bit += toread; \
if (bit >= 8) { \
byte++; \
bit = 0; \
} \
nbits -= toread; \
} \
\
return ret; \
} \
\
static inline guint##bits \
gst_bit_reader_get_bits_uint##bits##_unchecked (GstBitReader *reader, guint nbits) \
{ \
guint##bits ret; \
\
ret = gst_bit_reader_peek_bits_uint##bits##_unchecked (reader, nbits); \
\
gst_bit_reader_skip_unchecked (reader, nbits); \
\
return ret; \
}
__GST_BIT_READER_READ_BITS_UNCHECKED (8)
__GST_BIT_READER_READ_BITS_UNCHECKED (16)
__GST_BIT_READER_READ_BITS_UNCHECKED (32)
__GST_BIT_READER_READ_BITS_UNCHECKED (64)
#undef __GST_BIT_READER_READ_BITS_UNCHECKED
/* unchecked variants -- do not use */
static inline guint
_gst_bit_reader_get_size_unchecked (const GstBitReader * reader)
{
return reader->size * 8;
}
static inline guint
_gst_bit_reader_get_pos_unchecked (const GstBitReader * reader)
{
return reader->byte * 8 + reader->bit;
}
static inline guint
_gst_bit_reader_get_remaining_unchecked (const GstBitReader * reader)
{
return reader->size * 8 - (reader->byte * 8 + reader->bit);
}
/* inlined variants -- do not use directly */
static inline guint
_gst_bit_reader_get_size_inline (const GstBitReader * reader)
{
g_return_val_if_fail (reader != NULL, 0);
return _gst_bit_reader_get_size_unchecked (reader);
}
static inline guint
_gst_bit_reader_get_pos_inline (const GstBitReader * reader)
{
g_return_val_if_fail (reader != NULL, 0);
return _gst_bit_reader_get_pos_unchecked (reader);
}
static inline guint
_gst_bit_reader_get_remaining_inline (const GstBitReader * reader)
{
g_return_val_if_fail (reader != NULL, 0);
return _gst_bit_reader_get_remaining_unchecked (reader);
}
static inline gboolean
_gst_bit_reader_skip_inline (GstBitReader * reader, guint nbits)
{
g_return_val_if_fail (reader != NULL, FALSE);
if (_gst_bit_reader_get_remaining_unchecked (reader) < nbits)
return FALSE;
gst_bit_reader_skip_unchecked (reader, nbits);
return TRUE;
}
static inline gboolean
_gst_bit_reader_skip_to_byte_inline (GstBitReader * reader)
{
g_return_val_if_fail (reader != NULL, FALSE);
if (reader->byte > reader->size)
return FALSE;
gst_bit_reader_skip_to_byte_unchecked (reader);
return TRUE;
}
#define __GST_BIT_READER_READ_BITS_INLINE(bits) \
static inline gboolean \
_gst_bit_reader_get_bits_uint##bits##_inline (GstBitReader *reader, guint##bits *val, guint nbits) \
{ \
g_return_val_if_fail (reader != NULL, FALSE); \
g_return_val_if_fail (val != NULL, FALSE); \
g_return_val_if_fail (nbits <= bits, FALSE); \
\
if (_gst_bit_reader_get_remaining_unchecked (reader) < nbits) \
return FALSE; \
\
*val = gst_bit_reader_get_bits_uint##bits##_unchecked (reader, nbits); \
return TRUE; \
} \
\
static inline gboolean \
_gst_bit_reader_peek_bits_uint##bits##_inline (const GstBitReader *reader, guint##bits *val, guint nbits) \
{ \
g_return_val_if_fail (reader != NULL, FALSE); \
g_return_val_if_fail (val != NULL, FALSE); \
g_return_val_if_fail (nbits <= bits, FALSE); \
\
if (_gst_bit_reader_get_remaining_unchecked (reader) < nbits) \
return FALSE; \
\
*val = gst_bit_reader_peek_bits_uint##bits##_unchecked (reader, nbits); \
return TRUE; \
}
__GST_BIT_READER_READ_BITS_INLINE (8)
__GST_BIT_READER_READ_BITS_INLINE (16)
__GST_BIT_READER_READ_BITS_INLINE (32)
__GST_BIT_READER_READ_BITS_INLINE (64)
#undef __GST_BIT_READER_READ_BITS_INLINE
#ifndef GST_BIT_READER_DISABLE_INLINES
#define gst_bit_reader_get_size(reader) \
_gst_bit_reader_get_size_inline (reader)
#define gst_bit_reader_get_pos(reader) \
_gst_bit_reader_get_pos_inline (reader)
#define gst_bit_reader_get_remaining(reader) \
_gst_bit_reader_get_remaining_inline (reader)
/* we use defines here so we can add the G_LIKELY() */
#define gst_bit_reader_skip(reader, nbits)\
G_LIKELY (_gst_bit_reader_skip_inline(reader, nbits))
#define gst_bit_reader_skip_to_byte(reader)\
G_LIKELY (_gst_bit_reader_skip_to_byte_inline(reader))
#define gst_bit_reader_get_bits_uint8(reader, val, nbits) \
G_LIKELY (_gst_bit_reader_get_bits_uint8_inline (reader, val, nbits))
#define gst_bit_reader_get_bits_uint16(reader, val, nbits) \
G_LIKELY (_gst_bit_reader_get_bits_uint16_inline (reader, val, nbits))
#define gst_bit_reader_get_bits_uint32(reader, val, nbits) \
G_LIKELY (_gst_bit_reader_get_bits_uint32_inline (reader, val, nbits))
#define gst_bit_reader_get_bits_uint64(reader, val, nbits) \
G_LIKELY (_gst_bit_reader_get_bits_uint64_inline (reader, val, nbits))
#define gst_bit_reader_peek_bits_uint8(reader, val, nbits) \
G_LIKELY (_gst_bit_reader_peek_bits_uint8_inline (reader, val, nbits))
#define gst_bit_reader_peek_bits_uint16(reader, val, nbits) \
G_LIKELY (_gst_bit_reader_peek_bits_uint16_inline (reader, val, nbits))
#define gst_bit_reader_peek_bits_uint32(reader, val, nbits) \
G_LIKELY (_gst_bit_reader_peek_bits_uint32_inline (reader, val, nbits))
#define gst_bit_reader_peek_bits_uint64(reader, val, nbits) \
G_LIKELY (_gst_bit_reader_peek_bits_uint64_inline (reader, val, nbits))
#endif
G_END_DECLS G_END_DECLS
#endif /* __GST_BIT_READER_H__ */ #endif /* __GST_BIT_READER_H__ */

View file

@ -112,10 +112,10 @@ GST_START_TEST (test_get_bits)
0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21
}; };
GstBitReader reader = GST_BIT_READER_INIT (data, 16); GstBitReader reader = GST_BIT_READER_INIT (data, 16);
guint8 a; guint8 a = 0;
guint16 b; guint16 b = 0;
guint32 c; guint32 c = 0;
guint64 d; guint64 d = 0;
/* 8 bit */ /* 8 bit */
GET_CHECK (&reader, a, 8, 8, 0x12); GET_CHECK (&reader, a, 8, 8, 0x12);