部分图片文字来自网络文章,侵权必删
内存分配和管理是多媒体中非常重要的主题。一个高清视频可能会使用几MB来存储单个图像帧,尽可能复用内存并减少内存拷贝非常重要。多媒体系统通常使用专用芯片(如 DSP 或 GPU)来执行繁重的工作(尤其是视频),这些专用芯片通常对其操作的内存及其访问方式有严格的要求。
本章讨论 GStreamer 插件可用的内存管理功能。我们将首先讨论管理对内存访问的低级 GstMemory 对象,然后继续讨论GstMemory的主要使用者之一:GstBuffer,它用于在element之间和与应用程序之间交换数据。我们还将讨论 GstMeta,此对象可以放置在缓冲区上以提供额外信息。我们还将讨论 GstBufferPool,它允许更有效地管理相同大小的缓冲区。
结束最后我们将研究 GST_QUERY_ALLOCATION 查询,它用于协商元素之间的内存管理选项。
一、GstMemory
GstMemory 是一个管理内存区域的对象,此内存对象指向“最大尺寸(maxsize)”的内存区域。可以访问的内存区域从“偏移量(offset)”开始,大小为“size”字节。创建 GstMemory 后其最大尺寸将无法再更改,但其“offset”和“size”可以更改。
/**
* GstMemory:
* @mini_object: parent structure
* @allocator: pointer to the #GstAllocator
* @parent: parent memory block
* @maxsize: the maximum size allocated
* @align: the alignment of the memory
* @offset: the offset where valid data starts
* @size: the size of valid data
*
* Base structure for memory implementations. Custom memory will put this structure
* as the first member of their structure.
*/
struct _GstMemory {
GstMiniObject mini_object;
GstAllocator *allocator;
GstMemory *parent;
gsize maxsize;
gsize align;
gsize offset;
gsize size;
};
1. GstAllocator
GstMemory 对象由 GstAllocator 对象创建。大多数分配器都实现默认的 gst_allocator_alloc() 方法,但有些分配器可能会实现不同的方法,例如当需要其他参数来分配特定内存时。
系统内存、共享内存和由 DMAbuf 文件描述符支持的内存存在不同的分配器。如果需要实现对新内存类型的支持,您必须实现新的分配器对象。
2. GstMemory 应用接口示例
对 GstMemory 对象包装的内存的数据访问始终受到 gst_memory_map() 和 gst_memory_unmap()函数对的保护:应用程序使用gst_memory_map映射内存(指定读写访问模式),该函数返回指向有效内存区域的指针,然后可以根据请求的访问模式对其进行访问;访问结束后应用程序使用gst_memory_unmap函数解除内存映射。
以下是创建 GstMemory 对象并使用 gst_memory_map/gst_memory_unmap函数对 访问内存区域的示例:
[...]
GstMemory *mem;
GstMapInfo info;
gint i;
/* allocate 100 bytes */
mem = gst_allocator_alloc (NULL, 100, NULL);
/* get access to the memory in write mode */
gst_memory_map (mem, &info, GST_MAP_WRITE);
/* fill with pattern */
for (i = 0; i < info.size; i++)
info.data[i] = i;
/* release memory */
gst_memory_unmap (mem, &info);
[...]
二、GstBuffer
GstBuffer 是一个轻量级对象,包含内存和元数据,它从上游element传递到下游elenent,表征被下游element获取的多媒体内容。GstBuffer 包含一个或多个 GstMemory 对象,这些对象保存缓冲区的数据。
/**
* GstBuffer:
* @mini_object: the parent structure
* @pool: pointer to the pool owner of the buffer
* @pts: presentation timestamp of the buffer, can be #GST_CLOCK_TIME_NONE when the
* pts is not known or relevant. The pts contains the timestamp when the
* media should be presented to the user.
* @dts: decoding timestamp of the buffer, can be #GST_CLOCK_TIME_NONE when the
* dts is not known or relevant. The dts contains the timestamp when the
* media should be processed.
* @duration: duration in time of the buffer data, can be #GST_CLOCK_TIME_NONE
* when the duration is not known or relevant.
* @offset: a media specific offset for the buffer data.
* For