From 8d728abd8c59270cdd5acdcc42f7e8f7880a4982 Mon Sep 17 00:00:00 2001 From: Thijs Vermeir Date: Wed, 20 May 2015 12:11:31 +0200 Subject: [PATCH] x265enc: Only copy VPS/SPS/PPS NAL units in header buffer Previously the VPS unit was detected and all next packets where copied into the header buffer assuming only SPS and PPS would follow. This is not always true, also other types of NAL units follow the VPS unit and where copied to the header buffer. Now the VPS/SPS/PPS are explicitely detected and copied in the header buffer. --- ext/x265/gstx265enc.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/ext/x265/gstx265enc.c b/ext/x265/gstx265enc.c index 325ab2270e..47b95a66ae 100644 --- a/ext/x265/gstx265enc.c +++ b/ext/x265/gstx265enc.c @@ -768,7 +768,8 @@ static GstBuffer * gst_x265_enc_get_header_buffer (GstX265Enc * encoder) { x265_nal *nal; - guint32 i_nal, i, offset, vps_idx; + guint32 i_nal, i, offset; + gint32 vps_idx, sps_idx, pps_idx; int header_return; GstBuffer *buf; @@ -785,13 +786,18 @@ gst_x265_enc_get_header_buffer (GstX265Enc * encoder) * The usefull headers are sequential (VPS, SPS and PPS), so we look for this * nal units and only copy these tree nal units as the header */ - for (vps_idx = 0; vps_idx < i_nal; vps_idx++) { - if (nal[vps_idx].type == 32) { - break; + vps_idx = sps_idx = pps_idx = -1; + for (i = 0; i < i_nal; i++) { + if (nal[i].type == 32) { + vps_idx = i; + } else if (nal[i].type == 33) { + sps_idx = i; + } else if (nal[i].type == 34) { + pps_idx = i; } } - if (vps_idx >= i_nal - 3) { + if (vps_idx == -1 || sps_idx == -1 || pps_idx == -1) { GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, ("Encode x265 header failed."), ("x265_encoder_headers did not return VPS, SPS and PPS")); return FALSE; @@ -800,13 +806,13 @@ gst_x265_enc_get_header_buffer (GstX265Enc * encoder) offset = 0; buf = gst_buffer_new_allocate (NULL, - nal[vps_idx].sizeBytes + nal[vps_idx + 1].sizeBytes + nal[vps_idx + - 2].sizeBytes, NULL); - for (i = 0; i < i_nal; i++) { - gst_buffer_fill (buf, offset, nal[i + vps_idx].payload, - nal[i + vps_idx].sizeBytes); - offset += nal[i + vps_idx].sizeBytes; - } + nal[vps_idx].sizeBytes + nal[sps_idx].sizeBytes + nal[pps_idx].sizeBytes, + NULL); + gst_buffer_fill (buf, offset, nal[vps_idx].payload, nal[vps_idx].sizeBytes); + offset += nal[vps_idx].sizeBytes; + gst_buffer_fill (buf, offset, nal[sps_idx].payload, nal[sps_idx].sizeBytes); + offset += nal[sps_idx].sizeBytes; + gst_buffer_fill (buf, offset, nal[pps_idx].payload, nal[pps_idx].sizeBytes); return buf; }