Skip to content

Commit 2a1dece

Browse files
committed
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.
1 parent 4ef3f49 commit 2a1dece

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/rtp/rtpdec_h264.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ decode_hevc_nal_unit(struct video_frame *frame, int *total_length, int pass,
271271
int fu_length = 0;
272272
enum hevc_nal_type type = pass == 0
273273
? process_hevc_nal(nal, frame, data, data_len)
274-
: HEVC_NALU_HDR_GET_TYPE(&nal);
274+
: HEVC_NALU_HDR_GET_TYPE(nal);
275275

276276
if (type <= NAL_HEVC_MAX) {
277277
if (pass == 0) {

src/rtp/rtpdec_h264.h

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,6 @@ struct coded_data;
9090
struct decode_data_rtsp;
9191
struct video_desc;
9292

93-
#define H264_NALU_HDR_GET_TYPE(nal) (*(const uint8_t *) (nal) & 0x1F)
94-
#define H264_NALU_HDR_GET_NRI(nal) ((*(const uint8_t *) (nal) & 0x60) >> 5)
95-
96-
/// @param nal pointer to **big-endian** NAL header
97-
#define HEVC_NALU_HDR_GET_TYPE(nal) (*(const uint8_t *) (nal) >> 1)
98-
#define HEVC_NALU_HDR_GET_LAYER_ID(nal) \
99-
((((const uint8_t *) (nal))[0] & 0x1) << 5 | \
100-
((const uint8_t *) (nal))[1] >> 3)
101-
#define HEVC_NALU_HDR_GET_TID(nal) \
102-
(((const uint8_t *) (nal))[1] & 0x7)
103-
104-
#define NALU_HDR_GET_TYPE(nal, is_hevc) \
105-
((is_hevc) ? HEVC_NALU_HDR_GET_TYPE((nal)) \
106-
: H264_NALU_HDR_GET_TYPE((nal)))
107-
10893
int decode_frame_h2645(struct coded_data *cdata, void *decode_data);
10994
struct video_frame *get_sps_pps_frame(const struct video_desc *desc,
11095
struct decode_data_rtsp *decode_data);
@@ -117,4 +102,35 @@ int width_height_from_hevc_sps(int *widthOut, int *heightOut,
117102
}
118103
#endif
119104

105+
// cast just pointers to character types to avoid mistakes
106+
#ifndef __cplusplus
107+
#define RTPDEC_SAFE_CAST(var) \
108+
_Generic((var), \
109+
char *: ((const uint8_t *) (var)), \
110+
unsigned char *: ((const uint8_t *) (var)), \
111+
const char *: ((const uint8_t *) (var)), \
112+
const unsigned char *: ((const uint8_t *) (var)))
113+
#else
114+
template <typename T>
115+
static inline auto
116+
RTPDEC_SAFE_CAST(T var)
117+
{
118+
static_assert(sizeof var[0] == 1);
119+
return (const uint8_t *) var;
120+
}
121+
#endif
122+
123+
#define H264_NALU_HDR_GET_TYPE(nal) (*RTPDEC_SAFE_CAST(nal) & 0x1F)
124+
#define H264_NALU_HDR_GET_NRI(nal) ((*RTPDEC_SAFE_CAST(nal) & 0x60) >> 5)
125+
126+
#define HEVC_NALU_HDR_GET_TYPE(nal) (*RTPDEC_SAFE_CAST(nal) >> 1)
127+
#define HEVC_NALU_HDR_GET_LAYER_ID(nal) \
128+
(((RTPDEC_SAFE_CAST(nal))[0] & 0x1) << 5 | \
129+
(RTPDEC_SAFE_CAST(nal))[1] >> 3)
130+
#define HEVC_NALU_HDR_GET_TID(nal) (RTPDEC_SAFE_CAST(nal)[1] & 0x7)
131+
132+
#define NALU_HDR_GET_TYPE(nal, is_hevc) \
133+
((is_hevc) ? HEVC_NALU_HDR_GET_TYPE((nal)) \
134+
: H264_NALU_HDR_GET_TYPE((nal)))
135+
120136
#endif

0 commit comments

Comments
 (0)