From 7ec19de720873defc3a1f02cc57638e351ab264e Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Thu, 10 Mar 2022 01:43:03 +0300 Subject: [PATCH] libosmocodec: osmo_hr_check_sid(): simplify the logic According to TS 101 318, section 5.2.2, a SID frame is identified by a SID codeword consisting of 79 bits (r34..r112) which are all 1. Given that there are no gaps in the codeword, we don't really need to use bitvec_get_bit_pos() and check each field individually. This brings additional complexity and negatively affects performance. Instead, let's use bitvec_get_bit_pos() to check all bits in a row. Change-Id: I678f8ff92317fe87f1aca511a3a062ad898e55cc Related: SYS#5853 --- src/codec/gsm620.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/codec/gsm620.c b/src/codec/gsm620.c index cf7cfd75f..4eae514e8 100644 --- a/src/codec/gsm620.c +++ b/src/codec/gsm620.c @@ -264,12 +264,6 @@ const uint16_t gsm620_voiced_bitorder[112] = { 81, /* Code 3:7 */ }; -static inline uint16_t mask(const uint8_t msb) -{ - const uint16_t m = (uint16_t)1 << (msb - 1); - return (m - 1) ^ m; -} - /*! Check whether RTP frame contains HR SID code word according to * TS 101 318 ยง5.2.2 * \param[in] rtp_payload Buffer with RTP payload @@ -278,15 +272,15 @@ static inline uint16_t mask(const uint8_t msb) */ bool osmo_hr_check_sid(const uint8_t *rtp_payload, size_t payload_len) { - uint8_t i, bits[] = { 1, 2, 8, 9, 5, 4, 9, 5, 4, 9, 5, 4, 9, 5 }; - struct bitvec bv; - bv.data = (uint8_t *) rtp_payload; - bv.data_len = payload_len; - bv.cur_bit = 33; + struct bitvec bv = { + .data = (uint8_t *)rtp_payload, + .data_len = payload_len, + }; - /* code word is all 1 at given bits, numbered from 1, MODE is always 3 */ - for (i = 0; i < ARRAY_SIZE(bits); i++) - if (bitvec_get_uint(&bv, bits[i]) != mask(bits[i])) + /* A SID frame is identified by a SID codeword consisting of 79 bits which are all 1, + * so we basically check if all bits in range r34..r112 (inclusive) are 1. */ + for (bv.cur_bit = 33; bv.cur_bit < bv.data_len * 8; bv.cur_bit++) + if (bitvec_get_bit_pos(&bv, bv.cur_bit) != ONE) return false; return true;