diff options
author | Ognyan Tonchev <ognyan@axis.com> | 2020-02-06 11:23:24 +0300 |
---|---|---|
committer | Sebastian Dröge <slomo@coaxion.net> | 2020-03-06 13:44:16 +0300 |
commit | a78a74bff08564fe8ab33841af8e62fecf090992 (patch) | |
tree | 0d83a0df81ad63dc0f681a877755ef327a1f8c99 /gst/rtp | |
parent | 3e0d5577444b32579bdf5b69d720a322322ff7bc (diff) |
rtph26x: Use gst_memory_map() instead of gst_buffer_map() in avc mode
gst_buffer_map () results in memcopying when a GstBuffer contains
more than one GstMemory and when AVC (length-prefixed) alignment is used.
This has quite an impact on performance on systems with limited amount of
resources. With this patch the whole GstBuffer will not be mapped at once,
instead each individual GstMemory will be iterated and mapped separately.
Diffstat (limited to 'gst/rtp')
-rw-r--r-- | gst/rtp/gstbuffermemory.c | 116 | ||||
-rw-r--r-- | gst/rtp/gstbuffermemory.h | 64 | ||||
-rw-r--r-- | gst/rtp/gstrtph264pay.c | 60 | ||||
-rw-r--r-- | gst/rtp/gstrtph265pay.c | 53 | ||||
-rw-r--r-- | gst/rtp/meson.build | 1 |
5 files changed, 247 insertions, 47 deletions
diff --git a/gst/rtp/gstbuffermemory.c b/gst/rtp/gstbuffermemory.c new file mode 100644 index 000000000..3b28417ba --- /dev/null +++ b/gst/rtp/gstbuffermemory.c @@ -0,0 +1,116 @@ +/* GStreamer + * Copyright (C) 2020 Ognyan Tonchev <ognyan at axis dot com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "gstbuffermemory.h" + +gboolean +gst_buffer_memory_map (GstBuffer * buffer, GstBufferMemoryMap * map) +{ + GstMemory *mem; + + g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE); + g_return_val_if_fail (map != NULL, FALSE); + + if (gst_buffer_n_memory (buffer) == 0) { + GST_DEBUG ("no memory blocks in buffer"); + return FALSE; + } + + mem = gst_buffer_get_memory (buffer, 0); + + if (!gst_memory_map (mem, &map->map, GST_MAP_READ)) { + GST_ERROR ("failed to map memory"); + gst_memory_unref (mem); + return FALSE; + } + + map->buf = buffer; + map->mem = mem; + map->data = map->map.data; + map->size = map->map.size; + map->index = 0; + + return TRUE; +} + +static gboolean +buffer_memory_map_next (GstBufferMemoryMap * map) +{ + if (!map->mem) + return FALSE; + + gst_memory_unmap (map->mem, &map->map); + gst_memory_unref (map->mem); + map->mem = NULL; + map->data = NULL; + map->size = 0; + + map->index++; + + if (map->index >= gst_buffer_n_memory (map->buf)) { + GST_DEBUG ("no more memory blocks in buffer"); + return FALSE; + } + + map->mem = gst_buffer_get_memory (map->buf, map->index); + + if (!gst_memory_map (map->mem, &map->map, GST_MAP_READ)) { + GST_ERROR ("failed to map memory"); + gst_memory_unref (map->mem); + map->mem = NULL; + return FALSE; + } + + map->data = map->map.data; + map->size = map->map.size; + + return TRUE; +} + +gboolean +gst_buffer_memory_advance_bytes (GstBufferMemoryMap * map, gsize size) +{ + gsize offset = size; + + g_return_val_if_fail (map != NULL, FALSE); + + while (offset >= map->size) { + offset -= map->size; + GST_DEBUG ("switching memory"); + if (!buffer_memory_map_next (map)) + return FALSE; + } + + map->data += offset; + map->size -= offset; + + return TRUE; +} + +void +gst_buffer_memory_unmap (GstBufferMemoryMap * map) +{ + g_return_if_fail (map != NULL); + + if (map->mem) { + gst_memory_unmap (map->mem, &map->map); + gst_memory_unref (map->mem); + map->mem = NULL; + } +} diff --git a/gst/rtp/gstbuffermemory.h b/gst/rtp/gstbuffermemory.h new file mode 100644 index 000000000..cc00d4c1b --- /dev/null +++ b/gst/rtp/gstbuffermemory.h @@ -0,0 +1,64 @@ +/* GStreamer + * Copyright (C) 2020 Ognyan Tonchev <ognyan at axis dot com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GST_BUFFER_MEMORY_H__ +#define __GST_BUFFER_MEMORY_H__ + +#include <gst/gst.h> + +G_BEGIN_DECLS + +struct _GstBufferMemoryMap +{ + /* private datas */ + + GstBuffer *buf; + GstMemory *mem; + GstMapInfo map; + guint index; + + /* public datas */ + + /* data of the currently mapped memory */ + const guint8 *data; + + /* size of the currently mapped memory */ + gsize size; + + /* When advancing through the data with gst_buffer_memory_advance_bytes () + * the data field is also advanced and the size field decreased with the + * corresponding number of bytes. If all the bytes from the currently mapped + * GstMemory have been consumed then a new GstMemory will be mapped and data + * and size fileds will be updated. + * */ +}; +typedef struct _GstBufferMemoryMap GstBufferMemoryMap; + +G_GNUC_INTERNAL +gboolean gst_buffer_memory_map (GstBuffer * buffer, GstBufferMemoryMap * map); + +G_GNUC_INTERNAL +gboolean gst_buffer_memory_advance_bytes (GstBufferMemoryMap * map, gsize size); + +G_GNUC_INTERNAL +void gst_buffer_memory_unmap (GstBufferMemoryMap * map); + +G_END_DECLS + +#endif /* __GST_BUFFER_MEMORY_H__ */ diff --git a/gst/rtp/gstrtph264pay.c b/gst/rtp/gstrtph264pay.c index b5d3e6ab7..623b68dac 100644 --- a/gst/rtp/gstrtph264pay.c +++ b/gst/rtp/gstrtph264pay.c @@ -33,6 +33,7 @@ #include "gstrtph264pay.h" #include "gstrtputils.h" +#include "gstbuffermemory.h" #define IDR_TYPE_ID 5 @@ -1342,7 +1343,6 @@ gst_rtp_h264_pay_handle_buffer (GstRTPBasePayload * basepayload, GstFlowReturn ret; gsize size; guint nal_len, i; - GstMapInfo map; const guint8 *data; GstClockTime dts, pts; GArray *nal_queue; @@ -1364,16 +1364,6 @@ gst_rtp_h264_pay_handle_buffer (GstRTPBasePayload * basepayload, /* In AVC mode, there is no adapter, so nothing to drain */ if (draining) return GST_FLOW_OK; - gst_buffer_map (buffer, &map, GST_MAP_READ); - data = map.data; - size = map.size; - pts = GST_BUFFER_PTS (buffer); - dts = GST_BUFFER_DTS (buffer); - rtph264pay->delta_unit = GST_BUFFER_FLAG_IS_SET (buffer, - GST_BUFFER_FLAG_DELTA_UNIT); - rtph264pay->discont = GST_BUFFER_IS_DISCONT (buffer); - marker = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_MARKER); - GST_DEBUG_OBJECT (basepayload, "got %" G_GSIZE_FORMAT " bytes", size); } else { if (buffer) { if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) { @@ -1417,29 +1407,43 @@ gst_rtp_h264_pay_handle_buffer (GstRTPBasePayload * basepayload, /* now loop over all NAL units and put them in a packet */ if (avc) { + GstBufferMemoryMap memory; + gsize remaining_buffer_size; guint nal_length_size; gsize offset = 0; + gst_buffer_memory_map (buffer, &memory); + remaining_buffer_size = gst_buffer_get_size (buffer); + + pts = GST_BUFFER_PTS (buffer); + dts = GST_BUFFER_DTS (buffer); + rtph264pay->delta_unit = GST_BUFFER_FLAG_IS_SET (buffer, + GST_BUFFER_FLAG_DELTA_UNIT); + rtph264pay->discont = GST_BUFFER_IS_DISCONT (buffer); + marker = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_MARKER); + GST_DEBUG_OBJECT (basepayload, "got %" G_GSIZE_FORMAT " bytes", + remaining_buffer_size); + nal_length_size = rtph264pay->nal_length_size; - while (size > nal_length_size) { + while (remaining_buffer_size > nal_length_size) { gint i; gboolean end_of_au = FALSE; nal_len = 0; for (i = 0; i < nal_length_size; i++) { - nal_len = ((nal_len << 8) + data[i]); + nal_len = (nal_len << 8) + *memory.data; + if (!gst_buffer_memory_advance_bytes (&memory, 1)) + break; } - /* skip the length bytes, make sure we don't run past the buffer size */ - data += nal_length_size; offset += nal_length_size; - size -= nal_length_size; + remaining_buffer_size -= nal_length_size; - if (size >= nal_len) { + if (remaining_buffer_size >= nal_len) { GST_DEBUG_OBJECT (basepayload, "got NAL of size %u", nal_len); } else { - nal_len = size; + nal_len = remaining_buffer_size; GST_DEBUG_OBJECT (basepayload, "got incomplete NAL of size %u", nal_len); } @@ -1447,7 +1451,7 @@ gst_rtp_h264_pay_handle_buffer (GstRTPBasePayload * basepayload, /* If we're at the end of the buffer, then we're at the end of the * access unit */ - if (size - nal_len <= nal_length_size) { + if (remaining_buffer_size - nal_len <= nal_length_size) { if (rtph264pay->alignment == GST_H264_ALIGNMENT_AU || marker) end_of_au = TRUE; } @@ -1469,10 +1473,19 @@ gst_rtp_h264_pay_handle_buffer (GstRTPBasePayload * basepayload, if (ret != GST_FLOW_OK) break; - data += nal_len; + /* Skip current nal. If it is split over multiple GstMemory + * advance_bytes () will switch to the correct GstMemory. The payloader + * does not access those bytes directly but uses gst_buffer_copy_region () + * to create a sub-buffer referencing the nal instead */ + if (!gst_buffer_memory_advance_bytes (&memory, nal_len)) + break; + offset += nal_len; - size -= nal_len; + remaining_buffer_size -= nal_len; } + + gst_buffer_memory_unmap (&memory); + gst_buffer_unref (buffer); } else { guint next; gboolean update = FALSE; @@ -1638,10 +1651,7 @@ gst_rtp_h264_pay_handle_buffer (GstRTPBasePayload * basepayload, done: - if (avc) { - gst_buffer_unmap (buffer, &map); - gst_buffer_unref (buffer); - } else { + if (!avc) { gst_adapter_unmap (rtph264pay->adapter); } diff --git a/gst/rtp/gstrtph265pay.c b/gst/rtp/gstrtph265pay.c index d5285f051..420f164b8 100644 --- a/gst/rtp/gstrtph265pay.c +++ b/gst/rtp/gstrtph265pay.c @@ -34,6 +34,7 @@ #include "gstrtph265pay.h" #include "gstrtputils.h" +#include "gstbuffermemory.h" #define AP_TYPE_ID 48 #define FU_TYPE_ID 49 @@ -1424,7 +1425,6 @@ gst_rtp_h265_pay_handle_buffer (GstRTPBasePayload * basepayload, GstFlowReturn ret; gsize size; guint nal_len, i; - GstMapInfo map; const guint8 *data; GstClockTime dts, pts; GArray *nal_queue; @@ -1446,13 +1446,6 @@ gst_rtp_h265_pay_handle_buffer (GstRTPBasePayload * basepayload, /* In hevc mode, there is no adapter, so nothing to drain */ if (draining) return GST_FLOW_OK; - gst_buffer_map (buffer, &map, GST_MAP_READ); - data = map.data; - size = map.size; - pts = GST_BUFFER_PTS (buffer); - dts = GST_BUFFER_DTS (buffer); - marker = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_MARKER); - GST_DEBUG_OBJECT (basepayload, "got %" G_GSIZE_FORMAT " bytes", size); } else { if (buffer) { if (gst_adapter_available (rtph265pay->adapter) == 0) @@ -1478,6 +1471,8 @@ gst_rtp_h265_pay_handle_buffer (GstRTPBasePayload * basepayload, /* now loop over all NAL units and put them in a packet */ if (hevc) { + GstBufferMemoryMap memory; + gsize remaining_buffer_size; guint nal_length_size; gsize offset = 0; GPtrArray *paybufs; @@ -1485,23 +1480,32 @@ gst_rtp_h265_pay_handle_buffer (GstRTPBasePayload * basepayload, paybufs = g_ptr_array_new (); nal_length_size = rtph265pay->nal_length_size; - while (size > nal_length_size) { + gst_buffer_memory_map (buffer, &memory); + remaining_buffer_size = gst_buffer_get_size (buffer); + + pts = GST_BUFFER_PTS (buffer); + dts = GST_BUFFER_DTS (buffer); + marker = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_MARKER); + GST_DEBUG_OBJECT (basepayload, "got %" G_GSIZE_FORMAT " bytes", + remaining_buffer_size); + + while (remaining_buffer_size > nal_length_size) { gint i; nal_len = 0; for (i = 0; i < nal_length_size; i++) { - nal_len = ((nal_len << 8) + data[i]); + nal_len = (nal_len << 8) + *memory.data; + if (!gst_buffer_memory_advance_bytes (&memory, 1)) + break; } - /* skip the length bytes, make sure we don't run past the buffer size */ - data += nal_length_size; offset += nal_length_size; - size -= nal_length_size; + remaining_buffer_size -= nal_length_size; - if (size >= nal_len) { + if (remaining_buffer_size >= nal_len) { GST_DEBUG_OBJECT (basepayload, "got NAL of size %u", nal_len); } else { - nal_len = size; + nal_len = remaining_buffer_size; GST_DEBUG_OBJECT (basepayload, "got incomplete NAL of size %u", nal_len); } @@ -1514,7 +1518,7 @@ gst_rtp_h265_pay_handle_buffer (GstRTPBasePayload * basepayload, * access unit */ GST_BUFFER_FLAG_UNSET (paybuf, GST_BUFFER_FLAG_MARKER); - if (size - nal_len <= nal_length_size) { + if (remaining_buffer_size - nal_len <= nal_length_size) { if (rtph265pay->alignment == GST_H265_ALIGNMENT_AU || marker) GST_BUFFER_FLAG_SET (paybuf, GST_BUFFER_FLAG_MARKER); } @@ -1525,11 +1529,19 @@ gst_rtp_h265_pay_handle_buffer (GstRTPBasePayload * basepayload, discont = FALSE; } - data += nal_len; + /* Skip current nal. If it is split over multiple GstMemory + * advance_bytes () will switch to the correct GstMemory. The payloader + * does not access those bytes directly but uses gst_buffer_copy_region () + * to create a sub-buffer referencing the nal instead */ + if (!gst_buffer_memory_advance_bytes (&memory, nal_len)) + break; offset += nal_len; - size -= nal_len; + remaining_buffer_size -= nal_len; } ret = gst_rtp_h265_pay_payload_nal (basepayload, paybufs, dts, pts); + + gst_buffer_memory_unmap (&memory); + gst_buffer_unref (buffer); } else { guint next; gboolean update = FALSE; @@ -1658,10 +1670,7 @@ gst_rtp_h265_pay_handle_buffer (GstRTPBasePayload * basepayload, } done: - if (hevc) { - gst_buffer_unmap (buffer, &map); - gst_buffer_unref (buffer); - } else { + if (!hevc) { gst_adapter_unmap (rtph265pay->adapter); } diff --git a/gst/rtp/meson.build b/gst/rtp/meson.build index 5bfdc3be7..d57a195ae 100644 --- a/gst/rtp/meson.build +++ b/gst/rtp/meson.build @@ -1,6 +1,7 @@ rtp_sources = [ 'dboolhuff.c', 'fnv1hash.c', + 'gstbuffermemory.c', 'gstrtp.c', 'gstrtpchannels.c', 'gstrtpac3depay.c', |