gstmemory.h 11 KB

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