gstmemory.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* GStreamer
  2. * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.be>
  3. *
  4. * gstmemory.h: Header for memory blocks
  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_MEMORY_H__
  22. #define __GST_MEMORY_H__
  23. #include <gst/gstconfig.h>
  24. #include <glib-object.h>
  25. #include <gst/gstminiobject.h>
  26. G_BEGIN_DECLS
  27. GST_EXPORT GType _gst_memory_type;
  28. #define GST_TYPE_MEMORY (_gst_memory_type)
  29. GType gst_memory_get_type(void);
  30. typedef struct _GstMemory GstMemory;
  31. typedef struct _GstAllocator GstAllocator;
  32. #define GST_MEMORY_CAST(mem) ((GstMemory *)(mem))
  33. /**
  34. * GstMemoryFlags:
  35. * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
  36. * memory with #GST_MAP_WRITE.
  37. * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
  38. * made when this memory needs to be shared between buffers.
  39. * @GST_MEMORY_FLAG_ZERO_PREFIXED: the memory prefix is filled with 0 bytes
  40. * @GST_MEMORY_FLAG_ZERO_PADDED: the memory padding is filled with 0 bytes
  41. * @GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS: the memory is physically contiguous. (Since 1.2)
  42. * @GST_MEMORY_FLAG_NOT_MAPPABLE: the memory can't be mapped via gst_memory_map() without any preconditions. (Since 1.2)
  43. * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
  44. *
  45. * Flags for wrapped memory.
  46. */
  47. typedef enum {
  48. GST_MEMORY_FLAG_READONLY = GST_MINI_OBJECT_FLAG_LOCK_READONLY,
  49. GST_MEMORY_FLAG_NO_SHARE = (GST_MINI_OBJECT_FLAG_LAST << 0),
  50. GST_MEMORY_FLAG_ZERO_PREFIXED = (GST_MINI_OBJECT_FLAG_LAST << 1),
  51. GST_MEMORY_FLAG_ZERO_PADDED = (GST_MINI_OBJECT_FLAG_LAST << 2),
  52. GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS = (GST_MINI_OBJECT_FLAG_LAST << 3),
  53. GST_MEMORY_FLAG_NOT_MAPPABLE = (GST_MINI_OBJECT_FLAG_LAST << 4),
  54. GST_MEMORY_FLAG_LAST = (GST_MINI_OBJECT_FLAG_LAST << 16)
  55. } GstMemoryFlags;
  56. /**
  57. * GST_MEMORY_FLAGS:
  58. * @mem: a #GstMemory.
  59. *
  60. * A flags word containing #GstMemoryFlags flags set on @mem
  61. */
  62. #define GST_MEMORY_FLAGS(mem) GST_MINI_OBJECT_FLAGS (mem)
  63. /**
  64. * GST_MEMORY_FLAG_IS_SET:
  65. * @mem: a #GstMemory.
  66. * @flag: the #GstMemoryFlags to check.
  67. *
  68. * Gives the status of a specific flag on a @mem.
  69. */
  70. #define GST_MEMORY_FLAG_IS_SET(mem,flag) GST_MINI_OBJECT_FLAG_IS_SET (mem,flag)
  71. /**
  72. * GST_MEMORY_FLAG_UNSET:
  73. * @mem: a #GstMemory.
  74. * @flag: the #GstMemoryFlags to clear.
  75. *
  76. * Clear a specific flag on a @mem.
  77. */
  78. #define GST_MEMORY_FLAG_UNSET(mem,flag) GST_MINI_OBJECT_FLAG_UNSET (mem, flag)
  79. /**
  80. * GST_MEMORY_IS_READONLY:
  81. * @mem: a #GstMemory.
  82. *
  83. * Check if @mem is readonly.
  84. */
  85. #define GST_MEMORY_IS_READONLY(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
  86. /**
  87. * GST_MEMORY_IS_NO_SHARE:
  88. * @mem: a #GstMemory.
  89. *
  90. * Check if @mem cannot be shared between buffers
  91. */
  92. #define GST_MEMORY_IS_NO_SHARE(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NO_SHARE)
  93. /**
  94. * GST_MEMORY_IS_ZERO_PREFIXED:
  95. * @mem: a #GstMemory.
  96. *
  97. * Check if the prefix in @mem is 0 filled.
  98. */
  99. #define GST_MEMORY_IS_ZERO_PREFIXED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
  100. /**
  101. * GST_MEMORY_IS_ZERO_PADDED:
  102. * @mem: a #GstMemory.
  103. *
  104. * Check if the padding in @mem is 0 filled.
  105. */
  106. #define GST_MEMORY_IS_ZERO_PADDED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
  107. /**
  108. * GST_MEMORY_IS_PHYSICALLY_CONTIGUOUS:
  109. * @mem: a #GstMemory.
  110. *
  111. * Check if @mem is physically contiguous.
  112. *
  113. * Since: 1.2
  114. */
  115. #define GST_MEMORY_IS_PHYSICALLY_CONTIGUOUS(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS)
  116. /**
  117. * GST_MEMORY_IS_NOT_MAPPABLE:
  118. * @mem: a #GstMemory.
  119. *
  120. * Check if @mem can't be mapped via gst_memory_map() without any preconditions
  121. *
  122. * Since: 1.2
  123. */
  124. #define GST_MEMORY_IS_NOT_MAPPABLE(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NOT_MAPPABLE)
  125. /**
  126. * GstMemory:
  127. * @mini_object: parent structure
  128. * @allocator: pointer to the #GstAllocator
  129. * @parent: parent memory block
  130. * @maxsize: the maximum size allocated
  131. * @align: the alignment of the memory
  132. * @offset: the offset where valid data starts
  133. * @size: the size of valid data
  134. *
  135. * Base structure for memory implementations. Custom memory will put this structure
  136. * as the first member of their structure.
  137. */
  138. struct _GstMemory {
  139. GstMiniObject mini_object;
  140. GstAllocator *allocator;
  141. GstMemory *parent;
  142. gsize maxsize;
  143. gsize align;
  144. gsize offset;
  145. gsize size;
  146. };
  147. /**
  148. * GstMapFlags:
  149. * @GST_MAP_READ: map for read access
  150. * @GST_MAP_WRITE: map for write access
  151. * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
  152. *
  153. * Flags used when mapping memory
  154. */
  155. typedef enum {
  156. GST_MAP_READ = GST_LOCK_FLAG_READ,
  157. GST_MAP_WRITE = GST_LOCK_FLAG_WRITE,
  158. GST_MAP_FLAG_LAST = (1 << 16)
  159. } GstMapFlags;
  160. /**
  161. * GST_MAP_READWRITE:
  162. *
  163. * GstMapFlags value alias for GST_MAP_READ | GST_MAP_WRITE
  164. */
  165. #define GST_MAP_READWRITE (GST_MAP_READ | GST_MAP_WRITE)
  166. /**
  167. * GstMapInfo:
  168. * @memory: a pointer to the mapped memory
  169. * @flags: flags used when mapping the memory
  170. * @data: (array length=size): a pointer to the mapped data
  171. * @size: the valid size in @data
  172. * @maxsize: the maximum bytes in @data
  173. * @user_data: extra private user_data that the implementation of the memory
  174. * can use to store extra info.
  175. *
  176. * A structure containing the result of a map operation such as
  177. * gst_memory_map(). It contains the data and size.
  178. */
  179. typedef struct {
  180. GstMemory *memory;
  181. GstMapFlags flags;
  182. guint8 *data;
  183. gsize size;
  184. gsize maxsize;
  185. /*< protected >*/
  186. gpointer user_data[4];
  187. /*< private >*/
  188. gpointer _gst_reserved[GST_PADDING];
  189. } GstMapInfo;
  190. /**
  191. * GST_MAP_INFO_INIT:
  192. *
  193. * Initializer for #GstMapInfo
  194. */
  195. #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0, {0, }, {0, }}
  196. /**
  197. * GstMemoryMapFunction:
  198. * @mem: a #GstMemory
  199. * @maxsize: size to map
  200. * @flags: access mode for the memory
  201. *
  202. * Get the memory of @mem that can be accessed according to the mode specified
  203. * in @flags. The function should return a pointer that contains at least
  204. * @maxsize bytes.
  205. *
  206. * Returns: a pointer to memory of which at least @maxsize bytes can be
  207. * accessed according to the access pattern in @flags.
  208. */
  209. typedef gpointer (*GstMemoryMapFunction) (GstMemory *mem, gsize maxsize, GstMapFlags flags);
  210. /**
  211. * GstMemoryMapFullFunction:
  212. * @mem: a #GstMemory
  213. * @info: the #GstMapInfo to map with
  214. * @maxsize: size to map
  215. *
  216. * Get the memory of @mem that can be accessed according to the mode specified
  217. * in @info's flags. The function should return a pointer that contains at least
  218. * @maxsize bytes.
  219. *
  220. * Returns: a pointer to memory of which at least @maxsize bytes can be
  221. * accessed according to the access pattern in @info's flags.
  222. */
  223. typedef gpointer (*GstMemoryMapFullFunction) (GstMemory *mem, GstMapInfo * info, gsize maxsize);
  224. /**
  225. * GstMemoryUnmapFunction:
  226. * @mem: a #GstMemory
  227. *
  228. * Return the pointer previously retrieved with gst_memory_map().
  229. */
  230. typedef void (*GstMemoryUnmapFunction) (GstMemory *mem);
  231. /**
  232. * GstMemoryUnmapFullFunction:
  233. * @mem: a #GstMemory
  234. * @info: a #GstMapInfo
  235. *
  236. * Return the pointer previously retrieved with gst_memory_map() with @info.
  237. */
  238. typedef void (*GstMemoryUnmapFullFunction) (GstMemory *mem, GstMapInfo * info);
  239. /**
  240. * GstMemoryCopyFunction:
  241. * @mem: a #GstMemory
  242. * @offset: an offset
  243. * @size: a size or -1
  244. *
  245. * Copy @size bytes from @mem starting at @offset and return them wrapped in a
  246. * new GstMemory object.
  247. * If @size is set to -1, all bytes starting at @offset are copied.
  248. *
  249. * Returns: a new #GstMemory object wrapping a copy of the requested region in
  250. * @mem.
  251. */
  252. typedef GstMemory * (*GstMemoryCopyFunction) (GstMemory *mem, gssize offset, gssize size);
  253. /**
  254. * GstMemoryShareFunction:
  255. * @mem: a #GstMemory
  256. * @offset: an offset
  257. * @size: a size or -1
  258. *
  259. * Share @size bytes from @mem starting at @offset and return them wrapped in a
  260. * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
  261. * shared. This function does not make a copy of the bytes in @mem.
  262. *
  263. * Returns: a new #GstMemory object sharing the requested region in @mem.
  264. */
  265. typedef GstMemory * (*GstMemoryShareFunction) (GstMemory *mem, gssize offset, gssize size);
  266. /**
  267. * GstMemoryIsSpanFunction:
  268. * @mem1: a #GstMemory
  269. * @mem2: a #GstMemory
  270. * @offset: a result offset
  271. *
  272. * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
  273. * @mem1 in the parent buffer in @offset.
  274. *
  275. * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
  276. */
  277. typedef gboolean (*GstMemoryIsSpanFunction) (GstMemory *mem1, GstMemory *mem2, gsize *offset);
  278. void gst_memory_init (GstMemory *mem, GstMemoryFlags flags,
  279. GstAllocator *allocator, GstMemory *parent,
  280. gsize maxsize, gsize align,
  281. gsize offset, gsize size);
  282. gboolean gst_memory_is_type (GstMemory *mem, const gchar *mem_type);
  283. /* refcounting */
  284. /**
  285. * gst_memory_ref:
  286. * @memory: The memory to refcount
  287. *
  288. * Increase the refcount of this memory.
  289. *
  290. * Returns: (transfer full): @memory (for convenience when doing assignments)
  291. */
  292. static inline GstMemory *
  293. gst_memory_ref (GstMemory * memory)
  294. {
  295. return (GstMemory *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (memory));
  296. }
  297. /**
  298. * gst_memory_unref:
  299. * @memory: (transfer full): the memory to refcount
  300. *
  301. * Decrease the refcount of an memory, freeing it if the refcount reaches 0.
  302. */
  303. static inline void
  304. gst_memory_unref (GstMemory * memory)
  305. {
  306. gst_mini_object_unref (GST_MINI_OBJECT_CAST (memory));
  307. }
  308. /* getting/setting memory properties */
  309. gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
  310. void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
  311. #define gst_memory_lock(m,f) gst_mini_object_lock (GST_MINI_OBJECT_CAST (m), (f))
  312. #define gst_memory_unlock(m,f) gst_mini_object_unlock (GST_MINI_OBJECT_CAST (m), (f))
  313. #define gst_memory_is_writable(m) gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (m))
  314. #define gst_memory_make_writable(m) GST_MEMORY_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (m)))
  315. /* retrieving data */
  316. GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
  317. gboolean gst_memory_map (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
  318. void gst_memory_unmap (GstMemory *mem, GstMapInfo *info);
  319. /* copy and subregions */
  320. GstMemory * gst_memory_copy (GstMemory *mem, gssize offset, gssize size);
  321. GstMemory * gst_memory_share (GstMemory *mem, gssize offset, gssize size);
  322. /* span memory */
  323. gboolean gst_memory_is_span (GstMemory *mem1, GstMemory *mem2, gsize *offset);
  324. #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
  325. G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstMemory, gst_memory_unref)
  326. #endif
  327. #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
  328. G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstAllocator, gst_object_unref)
  329. #endif
  330. G_END_DECLS
  331. #endif /* __GST_MEMORY_H__ */