I am trying to depacketize a raw RTP H265 stream and reconstruct it so that it can be read by a decoder. I have been able to extract individual and fragmented units from the RTP buffers by identifying the NALs and FU details. However, I couldn't find precise details on handling the NALs to be put in front of the buffer for Fragmented units.
Here is what I do:
When the payload header type is 49, I know it is a Fragmented unit.
I then look for the third byte whose trailing 6 bytes give you the FU type/ NAL type and the first two bits tells if its a start or an end slice of the frame. Here is the code I have for reconstructing the NAL:
if(type == 49){
unsigned char fu_header = (unsigned char) *(rtpPayloadPos+2);
unsigned char fu_head_se = (fu_header & 0xC0) >> 6;
nal_type = fu_header & 0x3F;
switch(fu_head_se){
case 0x02: //nal start
{
payloadType = RTP_PAYLOAD_FU_START_H265_NAL;
*h265DataPos = rtpPlayloadPos-3;
*(*h265DataPos) = 0x00;
*((*h265DataPos)+1) = 0x00;
*((*h265DataPos)+2) = 0x00;
*((*h265DataPos)+3) = 0x01;
*((*h265DataPos)+4) = (fu_header << 1);
*((*h265DataPos)+5) = 0x01;
h265Len = len - headLen + 3;
}
break;
case 0x00: //nal middle
{
*h265DataPos = rtpPlayloadPos+2;
h265Len = len - headLen - 2;
payloadType = RTP_PAYLOAD_FU_MIDDLE_H265_NAL;
}
break;
case 0x01: //nal end
{
*h265DataPos = rtpPlayloadPos+2;
h265Len = len - headLen - 2;
payloadType = RTP_PAYLOAD_FU_END_H265_NAL;
}
break;
default:
printf("Unknown fu head\n");
return -1;
}
However, I am not sure if the way I am reconstructing the NAL is right. Since the decoder complains alot and the video is almost green and jittery. Can someone tell me how to reconstruct the FU frame properly ?
I have been referring the ITU H265 document and this link: https://datatracker.ietf.org/doc/html/draft-ietf-payload-rtp-h265-15.