Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
inference_jpeg.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file inference_jpeg.c
20 * @brief Streaming JPEG input for the same preprocessing used by live frames.
21 * @details Application scratch is two rows, not a decoded full-resolution RGB image;
22 * libjpeg's internal allocations remain separate from this bounded workspace.
23 */
24#include "inference_jpeg.h"
25#include "jpeg_marker_guard.h"
26
27#include <stdio.h>
28#include <jpeglib.h>
29#include <math.h>
30#include <setjmp.h>
31#include <string.h>
32#include <sys/stat.h>
33
34/** @brief Caller-owned codec/error state that remains valid across decoder longjmp exits. */
36 struct jpeg_decompress_struct codec;
37 struct jpeg_error_mgr failure;
38 void (*emit)(j_common_ptr, int);
39 jmp_buf jump;
40 FILE *input;
41 bool created;
42 unsigned char row[8192 * 3];
43 union { float alignment; unsigned char bytes[2048 * 3 * sizeof(float)]; } prepared;
44};
45
46/** @brief Convert libjpeg's fatal callback into a failed input instead of terminating the CAM worker. */
47static void jpeg_failure(j_common_ptr codec)
48{
49 struct jpeg_reader *reader = codec->client_data;
50 longjmp(reader->jump, 1);
51}
52
53/** @brief Check JPEG marker bounds ahead of normal codec diagnostics.
54 * @param codec Decoder carrying the borrowed jpeg_reader in client_data.
55 * @param level libjpeg diagnostic level.
56 * @details Preserve the original handler for accepted messages so the
57 * inference wrapper can still observe codec warnings. */
58static void jpeg_message(j_common_ptr codec, int level)
59{
60 struct jpeg_reader *reader = codec->client_data;
62 reader->emit(codec, level);
63}
64
65/** @brief Fill a bound input tensor; ignore partial tensor/transform contents after any failure. */
66static bool decode_jpeg(struct jpeg_reader *reader, struct inference_context *context)
67{
68 /* Keeping codec ownership in the caller avoids indeterminate cleanup locals after longjmp. */
69 if (setjmp(reader->jump)) return false;
70 reader->codec.err = jpeg_std_error(&reader->failure);
71 reader->emit = reader->failure.emit_message;
72 reader->failure.emit_message = jpeg_message;
73 reader->failure.error_exit = jpeg_failure;
74 reader->codec.client_data = reader;
75 reader->created = true;
76 jpeg_create_decompress(&reader->codec);
77 jpeg_stdio_src(&reader->codec, reader->input);
78 if (jpeg_read_header(&reader->codec, TRUE) != JPEG_HEADER_OK ||
79 !reader->codec.image_width || !reader->codec.image_height ||
80 reader->codec.image_width > 8192 || reader->codec.image_height > 8192) return false;
81 reader->codec.out_color_space = JCS_RGB;
82 if (!jpeg_start_decompress(&reader->codec) || reader->codec.output_components != 3) return false;
83 const struct inference_image_spec *spec = &context->model.image;
84 if (!spec->width || !spec->height || !spec->channels || spec->units != INFERENCE_PIXEL ||
85 (spec->type != ONNX_FLOAT32 && spec->type != ONNX_UINT8 && spec->type != ONNX_INT8)) return false;
86 struct inference_image_transform *transform = &context->transform;
87 *transform = (struct inference_image_transform){
88 .source_width = reader->codec.output_width, .source_height = reader->codec.output_height,
89 .input_width = spec->width, .input_height = spec->height,
90 .content_width = spec->width, .content_height = spec->height
91 };
92 if (spec->letterbox) {
93 const double horizontal = (double)spec->width / transform->source_width;
94 const double vertical = (double)spec->height / transform->source_height;
95 const double scale = horizontal < vertical ? horizontal : vertical;
96 transform->content_width = (unsigned)floor(transform->source_width * scale + 0.5);
97 transform->content_height = (unsigned)floor(transform->source_height * scale + 0.5);
98 if (!transform->content_width) transform->content_width = 1;
99 if (!transform->content_height) transform->content_height = 1;
100 transform->left = (spec->width - transform->content_width) / 2;
101 transform->top = (spec->height - transform->content_height) / 2;
102 }
103 const size_t element = spec->type == ONNX_FLOAT32 ? sizeof(float) : 1;
104 const size_t row_bytes = (size_t)transform->content_width * spec->channels * element;
105 struct inference_image_spec row_spec = *spec;
106 row_spec.width = transform->content_width;
107 row_spec.height = 1;
108 row_spec.letterbox = false;
109 row_spec.layout = INFERENCE_NHWC;
110 /* Padding is normalized exactly like image samples, including quantized input ranges. */
111 for (unsigned row = 0; row < spec->height; ++row) {
112 for (unsigned column = 0; column < spec->width; ++column) {
113 for (unsigned channel = 0; channel < spec->channels; ++channel) {
114 const double padding = (spec->padding - spec->mean[channel]) * spec->scale[channel];
115 if (!isfinite(padding)) return false;
116 const size_t index = spec->layout == INFERENCE_NCHW ?
117 ((size_t)channel * spec->height + row) * spec->width + column :
118 ((size_t)row * spec->width + column) * spec->channels + channel;
119 if (spec->type == ONNX_FLOAT32) {
120 ((float *)context->model.inputs[0].data)[index] = (float)padding;
121 if (!isfinite(((float *)context->model.inputs[0].data)[index])) return false;
122 } else if (spec->type == ONNX_UINT8) {
123 if (padding < 0 || padding > UINT8_MAX) return false;
124 ((uint8_t *)context->model.inputs[0].data)[index] = (uint8_t)lround(padding);
125 } else {
126 if (padding < INT8_MIN || padding > INT8_MAX) return false;
127 ((int8_t *)context->model.inputs[0].data)[index] = (int8_t)lround(padding);
128 }
129 }
130 }
131 }
132 unsigned target_row = 0;
133 /* Consume every source row to validate the JPEG, but retain only rows selected by nearest resize. */
134 while (reader->codec.output_scanline < reader->codec.output_height) {
135 const unsigned source_row = reader->codec.output_scanline;
136 JSAMPROW row_pointer = reader->row;
137 if (jpeg_read_scanlines(&reader->codec, &row_pointer, 1) != 1) return false;
138 while (target_row < transform->content_height) {
139 const unsigned selected = (unsigned)(((uint64_t)target_row * 2 + 1) * transform->source_height /
140 ((uint64_t)transform->content_height * 2));
141 if (selected != source_row) break;
142 const struct inference_image image = {.planes = {reader->row},
143 .lengths = {(size_t)transform->source_width * 3}, .strides = {(size_t)transform->source_width * 3},
144 .width = transform->source_width, .height = 1, .format = INFERENCE_RGB8};
145 struct inference_image_transform unused;
146 char error[128];
147 /* Reuse the live-image conversion so color order and normalization cannot drift between sources. */
148 if (!inference_image_prepare(&image, &row_spec, reader->prepared.bytes, row_bytes, &unused, error, sizeof(error))) return false;
149 for (unsigned column = 0; column < transform->content_width; ++column) {
150 for (unsigned channel = 0; channel < spec->channels; ++channel) {
151 const size_t input = ((size_t)column * spec->channels + channel) * element;
152 const unsigned final_row = target_row + transform->top, final_column = column + transform->left;
153 const size_t output = (spec->layout == INFERENCE_NCHW ?
154 ((size_t)channel * spec->height + final_row) * spec->width + final_column :
155 ((size_t)final_row * spec->width + final_column) * spec->channels + channel) * element;
156 for (size_t byte = 0; byte < element; ++byte)
157 ((unsigned char *)context->model.inputs[0].data)[output + byte] = reader->prepared.bytes[input + byte];
158 }
159 }
160 ++target_row;
161 }
162 }
163 /* libjpeg may repair truncated input; repaired pixels are not qualified model evidence. */
164 return target_row == transform->content_height && jpeg_finish_decompress(&reader->codec) && !reader->failure.num_warnings;
165}
166
167bool inference_jpeg_prepare(struct inference_context *context, const char *filename,
168 char *error, size_t capacity)
169{
170 if (!error || !capacity) return false;
171 error[0] = '\0';
172 if (!context || !filename || !filename[0]) {
173 snprintf(error, capacity, "Invalid inference JPEG request");
174 return false;
175 }
176 if (!context->storage || context->model.input_count != 1 || !context->model.image.channels) {
177 snprintf(error, capacity, "Model does not declare one image input");
178 return false;
179 }
180 const struct inference_image_spec *spec = &context->model.image;
181 const size_t element = spec->type == ONNX_FLOAT32 ? sizeof(float) : 1;
182 if (!context->model.inputs[0].data || !spec->width || spec->width > 2048 ||
183 !spec->height || spec->height > 2048 || (spec->channels != 1 && spec->channels != 3) ||
184 (spec->layout != INFERENCE_NCHW && spec->layout != INFERENCE_NHWC) ||
185 (spec->type != ONNX_FLOAT32 && spec->type != ONNX_UINT8 && spec->type != ONNX_INT8) ||
186 spec->units != INFERENCE_PIXEL || !isfinite(spec->padding) ||
187 context->model.inputs[0].bytes != (size_t)spec->width * spec->height * spec->channels * element ||
188 (spec->type == ONNX_FLOAT32 && (uintptr_t)context->model.inputs[0].data % _Alignof(float))) {
189 snprintf(error, capacity, "Invalid JPEG tensor storage or preprocessing contract");
190 return false;
191 }
192 for (unsigned channel = 0; channel < spec->channels; ++channel) {
193 if (!isfinite(spec->scale[channel]) || !spec->scale[channel] || !isfinite(spec->mean[channel])) {
194 snprintf(error, capacity, "Invalid JPEG normalization contract");
195 return false;
196 }
197 }
198 struct stat status;
199 if (stat(filename, &status) || !S_ISREG(status.st_mode) || status.st_size <= 0 || status.st_size > 128 * 1024 * 1024) {
200 snprintf(error, capacity, "Invalid inference JPEG: %s", filename);
201 return false;
202 }
203 struct jpeg_reader reader = {0};
204 reader.input = fopen(filename, "rb");
205 if (!reader.input) {
206 snprintf(error, capacity, "Cannot read inference JPEG: %s", filename);
207 return false;
208 }
209 bool valid = decode_jpeg(&reader, context);
210 if (reader.created) jpeg_destroy_decompress(&reader.codec);
211 if (fclose(reader.input)) valid = false;
212 if (!valid) snprintf(error, capacity, "Invalid JPEG or image preprocessing contract: %s", filename);
213 else error[0] = '\0';
214 return valid;
215}
static unsigned capacity[COVERAGE_CAMERAS]
@ INFERENCE_NHWC
@ INFERENCE_NCHW
@ INFERENCE_PIXEL
static bool decode_jpeg(struct jpeg_reader *reader, struct inference_context *context)
Fill a bound input tensor; ignore partial tensor/transform contents after any failure.
bool inference_jpeg_prepare(struct inference_context *context, const char *filename, char *error, size_t capacity)
Stream a JPEG into an open context's single declared image input.
static void jpeg_failure(j_common_ptr codec)
Convert libjpeg's fatal callback into a failed input instead of terminating the CAM worker.
static void jpeg_message(j_common_ptr codec, int level)
Check JPEG marker bounds ahead of normal codec diagnostics.
Bounded JPEG-to-tensor preparation without running the graph or modifying the input file.
Reject invalid Huffman selectors before the bundled codec indexes its tables.
static void usher_jpeg_validate_marker(j_common_ptr state)
Validate marker parameters from an error manager's emit_message callback.
uint8_t row[LWIR_MAX_WIDTH *3U]
Definition lwir_image.c:63
struct jpeg_error_mgr error
Definition lwir_image.c:60
@ ONNX_FLOAT32
@ ONNX_UINT8
@ ONNX_INT8
Independent, noncopyable model owner; mapped tensor buffers and session are reused between jobs.
struct inference_image_transform transform
struct inference_model model
Model-owned preprocessing; normalized samples are (sample - mean) * scale.
enum inference_layout layout
enum inference_units units
enum onnx_element_type type
Exact resized content rectangle, including rounded letterbox offsets.
struct inference_image_spec image
struct onnx_tensor inputs[ONNX_TENSOR_LIMIT]
Caller-owned codec/error state that remains valid across decoder longjmp exits.
unsigned char bytes[2048 *3 *sizeof(float)]
struct jpeg_decompress_struct codec
void(* emit)(j_common_ptr, int)
union jpeg_reader::@046352252025233214237353161003055245043271123215 prepared
struct jpeg_error_mgr failure
unsigned char row[8192 *3]
bool valid
Definition usher.c:537