From 5eb42535470a739ae90651feb6963a4ca2c57f9d Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Sun, 19 Feb 2023 22:14:39 +0900 Subject: [PATCH] qsvh264enc: Optimization for byte-stream to packetized format conversion Allocate single memory instead of per NAL Part-of: --- .../gst-plugins-bad/sys/qsv/gstqsvh264enc.cpp | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/subprojects/gst-plugins-bad/sys/qsv/gstqsvh264enc.cpp b/subprojects/gst-plugins-bad/sys/qsv/gstqsvh264enc.cpp index 32aa9500e0..9167429574 100644 --- a/subprojects/gst-plugins-bad/sys/qsv/gstqsvh264enc.cpp +++ b/subprojects/gst-plugins-bad/sys/qsv/gstqsvh264enc.cpp @@ -1755,7 +1755,11 @@ gst_qsv_h264_enc_create_output_buffer (GstQsvEncoder * encoder, buf = gst_buffer_new_memdup (bitstream->Data + bitstream->DataOffset, bitstream->DataLength); } else { - buf = gst_buffer_new (); + std::vector < GstH264NalUnit > nalu_list; + gsize total_size = 0; + GstMapInfo info; + guint8 *data; + rst = gst_h264_parser_identify_nalu (self->parser, bitstream->Data + bitstream->DataOffset, 0, bitstream->DataLength, &nalu); @@ -1763,16 +1767,8 @@ gst_qsv_h264_enc_create_output_buffer (GstQsvEncoder * encoder, rst = GST_H264_PARSER_OK; while (rst == GST_H264_PARSER_OK) { - GstMemory *mem; - guint8 *data; - - data = (guint8 *) g_malloc0 (nalu.size + 4); - GST_WRITE_UINT32_BE (data, nalu.size); - memcpy (data + 4, nalu.data + nalu.offset, nalu.size); - - mem = gst_memory_new_wrapped ((GstMemoryFlags) 0, data, nalu.size + 4, - 0, nalu.size + 4, data, (GDestroyNotify) g_free); - gst_buffer_append_memory (buf, mem); + nalu_list.push_back (nalu); + total_size += nalu.size + 4; rst = gst_h264_parser_identify_nalu (self->parser, bitstream->Data + bitstream->DataOffset, nalu.offset + nalu.size, @@ -1781,6 +1777,21 @@ gst_qsv_h264_enc_create_output_buffer (GstQsvEncoder * encoder, if (rst == GST_H264_PARSER_NO_NAL_END) rst = GST_H264_PARSER_OK; } + + buf = gst_buffer_new_and_alloc (total_size); + gst_buffer_map (buf, &info, GST_MAP_WRITE); + data = (guint8 *) info.data; + + /* *INDENT-OFF* */ + for (const auto & it : nalu_list) { + GST_WRITE_UINT32_BE (data, it.size); + data += 4; + memcpy (data, it.data + it.offset, it.size); + data += it.size; + } + /* *INDENT-ON* */ + + gst_buffer_unmap (buf, &info); } /* This buffer must be the end of a frame boundary */