This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
laforge gerrit-no-reply at lists.osmocom.orglaforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmo-netif/+/17997 ) Change subject: amr: Fix OA<->BWE conversion. ...................................................................... amr: Fix OA<->BWE conversion. Size of a single AMR frame doesn't always shrink by a byte when converted from octet-aligned to bandwidth-efficient mode. It does shrink for AMR modes 2, 3, 4, 6, and 7 but doesn't shrink for AMR modes 0, 1, 5, and SID frames because we only remove 6 bits. So old code generated truncated AMR packets for those AMR modes. This patch fixes the length calculation by properly counting bits. Proper bit counting is also bringing us one small step closer to properly handlig multi-frame AMR packets. Change-Id: I9fc5fb92e9bada22a47a82fcfb0925e892e50ced --- M include/osmocom/netif/amr.h M src/amr.c M tests/amr/amr_test.c M tests/amr/amr_test.ok 4 files changed, 145 insertions(+), 75 deletions(-) Approvals: laforge: Looks good to me, approved pespin: Looks good to me, but someone else must approve Jenkins Builder: Verified diff --git a/include/osmocom/netif/amr.h b/include/osmocom/netif/amr.h index 6e37c99..c5a8e28 100644 --- a/include/osmocom/netif/amr.h +++ b/include/osmocom/netif/amr.h @@ -78,20 +78,34 @@ #define AMR_FT_SID 8 /* SID */ #define AMR_FT_MAX 9 -/* AMR voice frame length (in bytes, rounded), - * See also RFC 3267, chapter 3.6 */ -#define AMR_FT_0_LEN 12 /* 4.75 */ -#define AMR_FT_1_LEN 13 /* 5.15 */ -#define AMR_FT_2_LEN 15 /* 5.90 */ -#define AMR_FT_3_LEN 17 /* 6.70 */ -#define AMR_FT_4_LEN 19 /* 7.40 */ -#define AMR_FT_5_LEN 20 /* 7.95 */ -#define AMR_FT_6_LEN 26 /* 10.2 */ -#define AMR_FT_7_LEN 31 /* 12.2 */ -#define AMR_FT_SID_LEN 5 /* SID */ - -/* NOTE: the above constant refers to the length of one AMR speech frame-block, +/* AMR voice frame length (in bits). + * See also RFC 3267, chapter 3.6. + * + * NOTE: These constants refer to the length of one AMR speech frame-block, * not counting CMR, TOC. */ +#define AMR_FT_0_LEN_BITS 95 /* 4.75 */ +#define AMR_FT_1_LEN_BITS 103 /* 5.15 */ +#define AMR_FT_2_LEN_BITS 118 /* 5.90 */ +#define AMR_FT_3_LEN_BITS 134 /* 6.70 */ +#define AMR_FT_4_LEN_BITS 148 /* 7.40 */ +#define AMR_FT_5_LEN_BITS 159 /* 7.95 */ +#define AMR_FT_6_LEN_BITS 204 /* 10.2 */ +#define AMR_FT_7_LEN_BITS 244 /* 12.2 */ +#define AMR_FT_SID_LEN_BITS 39 /* SID */ + +/* AMR voice frame length (in bytes, rounded). + * + * NOTE: These constants refer to the length of one AMR speech frame-block, + * not counting CMR, TOC. */ +#define AMR_FT_0_LEN ((AMR_FT_0_LEN_BITS+7)/8) /* 4.75 */ +#define AMR_FT_1_LEN ((AMR_FT_1_LEN_BITS+7)/8) /* 5.15 */ +#define AMR_FT_2_LEN ((AMR_FT_2_LEN_BITS+7)/8) /* 5.90 */ +#define AMR_FT_3_LEN ((AMR_FT_3_LEN_BITS+7)/8) /* 6.70 */ +#define AMR_FT_4_LEN ((AMR_FT_4_LEN_BITS+7)/8) /* 7.40 */ +#define AMR_FT_5_LEN ((AMR_FT_5_LEN_BITS+7)/8) /* 7.95 */ +#define AMR_FT_6_LEN ((AMR_FT_6_LEN_BITS+7)/8) /* 10.2 */ +#define AMR_FT_7_LEN ((AMR_FT_7_LEN_BITS+7)/8) /* 12.2 */ +#define AMR_FT_SID_LEN ((AMR_FT_SID_LEN_BITS+7)/8) /* SID */ int osmo_amr_ft_valid(uint8_t amr_ft); size_t osmo_amr_bytes(uint8_t amr_cmr); diff --git a/src/amr.c b/src/amr.c index 5609c46..a6b8361 100644 --- a/src/amr.c +++ b/src/amr.c @@ -29,6 +29,18 @@ * 7 12.20 244 31 */ +static size_t amr_ft_to_bits[AMR_FT_MAX] = { + [AMR_FT_0] = AMR_FT_0_LEN_BITS, + [AMR_FT_1] = AMR_FT_1_LEN_BITS, + [AMR_FT_2] = AMR_FT_2_LEN_BITS, + [AMR_FT_3] = AMR_FT_3_LEN_BITS, + [AMR_FT_4] = AMR_FT_4_LEN_BITS, + [AMR_FT_5] = AMR_FT_5_LEN_BITS, + [AMR_FT_6] = AMR_FT_6_LEN_BITS, + [AMR_FT_7] = AMR_FT_7_LEN_BITS, + [AMR_FT_SID] = AMR_FT_SID_LEN_BITS, +}; + static size_t amr_ft_to_bytes[AMR_FT_MAX] = { [AMR_FT_0] = AMR_FT_0_LEN, [AMR_FT_1] = AMR_FT_1_LEN, @@ -41,6 +53,11 @@ [AMR_FT_SID] = AMR_FT_SID_LEN, }; +size_t osmo_amr_bits(uint8_t amr_ft) +{ + return amr_ft_to_bits[amr_ft]; +} + size_t osmo_amr_bytes(uint8_t amr_ft) { return amr_ft_to_bytes[amr_ft]; @@ -119,8 +136,10 @@ int osmo_amr_oa_to_bwe(uint8_t *payload, unsigned int payload_len) { struct amr_hdr *oa_hdr = (struct amr_hdr *)payload; + unsigned int ft = oa_hdr->ft; unsigned int frame_len = payload_len - sizeof(struct amr_hdr); unsigned int i; + int bwe_payload_len; /* This implementation is not capable to handle multi-frame * packets, so we need to make sure that the frame we operate on @@ -137,8 +156,10 @@ payload[i + 2] = payload[i + 2] << 6; } - /* The overall saving is one byte! */ - return payload_len - 1; + /* Calculate new payload length */ + bwe_payload_len = (10 + osmo_amr_bits(ft) + 7) / 8; + + return bwe_payload_len; } /*! Convert an AMR frame from bandwith-efficient mode to octet-aligned mode. @@ -150,8 +171,10 @@ unsigned int payload_maxlen) { uint8_t buf[256]; - unsigned int frame_len = payload_len - 1; + /* The header is only valid after shifting first two bytes to OA mode */ + struct amr_hdr *oa_hdr; unsigned int i; + int oa_payload_len; memset(buf, 0, sizeof(buf)); @@ -165,12 +188,16 @@ buf[1] = payload[0] << 4; buf[1] |= (payload[1] >> 4) & 0x0c; - for (i = 0; i < frame_len - 1; i++) { + /* Calculate new payload length */ + oa_hdr = (struct amr_hdr *)buf; + oa_payload_len = 2 + osmo_amr_bytes(oa_hdr->ft); + + for (i = 0; i < oa_payload_len - 2; i++) { buf[i + 2] = payload[i + 1] << 2; buf[i + 2] |= payload[i + 2] >> 6; } buf[i + 2] = payload[i + 1] << 2; - memcpy(payload, buf, payload_len + 1); - return payload_len + 1; + memcpy(payload, buf, oa_payload_len); + return oa_payload_len; } diff --git a/tests/amr/amr_test.c b/tests/amr/amr_test.c index fff686c..af10289 100644 --- a/tests/amr/amr_test.c +++ b/tests/amr/amr_test.c @@ -44,6 +44,9 @@ "100c4e9ba850e30d5d53d04de41e7c", "100c6c18e7b7fff53aeb055e7d1c54", "100c1fb967f7f1fdf547bf2e61c060", + "0004f89d67f1160935bde1996840", + "0004633cc7f0630439ffe0000000", + "0004eb81fc0758973b9edc782552", "a038ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00fc", /* test pattern */ "END", }; @@ -87,7 +90,6 @@ return; printf("\n"); printf("Sample No.: %i\n", i); - len = strlen(oa_amr_samples[i]); len = osmo_hexparse(oa_amr_samples[i], buf, sizeof(buf)); OSMO_ASSERT(len > 0); @@ -102,11 +104,6 @@ dump_bits(buf, rc); printf("\n"); printf(" rc: %i\n", rc); - - if (rc > 0) { - OSMO_ASSERT(rc == len - 1); - OSMO_ASSERT(buf[len - 1] == 0x00); - } i++; } } @@ -126,7 +123,6 @@ return; printf("\n"); printf("Sample No.: %i\n", i); - len = strlen(bwe_amr_samples[i]); len = osmo_hexparse(bwe_amr_samples[i], buf, sizeof(buf)); OSMO_ASSERT(len > 0); @@ -142,7 +138,6 @@ printf("\n"); printf(" rc: %i\n", rc); - OSMO_ASSERT(rc == len + 1); i++; } } @@ -151,6 +146,8 @@ { uint8_t buf[256]; uint8_t buf_chk[256]; + struct amr_hdr *oa_hd = (struct amr_hdr *)buf; + unsigned int ft; unsigned int i = 0; int len; @@ -163,16 +160,21 @@ while (1) { if (strcmp(oa_amr_samples[i], "END") == 0) return; - printf("Sample No.: %i...\n", i); - len = strlen(oa_amr_samples[i]); + printf("Sample No.: %i...", i); len = osmo_hexparse(oa_amr_samples[i], buf, sizeof(buf)); OSMO_ASSERT(len > 0); + ft = oa_hd->ft; + OSMO_ASSERT(osmo_amr_bytes(ft) + 2 == len); + printf(" AMR mode: %d, OA: %d bytes,", ft, len); memcpy(buf_chk, buf, sizeof(buf)); rc = osmo_amr_oa_to_bwe(buf, len); OSMO_ASSERT(rc > 0); + printf(" BE: %d bytes,", rc); rc = osmo_amr_bwe_to_oa(buf, rc, sizeof(buf)); + printf(" OA: %d bytes\n", rc); + OSMO_ASSERT(len == rc); OSMO_ASSERT(memcmp(buf, buf_chk, len) == 0); i++; } diff --git a/tests/amr/amr_test.ok b/tests/amr/amr_test.ok index ea34fea..692563d 100644 --- a/tests/amr/amr_test.ok +++ b/tests/amr/amr_test.ok @@ -47,23 +47,23 @@ Sample No.: 6 octet aligned: 502c98ab841e491ff7a1a555016a32a3c7f913210630 01010000001011001001100010101011100001000001111001001001000111111111011110100001101001010101010100000001011010100011001010100011110001111111100100010011001000010000011000110000 - bw-efficient: 52e62ae1079247fde86955405a8ca8f1fe44c8418c - 010100101110011000101010111000010000011110010010010001111111110111101000011010010101010101000000010110101000110010101000111100011111111001000100110010000100000110001100 - rc: 21 + bw-efficient: 52e62ae1079247fde86955405a8ca8f1fe44c8418c00 + 01010010111001100010101011100001000001111001001001000111111111011110100001101001010101010100000001011010100011001010100011110001111111100100010011001000010000011000110000000000 + rc: 22 Sample No.: 7 octet aligned: 502cc5459a0d200e7097c4dfe86ec8d27f1756d776f0 01010000001011001100010101000101100110100000110100100000000011100111000010010111110001001101111111101000011011101100100011010010011111110001011101010110110101110111011011110000 - bw-efficient: 52f151668348039c25f137fa1bb2349fc5d5b5ddbc - 010100101111000101010001011001101000001101001000000000111001110000100101111100010011011111111010000110111011001000110100100111111100010111010101101101011101110110111100 - rc: 21 + bw-efficient: 52f151668348039c25f137fa1bb2349fc5d5b5ddbc00 + 01010010111100010101000101100110100000110100100000000011100111000010010111110001001101111111101000011011101100100011010010011111110001011101010110110101110111011011110000000000 + rc: 22 Sample No.: 8 octet aligned: 502c42b332081813d7e916e7aa5e80d7fde812b8c080 01010000001011000100001010110011001100100000100000011000000100111101011111101001000101101110011110101010010111101000000011010111111111011110100000010010101110001100000010000000 - bw-efficient: 52d0accc820604f5fa45b9ea97a035ff7a04ae3020 - 010100101101000010101100110011001000001000000110000001001111010111111010010001011011100111101010100101111010000000110101111111110111101000000100101011100011000000100000 - rc: 21 + bw-efficient: 52d0accc820604f5fa45b9ea97a035ff7a04ae302000 + 01010010110100001010110011001100100000100000011000000100111101011111101001000101101110011110101010010111101000000011010111111111011110100000010010101110001100000010000000000000 + rc: 22 Sample No.: 9 octet aligned: 40240343e959c79bacd20c77501054880a718db200 @@ -110,25 +110,46 @@ Sample No.: 15 octet aligned: 100c4e9ba850e30d5d53d04de41e7c 000100000000110001001110100110111010100001010000111000110000110101011101010100111101000001001101111001000001111001111100 - bw-efficient: 10d3a6ea1438c35754f41379079f - 0001000011010011101001101110101000010100001110001100001101010111010101001111010000010011011110010000011110011111 - rc: 14 + bw-efficient: 10d3a6ea1438c35754f41379079f00 + 000100001101001110100110111010100001010000111000110000110101011101010100111101000001001101111001000001111001111100000000 + rc: 15 Sample No.: 16 octet aligned: 100c6c18e7b7fff53aeb055e7d1c54 000100000000110001101100000110001110011110110111111111111111010100111010111010110000010101011110011111010001110001010100 - bw-efficient: 10db0639edfffd4ebac1579f4715 - 0001000011011011000001100011100111101101111111111111110101001110101110101100000101010111100111110100011100010101 - rc: 14 + bw-efficient: 10db0639edfffd4ebac1579f471500 + 000100001101101100000110001110011110110111111111111111010100111010111010110000010101011110011111010001110001010100000000 + rc: 15 Sample No.: 17 octet aligned: 100c1fb967f7f1fdf547bf2e61c060 000100000000110000011111101110010110011111110111111100011111110111110101010001111011111100101110011000011100000001100000 - bw-efficient: 10c7ee59fdfc7f7d51efcb987018 - 0001000011000111111011100101100111111101111111000111111101111101010100011110111111001011100110000111000000011000 - rc: 14 + bw-efficient: 10c7ee59fdfc7f7d51efcb98701800 + 000100001100011111101110010110011111110111111100011111110111110101010001111011111100101110011000011100000001100000000000 + rc: 15 Sample No.: 18 + octet aligned: 0004f89d67f1160935bde1996840 + 0000000000000100111110001001110101100111111100010001011000001001001101011011110111100001100110010110100001000000 + bw-efficient: 007e2759fc45824d6f78665a1000 + 0000000001111110001001110101100111111100010001011000001001001101011011110111100001100110010110100001000000000000 + rc: 14 + +Sample No.: 19 + octet aligned: 0004633cc7f0630439ffe0000000 + 0000000000000100011000110011110011000111111100000110001100000100001110011111111111100000000000000000000000000000 + bw-efficient: 0058cf31fc18c10e7ff800000000 + 0000000001011000110011110011000111111100000110001100000100001110011111111111100000000000000000000000000000000000 + rc: 14 + +Sample No.: 20 + octet aligned: 0004eb81fc0758973b9edc782552 + 0000000000000100111010111000000111111100000001110101100010010111001110111001111011011100011110000010010101010010 + bw-efficient: 007ae07f01d625cee7b71e095480 + 0000000001111010111000000111111100000001110101100010010111001110111001111011011100011110000010010101010010000000 + rc: 14 + +Sample No.: 21 octet aligned: a038ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00fc 101000000011100011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111100 bw-efficient: a3bfc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03f @@ -141,16 +162,16 @@ Sample No.: 0 bw-efficient: f4495c7cda8f80 11110100010010010101110001111100110110101000111110000000 - octet aligned: f0442571f36a3e00 - 1111000001000100001001010111000111110011011010100011111000000000 - rc: 8 + octet aligned: f0442571f36a3e + 11110000010001000010010101110001111100110110101000111110 + rc: 7 Sample No.: 1 bw-efficient: f44aaa6c969780 11110100010010101010101001101100100101101001011110000000 - octet aligned: f0442aa9b25a5e00 - 1111000001000100001010101010100110110010010110100101111000000000 - rc: 8 + octet aligned: f0442aa9b25a5e + 11110000010001000010101010101001101100100101101001011110 + rc: 7 Sample No.: 2 bw-efficient: f3d09c20e32da600c025a72e0a9b360386e40f87e19282094adc1a11e397d1d4 @@ -183,31 +204,34 @@ Sample No.: 6 bw-efficient: a7bfc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03f 1010011110111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111 - octet aligned: a078ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00fc - 101000000111100011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111100 - rc: 33 + octet aligned: a078ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00fc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + 10100000011110001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + rc: 136 Testing conversion from octet-aligned to bw-efficient and inverse: -Sample No.: 0... -Sample No.: 1... -Sample No.: 2... -Sample No.: 3... -Sample No.: 4... -Sample No.: 5... -Sample No.: 6... -Sample No.: 7... -Sample No.: 8... -Sample No.: 9... -Sample No.: 10... -Sample No.: 11... -Sample No.: 12... -Sample No.: 13... -Sample No.: 14... -Sample No.: 15... -Sample No.: 16... -Sample No.: 17... -Sample No.: 18... +Sample No.: 0... AMR mode: 7, OA: 33 bytes, BE: 32 bytes, OA: 33 bytes +Sample No.: 1... AMR mode: 7, OA: 33 bytes, BE: 32 bytes, OA: 33 bytes +Sample No.: 2... AMR mode: 7, OA: 33 bytes, BE: 32 bytes, OA: 33 bytes +Sample No.: 3... AMR mode: 6, OA: 28 bytes, BE: 27 bytes, OA: 28 bytes +Sample No.: 4... AMR mode: 6, OA: 28 bytes, BE: 27 bytes, OA: 28 bytes +Sample No.: 5... AMR mode: 6, OA: 28 bytes, BE: 27 bytes, OA: 28 bytes +Sample No.: 6... AMR mode: 5, OA: 22 bytes, BE: 22 bytes, OA: 22 bytes +Sample No.: 7... AMR mode: 5, OA: 22 bytes, BE: 22 bytes, OA: 22 bytes +Sample No.: 8... AMR mode: 5, OA: 22 bytes, BE: 22 bytes, OA: 22 bytes +Sample No.: 9... AMR mode: 4, OA: 21 bytes, BE: 20 bytes, OA: 21 bytes +Sample No.: 10... AMR mode: 4, OA: 21 bytes, BE: 20 bytes, OA: 21 bytes +Sample No.: 11... AMR mode: 4, OA: 21 bytes, BE: 20 bytes, OA: 21 bytes +Sample No.: 12... AMR mode: 2, OA: 17 bytes, BE: 16 bytes, OA: 17 bytes +Sample No.: 13... AMR mode: 2, OA: 17 bytes, BE: 16 bytes, OA: 17 bytes +Sample No.: 14... AMR mode: 2, OA: 17 bytes, BE: 16 bytes, OA: 17 bytes +Sample No.: 15... AMR mode: 1, OA: 15 bytes, BE: 15 bytes, OA: 15 bytes +Sample No.: 16... AMR mode: 1, OA: 15 bytes, BE: 15 bytes, OA: 15 bytes +Sample No.: 17... AMR mode: 1, OA: 15 bytes, BE: 15 bytes, OA: 15 bytes +Sample No.: 18... AMR mode: 0, OA: 14 bytes, BE: 14 bytes, OA: 14 bytes +Sample No.: 19... AMR mode: 0, OA: 14 bytes, BE: 14 bytes, OA: 14 bytes +Sample No.: 20... AMR mode: 0, OA: 14 bytes, BE: 14 bytes, OA: 14 bytes +Sample No.: 21... AMR mode: 7, OA: 33 bytes, BE: 32 bytes, OA: 33 bytes Testing detection of octet-aligned mode payloads: @@ -230,6 +254,9 @@ Sample No.: 16 ==>octet aligned Sample No.: 17 ==>octet aligned Sample No.: 18 ==>octet aligned +Sample No.: 19 ==>octet aligned +Sample No.: 20 ==>octet aligned +Sample No.: 21 ==>octet aligned Sample No.: 0 ==>bandwith efficient Sample No.: 1 ==>bandwith efficient Sample No.: 2 ==>bandwith efficient -- To view, visit https://gerrit.osmocom.org/c/libosmo-netif/+/17997 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-netif Gerrit-Branch: master Gerrit-Change-Id: I9fc5fb92e9bada22a47a82fcfb0925e892e50ced Gerrit-Change-Number: 17997 Gerrit-PatchSet: 8 Gerrit-Owner: ipse <Alexander.Chemeris at gmail.com> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: dexter <pmaier at sysmocom.de> Gerrit-Reviewer: ipse <Alexander.Chemeris at gmail.com> Gerrit-Reviewer: laforge <laforge at osmocom.org> Gerrit-Reviewer: pespin <pespin at sysmocom.de> Gerrit-MessageType: merged -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200514/55f34509/attachment.htm>