首先來介紹下h265(HEVC)nal單元頭,,與h264的nal層相比,,h265的nal unit header有兩個(gè)字節(jié)構(gòu)成,,如下圖所示:
從圖中可以看出hHEVC的nal包結(jié)構(gòu)與h264有明顯的不同,,hevc加入了nal所在的時(shí)間層的ID,,取去除了nal_ref_idc,此信息合并到了naltype中,,通常情況下F為0,,layerid為0,TID為1。
nal單元的類型有如下幾種:
- enum NalUnitType
- {
- NAL_UNIT_CODED_SLICE_TRAIL_N = 0, // 0
- NAL_UNIT_CODED_SLICE_TRAIL_R, // 1
-
- NAL_UNIT_CODED_SLICE_TSA_N, // 2
- NAL_UNIT_CODED_SLICE_TLA, // 3 // Current name in the spec: TSA_R
-
- NAL_UNIT_CODED_SLICE_STSA_N, // 4
- NAL_UNIT_CODED_SLICE_STSA_R, // 5
-
- NAL_UNIT_CODED_SLICE_RADL_N, // 6
- NAL_UNIT_CODED_SLICE_DLP, // 7 // Current name in the spec: RADL_R
-
- NAL_UNIT_CODED_SLICE_RASL_N, // 8
- NAL_UNIT_CODED_SLICE_TFD, // 9 // Current name in the spec: RASL_R
-
- NAL_UNIT_RESERVED_10,
- NAL_UNIT_RESERVED_11,
- NAL_UNIT_RESERVED_12,
- NAL_UNIT_RESERVED_13,
- NAL_UNIT_RESERVED_14,
- NAL_UNIT_RESERVED_15, NAL_UNIT_CODED_SLICE_BLA, // 16 // Current name in the spec: BLA_W_LP
- NAL_UNIT_CODED_SLICE_BLA, // 16 // Current name in the spec: BLA_W_LP
- NAL_UNIT_CODED_SLICE_BLANT, // 17 // Current name in the spec: BLA_W_DLP
- NAL_UNIT_CODED_SLICE_BLA_N_LP, // 18
- NAL_UNIT_CODED_SLICE_IDR, // 19 // Current name in the spec: IDR_W_DLP
- NAL_UNIT_CODED_SLICE_IDR_N_LP, // 20
- NAL_UNIT_CODED_SLICE_CRA, // 21
- NAL_UNIT_RESERVED_22,
- NAL_UNIT_RESERVED_23,
-
- NAL_UNIT_RESERVED_24,
- NAL_UNIT_RESERVED_25,
- NAL_UNIT_RESERVED_26,
- NAL_UNIT_RESERVED_27,
- NAL_UNIT_RESERVED_28,
- NAL_UNIT_RESERVED_29,
- NAL_UNIT_RESERVED_30,
- NAL_UNIT_RESERVED_31,
-
- NAL_UNIT_VPS, // 32
- NAL_UNIT_SPS, // 33
- NAL_UNIT_PPS, // 34
- NAL_UNIT_ACCESS_UNIT_DELIMITER, // 35
- NAL_UNIT_EOS, // 36
- NAL_UNIT_EOB, // 37
- NAL_UNIT_FILLER_DATA, // 38
- NAL_UNIT_SEI, // 39 Prefix SEI
- NAL_UNIT_SEI_SUFFIX, // 40 Suffix SEI
- NAL_UNIT_RESERVED_41,
- NAL_UNIT_RESERVED_42,
- NAL_UNIT_RESERVED_43,
- NAL_UNIT_RESERVED_44,
- NAL_UNIT_RESERVED_45,
- NAL_UNIT_RESERVED_46,
- NAL_UNIT_RESERVED_47,
- NAL_UNIT_UNSPECIFIED_48,
- NAL_UNIT_UNSPECIFIED_49,
- NAL_UNIT_UNSPECIFIED_50,
- NAL_UNIT_UNSPECIFIED_51,
- NAL_UNIT_UNSPECIFIED_52,
- NAL_UNIT_UNSPECIFIED_53,
- NAL_UNIT_UNSPECIFIED_54,
- NAL_UNIT_UNSPECIFIED_55,
- NAL_UNIT_UNSPECIFIED_56,
- NAL_UNIT_UNSPECIFIED_57,
- NAL_UNIT_UNSPECIFIED_58,
- NAL_UNIT_UNSPECIFIED_59,
- NAL_UNIT_UNSPECIFIED_60,
- NAL_UNIT_UNSPECIFIED_61,
- NAL_UNIT_UNSPECIFIED_62,
- NAL_UNIT_UNSPECIFIED_63,
- NAL_UNIT_INVALID,
- };
下面接收下fu分組打包方式,,fu分組包頭格式如下:
fus包頭包含了兩個(gè)字節(jié)的payloadhdr,,一個(gè)字節(jié)的fu header,fu header與h264一樣,,結(jié)構(gòu)如下圖,,包含開始位(1b)、停止位(1b),、futype(6b)
paylodhdr兩個(gè)自己的賦值,,其實(shí)就是把hevc幀數(shù)據(jù)的nal unit header的naltype替換為49即可,下面是從ffmpeg源碼中截取出來的fu打包方式代碼片段:
- static void nal_send(AVFormatContext *ctx, const uint8_t *buf, int len, int last_packet_of_frame)
- {
- RTPMuxContext *rtp_ctx = ctx->priv_data;
- int rtp_payload_size = rtp_ctx->max_payload_size - RTP_HEVC_HEADERS_SIZE;
- int nal_type = (buf[0] >> 1) & 0x3F;
-
- /* send it as one single NAL unit? */
- if (len <= rtp_ctx->max_payload_size) //小于對(duì)定的最大值時(shí),,直接發(fā)送(最大值一般小于mtu)
- {
- /* use the original NAL unit buffer and transmit it as RTP payload */
- ff_rtp_send_data(ctx, buf, len, last_packet_of_frame);
-
- }
- else //大于最大值時(shí)進(jìn)行fu分組發(fā)送
- {
- /*
- create the HEVC payload header and transmit the buffer as fragmentation units (FU)
-
- 0 1
- 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- |F| Type | LayerId | TID |
- +-------------+-----------------+
-
- F = 0
- Type = 49 (fragmentation unit (FU))
- LayerId = 0
- TID = 1
- */
- rtp_ctx->buf[0] = 49 << 1;
- rtp_ctx->buf[1] = 1;
- //此處為paylaodhdr,規(guī)范賦值應(yīng)該是替換hevc數(shù)據(jù)nal 的payloadhdr的type
- //rtp_ctx->buf[0] = (buf[0] &0x81) | (49<<1);
- //rtp_ctx->buf[1] = buf[1]
- /*
- create the FU header
-
- 0 1 2 3 4 5 6 7
- +-+-+-+-+-+-+-+-+
- |S|E| FuType |
- +---------------+
-
- S = variable
- E = variable
- FuType = NAL unit type
- */
- rtp_ctx->buf[2] = nal_type;
- /* set the S bit: mark as start fragment */
- rtp_ctx->buf[2] |= 1 << 7;
- /* pass the original NAL header */
- //此處要注意,,當(dāng)是分組的第一報(bào)數(shù)據(jù)時(shí),應(yīng)該覆蓋掉前兩個(gè)字節(jié)的數(shù)據(jù),,h264要覆蓋前一個(gè)字節(jié)的數(shù)據(jù),,即是第一包要去除hevc幀數(shù)據(jù)的paylaodhdr
- buf += 2;
- len -= 2;
- while (len > rtp_payload_size)
- {
- /* complete and send current RTP packet */
- memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, rtp_payload_size);
- ff_rtp_send_data(ctx, rtp_ctx->buf, rtp_ctx->max_payload_size, 0);
-
- buf += rtp_payload_size;
- len -= rtp_payload_size;
-
- /* reset the S bit */
- rtp_ctx->buf[2] &= ~(1 << 7);
-
- }
-
- /* set the E bit: mark as last fragment */
- rtp_ctx->buf[2] |= 1 << 6;
-
- /* complete and send last RTP packet */
- memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, len);
- ff_rtp_send_data(ctx, rtp_ctx->buf, len + 2, last_packet_of_frame);
-
- }
- }
通過rtp發(fā)送hevc視頻數(shù)據(jù),當(dāng)hevc幀數(shù)據(jù)大于mtu時(shí),,應(yīng)該進(jìn)行fu分組發(fā)送,,從上面代碼流程就是對(duì)超過max_payload_size數(shù)據(jù)進(jìn)行fu分組的流程,這個(gè)h264 fu-A很類似,,很容易理解,。
參考規(guī)范:
https://tools./html/draft-ietf-payload-rtp-h265-14
ffmpeg相關(guān)代碼
https://www./doxygen/2.5/rtpenc__hevc_8c_source.html
|