From 959320264a09d2a3e5f85f4b5ce28d6f8f76b485 Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Thu, 5 Mar 2020 15:18:35 +0900 Subject: [PATCH] h265parser: Add helper macro for nal type classification Add some macros to remove code duplication and to make it more readable --- gst-libs/gst/codecparsers/gsth265parser.c | 6 +-- gst-libs/gst/codecparsers/gsth265parser.h | 66 +++++++++++++++++++++++++++++ gst/videoparsers/gsth265parse.c | 16 +++---- tests/check/libs/h265parser.c | 69 +++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 15 deletions(-) diff --git a/gst-libs/gst/codecparsers/gsth265parser.c b/gst-libs/gst/codecparsers/gsth265parser.c index 325b367..46e304b 100644 --- a/gst-libs/gst/codecparsers/gsth265parser.c +++ b/gst-libs/gst/codecparsers/gsth265parser.c @@ -2269,8 +2269,7 @@ gst_h265_parser_parse_slice_hdr (GstH265Parser * parser, READ_UINT8 (&nr, slice->first_slice_segment_in_pic_flag, 1); - if (nalu->type >= GST_H265_NAL_SLICE_BLA_W_LP - && nalu->type <= RESERVED_IRAP_NAL_TYPE_MAX) + if (GST_H265_IS_NAL_TYPE_IRAP (nalu->type)) READ_UINT8 (&nr, slice->no_output_of_prior_pics_flag, 1); READ_UE_MAX (&nr, pps_id, GST_H265_MAX_PPS_COUNT - 1); @@ -2321,8 +2320,7 @@ gst_h265_parser_parse_slice_hdr (GstH265Parser * parser, if (sps->separate_colour_plane_flag == 1) READ_UINT8 (&nr, slice->colour_plane_id, 2); - if ((nalu->type != GST_H265_NAL_SLICE_IDR_W_RADL) - && (nalu->type != GST_H265_NAL_SLICE_IDR_N_LP)) { + if (!GST_H265_IS_NAL_TYPE_IDR (nalu->type)) { READ_UINT16 (&nr, slice->pic_order_cnt_lsb, (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)); diff --git a/gst-libs/gst/codecparsers/gsth265parser.h b/gst-libs/gst/codecparsers/gsth265parser.h index e88f48b..be62b13 100644 --- a/gst-libs/gst/codecparsers/gsth265parser.h +++ b/gst-libs/gst/codecparsers/gsth265parser.h @@ -43,6 +43,72 @@ G_BEGIN_DECLS #define GST_H265_IS_I_SLICE(slice) ((slice)->type == GST_H265_I_SLICE) /** + * GST_H265_IS_NAL_TYPE_IDR: + * @nal_type: a #GstH265NalUnitType + * + * Check whether @nal_type is IDR or not + * + * Since: 1.18 + */ +#define GST_H265_IS_NAL_TYPE_IDR(nal_type) \ + ((nal_type) == GST_H265_NAL_SLICE_IDR_W_RADL || (nal_type) == GST_H265_NAL_SLICE_IDR_N_LP) + +/** + * GST_H265_IS_NAL_TYPE_IRAP: + * @nal_type: a #GstH265NalUnitType + * + * Check whether @nal_type is IRAP or not + * + * Since: 1.18 + */ +#define GST_H265_IS_NAL_TYPE_IRAP(nal_type) \ + ((nal_type) >= GST_H265_NAL_SLICE_BLA_W_LP && (nal_type) <= RESERVED_IRAP_NAL_TYPE_MAX) + +/** + * GST_H265_IS_NAL_TYPE_BLA: + * @nal_type: a #GstH265NalUnitType + * + * Check whether @nal_type is BLA or not + * + * Since: 1.18 + */ +#define GST_H265_IS_NAL_TYPE_BLA(nal_type) \ + ((nal_type) >= GST_H265_NAL_SLICE_BLA_W_LP && (nal_type) <= GST_H265_NAL_SLICE_BLA_N_LP) + +/** + * GST_H265_IS_NAL_TYPE_CRA: + * @nal_type: a #GstH265NalUnitType + * + * Check whether @nal_type is CRA or not + * + * Since: 1.18 + */ +#define GST_H265_IS_NAL_TYPE_CRA(nal_type) \ + ((nal_type) == GST_H265_NAL_SLICE_CRA_NUT) + +/** + * GST_H265_IS_NAL_TYPE_RADL: + * @nal_type: a #GstH265NalUnitType + * + * Check whether @nal_type is RADL or not + * + * Since: 1.18 + */ +#define GST_H265_IS_NAL_TYPE_RADL(nal_type) \ + ((nal_type) == GST_H265_NAL_SLICE_RADL_N || (nal_type) == GST_H265_NAL_SLICE_RADL_R) + +/** + * GST_H265_IS_NAL_TYPE_RASL: + * @nal_type: a #GstH265NalUnitType + * + * Check whether @nal_type is RASL or not + * + * Since: 1.18 + */ +#define GST_H265_IS_NAL_TYPE_RASL(nal_type) \ + ((nal_type) == GST_H265_NAL_SLICE_RASL_N || (nal_type) == GST_H265_NAL_SLICE_RASL_R) + +/** * GstH265Profile: * @GST_H265_PROFILE_MAIN: Main profile (A.3.2) * @GST_H265_PROFILE_MAIN_10: Main 10 profile (A.3.3) diff --git a/gst/videoparsers/gsth265parse.c b/gst/videoparsers/gsth265parse.c index fb1c94c..693c4d8 100644 --- a/gst/videoparsers/gsth265parse.c +++ b/gst/videoparsers/gsth265parse.c @@ -907,19 +907,15 @@ gst_h265_parse_process_nal (GstH265Parse * h265parse, GstH265NalUnit * nalu) * 1) the first AU in bitstream is CRA * 2) or the first AU following EOS nal is CRA * 3) or it has HandleCraAsBlaFlag equal to 1 */ - if (nal_type == GST_H265_NAL_SLICE_IDR_W_RADL || - nal_type == GST_H265_NAL_SLICE_IDR_N_LP) { + if (GST_H265_IS_NAL_TYPE_IDR (nal_type)) { /* NoRaslOutputFlag is equal to 1 for each IDR */ no_rasl_output_flag = TRUE; - } else if (nal_type == GST_H265_NAL_SLICE_BLA_W_LP || - nal_type == GST_H265_NAL_SLICE_BLA_W_RADL || - nal_type == GST_H265_NAL_SLICE_BLA_N_LP) { + } else if (GST_H265_IS_NAL_TYPE_BLA (nal_type)) { /* NoRaslOutputFlag is equal to 1 for each BLA */ no_rasl_output_flag = TRUE; } - is_irap = ((nal_type >= GST_H265_NAL_SLICE_BLA_W_LP) - && (nal_type <= GST_H265_NAL_SLICE_CRA_NUT)) ? TRUE : FALSE; + is_irap = GST_H265_IS_NAL_TYPE_IRAP (nal_type); if (no_rasl_output_flag && is_irap && slice.first_slice_segment_in_pic_flag == 1) { @@ -1015,8 +1011,7 @@ gst_h265_parse_collect_nal (GstH265Parse * h265parse, const guint8 * data, * i.e. other types become aggregated in front of it */ h265parse->picture_start |= ((nal_type >= GST_H265_NAL_SLICE_TRAIL_N && nal_type <= GST_H265_NAL_SLICE_RASL_R) - || (nal_type >= GST_H265_NAL_SLICE_BLA_W_LP - && nal_type <= RESERVED_IRAP_NAL_TYPE_MAX)); + || GST_H265_IS_NAL_TYPE_IRAP (nal_type)); /* consider a coded slices (IRAP or not) to start a picture, * (so ending the previous one) if first_slice_segment_in_pic_flag == 1*/ @@ -1033,8 +1028,7 @@ gst_h265_parse_collect_nal (GstH265Parse * h265parse, const guint8 * data, complete |= h265parse->picture_start && (((nal_type >= GST_H265_NAL_SLICE_TRAIL_N && nal_type <= GST_H265_NAL_SLICE_RASL_R) - || (nal_type >= GST_H265_NAL_SLICE_BLA_W_LP - && nal_type <= RESERVED_IRAP_NAL_TYPE_MAX)) + || GST_H265_IS_NAL_TYPE_IRAP (nal_type)) && (nnalu.data[nnalu.offset + 2] & 0x80)); } diff --git a/tests/check/libs/h265parser.c b/tests/check/libs/h265parser.c index 3ed25dd..22f739d 100644 --- a/tests/check/libs/h265parser.c +++ b/tests/check/libs/h265parser.c @@ -524,6 +524,74 @@ GST_START_TEST (test_h265_parse_pps) GST_END_TEST; +typedef struct +{ + GstH265NalUnitType type; + gboolean is_idr; + gboolean is_irap; + gboolean is_bla; + gboolean is_cra; + gboolean is_radl; + gboolean is_rasl; +} H265NalTypeTestVector; + +GST_START_TEST (test_h265_nal_type_classification) +{ + gint i; + /* *INDENT-OFF* */ + H265NalTypeTestVector test_vector[] = { + /* NAL-TYPE IDR IRAP BLA CRA RADL RASL */ + {GST_H265_NAL_SLICE_TRAIL_N, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_TRAIL_R, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_TSA_N, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_TSA_R, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_STSA_N, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_STSA_R, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_RADL_N, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE}, + {GST_H265_NAL_SLICE_RADL_R, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE}, + {GST_H265_NAL_SLICE_RASL_N, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE }, + {GST_H265_NAL_SLICE_RASL_R, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE }, + /* 10 ~ 15: reserved non-irap sublayer nal */ + {GST_H265_NAL_SLICE_BLA_W_LP, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_BLA_W_RADL, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_BLA_N_LP, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_IDR_W_RADL, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_IDR_N_LP, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE}, + {GST_H265_NAL_SLICE_CRA_NUT, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE}, + /* 22 ~ 23: reserved irap nal */ + {(GstH265NalUnitType) 22, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE}, + {(GstH265NalUnitType) 23, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE}, + }; + /* *INDENT-ON* */ + + for (i = 0; i < G_N_ELEMENTS (test_vector); i++) { + assert_equals_int (GST_H265_IS_NAL_TYPE_IDR (test_vector[i].type), + test_vector[i].is_idr); + assert_equals_int (GST_H265_IS_NAL_TYPE_IRAP (test_vector[i].type), + test_vector[i].is_irap); + assert_equals_int (GST_H265_IS_NAL_TYPE_BLA (test_vector[i].type), + test_vector[i].is_bla); + assert_equals_int (GST_H265_IS_NAL_TYPE_CRA (test_vector[i].type), + test_vector[i].is_cra); + assert_equals_int (GST_H265_IS_NAL_TYPE_RADL (test_vector[i].type), + test_vector[i].is_radl); + assert_equals_int (GST_H265_IS_NAL_TYPE_RASL (test_vector[i].type), + test_vector[i].is_rasl); + } + + for (i = RESERVED_NON_IRAP_NAL_TYPE_MIN; + i <= UNSPECIFIED_NON_VCL_NAL_TYPE_MAX; i++) { + assert_equals_int (GST_H265_IS_NAL_TYPE_IDR (i), FALSE); + assert_equals_int (GST_H265_IS_NAL_TYPE_IRAP (i), FALSE); + assert_equals_int (GST_H265_IS_NAL_TYPE_BLA (i), FALSE); + assert_equals_int (GST_H265_IS_NAL_TYPE_CRA (i), FALSE); + assert_equals_int (GST_H265_IS_NAL_TYPE_RADL (i), FALSE); + assert_equals_int (GST_H265_IS_NAL_TYPE_RASL (i), FALSE); + } +} + +GST_END_TEST; + static Suite * h265parser_suite (void) { @@ -541,6 +609,7 @@ h265parser_suite (void) tcase_add_test (tc_chain, test_h265_format_range_profiles_partial_match); tcase_add_test (tc_chain, test_h265_parse_vps); tcase_add_test (tc_chain, test_h265_parse_pps); + tcase_add_test (tc_chain, test_h265_nal_type_classification); return s; } -- 2.7.4