From 2a1deceab80f64290cd3c48417175bdc25166e87 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Fri, 28 Mar 2025 12:55:15 +0100 Subject: [PATCH] rtpdec_h264: fixed incorrect address-of taken Using operator & makes it actually the type uint8_t (*)[2], althoug the pointer value remained the same. The cast then pruned the type - added checks before the cast if the value given is really ptr to a char type. Dropped the HEVC hint - now little-endian uint16_t ptr cannot be passed by mistake. --- src/rtp/rtpdec_h264.c | 2 +- src/rtp/rtpdec_h264.h | 46 +++++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/rtp/rtpdec_h264.c b/src/rtp/rtpdec_h264.c index 47a1d36c9..b78b11289 100644 --- a/src/rtp/rtpdec_h264.c +++ b/src/rtp/rtpdec_h264.c @@ -271,7 +271,7 @@ decode_hevc_nal_unit(struct video_frame *frame, int *total_length, int pass, int fu_length = 0; enum hevc_nal_type type = pass == 0 ? process_hevc_nal(nal, frame, data, data_len) - : HEVC_NALU_HDR_GET_TYPE(&nal); + : HEVC_NALU_HDR_GET_TYPE(nal); if (type <= NAL_HEVC_MAX) { if (pass == 0) { diff --git a/src/rtp/rtpdec_h264.h b/src/rtp/rtpdec_h264.h index 1d52fe3fa..18678195e 100644 --- a/src/rtp/rtpdec_h264.h +++ b/src/rtp/rtpdec_h264.h @@ -90,21 +90,6 @@ struct coded_data; struct decode_data_rtsp; struct video_desc; -#define H264_NALU_HDR_GET_TYPE(nal) (*(const uint8_t *) (nal) & 0x1F) -#define H264_NALU_HDR_GET_NRI(nal) ((*(const uint8_t *) (nal) & 0x60) >> 5) - -/// @param nal pointer to **big-endian** NAL header -#define HEVC_NALU_HDR_GET_TYPE(nal) (*(const uint8_t *) (nal) >> 1) -#define HEVC_NALU_HDR_GET_LAYER_ID(nal) \ - ((((const uint8_t *) (nal))[0] & 0x1) << 5 | \ - ((const uint8_t *) (nal))[1] >> 3) -#define HEVC_NALU_HDR_GET_TID(nal) \ - (((const uint8_t *) (nal))[1] & 0x7) - -#define NALU_HDR_GET_TYPE(nal, is_hevc) \ - ((is_hevc) ? HEVC_NALU_HDR_GET_TYPE((nal)) \ - : H264_NALU_HDR_GET_TYPE((nal))) - int decode_frame_h2645(struct coded_data *cdata, void *decode_data); struct video_frame *get_sps_pps_frame(const struct video_desc *desc, struct decode_data_rtsp *decode_data); @@ -117,4 +102,35 @@ int width_height_from_hevc_sps(int *widthOut, int *heightOut, } #endif +// cast just pointers to character types to avoid mistakes +#ifndef __cplusplus +#define RTPDEC_SAFE_CAST(var) \ + _Generic((var), \ + char *: ((const uint8_t *) (var)), \ + unsigned char *: ((const uint8_t *) (var)), \ + const char *: ((const uint8_t *) (var)), \ + const unsigned char *: ((const uint8_t *) (var))) +#else +template +static inline auto +RTPDEC_SAFE_CAST(T var) +{ + static_assert(sizeof var[0] == 1); + return (const uint8_t *) var; +} +#endif + +#define H264_NALU_HDR_GET_TYPE(nal) (*RTPDEC_SAFE_CAST(nal) & 0x1F) +#define H264_NALU_HDR_GET_NRI(nal) ((*RTPDEC_SAFE_CAST(nal) & 0x60) >> 5) + +#define HEVC_NALU_HDR_GET_TYPE(nal) (*RTPDEC_SAFE_CAST(nal) >> 1) +#define HEVC_NALU_HDR_GET_LAYER_ID(nal) \ + (((RTPDEC_SAFE_CAST(nal))[0] & 0x1) << 5 | \ + (RTPDEC_SAFE_CAST(nal))[1] >> 3) +#define HEVC_NALU_HDR_GET_TID(nal) (RTPDEC_SAFE_CAST(nal)[1] & 0x7) + +#define NALU_HDR_GET_TYPE(nal, is_hevc) \ + ((is_hevc) ? HEVC_NALU_HDR_GET_TYPE((nal)) \ + : H264_NALU_HDR_GET_TYPE((nal))) + #endif