4 This document describes the design for buffers.
6 A GstBuffer is the object that is passed from an upstream element to a
7 downstream element and contains memory and metadata information.
13 * allocation, free, low fragmentation
14 - Must be able to attach multiple memory blocks to the buffer
15 - Must be able to attach artibtrary metadata to buffers
16 - efficient handling of subbuffer, copy, span, trim
21 The Buffers is writable when the refcount is 1. This means that:
23 - metadata can be added/removed and the metadata can be changed
24 - GstMemory blocks can be added/removed
26 The individual memory blocks have their own refcounting and READONLY flags
27 that might influence their writability.
29 Buffers can be made writable with gst_buffer_make_writable(). This will copy the
30 buffer with the metadata and will ref the memory in the buffer. This means that
31 the memory is not automatically copied when copying buffers.
37 A GstBuffer contains an array of pointers to GstMemory objects.
39 When the buffer is writable, gst_buffer_insert_memory() can be used to add a
40 new GstMemory object to the buffer. When the array of memory is full, memory
41 will be merged to make room for the new memory object.
43 gst_buffer_n_memory() is used to get the amount of memory blocks on the
46 With gst_buffer_peek_memory(), memory can be retrieved from the memory array.
47 The desired access pattern for the memory block should be specified so that
48 appropriate checks can be made and, in case of GST_MAP_WRITE, a writable copy
49 can be constructed when needed.
51 gst_buffer_remove_memory_range() and gst_buffer_remove_memory() can be used to
52 remove memory from the GstBuffer.
58 Subbuffers are made by copying only a region of the memory blocks and copying
65 Spanning will merge together the data of 2 buffers into a new buffer
71 Accessing the data of the buffer can happen by retrieving the individual
72 GstMemory objects in the GstBuffer or my using the gst_buffer_map() and
73 gst_buffer_unmap() function.
75 The _map and _unmap function will always return the memory of all blocks as one
76 large contiguous region of memory. Using the _map and _unmap function might be
77 more convenient than accessing the individual memory blocks at the expense of
78 being more expensive because it might perform memcpy operations.
80 For buffers with only one GstMemory object (the most common case), _map and
81 _unmap have no performance penalty at all.
84 * Read access with 1 memory block
86 The memory block is accessed and mapped for read access.
87 The memory block is unmapped after usage
89 * write access with 1 memory block
91 The buffer should be writable or this operation will fail..
92 The memory block is accessed. If the memory block is readonly, a copy is made
93 and the original memory block is replaced with this copy. then the memory
94 block is mapped in write mode.
95 The memory block is unmapped after usage.
97 * Read access with multiple memory blocks
99 The memory blocks are combined into one large memory block. If the buffer is
100 writable, The memory blocks are replace with this new memory block. If the
101 buffer is not writable, the memory is returned as is.
102 The memory block is then mapped in read mode.
104 When the memory is unmapped after usage and the buffer has multiple memory
105 blocks, this means that the map operation was not able to store the combined
106 buffer and it thus returned memory that should be freed. Otherwise, the memory
109 * Write access with multiple memory blocks
111 The buffer should be writable or the operation fails. The memory blocks are
112 combined into one large memory block and the existing blocks are replaced with
113 this new block. The memory is then mapped in write mode.
114 The memory is unmapped after usage.
120 Generating RTP packets from h264 video
121 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123 We receive as input a GstBuffer with an encoded h264 image and we need to
124 create RTP packets containing this h264 data as the payload. We typically need
125 to fragment the h264 data into multiple packets, each with their own RTP and
126 payload specific header.
128 +-------+-------+---------------------------+--------+
129 input H264 buffer: | NALU1 | NALU2 | ..... | NALUx |
130 +-------+-------+---------------------------+--------+
133 array of +-+ +-------+ +-+ +-------+ +-+ +-------+
134 output buffers: | | | NALU1 | | | | NALU2 | .... | | | NALUx |
135 +-+ +-------+ +-+ +-------+ +-+ +-------+
137 \-----------/ \-----------/
140 The output buffer array consists of x buffers consisting of an RTP payload header
141 and a subbuffer of the original input H264 buffer. Since the rtp headers and
142 the h264 data don't need to be contiguous in memory, they are added to the buffer
143 as separate GstMemory blocks and we can avoid to memcpy the h264 data into
146 A typical udpsink will then use something like sendmsg to send the memory regions
147 on the network inside one UDP packet. This will further avoid having to memcpy
148 data into contiguous memory.
150 Using bufferlists, the complete array of output buffers can be pushed in one
151 operation to the peer element.