Edge AI Add-on API 2.2.0
Loading...
Searching...
No Matches
nrf_edgeai_obsv_encode_sizes.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
15#ifndef NRF_EDGEAI_OBSV_ENCODE_SIZES_H
16#define NRF_EDGEAI_OBSV_ENCODE_SIZES_H
17
28/* --- CBOR encoding primitives --- */
29
30/*
31 * These constants are CBOR wire-format budgets, not C struct sizes.
32 * CBOR is variable-length; the in-memory structs do not bound encoded size.
33 *
34 * Helper macros (file-private):
35 *
36 * _CBOR_TSTR(s)
37 * Encoded size of a text-string literal s whose length is at most 23 bytes.
38 * CBOR represents it as one header byte (0x60 | strlen(s)) followed by the
39 * raw UTF-8 characters. Because sizeof(s) == strlen(s) + 1 (null terminator),
40 * sizeof(s) equals the CBOR encoded size exactly.
41 *
42 * WARNING: the string literals used here must exactly match those in
43 * nrf_edgeai_obsv_encode.c. If a key is renamed there, update the
44 * corresponding _CBOR_TSTR() call here too.
45 *
46 * _CBOR_SMALL_HDR
47 * One-byte map/list header, valid when the element count is <= 23.
48 * In CBOR, the initial byte encodes both the major type (3 high bits)
49 * and the count (5 low bits) when the count fits directly, so no
50 * additional length bytes follow. Used for fixed-arity maps in the encoder.
51 *
52 * _CBOR_LARGE_HDR
53 * Three-byte map/list header, valid for element counts up to 65535.
54 * Used wherever the count is a runtime value (metrics list, data rows).
55 */
56#define _CBOR_TSTR(s) sizeof(s)
57#define _CBOR_SMALL_HDR 1U
58#define _CBOR_LARGE_HDR 3U
59
61#define _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST (1U + sizeof(uint32_t))
62
81#define _NRF_EDGEAI_OBSV_ENCODE_OUTER_FIXED \
82 (_CBOR_SMALL_HDR \
83 + _CBOR_TSTR("format_version") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
84 + _CBOR_TSTR("num_inferences") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
85 + _CBOR_TSTR("num_features") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
86 + _CBOR_TSTR("model") + _CBOR_SMALL_HDR \
87 + _CBOR_TSTR("id") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
88 + _CBOR_TSTR("num_classes") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
89 + _CBOR_TSTR("num_features") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
90 + _CBOR_TSTR("version") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
91 + _CBOR_TSTR("metrics") + _CBOR_LARGE_HDR)
92
104#define _NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED \
105 (_CBOR_SMALL_HDR \
106 + _CBOR_TSTR("id") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
107 + _CBOR_TSTR("v") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
108 + _CBOR_TSTR("d") + _CBOR_LARGE_HDR)
109
114#define _NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED _CBOR_LARGE_HDR
115
116/* --- optional metrics (0 when the corresponding Kconfig option is off) --- */
117
118#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_PROBS_DISTRIBUTION)
119
120#define _NRF_EDGEAI_OBSV_ENCODE_PD_PER_CLASS \
121 (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
122 (CONFIG_NRF_EDGEAI_OBSV_PROBS_DISTRIBUTION_BIN_NUM * \
123 _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))
124
125#define _NRF_EDGEAI_OBSV_ENCODE_PD \
126 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
127 (CONFIG_NRF_EDGEAI_OBSV_MAX_CLASSES * _NRF_EDGEAI_OBSV_ENCODE_PD_PER_CLASS))
128
129#else
130#define _NRF_EDGEAI_OBSV_ENCODE_PD 0
131#endif
132
133#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_TRANSITION_MATRIX)
134
135#define _NRF_EDGEAI_OBSV_ENCODE_TM_PER_ROW \
136 (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
137 (CONFIG_NRF_EDGEAI_OBSV_MAX_CLASSES * _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))
138
139#define _NRF_EDGEAI_OBSV_ENCODE_TM \
140 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
141 (CONFIG_NRF_EDGEAI_OBSV_MAX_CLASSES * _NRF_EDGEAI_OBSV_ENCODE_TM_PER_ROW))
142
143#else
144#define _NRF_EDGEAI_OBSV_ENCODE_TM 0
145#endif
146
147#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_PREDICTION_SWITCHING_RATE)
148
149/* Prediction switching rate emits a fixed 1 x 2 row: [switches, comparisons]. */
150#define _NRF_EDGEAI_OBSV_ENCODE_PSR \
151 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
152 (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
153 (2U * _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST)))
154
155#else
156#define _NRF_EDGEAI_OBSV_ENCODE_PSR 0
157#endif
158
159#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_PROBS_ENTROPY_DIST)
160
161/* Entropy distribution emits a single 1 x bin_num histogram row. */
162#define _NRF_EDGEAI_OBSV_ENCODE_PED \
163 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
164 (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
165 (CONFIG_NRF_EDGEAI_OBSV_PROBS_ENTROPY_DIST_BIN_NUM * \
166 _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST)))
167
168#else
169#define _NRF_EDGEAI_OBSV_ENCODE_PED 0
170#endif
171
172#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_PROBS_TOP2_MARGIN_DIST)
173
174/* Top-2 margin distribution emits a single 1 x bin_num histogram row. */
175#define _NRF_EDGEAI_OBSV_ENCODE_PMD \
176 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
177 (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
178 (CONFIG_NRF_EDGEAI_OBSV_PROBS_TOP2_MARGIN_DIST_BIN_NUM * \
179 _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST)))
180
181#else
182#define _NRF_EDGEAI_OBSV_ENCODE_PMD 0
183#endif
184
185#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_MEL_ENERGY_DESC)
186
187/* Mel energy descriptor emits 4 rows (mean, max, dynamic range, floor) x bin_num. */
188#define _NRF_EDGEAI_OBSV_ENCODE_MED \
189 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
190 (4U * (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
191 (CONFIG_NRF_EDGEAI_OBSV_MEL_ENERGY_DESC_BIN_NUM * \
192 _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))))
193
194#else
195#define _NRF_EDGEAI_OBSV_ENCODE_MED 0
196#endif
197
198#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_MEL_SPECTRAL_DESC)
199
200/* Mel spectral descriptor emits 8 rows (band ratios + spectral shape) x bin_num. */
201#define _NRF_EDGEAI_OBSV_ENCODE_MSD \
202 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
203 (8U * (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
204 (CONFIG_NRF_EDGEAI_OBSV_MEL_SPECTRAL_DESC_BIN_NUM * \
205 _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))))
206
207#else
208#define _NRF_EDGEAI_OBSV_ENCODE_MSD 0
209#endif
210
230#define NRF_EDGEAI_OBSV_ENCODE_METRIC_SIZE(n_rows, n_cols) \
231 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
232 (n_rows) * (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
233 (n_cols) * _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))
234
243#define NRF_EDGEAI_OBSV_ENCODE_MAX_SIZE \
244 (_NRF_EDGEAI_OBSV_ENCODE_OUTER_FIXED + _NRF_EDGEAI_OBSV_ENCODE_PD + \
245 _NRF_EDGEAI_OBSV_ENCODE_TM + _NRF_EDGEAI_OBSV_ENCODE_PSR + \
246 _NRF_EDGEAI_OBSV_ENCODE_PED + _NRF_EDGEAI_OBSV_ENCODE_PMD + \
247 _NRF_EDGEAI_OBSV_ENCODE_MED + _NRF_EDGEAI_OBSV_ENCODE_MSD + \
248 CONFIG_NRF_EDGEAI_OBSV_EXTRA_ENCODE_BYTES)
249
256#define _NRF_EDGEAI_OBSV_ENCODE_LIST_ARRAY_OVERHEAD _CBOR_SMALL_HDR
257
265#define NRF_EDGEAI_OBSV_ENCODE_LIST_BUF_SIZE(n) \
266 (_NRF_EDGEAI_OBSV_ENCODE_LIST_ARRAY_OVERHEAD + (n) * NRF_EDGEAI_OBSV_ENCODE_MAX_SIZE)
267
272#endif /* NRF_EDGEAI_OBSV_ENCODE_SIZES_H */