gstbytewriter.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* GStreamer byte writer
  2. *
  3. * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #ifndef __GST_BYTE_WRITER_H__
  21. #define __GST_BYTE_WRITER_H__
  22. #include <gst/gst.h>
  23. #include <gst/base/gstbytereader.h>
  24. #include <string.h>
  25. G_BEGIN_DECLS
  26. #define GST_BYTE_WRITER(writer) ((GstByteWriter *) (writer))
  27. /**
  28. * GstByteWriter:
  29. * @parent: #GstByteReader parent
  30. * @alloc_size: Allocation size of the data
  31. * @fixed: If %TRUE no reallocations are allowed
  32. * @owned: If %FALSE no reallocations are allowed and copies of data are returned
  33. *
  34. * A byte writer instance.
  35. */
  36. typedef struct {
  37. GstByteReader parent;
  38. guint alloc_size;
  39. gboolean fixed;
  40. gboolean owned;
  41. /* < private > */
  42. gpointer _gst_reserved[GST_PADDING];
  43. } GstByteWriter;
  44. GstByteWriter * gst_byte_writer_new (void) G_GNUC_MALLOC;
  45. GstByteWriter * gst_byte_writer_new_with_size (guint size, gboolean fixed) G_GNUC_MALLOC;
  46. GstByteWriter * gst_byte_writer_new_with_data (guint8 *data, guint size, gboolean initialized) G_GNUC_MALLOC;
  47. void gst_byte_writer_init (GstByteWriter *writer);
  48. void gst_byte_writer_init_with_size (GstByteWriter *writer, guint size, gboolean fixed);
  49. void gst_byte_writer_init_with_data (GstByteWriter *writer, guint8 *data,
  50. guint size, gboolean initialized);
  51. void gst_byte_writer_free (GstByteWriter *writer);
  52. guint8 * gst_byte_writer_free_and_get_data (GstByteWriter *writer);
  53. GstBuffer * gst_byte_writer_free_and_get_buffer (GstByteWriter *writer) G_GNUC_MALLOC;
  54. void gst_byte_writer_reset (GstByteWriter *writer);
  55. guint8 * gst_byte_writer_reset_and_get_data (GstByteWriter *writer);
  56. GstBuffer * gst_byte_writer_reset_and_get_buffer (GstByteWriter *writer) G_GNUC_MALLOC;
  57. /**
  58. * gst_byte_writer_get_pos:
  59. * @writer: #GstByteWriter instance
  60. *
  61. * Returns: The current position of the read/write cursor
  62. */
  63. /**
  64. * gst_byte_writer_set_pos:
  65. * @writer: #GstByteWriter instance
  66. * @pos: new position
  67. *
  68. * Sets the current read/write cursor of @writer. The new position
  69. * can only be between 0 and the current size.
  70. *
  71. * Returns: %TRUE if the new position could be set
  72. */
  73. /**
  74. * gst_byte_writer_get_size:
  75. * @writer: #GstByteWriter instance
  76. *
  77. * Returns: The current, initialized size of the data
  78. */
  79. static inline guint
  80. gst_byte_writer_get_pos (const GstByteWriter *writer)
  81. {
  82. return gst_byte_reader_get_pos ((const GstByteReader *) writer);
  83. }
  84. static inline gboolean
  85. gst_byte_writer_set_pos (GstByteWriter *writer, guint pos)
  86. {
  87. return gst_byte_reader_set_pos (GST_BYTE_READER (writer), pos);
  88. }
  89. static inline guint
  90. gst_byte_writer_get_size (const GstByteWriter *writer)
  91. {
  92. return gst_byte_reader_get_size ((const GstByteReader *) writer);
  93. }
  94. guint gst_byte_writer_get_remaining (const GstByteWriter *writer);
  95. gboolean gst_byte_writer_ensure_free_space (GstByteWriter *writer, guint size);
  96. gboolean gst_byte_writer_put_uint8 (GstByteWriter *writer, guint8 val);
  97. gboolean gst_byte_writer_put_int8 (GstByteWriter *writer, gint8 val);
  98. gboolean gst_byte_writer_put_uint16_be (GstByteWriter *writer, guint16 val);
  99. gboolean gst_byte_writer_put_uint16_le (GstByteWriter *writer, guint16 val);
  100. gboolean gst_byte_writer_put_int16_be (GstByteWriter *writer, gint16 val);
  101. gboolean gst_byte_writer_put_int16_le (GstByteWriter *writer, gint16 val);
  102. gboolean gst_byte_writer_put_uint24_be (GstByteWriter *writer, guint32 val);
  103. gboolean gst_byte_writer_put_uint24_le (GstByteWriter *writer, guint32 val);
  104. gboolean gst_byte_writer_put_int24_be (GstByteWriter *writer, gint32 val);
  105. gboolean gst_byte_writer_put_int24_le (GstByteWriter *writer, gint32 val);
  106. gboolean gst_byte_writer_put_uint32_be (GstByteWriter *writer, guint32 val);
  107. gboolean gst_byte_writer_put_uint32_le (GstByteWriter *writer, guint32 val);
  108. gboolean gst_byte_writer_put_int32_be (GstByteWriter *writer, gint32 val);
  109. gboolean gst_byte_writer_put_int32_le (GstByteWriter *writer, gint32 val);
  110. gboolean gst_byte_writer_put_uint64_be (GstByteWriter *writer, guint64 val);
  111. gboolean gst_byte_writer_put_uint64_le (GstByteWriter *writer, guint64 val);
  112. gboolean gst_byte_writer_put_int64_be (GstByteWriter *writer, gint64 val);
  113. gboolean gst_byte_writer_put_int64_le (GstByteWriter *writer, gint64 val);
  114. gboolean gst_byte_writer_put_float32_be (GstByteWriter *writer, gfloat val);
  115. gboolean gst_byte_writer_put_float32_le (GstByteWriter *writer, gfloat val);
  116. gboolean gst_byte_writer_put_float64_be (GstByteWriter *writer, gdouble val);
  117. gboolean gst_byte_writer_put_float64_le (GstByteWriter *writer, gdouble val);
  118. gboolean gst_byte_writer_put_data (GstByteWriter *writer, const guint8 *data, guint size);
  119. gboolean gst_byte_writer_fill (GstByteWriter *writer, guint8 value, guint size);
  120. gboolean gst_byte_writer_put_string_utf8 (GstByteWriter *writer, const gchar *data);
  121. gboolean gst_byte_writer_put_string_utf16 (GstByteWriter *writer, const guint16 *data);
  122. gboolean gst_byte_writer_put_string_utf32 (GstByteWriter *writer, const guint32 *data);
  123. gboolean gst_byte_writer_put_buffer (GstByteWriter *writer, GstBuffer * buffer, gsize offset, gssize size);
  124. /**
  125. * gst_byte_writer_put_string:
  126. * @writer: #GstByteWriter instance
  127. * @data: (in) (array zero-terminated=1): Null terminated string
  128. *
  129. * Write a NUL-terminated string to @writer (including the terminator). The
  130. * string is assumed to be in an 8-bit encoding (e.g. ASCII,UTF-8 or
  131. * ISO-8859-1).
  132. *
  133. * Returns: %TRUE if the string could be written
  134. */
  135. #define gst_byte_writer_put_string(writer, data) \
  136. gst_byte_writer_put_string_utf8(writer, data)
  137. static inline guint
  138. _gst_byte_writer_next_pow2 (guint n)
  139. {
  140. guint ret = 16;
  141. /* We start with 16, smaller allocations make no sense */
  142. while (ret < n && ret > 0)
  143. ret <<= 1;
  144. return ret ? ret : n;
  145. }
  146. static inline gboolean
  147. _gst_byte_writer_ensure_free_space_inline (GstByteWriter * writer, guint size)
  148. {
  149. gpointer data;
  150. if (G_LIKELY (size <= writer->alloc_size - writer->parent.byte))
  151. return TRUE;
  152. if (G_UNLIKELY (writer->fixed || !writer->owned))
  153. return FALSE;
  154. if (G_UNLIKELY (writer->parent.byte > G_MAXUINT - size))
  155. return FALSE;
  156. writer->alloc_size = _gst_byte_writer_next_pow2 (writer->parent.byte + size);
  157. data = g_try_realloc ((guint8 *) writer->parent.data, writer->alloc_size);
  158. if (G_UNLIKELY (data == NULL))
  159. return FALSE;
  160. writer->parent.data = (guint8 *) data;
  161. return TRUE;
  162. }
  163. #define __GST_BYTE_WRITER_CREATE_WRITE_FUNC(bits,type,name,write_func) \
  164. static inline void \
  165. gst_byte_writer_put_##name##_unchecked (GstByteWriter *writer, type val) \
  166. { \
  167. guint8 *write_data; \
  168. \
  169. write_data = (guint8 *) writer->parent.data + writer->parent.byte; \
  170. write_func (write_data, val); \
  171. writer->parent.byte += bits/8; \
  172. writer->parent.size = MAX (writer->parent.size, writer->parent.byte); \
  173. } \
  174. \
  175. static inline gboolean \
  176. _gst_byte_writer_put_##name##_inline (GstByteWriter *writer, type val) \
  177. { \
  178. g_return_val_if_fail (writer != NULL, FALSE); \
  179. \
  180. if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline(writer, bits/8))) \
  181. return FALSE; \
  182. \
  183. gst_byte_writer_put_##name##_unchecked (writer, val); \
  184. \
  185. return TRUE; \
  186. }
  187. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (8, guint8, uint8, GST_WRITE_UINT8)
  188. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (8, gint8, int8, GST_WRITE_UINT8)
  189. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (16, guint16, uint16_le, GST_WRITE_UINT16_LE)
  190. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (16, guint16, uint16_be, GST_WRITE_UINT16_BE)
  191. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (16, gint16, int16_le, GST_WRITE_UINT16_LE)
  192. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (16, gint16, int16_be, GST_WRITE_UINT16_BE)
  193. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (24, guint32, uint24_le, GST_WRITE_UINT24_LE)
  194. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (24, guint32, uint24_be, GST_WRITE_UINT24_BE)
  195. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (24, gint32, int24_le, GST_WRITE_UINT24_LE)
  196. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (24, gint32, int24_be, GST_WRITE_UINT24_BE)
  197. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, guint32, uint32_le, GST_WRITE_UINT32_LE)
  198. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, guint32, uint32_be, GST_WRITE_UINT32_BE)
  199. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, gint32, int32_le, GST_WRITE_UINT32_LE)
  200. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, gint32, int32_be, GST_WRITE_UINT32_BE)
  201. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, guint64, uint64_le, GST_WRITE_UINT64_LE)
  202. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, guint64, uint64_be, GST_WRITE_UINT64_BE)
  203. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, gint64, int64_le, GST_WRITE_UINT64_LE)
  204. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, gint64, int64_be, GST_WRITE_UINT64_BE)
  205. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, gfloat, float32_be, GST_WRITE_FLOAT_BE)
  206. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, gfloat, float32_le, GST_WRITE_FLOAT_LE)
  207. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, gdouble, float64_be, GST_WRITE_DOUBLE_BE)
  208. __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, gdouble, float64_le, GST_WRITE_DOUBLE_LE)
  209. #undef __GST_BYTE_WRITER_CREATE_WRITE_FUNC
  210. static inline void
  211. gst_byte_writer_put_data_unchecked (GstByteWriter * writer, const guint8 * data,
  212. guint size)
  213. {
  214. memcpy ((guint8 *) & writer->parent.data[writer->parent.byte], data, size);
  215. writer->parent.byte += size;
  216. writer->parent.size = MAX (writer->parent.size, writer->parent.byte);
  217. }
  218. static inline gboolean
  219. _gst_byte_writer_put_data_inline (GstByteWriter * writer, const guint8 * data,
  220. guint size)
  221. {
  222. g_return_val_if_fail (writer != NULL, FALSE);
  223. if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline (writer, size)))
  224. return FALSE;
  225. gst_byte_writer_put_data_unchecked (writer, data, size);
  226. return TRUE;
  227. }
  228. static inline void
  229. gst_byte_writer_fill_unchecked (GstByteWriter * writer, guint8 value, guint size)
  230. {
  231. memset ((guint8 *) & writer->parent.data[writer->parent.byte], value, size);
  232. writer->parent.byte += size;
  233. writer->parent.size = MAX (writer->parent.size, writer->parent.byte);
  234. }
  235. static inline gboolean
  236. _gst_byte_writer_fill_inline (GstByteWriter * writer, guint8 value, guint size)
  237. {
  238. g_return_val_if_fail (writer != NULL, FALSE);
  239. if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline (writer, size)))
  240. return FALSE;
  241. gst_byte_writer_fill_unchecked (writer, value, size);
  242. return TRUE;
  243. }
  244. static inline void
  245. gst_byte_writer_put_buffer_unchecked (GstByteWriter * writer, GstBuffer * buffer,
  246. gsize offset, gssize size)
  247. {
  248. if (size == -1) {
  249. size = gst_buffer_get_size (buffer);
  250. if (offset >= (gsize) size)
  251. return;
  252. size -= offset;
  253. }
  254. gst_buffer_extract (buffer, offset,
  255. (guint8 *) & writer->parent.data[writer->parent.byte], size);
  256. writer->parent.byte += size;
  257. writer->parent.size = MAX (writer->parent.size, writer->parent.byte);
  258. }
  259. static inline gboolean
  260. _gst_byte_writer_put_buffer_inline (GstByteWriter * writer, GstBuffer * buffer,
  261. gsize offset, gssize size)
  262. {
  263. g_return_val_if_fail (writer != NULL, FALSE);
  264. g_return_val_if_fail (size >= -1, FALSE);
  265. if (size == -1) {
  266. size = gst_buffer_get_size (buffer);
  267. if (offset >= (gsize) size)
  268. return TRUE;
  269. size -= offset;
  270. }
  271. if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline (writer, size)))
  272. return FALSE;
  273. gst_byte_writer_put_buffer_unchecked (writer, buffer, offset, size);
  274. return TRUE;
  275. }
  276. #ifndef GST_BYTE_WRITER_DISABLE_INLINES
  277. /* we use defines here so we can add the G_LIKELY() */
  278. #define gst_byte_writer_ensure_free_space(writer, size) \
  279. G_LIKELY (_gst_byte_writer_ensure_free_space_inline (writer, size))
  280. #define gst_byte_writer_put_uint8(writer, val) \
  281. G_LIKELY (_gst_byte_writer_put_uint8_inline (writer, val))
  282. #define gst_byte_writer_put_int8(writer, val) \
  283. G_LIKELY (_gst_byte_writer_put_int8_inline (writer, val))
  284. #define gst_byte_writer_put_uint16_be(writer, val) \
  285. G_LIKELY (_gst_byte_writer_put_uint16_be_inline (writer, val))
  286. #define gst_byte_writer_put_uint16_le(writer, val) \
  287. G_LIKELY (_gst_byte_writer_put_uint16_le_inline (writer, val))
  288. #define gst_byte_writer_put_int16_be(writer, val) \
  289. G_LIKELY (_gst_byte_writer_put_int16_be_inline (writer, val))
  290. #define gst_byte_writer_put_int16_le(writer, val) \
  291. G_LIKELY (_gst_byte_writer_put_int16_le_inline (writer, val))
  292. #define gst_byte_writer_put_uint24_be(writer, val) \
  293. G_LIKELY (_gst_byte_writer_put_uint24_be_inline (writer, val))
  294. #define gst_byte_writer_put_uint24_le(writer, val) \
  295. G_LIKELY (_gst_byte_writer_put_uint24_le_inline (writer, val))
  296. #define gst_byte_writer_put_int24_be(writer, val) \
  297. G_LIKELY (_gst_byte_writer_put_int24_be_inline (writer, val))
  298. #define gst_byte_writer_put_int24_le(writer, val) \
  299. G_LIKELY (_gst_byte_writer_put_int24_le_inline (writer, val))
  300. #define gst_byte_writer_put_uint32_be(writer, val) \
  301. G_LIKELY (_gst_byte_writer_put_uint32_be_inline (writer, val))
  302. #define gst_byte_writer_put_uint32_le(writer, val) \
  303. G_LIKELY (_gst_byte_writer_put_uint32_le_inline (writer, val))
  304. #define gst_byte_writer_put_int32_be(writer, val) \
  305. G_LIKELY (_gst_byte_writer_put_int32_be_inline (writer, val))
  306. #define gst_byte_writer_put_int32_le(writer, val) \
  307. G_LIKELY (_gst_byte_writer_put_int32_le_inline (writer, val))
  308. #define gst_byte_writer_put_uint64_be(writer, val) \
  309. G_LIKELY (_gst_byte_writer_put_uint64_be_inline (writer, val))
  310. #define gst_byte_writer_put_uint64_le(writer, val) \
  311. G_LIKELY (_gst_byte_writer_put_uint64_le_inline (writer, val))
  312. #define gst_byte_writer_put_int64_be(writer, val) \
  313. G_LIKELY (_gst_byte_writer_put_int64_be_inline (writer, val))
  314. #define gst_byte_writer_put_int64_le(writer, val) \
  315. G_LIKELY (_gst_byte_writer_put_int64_le_inline (writer, val))
  316. #define gst_byte_writer_put_float32_be(writer, val) \
  317. G_LIKELY (_gst_byte_writer_put_float32_be_inline (writer, val))
  318. #define gst_byte_writer_put_float32_le(writer, val) \
  319. G_LIKELY (_gst_byte_writer_put_float32_le_inline (writer, val))
  320. #define gst_byte_writer_put_float64_be(writer, val) \
  321. G_LIKELY (_gst_byte_writer_put_float64_be_inline (writer, val))
  322. #define gst_byte_writer_put_float64_le(writer, val) \
  323. G_LIKELY (_gst_byte_writer_put_float64_le_inline (writer, val))
  324. #define gst_byte_writer_put_data(writer, data, size) \
  325. G_LIKELY (_gst_byte_writer_put_data_inline (writer, data, size))
  326. #define gst_byte_writer_fill(writer, val, size) \
  327. G_LIKELY (_gst_byte_writer_fill_inline (writer, val, size))
  328. #define gst_byte_writer_put_buffer(writer, buffer, offset, size) \
  329. G_LIKELY (_gst_byte_writer_put_buffer_inline (writer, buffer, offset, size))
  330. #endif
  331. G_END_DECLS
  332. #endif /* __GST_BYTE_WRITER_H__ */