gstbytereader.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /* GStreamer byte reader
  2. *
  3. * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
  4. * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. */
  21. #ifndef __GST_BYTE_READER_H__
  22. #define __GST_BYTE_READER_H__
  23. #include <gst/gst.h>
  24. G_BEGIN_DECLS
  25. #define GST_BYTE_READER(reader) ((GstByteReader *) (reader))
  26. /**
  27. * GstByteReader:
  28. * @data: (array length=size): Data from which the bit reader will
  29. * read
  30. * @size: Size of @data in bytes
  31. * @byte: Current byte position
  32. *
  33. * A byte reader instance.
  34. */
  35. typedef struct {
  36. const guint8 *data;
  37. guint size;
  38. guint byte; /* Byte position */
  39. /* < private > */
  40. gpointer _gst_reserved[GST_PADDING];
  41. } GstByteReader;
  42. GstByteReader * gst_byte_reader_new (const guint8 *data, guint size) G_GNUC_MALLOC;
  43. void gst_byte_reader_free (GstByteReader *reader);
  44. void gst_byte_reader_init (GstByteReader *reader, const guint8 *data, guint size);
  45. gboolean gst_byte_reader_peek_sub_reader (GstByteReader * reader,
  46. GstByteReader * sub_reader,
  47. guint size);
  48. gboolean gst_byte_reader_get_sub_reader (GstByteReader * reader,
  49. GstByteReader * sub_reader,
  50. guint size);
  51. gboolean gst_byte_reader_set_pos (GstByteReader *reader, guint pos);
  52. guint gst_byte_reader_get_pos (const GstByteReader *reader);
  53. guint gst_byte_reader_get_remaining (const GstByteReader *reader);
  54. guint gst_byte_reader_get_size (const GstByteReader *reader);
  55. gboolean gst_byte_reader_skip (GstByteReader *reader, guint nbytes);
  56. gboolean gst_byte_reader_get_uint8 (GstByteReader *reader, guint8 *val);
  57. gboolean gst_byte_reader_get_int8 (GstByteReader *reader, gint8 *val);
  58. gboolean gst_byte_reader_get_uint16_le (GstByteReader *reader, guint16 *val);
  59. gboolean gst_byte_reader_get_int16_le (GstByteReader *reader, gint16 *val);
  60. gboolean gst_byte_reader_get_uint16_be (GstByteReader *reader, guint16 *val);
  61. gboolean gst_byte_reader_get_int16_be (GstByteReader *reader, gint16 *val);
  62. gboolean gst_byte_reader_get_uint24_le (GstByteReader *reader, guint32 *val);
  63. gboolean gst_byte_reader_get_int24_le (GstByteReader *reader, gint32 *val);
  64. gboolean gst_byte_reader_get_uint24_be (GstByteReader *reader, guint32 *val);
  65. gboolean gst_byte_reader_get_int24_be (GstByteReader *reader, gint32 *val);
  66. gboolean gst_byte_reader_get_uint32_le (GstByteReader *reader, guint32 *val);
  67. gboolean gst_byte_reader_get_int32_le (GstByteReader *reader, gint32 *val);
  68. gboolean gst_byte_reader_get_uint32_be (GstByteReader *reader, guint32 *val);
  69. gboolean gst_byte_reader_get_int32_be (GstByteReader *reader, gint32 *val);
  70. gboolean gst_byte_reader_get_uint64_le (GstByteReader *reader, guint64 *val);
  71. gboolean gst_byte_reader_get_int64_le (GstByteReader *reader, gint64 *val);
  72. gboolean gst_byte_reader_get_uint64_be (GstByteReader *reader, guint64 *val);
  73. gboolean gst_byte_reader_get_int64_be (GstByteReader *reader, gint64 *val);
  74. gboolean gst_byte_reader_peek_uint8 (const GstByteReader *reader, guint8 *val);
  75. gboolean gst_byte_reader_peek_int8 (const GstByteReader *reader, gint8 *val);
  76. gboolean gst_byte_reader_peek_uint16_le (const GstByteReader *reader, guint16 *val);
  77. gboolean gst_byte_reader_peek_int16_le (const GstByteReader *reader, gint16 *val);
  78. gboolean gst_byte_reader_peek_uint16_be (const GstByteReader *reader, guint16 *val);
  79. gboolean gst_byte_reader_peek_int16_be (const GstByteReader *reader, gint16 *val);
  80. gboolean gst_byte_reader_peek_uint24_le (const GstByteReader *reader, guint32 *val);
  81. gboolean gst_byte_reader_peek_int24_le (const GstByteReader *reader, gint32 *val);
  82. gboolean gst_byte_reader_peek_uint24_be (const GstByteReader *reader, guint32 *val);
  83. gboolean gst_byte_reader_peek_int24_be (const GstByteReader *reader, gint32 *val);
  84. gboolean gst_byte_reader_peek_uint32_le (const GstByteReader *reader, guint32 *val);
  85. gboolean gst_byte_reader_peek_int32_le (const GstByteReader *reader, gint32 *val);
  86. gboolean gst_byte_reader_peek_uint32_be (const GstByteReader *reader, guint32 *val);
  87. gboolean gst_byte_reader_peek_int32_be (const GstByteReader *reader, gint32 *val);
  88. gboolean gst_byte_reader_peek_uint64_le (const GstByteReader *reader, guint64 *val);
  89. gboolean gst_byte_reader_peek_int64_le (const GstByteReader *reader, gint64 *val);
  90. gboolean gst_byte_reader_peek_uint64_be (const GstByteReader *reader, guint64 *val);
  91. gboolean gst_byte_reader_peek_int64_be (const GstByteReader *reader, gint64 *val);
  92. gboolean gst_byte_reader_get_float32_le (GstByteReader *reader, gfloat *val);
  93. gboolean gst_byte_reader_get_float32_be (GstByteReader *reader, gfloat *val);
  94. gboolean gst_byte_reader_get_float64_le (GstByteReader *reader, gdouble *val);
  95. gboolean gst_byte_reader_get_float64_be (GstByteReader *reader, gdouble *val);
  96. gboolean gst_byte_reader_peek_float32_le (const GstByteReader *reader, gfloat *val);
  97. gboolean gst_byte_reader_peek_float32_be (const GstByteReader *reader, gfloat *val);
  98. gboolean gst_byte_reader_peek_float64_le (const GstByteReader *reader, gdouble *val);
  99. gboolean gst_byte_reader_peek_float64_be (const GstByteReader *reader, gdouble *val);
  100. gboolean gst_byte_reader_dup_data (GstByteReader * reader, guint size, guint8 ** val);
  101. gboolean gst_byte_reader_get_data (GstByteReader * reader, guint size, const guint8 ** val);
  102. gboolean gst_byte_reader_peek_data (const GstByteReader * reader, guint size, const guint8 ** val);
  103. #define gst_byte_reader_dup_string(reader,str) \
  104. gst_byte_reader_dup_string_utf8(reader,str)
  105. gboolean gst_byte_reader_dup_string_utf8 (GstByteReader * reader, gchar ** str);
  106. gboolean gst_byte_reader_dup_string_utf16 (GstByteReader * reader, guint16 ** str);
  107. gboolean gst_byte_reader_dup_string_utf32 (GstByteReader * reader, guint32 ** str);
  108. #define gst_byte_reader_skip_string(reader) \
  109. gst_byte_reader_skip_string_utf8(reader)
  110. gboolean gst_byte_reader_skip_string_utf8 (GstByteReader * reader);
  111. gboolean gst_byte_reader_skip_string_utf16 (GstByteReader * reader);
  112. gboolean gst_byte_reader_skip_string_utf32 (GstByteReader * reader);
  113. #define gst_byte_reader_get_string(reader,str) \
  114. gst_byte_reader_get_string_utf8(reader,str)
  115. #define gst_byte_reader_peek_string(reader,str) \
  116. gst_byte_reader_peek_string_utf8(reader,str)
  117. gboolean gst_byte_reader_get_string_utf8 (GstByteReader * reader, const gchar ** str);
  118. gboolean gst_byte_reader_peek_string_utf8 (const GstByteReader * reader, const gchar ** str);
  119. guint gst_byte_reader_masked_scan_uint32 (const GstByteReader * reader,
  120. guint32 mask,
  121. guint32 pattern,
  122. guint offset,
  123. guint size);
  124. guint gst_byte_reader_masked_scan_uint32_peek (const GstByteReader * reader,
  125. guint32 mask,
  126. guint32 pattern,
  127. guint offset,
  128. guint size,
  129. guint32 * value);
  130. /**
  131. * GST_BYTE_READER_INIT:
  132. * @data: Data from which the #GstByteReader should read
  133. * @size: Size of @data in bytes
  134. *
  135. * A #GstByteReader must be initialized with this macro, before it can be
  136. * used. This macro can used be to initialize a variable, but it cannot
  137. * be assigned to a variable. In that case you have to use
  138. * gst_byte_reader_init().
  139. */
  140. #define GST_BYTE_READER_INIT(data, size) {data, size, 0}
  141. /* unchecked variants */
  142. static inline void
  143. gst_byte_reader_skip_unchecked (GstByteReader * reader, guint nbytes)
  144. {
  145. reader->byte += nbytes;
  146. }
  147. #define __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(bits,type,lower,upper,adj) \
  148. \
  149. static inline type \
  150. gst_byte_reader_peek_##lower##_unchecked (const GstByteReader * reader) \
  151. { \
  152. type val = (type) GST_READ_##upper (reader->data + reader->byte); \
  153. adj \
  154. return val; \
  155. } \
  156. \
  157. static inline type \
  158. gst_byte_reader_get_##lower##_unchecked (GstByteReader * reader) \
  159. { \
  160. type val = gst_byte_reader_peek_##lower##_unchecked (reader); \
  161. reader->byte += bits / 8; \
  162. return val; \
  163. }
  164. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(8,guint8,uint8,UINT8,/* */)
  165. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(8,gint8,int8,UINT8,/* */)
  166. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(16,guint16,uint16_le,UINT16_LE,/* */)
  167. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(16,guint16,uint16_be,UINT16_BE,/* */)
  168. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(16,gint16,int16_le,UINT16_LE,/* */)
  169. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(16,gint16,int16_be,UINT16_BE,/* */)
  170. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,guint32,uint32_le,UINT32_LE,/* */)
  171. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,guint32,uint32_be,UINT32_BE,/* */)
  172. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,gint32,int32_le,UINT32_LE,/* */)
  173. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,gint32,int32_be,UINT32_BE,/* */)
  174. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(24,guint32,uint24_le,UINT24_LE,/* */)
  175. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(24,guint32,uint24_be,UINT24_BE,/* */)
  176. /* fix up the sign for 24-bit signed ints stored in 32-bit signed ints */
  177. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(24,gint32,int24_le,UINT24_LE,
  178. if (val & 0x00800000) val |= 0xff000000;)
  179. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(24,gint32,int24_be,UINT24_BE,
  180. if (val & 0x00800000) val |= 0xff000000;)
  181. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,guint64,uint64_le,UINT64_LE,/* */)
  182. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,guint64,uint64_be,UINT64_BE,/* */)
  183. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,gint64,int64_le,UINT64_LE,/* */)
  184. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,gint64,int64_be,UINT64_BE,/* */)
  185. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,gfloat,float32_le,FLOAT_LE,/* */)
  186. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,gfloat,float32_be,FLOAT_BE,/* */)
  187. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,gdouble,float64_le,DOUBLE_LE,/* */)
  188. __GST_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,gdouble,float64_be,DOUBLE_BE,/* */)
  189. #undef __GET_PEEK_BITS_UNCHECKED
  190. static inline const guint8 *
  191. gst_byte_reader_peek_data_unchecked (const GstByteReader * reader)
  192. {
  193. return (const guint8 *) (reader->data + reader->byte);
  194. }
  195. static inline const guint8 *
  196. gst_byte_reader_get_data_unchecked (GstByteReader * reader, guint size)
  197. {
  198. const guint8 *data;
  199. data = gst_byte_reader_peek_data_unchecked (reader);
  200. gst_byte_reader_skip_unchecked (reader, size);
  201. return data;
  202. }
  203. static inline guint8 *
  204. gst_byte_reader_dup_data_unchecked (GstByteReader * reader, guint size)
  205. {
  206. gconstpointer data = gst_byte_reader_get_data_unchecked (reader, size);
  207. return (guint8 *) g_memdup (data, size);
  208. }
  209. /* Unchecked variants that should not be used */
  210. static inline guint
  211. _gst_byte_reader_get_pos_unchecked (const GstByteReader * reader)
  212. {
  213. return reader->byte;
  214. }
  215. static inline guint
  216. _gst_byte_reader_get_remaining_unchecked (const GstByteReader * reader)
  217. {
  218. return reader->size - reader->byte;
  219. }
  220. static inline guint
  221. _gst_byte_reader_get_size_unchecked (const GstByteReader * reader)
  222. {
  223. return reader->size;
  224. }
  225. /* inlined variants (do not use directly) */
  226. static inline guint
  227. _gst_byte_reader_get_remaining_inline (const GstByteReader * reader)
  228. {
  229. g_return_val_if_fail (reader != NULL, 0);
  230. return _gst_byte_reader_get_remaining_unchecked (reader);
  231. }
  232. static inline guint
  233. _gst_byte_reader_get_size_inline (const GstByteReader * reader)
  234. {
  235. g_return_val_if_fail (reader != NULL, 0);
  236. return _gst_byte_reader_get_size_unchecked (reader);
  237. }
  238. #define __GST_BYTE_READER_GET_PEEK_BITS_INLINE(bits,type,name) \
  239. \
  240. static inline gboolean \
  241. _gst_byte_reader_peek_##name##_inline (const GstByteReader * reader, type * val) \
  242. { \
  243. g_return_val_if_fail (reader != NULL, FALSE); \
  244. g_return_val_if_fail (val != NULL, FALSE); \
  245. \
  246. if (_gst_byte_reader_get_remaining_unchecked (reader) < (bits / 8)) \
  247. return FALSE; \
  248. \
  249. *val = gst_byte_reader_peek_##name##_unchecked (reader); \
  250. return TRUE; \
  251. } \
  252. \
  253. static inline gboolean \
  254. _gst_byte_reader_get_##name##_inline (GstByteReader * reader, type * val) \
  255. { \
  256. g_return_val_if_fail (reader != NULL, FALSE); \
  257. g_return_val_if_fail (val != NULL, FALSE); \
  258. \
  259. if (_gst_byte_reader_get_remaining_unchecked (reader) < (bits / 8)) \
  260. return FALSE; \
  261. \
  262. *val = gst_byte_reader_get_##name##_unchecked (reader); \
  263. return TRUE; \
  264. }
  265. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(8,guint8,uint8)
  266. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(8,gint8,int8)
  267. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(16,guint16,uint16_le)
  268. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(16,guint16,uint16_be)
  269. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(16,gint16,int16_le)
  270. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(16,gint16,int16_be)
  271. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(32,guint32,uint32_le)
  272. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(32,guint32,uint32_be)
  273. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(32,gint32,int32_le)
  274. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(32,gint32,int32_be)
  275. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(24,guint32,uint24_le)
  276. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(24,guint32,uint24_be)
  277. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(24,gint32,int24_le)
  278. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(24,gint32,int24_be)
  279. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(64,guint64,uint64_le)
  280. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(64,guint64,uint64_be)
  281. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(64,gint64,int64_le)
  282. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(64,gint64,int64_be)
  283. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(32,gfloat,float32_le)
  284. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(32,gfloat,float32_be)
  285. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(64,gdouble,float64_le)
  286. __GST_BYTE_READER_GET_PEEK_BITS_INLINE(64,gdouble,float64_be)
  287. #undef __GST_BYTE_READER_GET_PEEK_BITS_INLINE
  288. #ifndef GST_BYTE_READER_DISABLE_INLINES
  289. #define gst_byte_reader_init(reader,data,size) \
  290. _gst_byte_reader_init_inline(reader,data,size)
  291. #define gst_byte_reader_get_remaining(reader) \
  292. _gst_byte_reader_get_remaining_inline(reader)
  293. #define gst_byte_reader_get_size(reader) \
  294. _gst_byte_reader_get_size_inline(reader)
  295. #define gst_byte_reader_get_pos(reader) \
  296. _gst_byte_reader_get_pos_inline(reader)
  297. /* we use defines here so we can add the G_LIKELY() */
  298. #define gst_byte_reader_get_uint8(reader,val) \
  299. G_LIKELY(_gst_byte_reader_get_uint8_inline(reader,val))
  300. #define gst_byte_reader_get_int8(reader,val) \
  301. G_LIKELY(_gst_byte_reader_get_int8_inline(reader,val))
  302. #define gst_byte_reader_get_uint16_le(reader,val) \
  303. G_LIKELY(_gst_byte_reader_get_uint16_le_inline(reader,val))
  304. #define gst_byte_reader_get_int16_le(reader,val) \
  305. G_LIKELY(_gst_byte_reader_get_int16_le_inline(reader,val))
  306. #define gst_byte_reader_get_uint16_be(reader,val) \
  307. G_LIKELY(_gst_byte_reader_get_uint16_be_inline(reader,val))
  308. #define gst_byte_reader_get_int16_be(reader,val) \
  309. G_LIKELY(_gst_byte_reader_get_int16_be_inline(reader,val))
  310. #define gst_byte_reader_get_uint24_le(reader,val) \
  311. G_LIKELY(_gst_byte_reader_get_uint24_le_inline(reader,val))
  312. #define gst_byte_reader_get_int24_le(reader,val) \
  313. G_LIKELY(_gst_byte_reader_get_int24_le_inline(reader,val))
  314. #define gst_byte_reader_get_uint24_be(reader,val) \
  315. G_LIKELY(_gst_byte_reader_get_uint24_be_inline(reader,val))
  316. #define gst_byte_reader_get_int24_be(reader,val) \
  317. G_LIKELY(_gst_byte_reader_get_int24_be_inline(reader,val))
  318. #define gst_byte_reader_get_uint32_le(reader,val) \
  319. G_LIKELY(_gst_byte_reader_get_uint32_le_inline(reader,val))
  320. #define gst_byte_reader_get_int32_le(reader,val) \
  321. G_LIKELY(_gst_byte_reader_get_int32_le_inline(reader,val))
  322. #define gst_byte_reader_get_uint32_be(reader,val) \
  323. G_LIKELY(_gst_byte_reader_get_uint32_be_inline(reader,val))
  324. #define gst_byte_reader_get_int32_be(reader,val) \
  325. G_LIKELY(_gst_byte_reader_get_int32_be_inline(reader,val))
  326. #define gst_byte_reader_get_uint64_le(reader,val) \
  327. G_LIKELY(_gst_byte_reader_get_uint64_le_inline(reader,val))
  328. #define gst_byte_reader_get_int64_le(reader,val) \
  329. G_LIKELY(_gst_byte_reader_get_int64_le_inline(reader,val))
  330. #define gst_byte_reader_get_uint64_be(reader,val) \
  331. G_LIKELY(_gst_byte_reader_get_uint64_be_inline(reader,val))
  332. #define gst_byte_reader_get_int64_be(reader,val) \
  333. G_LIKELY(_gst_byte_reader_get_int64_be_inline(reader,val))
  334. #define gst_byte_reader_peek_uint8(reader,val) \
  335. G_LIKELY(_gst_byte_reader_peek_uint8_inline(reader,val))
  336. #define gst_byte_reader_peek_int8(reader,val) \
  337. G_LIKELY(_gst_byte_reader_peek_int8_inline(reader,val))
  338. #define gst_byte_reader_peek_uint16_le(reader,val) \
  339. G_LIKELY(_gst_byte_reader_peek_uint16_le_inline(reader,val))
  340. #define gst_byte_reader_peek_int16_le(reader,val) \
  341. G_LIKELY(_gst_byte_reader_peek_int16_le_inline(reader,val))
  342. #define gst_byte_reader_peek_uint16_be(reader,val) \
  343. G_LIKELY(_gst_byte_reader_peek_uint16_be_inline(reader,val))
  344. #define gst_byte_reader_peek_int16_be(reader,val) \
  345. G_LIKELY(_gst_byte_reader_peek_int16_be_inline(reader,val))
  346. #define gst_byte_reader_peek_uint24_le(reader,val) \
  347. G_LIKELY(_gst_byte_reader_peek_uint24_le_inline(reader,val))
  348. #define gst_byte_reader_peek_int24_le(reader,val) \
  349. G_LIKELY(_gst_byte_reader_peek_int24_le_inline(reader,val))
  350. #define gst_byte_reader_peek_uint24_be(reader,val) \
  351. G_LIKELY(_gst_byte_reader_peek_uint24_be_inline(reader,val))
  352. #define gst_byte_reader_peek_int24_be(reader,val) \
  353. G_LIKELY(_gst_byte_reader_peek_int24_be_inline(reader,val))
  354. #define gst_byte_reader_peek_uint32_le(reader,val) \
  355. G_LIKELY(_gst_byte_reader_peek_uint32_le_inline(reader,val))
  356. #define gst_byte_reader_peek_int32_le(reader,val) \
  357. G_LIKELY(_gst_byte_reader_peek_int32_le_inline(reader,val))
  358. #define gst_byte_reader_peek_uint32_be(reader,val) \
  359. G_LIKELY(_gst_byte_reader_peek_uint32_be_inline(reader,val))
  360. #define gst_byte_reader_peek_int32_be(reader,val) \
  361. G_LIKELY(_gst_byte_reader_peek_int32_be_inline(reader,val))
  362. #define gst_byte_reader_peek_uint64_le(reader,val) \
  363. G_LIKELY(_gst_byte_reader_peek_uint64_le_inline(reader,val))
  364. #define gst_byte_reader_peek_int64_le(reader,val) \
  365. G_LIKELY(_gst_byte_reader_peek_int64_le_inline(reader,val))
  366. #define gst_byte_reader_peek_uint64_be(reader,val) \
  367. G_LIKELY(_gst_byte_reader_peek_uint64_be_inline(reader,val))
  368. #define gst_byte_reader_peek_int64_be(reader,val) \
  369. G_LIKELY(_gst_byte_reader_peek_int64_be_inline(reader,val))
  370. #define gst_byte_reader_get_float32_le(reader,val) \
  371. G_LIKELY(_gst_byte_reader_get_float32_le_inline(reader,val))
  372. #define gst_byte_reader_get_float32_be(reader,val) \
  373. G_LIKELY(_gst_byte_reader_get_float32_be_inline(reader,val))
  374. #define gst_byte_reader_get_float64_le(reader,val) \
  375. G_LIKELY(_gst_byte_reader_get_float64_le_inline(reader,val))
  376. #define gst_byte_reader_get_float64_be(reader,val) \
  377. G_LIKELY(_gst_byte_reader_get_float64_be_inline(reader,val))
  378. #define gst_byte_reader_peek_float32_le(reader,val) \
  379. G_LIKELY(_gst_byte_reader_peek_float32_le_inline(reader,val))
  380. #define gst_byte_reader_peek_float32_be(reader,val) \
  381. G_LIKELY(_gst_byte_reader_peek_float32_be_inline(reader,val))
  382. #define gst_byte_reader_peek_float64_le(reader,val) \
  383. G_LIKELY(_gst_byte_reader_peek_float64_le_inline(reader,val))
  384. #define gst_byte_reader_peek_float64_be(reader,val) \
  385. G_LIKELY(_gst_byte_reader_peek_float64_be_inline(reader,val))
  386. #endif /* GST_BYTE_READER_DISABLE_INLINES */
  387. static inline void
  388. _gst_byte_reader_init_inline (GstByteReader * reader, const guint8 * data, guint size)
  389. {
  390. g_return_if_fail (reader != NULL);
  391. reader->data = data;
  392. reader->size = size;
  393. reader->byte = 0;
  394. }
  395. static inline gboolean
  396. _gst_byte_reader_peek_sub_reader_inline (GstByteReader * reader,
  397. GstByteReader * sub_reader, guint size)
  398. {
  399. g_return_val_if_fail (reader != NULL, FALSE);
  400. g_return_val_if_fail (sub_reader != NULL, FALSE);
  401. if (_gst_byte_reader_get_remaining_unchecked (reader) < size)
  402. return FALSE;
  403. sub_reader->data = reader->data + reader->byte;
  404. sub_reader->byte = 0;
  405. sub_reader->size = size;
  406. return TRUE;
  407. }
  408. static inline gboolean
  409. _gst_byte_reader_get_sub_reader_inline (GstByteReader * reader,
  410. GstByteReader * sub_reader, guint size)
  411. {
  412. if (!_gst_byte_reader_peek_sub_reader_inline (reader, sub_reader, size))
  413. return FALSE;
  414. gst_byte_reader_skip_unchecked (reader, size);
  415. return TRUE;
  416. }
  417. static inline gboolean
  418. _gst_byte_reader_dup_data_inline (GstByteReader * reader, guint size, guint8 ** val)
  419. {
  420. g_return_val_if_fail (reader != NULL, FALSE);
  421. g_return_val_if_fail (val != NULL, FALSE);
  422. if (G_UNLIKELY (size > reader->size || _gst_byte_reader_get_remaining_unchecked (reader) < size))
  423. return FALSE;
  424. *val = gst_byte_reader_dup_data_unchecked (reader, size);
  425. return TRUE;
  426. }
  427. static inline gboolean
  428. _gst_byte_reader_get_data_inline (GstByteReader * reader, guint size, const guint8 ** val)
  429. {
  430. g_return_val_if_fail (reader != NULL, FALSE);
  431. g_return_val_if_fail (val != NULL, FALSE);
  432. if (G_UNLIKELY (size > reader->size || _gst_byte_reader_get_remaining_unchecked (reader) < size))
  433. return FALSE;
  434. *val = gst_byte_reader_get_data_unchecked (reader, size);
  435. return TRUE;
  436. }
  437. static inline gboolean
  438. _gst_byte_reader_peek_data_inline (const GstByteReader * reader, guint size, const guint8 ** val)
  439. {
  440. g_return_val_if_fail (reader != NULL, FALSE);
  441. g_return_val_if_fail (val != NULL, FALSE);
  442. if (G_UNLIKELY (size > reader->size || _gst_byte_reader_get_remaining_unchecked (reader) < size))
  443. return FALSE;
  444. *val = gst_byte_reader_peek_data_unchecked (reader);
  445. return TRUE;
  446. }
  447. static inline guint
  448. _gst_byte_reader_get_pos_inline (const GstByteReader * reader)
  449. {
  450. g_return_val_if_fail (reader != NULL, 0);
  451. return _gst_byte_reader_get_pos_unchecked (reader);
  452. }
  453. static inline gboolean
  454. _gst_byte_reader_skip_inline (GstByteReader * reader, guint nbytes)
  455. {
  456. g_return_val_if_fail (reader != NULL, FALSE);
  457. if (G_UNLIKELY (_gst_byte_reader_get_remaining_unchecked (reader) < nbytes))
  458. return FALSE;
  459. reader->byte += nbytes;
  460. return TRUE;
  461. }
  462. #ifndef GST_BYTE_READER_DISABLE_INLINES
  463. #define gst_byte_reader_dup_data(reader,size,val) \
  464. G_LIKELY(_gst_byte_reader_dup_data_inline(reader,size,val))
  465. #define gst_byte_reader_get_data(reader,size,val) \
  466. G_LIKELY(_gst_byte_reader_get_data_inline(reader,size,val))
  467. #define gst_byte_reader_peek_data(reader,size,val) \
  468. G_LIKELY(_gst_byte_reader_peek_data_inline(reader,size,val))
  469. #define gst_byte_reader_skip(reader,nbytes) \
  470. G_LIKELY(_gst_byte_reader_skip_inline(reader,nbytes))
  471. #endif /* GST_BYTE_READER_DISABLE_INLINES */
  472. G_END_DECLS
  473. #endif /* __GST_BYTE_READER_H__ */