Edge AI Add-on API 3.0.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
105#define _NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED \
106 (_CBOR_SMALL_HDR \
107 + _CBOR_TSTR("id") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
108 + _CBOR_TSTR("v") + _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST \
109 + _CBOR_TSTR("d") + _CBOR_LARGE_HDR)
110
122#define NRF_EDGEAI_OBSV_ENCODE_METRIC_CONFIG_SIZE(n_rows, n_cols) \
123 ((((n_rows) == 0U) || ((n_cols) == 0U)) \
124 ? 0U \
125 : (_CBOR_TSTR("c") + _CBOR_LARGE_HDR + \
126 (n_rows) * (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
127 (n_cols) * _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST)))
128
133#define _NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED _CBOR_LARGE_HDR
134
135/* --- optional metrics (0 when the corresponding Kconfig option is off) --- */
136
137#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_CLASS_PRED_DIST)
138
139/* Class predictions distribution emits 2*num_classes rows (probability
140 * distribution then streak distribution) x bin_num.
141 */
142#define _NRF_EDGEAI_OBSV_ENCODE_CPD_PER_ROW \
143 (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
144 (CONFIG_NRF_EDGEAI_OBSV_CLASS_PRED_DIST_BIN_NUM * \
145 _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))
146
147/* Config reported by the metric: one row of streak top and tolerance
148 * (NRF_EDGEAI_OBSV_CPD_CFG_COUNT columns in nrf_edgeai_obsv_metrics.h).
149 */
150#define _NRF_EDGEAI_OBSV_ENCODE_CPD \
151 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
152 NRF_EDGEAI_OBSV_ENCODE_METRIC_CONFIG_SIZE(1U, 2U) + \
153 (2U * CONFIG_NRF_EDGEAI_OBSV_MAX_CLASSES * _NRF_EDGEAI_OBSV_ENCODE_CPD_PER_ROW))
154
155#else
156#define _NRF_EDGEAI_OBSV_ENCODE_CPD 0
157#endif
158
159#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_TRANSITION_MATRIX)
160
161#define _NRF_EDGEAI_OBSV_ENCODE_TM_PER_ROW \
162 (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
163 (CONFIG_NRF_EDGEAI_OBSV_MAX_CLASSES * _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))
164
165#define _NRF_EDGEAI_OBSV_ENCODE_TM \
166 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
167 (CONFIG_NRF_EDGEAI_OBSV_MAX_CLASSES * _NRF_EDGEAI_OBSV_ENCODE_TM_PER_ROW))
168
169#else
170#define _NRF_EDGEAI_OBSV_ENCODE_TM 0
171#endif
172
173#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_MODEL_CERTAINTY_DESC)
174
175/* Model certainty descriptor emits 3 rows (entropy hist, top-2 margin hist,
176 * stability counters) x bin_num.
177 */
178#define _NRF_EDGEAI_OBSV_ENCODE_MCD \
179 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
180 (3U * (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
181 (CONFIG_NRF_EDGEAI_OBSV_MODEL_CERTAINTY_DESC_BIN_NUM * \
182 _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))))
183
184#else
185#define _NRF_EDGEAI_OBSV_ENCODE_MCD 0
186#endif
187
188#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_MEL_ENERGY_DESC)
189
190/* Mel energy descriptor emits 4 rows (mean, max, dynamic range, floor) x bin_num,
191 * plus a 1 x 2 config row (p01, p99; NRF_EDGEAI_OBSV_MED_CFG_COUNT columns).
192 */
193#define _NRF_EDGEAI_OBSV_ENCODE_MED \
194 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
195 NRF_EDGEAI_OBSV_ENCODE_METRIC_CONFIG_SIZE(1U, 2U) + \
196 (4U * (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
197 (CONFIG_NRF_EDGEAI_OBSV_MEL_ENERGY_DESC_BIN_NUM * \
198 _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))))
199
200#else
201#define _NRF_EDGEAI_OBSV_ENCODE_MED 0
202#endif
203
204#if defined(CONFIG_NRF_EDGEAI_OBSV_METRIC_MEL_SPECTRAL_DESC)
205
206/* Mel spectral descriptor emits 8 rows (band ratios + spectral shape) x bin_num. */
207#define _NRF_EDGEAI_OBSV_ENCODE_MSD \
208 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
209 (8U * (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
210 (CONFIG_NRF_EDGEAI_OBSV_MEL_SPECTRAL_DESC_BIN_NUM * \
211 _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))))
212
213#else
214#define _NRF_EDGEAI_OBSV_ENCODE_MSD 0
215#endif
216
236#define NRF_EDGEAI_OBSV_ENCODE_METRIC_SIZE(n_rows, n_cols) \
237 (_NRF_EDGEAI_OBSV_ENCODE_METRIC_BLOCK_FIXED + \
238 (n_rows) * (_NRF_EDGEAI_OBSV_ENCODE_TABLE_ROW_FIXED + \
239 (n_cols) * _NRF_EDGEAI_OBSV_ENCODE_UINT32_WORST))
240
249#define NRF_EDGEAI_OBSV_ENCODE_MAX_SIZE \
250 (_NRF_EDGEAI_OBSV_ENCODE_OUTER_FIXED + _NRF_EDGEAI_OBSV_ENCODE_CPD + \
251 _NRF_EDGEAI_OBSV_ENCODE_TM + _NRF_EDGEAI_OBSV_ENCODE_MCD + \
252 _NRF_EDGEAI_OBSV_ENCODE_MED + _NRF_EDGEAI_OBSV_ENCODE_MSD + \
253 CONFIG_NRF_EDGEAI_OBSV_EXTRA_ENCODE_BYTES)
254
261#define _NRF_EDGEAI_OBSV_ENCODE_LIST_ARRAY_OVERHEAD _CBOR_SMALL_HDR
262
270#define NRF_EDGEAI_OBSV_ENCODE_LIST_BUF_SIZE(n) \
271 (_NRF_EDGEAI_OBSV_ENCODE_LIST_ARRAY_OVERHEAD + (n) * NRF_EDGEAI_OBSV_ENCODE_MAX_SIZE)
272
277#endif /* NRF_EDGEAI_OBSV_ENCODE_SIZES_H */