Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 1 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 2 | * (C) 2016 by sysmocom - s.f.m.c. GmbH |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 3 | * All Rights Reserved |
| 4 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Max | ec8f192 | 2016-05-31 14:50:21 +0200 | [diff] [blame] | 25 | #include <stdbool.h> |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 26 | |
| 27 | #include <osmocom/core/utils.h> |
| 28 | #include <osmocom/codec/codec.h> |
| 29 | |
| 30 | const uint8_t sid_update[] = {0x20, 0x44, 0x29, 0xc2, 0x92, 0x91, 0xf4}; |
| 31 | const uint8_t sid_first[] = {0x20, 0x44, 0x00, 0x00, 0x00, 0x00, 0x04}; |
| 32 | |
| 33 | #define PAYLOAD_LEN 34 |
| 34 | #define SID_LEN 7 |
| 35 | |
| 36 | static const char * cmpr(int a, int b) |
| 37 | { |
| 38 | return (a == b) ? "OK" : "BAD"; |
| 39 | } |
| 40 | |
| 41 | static void test_sid_dec(const uint8_t *t, size_t len) |
| 42 | { |
Max | e9e5f8e | 2016-11-07 14:49:13 +0100 | [diff] [blame] | 43 | uint8_t cmr, tmp[SID_LEN], *t2 = NULL; |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 44 | enum osmo_amr_type ft; |
| 45 | enum osmo_amr_quality bfi; |
| 46 | int8_t sti, cmi; |
Max | e9e5f8e | 2016-11-07 14:49:13 +0100 | [diff] [blame] | 47 | if (t) { |
| 48 | memcpy(tmp, t, SID_LEN); |
| 49 | t2 = tmp; |
| 50 | } |
| 51 | int rc = osmo_amr_rtp_dec(t2, len, &cmr, &cmi, &ft, &bfi, &sti); |
| 52 | if (rc < 0) |
| 53 | return; |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 54 | printf("[%d] decode RTP %s%s: FT %s, CMR %s, CMI is %d, SID type %s\t", |
| 55 | rc, osmo_hexdump(tmp, len), cmpr(bfi, AMR_GOOD), |
| 56 | get_value_string(osmo_amr_type_names, ft), |
| 57 | get_value_string(osmo_amr_type_names, cmr), |
| 58 | cmi, sti ? "UPDATE" : "FIRST"); |
| 59 | if (sti == -1) |
| 60 | printf("FAIL: incompatible STI for SID\n"); |
| 61 | rc = osmo_amr_rtp_enc(tmp, cmr, ft, bfi); |
| 62 | printf("[%d] encode [%d]\n", rc, memcmp(tmp, t, SID_LEN)); |
| 63 | } |
| 64 | |
| 65 | static void test_amr_rt(uint8_t _cmr, enum osmo_amr_type _ft, |
| 66 | enum osmo_amr_quality _bfi) |
| 67 | { |
| 68 | uint8_t cmr, payload[PAYLOAD_LEN]; |
| 69 | enum osmo_amr_type ft; |
| 70 | enum osmo_amr_quality bfi; |
| 71 | int8_t sti, cmi; |
| 72 | int rc, re = osmo_amr_rtp_enc(payload, _cmr, _ft, _bfi); |
| 73 | rc = osmo_amr_rtp_dec(payload, PAYLOAD_LEN, &cmr, &cmi, &ft, &bfi, &sti); |
| 74 | printf("[%d/%d] %s, CMR: %s, FT: %s, BFI: %s, CMI: %d, STI: %d\n", re, |
| 75 | rc, get_value_string(osmo_amr_type_names, ft), |
| 76 | cmpr(_cmr, cmr), cmpr(_ft, ft), cmpr(_bfi, bfi), cmi, sti); |
| 77 | } |
| 78 | |
Max | ec8f192 | 2016-05-31 14:50:21 +0200 | [diff] [blame] | 79 | uint8_t fr[] = {0xd8, 0xa9, 0xb5, 0x1d, 0xda, 0xa8, 0x82, 0xcc, 0xec, 0x52, |
| 80 | 0x29, 0x05, 0xa8, 0xc3, 0xe3, 0x0e, 0xb0, 0x89, 0x7a, 0xee, |
| 81 | 0x42, 0xca, 0xc4, 0x97, 0x22, 0xe6, 0x9e, 0xa8, 0xb8, 0xec, |
| 82 | 0x52, 0x26, 0xbd}; |
| 83 | uint8_t sid_fr[] = {0xd7, 0x27, 0x93, 0xe5, 0xe3, 0x00, 0x00, 0x00, 0x00, |
| 84 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 85 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 86 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 87 | |
| 88 | uint8_t hr[] = {0x06, 0x46, 0x76, 0xb1, 0x8e, 0x48, 0x9a, 0x2f, 0x5e, 0x4c, |
| 89 | 0x22, 0x2b, 0x62, 0x25}; |
| 90 | uint8_t sid_hr[] = {0x03, 0x8e, 0xb6, 0xcb, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 91 | 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 92 | |
| 93 | static void test_sid_xr(uint8_t *t, size_t len, bool hr) |
| 94 | { |
| 95 | printf("%s SID ? %s:: %d\n", hr ? "HR" : "FR", osmo_hexdump(t, len), |
| 96 | hr ? osmo_hr_check_sid(t, len) : osmo_fr_check_sid(t, len)); |
| 97 | } |
| 98 | |
Harald Welte | 3851e8e | 2017-05-31 02:42:46 +0200 | [diff] [blame] | 99 | const uint8_t fr_sid1[33] = { 0xd7, 0xe0, 0xa3, 0x61, 0x2c, 0x00, 0x00, |
| 100 | 0x80, 0x49, 0x00, 0x00, 0x49, 0x00, 0x00, 0x80, 0x00, 0x04, |
| 101 | 0x00, 0x00, 0x00, 0x00, 0x12, 0x40, 0x00, 0x10, 0x08, 0x00, |
| 102 | 0x00, 0x00, 0x08, 0x2d, 0x04, 0x09 }; |
| 103 | |
| 104 | const uint8_t fr_sid2[33] = { 0xd7, 0xa2, 0xbb, 0x65, 0xe5, 0x00, 0x00, |
| 105 | 0x10, 0x00, 0x04, 0x82, 0x00, 0x00, 0x00, 0x00, 0x49, 0x00, |
| 106 | 0x02, 0x41, 0x00, 0x00, 0x00, 0x00, 0x20, 0x80, 0x01, 0x00, |
| 107 | 0x00, 0x00, 0x42, 0x40, 0x10, 0xd2 }; |
| 108 | |
| 109 | const uint8_t fr_sid3[33] = { 0xd9, 0x60, 0xab, 0x21, 0xea, 0x00, 0x00, |
| 110 | 0x80, 0x48, 0x20, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x24, |
| 111 | 0x80, 0x00, 0x00, 0x00, 0x10, 0x08, 0x00, 0x10, 0x08, 0x00, |
| 112 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x08 }; |
| 113 | |
| 114 | const uint8_t fr_sid4[33] = { 0xd8, 0x21, 0xab, 0x25, 0xea, 0x00, 0x00, |
| 115 | 0x00, 0x00, 0x04, 0x10, 0x00, 0x00, 0x00, 0x02, 0x41, 0x00, |
| 116 | 0x02, 0x48, 0x00, 0x00, 0x00, 0x01, 0x20, 0x00, 0x00, 0x00, |
| 117 | 0x00, 0x00, 0x42, 0x00, 0x02, 0x50 }; |
| 118 | |
| 119 | const uint8_t fr_sid5[33] = { 0xd9, 0x61, 0x9a, 0x65, 0x60, 0x00, 0x00, |
| 120 | 0x10, 0x00, 0x04, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, |
| 121 | 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, |
| 122 | 0x00, 0x00, 0x02, 0x40, 0x82, 0x52 }; |
| 123 | |
| 124 | const uint8_t fr_sid6[33] = { 0xd9, 0xa5, 0x93, 0xe1, 0xa0, 0x00, 0x00, |
| 125 | 0x10, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x09, 0x00, |
| 126 | 0x02, 0x40, 0x00, 0x00, 0x00, 0x01, 0x20, 0x80, 0x01, 0x00, |
| 127 | 0x00, 0x90, 0x02, 0x40, 0x02, 0x02 }; |
| 128 | |
| 129 | const uint8_t fr_sid7[33] = { 0xd8, 0x20, 0xa3, 0xe5, 0xaa, 0x00, 0x00, |
| 130 | 0x80, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x80, 0x00, 0x04, |
| 131 | 0x80, 0x00, 0x00, 0x00, 0x10, 0x08, 0x00, 0x12, 0x08, 0x00, |
| 132 | 0x00, 0x00, 0x00, 0x08, 0x00, 0x09 }; |
| 133 | |
| 134 | const uint8_t fr_sid8[33] = { 0xd7, 0xe0, 0xa3, 0xa1, 0x60, 0x00, 0x00, |
| 135 | 0x10, 0x00, 0x04, 0x92, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, |
| 136 | 0x00, 0x40, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 137 | 0x00, 0x12, 0x00, 0x40, 0x12, 0x00 }; |
| 138 | |
| 139 | const uint8_t fr_sid9[33] = { 0xd8, 0x20, 0xa3, 0xa5, 0x62, 0x00, 0x00, |
| 140 | 0x10, 0x00, 0x04, 0x82, 0x00, 0x00, 0x00, 0x02, 0x49, 0x00, |
| 141 | 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x24, 0x00, 0x00, 0x00, |
| 142 | 0x00, 0x10, 0x02, 0x00, 0x82, 0x02 }; |
| 143 | |
| 144 | const uint8_t fr_sid10[33] = { 0xd4, 0xaa, 0xba, 0x6e, 0xbb, 0x00, 0x00, |
| 145 | 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x10, 0x00, 0x20, |
| 146 | 0x90, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x08, 0x00, |
| 147 | 0x00, 0x00, 0x08, 0x2c, 0x20, 0x00 }; |
| 148 | |
| 149 | const uint8_t fr_sid11[33] = { 0xd9, 0x64, 0xbb, 0x6d, 0x62, 0x00, 0x00, |
| 150 | 0x80, 0x41, 0x00, 0x00, 0x48, 0x00, 0x00, 0x10, 0x00, 0x04, |
| 151 | 0x90, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, |
| 152 | 0x00, 0x00, 0x00, 0x2c, 0x24, 0x01 }; |
| 153 | |
| 154 | const uint8_t fr_sid12[33] = { 0xd8, 0x61, 0xb2, 0xa5, 0x62, 0x00, 0x00, |
| 155 | 0x00, 0x41, 0x20, 0x00, 0x48, 0x00, 0x00, 0x10, 0x00, 0x04, |
| 156 | 0x90, 0x00, 0x00, 0x00, 0x10, 0x08, 0x00, 0x10, 0x40, 0x00, |
| 157 | 0x00, 0x00, 0x01, 0x29, 0x24, 0x08 }; |
| 158 | |
| 159 | const uint8_t fr_sid13[33] = { 0xd9, 0x23, 0xba, 0xe5, 0xe2, 0x00, 0x00, |
| 160 | 0x80, 0x41, 0x20, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x04, |
| 161 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, |
| 162 | 0x00, 0x00, 0x09, 0x05, 0x20, 0x00 }; |
| 163 | |
| 164 | const uint8_t fr_sid14[33] = { 0xd8, 0x62, 0xa2, 0x61, 0x60, 0x00, 0x00, |
| 165 | 0x10, 0x00, 0x00, 0x92, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, |
| 166 | 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, |
| 167 | 0x00, 0x80, 0x00, 0x40, 0x02, 0x40 }; |
| 168 | |
| 169 | const uint8_t fr_sid15[33] = { 0xd9, 0xe4, 0xc3, 0x6d, 0x12, 0x00, 0x00, |
| 170 | 0x80, 0x00, 0x20, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 171 | 0x10, 0x00, 0x00, 0x00, 0x10, 0x48, 0x00, 0x10, 0x48, 0x00, |
| 172 | 0x00, 0x00, 0x00, 0x2d, 0x04, 0x00 }; |
| 173 | |
| 174 | const uint8_t fr_sid16[33] = { 0xd9, 0xa4, 0xc3, 0x29, 0x59, 0x00, 0x00, |
| 175 | 0x10, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, |
| 176 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x80, 0x00, 0x00, |
| 177 | 0x00, 0x00, 0x42, 0x00, 0x12, 0x02 }; |
| 178 | |
| 179 | static const uint8_t *fr_sids[] = { fr_sid1, fr_sid2, fr_sid3, fr_sid4, |
| 180 | fr_sid5, fr_sid6, fr_sid7, fr_sid8, fr_sid9, fr_sid10, fr_sid11, |
| 181 | fr_sid12, fr_sid13, fr_sid14, fr_sid15, fr_sid16 }; |
| 182 | |
| 183 | static void test_sid_fr(void) |
| 184 | { |
| 185 | unsigned int i; |
| 186 | |
| 187 | for (i = 0; i < ARRAY_SIZE(fr_sids); i++) { |
| 188 | int rc = osmo_fr_check_sid(fr_sids[i], 33); |
| 189 | printf("FR SID %s: %d\n", osmo_hexdump(fr_sids[i], 33), rc); |
| 190 | } |
| 191 | } |
| 192 | |
Harald Welte | 5785a4a | 2020-05-13 23:10:31 +0200 | [diff] [blame] | 193 | |
| 194 | |
| 195 | static void test_amr_s_d(void) |
| 196 | { |
| 197 | ubit_t in[244]; |
| 198 | ubit_t mid[244]; |
| 199 | ubit_t out[244]; |
| 200 | int i, j; |
| 201 | |
| 202 | for (j = AMR_4_75; j <= AMR_12_2; j++) { |
| 203 | unsigned int n_bits = gsm690_bitlength[j]; |
| 204 | |
| 205 | printf("=> AMR Mode %d (%d bits)\n", j, n_bits); |
| 206 | /* set a single bit in the input buffer */ |
| 207 | for (i = 0; i < n_bits; i++) { |
| 208 | |
| 209 | memset(in, 0, sizeof(in)); |
| 210 | in[i] = 1; |
| 211 | |
| 212 | /* re-order from s to d */ |
| 213 | osmo_amr_s_to_d(mid, in, n_bits, j); |
| 214 | |
| 215 | /* and back to d */ |
| 216 | osmo_amr_d_to_s(out, mid, n_bits, j); |
| 217 | |
| 218 | if (memcmp(in, out, n_bits)) { |
| 219 | printf("Error in bit %d of mode %d!\n", i, j); |
| 220 | printf("inp s-bits: %s\n", osmo_ubit_dump(in, n_bits)); |
| 221 | printf("mid d-bits: %s\n", osmo_ubit_dump(mid, n_bits)); |
| 222 | printf("out s-bits: %s\n", osmo_ubit_dump(out, n_bits)); |
| 223 | //OSMO_ASSERT(0); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 229 | int main(int argc, char **argv) |
| 230 | { |
| 231 | printf("AMR RTP payload decoder test:\n"); |
| 232 | test_sid_dec(sid_first, 7); |
| 233 | test_sid_dec(sid_update, 7); |
Max | e9e5f8e | 2016-11-07 14:49:13 +0100 | [diff] [blame] | 234 | test_sid_dec(NULL, 7); |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 235 | test_amr_rt(0, AMR_NO_DATA, AMR_BAD); |
| 236 | test_amr_rt(0, AMR_NO_DATA, AMR_GOOD); |
| 237 | test_amr_rt(AMR_12_2, AMR_12_2, AMR_BAD); |
| 238 | test_amr_rt(AMR_12_2, AMR_12_2, AMR_GOOD); |
| 239 | test_amr_rt(AMR_7_40, AMR_7_40, AMR_BAD); |
| 240 | test_amr_rt(AMR_7_40, AMR_7_40, AMR_GOOD); |
Max | ec8f192 | 2016-05-31 14:50:21 +0200 | [diff] [blame] | 241 | printf("FR RTP payload SID test:\n"); |
| 242 | test_sid_xr(sid_fr, 33, false); |
| 243 | test_sid_xr(fr, 33, false); |
| 244 | |
| 245 | printf("HR RTP payload SID test:\n"); |
| 246 | test_sid_xr(sid_hr, 14, true); |
| 247 | test_sid_xr(hr, 14, true); |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 248 | |
Harald Welte | 3851e8e | 2017-05-31 02:42:46 +0200 | [diff] [blame] | 249 | printf("FR RTP payload SID test:\n"); |
| 250 | test_sid_fr(); |
| 251 | |
Harald Welte | 5785a4a | 2020-05-13 23:10:31 +0200 | [diff] [blame] | 252 | printf("AMR s/d bit re-ordering test:\n"); |
| 253 | test_amr_s_d(); |
| 254 | |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | |