ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_fmt_stream_verify_plan.c
Go to the documentation of this file.
1
10
11#include <stddef.h>
12#include <stdint.h>
13#include <string.h>
14
15#include "jof_produce.h"
16#include "ra8_attributes.h"
17#include "ra8_fmt_stream.h"
18
39
47
67static ra8_err_t
68internal_exact(const ra8_fmt_source_t* source, uint64_t offset, uint8_t* bytes, size_t len)
69{
70 if ((offset > source->size) || ((uint64_t)len > (source->size - offset))) {
72 }
73 size_t done = 0U;
74 while (done < len) {
75 size_t got = 0U;
76 const ra8_err_t rc =
77 source->read_at(source->ctx, offset + done, &bytes[done], len - done, &got);
78 if (rc != k_ra8_ok) {
79 return rc;
80 }
81 if ((got == 0U) || (got > (len - done))) {
83 }
84 done += got;
85 }
86 return k_ra8_ok;
87}
88
103static uint16_t internal_be16(const uint8_t bytes[2])
104{
105 return (uint16_t)(((uint16_t)bytes[0] << 8U) | bytes[1]);
106}
107
122static uint32_t internal_be32(const uint8_t bytes[4])
123{
124 return ((uint32_t)bytes[0] << k_verify_u32_high_shift) | ((uint32_t)bytes[1] << 16U) |
125 ((uint32_t)bytes[2] << 8U) | bytes[3];
126}
127
143static bool internal_is_sof(uint8_t marker)
144{
145 const bool range =
146 (marker >= (uint8_t)k_verify_jpeg_sof0) && (marker <= (uint8_t)k_verify_jpeg_sof_last);
147 return range && (marker != (uint8_t)k_verify_jpeg_dht) &&
148 (marker != (uint8_t)k_verify_jpeg_jpg) && (marker != (uint8_t)k_verify_jpeg_dac);
149}
150
168static ra8_err_t internal_jpeg_bpp(const ra8_fmt_source_t* source, uint8_t* bpp)
169{
170 uint64_t offset = 2U;
171 while (offset < source->size) {
172 uint8_t byte = 0U;
173 do {
174 ra8_err_t rc = internal_exact(source, offset++, &byte, 1U);
175 if (rc != k_ra8_ok) {
176 return rc;
177 }
178 } while (byte != (uint8_t)k_verify_jpeg_marker);
179 do {
180 ra8_err_t rc = internal_exact(source, offset++, &byte, 1U);
181 if (rc != k_ra8_ok) {
182 return rc;
183 }
184 } while (byte == (uint8_t)k_verify_jpeg_marker);
185 uint8_t length_bytes[2];
186 ra8_err_t rc = internal_exact(source, offset, length_bytes, sizeof(length_bytes));
187 if (rc != k_ra8_ok) {
188 return rc;
189 }
190 const uint16_t segment = internal_be16(length_bytes);
191 if (segment < 2U) {
193 }
194 if (internal_is_sof(byte)) {
195 uint8_t components = 0U;
196 rc = internal_exact(source, offset + k_verify_jpeg_components, &components, 1U);
197 if (rc != k_ra8_ok) {
198 return rc;
199 }
200 if ((components != 1U) && (components != 3U)) {
202 }
203 *bpp = components;
204 return k_ra8_ok;
205 }
206 offset += segment;
207 }
209}
210
229static ra8_err_t internal_png_bpp(const ra8_fmt_source_t* source, uint8_t color_type, uint8_t* bpp)
230{
231 if (color_type == 0U) {
232 *bpp = 1U;
233 return k_ra8_ok;
234 }
235 if ((color_type == 4U) || (color_type == 6U)) {
236 *bpp = 4U;
237 return k_ra8_ok;
238 }
239 if (color_type == 2U) {
240 *bpp = 3U;
241 return k_ra8_ok;
242 }
243 if (color_type != 3U) {
245 }
246 uint64_t offset = k_verify_png_chunks;
247 *bpp = 3U;
248 while ((offset + k_verify_png_chunk_record) <= source->size) {
249 uint8_t chunk[8];
250 ra8_err_t rc = internal_exact(source, offset, chunk, sizeof(chunk));
251 if (rc != k_ra8_ok) {
252 return rc;
253 }
254 const uint32_t length = internal_be32(chunk);
255 const uint32_t type = internal_be32(&chunk[4]);
256 if ((uint64_t)length > (source->size - offset - k_verify_png_chunk_record)) {
258 }
259 if (type == k_verify_png_trns) {
260 *bpp = 4U;
261 }
262 if (type == k_verify_png_idat) {
263 return k_ra8_ok;
264 }
265 offset += k_verify_png_chunk_record + length;
266 }
268}
269
287static ra8_err_t internal_bpp(const ra8_fmt_source_t* source, uint8_t* bpp)
288{
289 uint8_t head[k_verify_png_head];
290 ra8_err_t rc = internal_exact(source, 0U, head, sizeof(head));
291 if (rc != k_ra8_ok) {
292 return rc;
293 }
294 if ((head[0] == (uint8_t)k_verify_jpeg_marker) && (head[1] == (uint8_t)k_verify_jpeg_soi)) {
295 return internal_jpeg_bpp(source, bpp);
296 }
297 static const uint8_t png[8] = {k_verify_png_sig_high,
298 'P',
299 'N',
300 'G',
305 if (memcmp(head, png, sizeof(png)) == 0) {
306 return internal_png_bpp(source, head[k_verify_png_color_type], bpp);
307 }
308 /* Compared as unsigned octet runs rather than as null-terminated strings:
309 * a fourCC is a fixed four-byte field with no terminator, and `head` is a
310 * uint8_t buffer, so both memcmp operands stay essentially unsigned. */
311 static const uint8_t riff[k_verify_fourcc_bytes] = {'R', 'I', 'F', 'F'};
312 static const uint8_t webp[k_verify_fourcc_bytes] = {'W', 'E', 'B', 'P'};
313 if ((memcmp(head, riff, sizeof(riff)) == 0) &&
314 (memcmp(&head[k_verify_webp_form_offset], webp, sizeof(webp)) == 0)) {
315 *bpp = 4U;
316 return k_ra8_ok;
317 }
319}
320
337{
338 return (source->validate == nullptr) ? k_ra8_ok : source->validate(source->ctx, source->size);
339}
340
343{
344 if ((source == nullptr) || (source->read_at == nullptr) || (out == nullptr)) {
345 return k_ra8_err_null_ptr;
346 }
349 ra8_err_t rc = ra8_fmt_jof_convert_requirements(source, &convert);
350 if (rc != k_ra8_ok) {
351 return rc;
352 }
353 rc = internal_bpp(source, &out->bpp);
354 if (rc != k_ra8_ok) {
355 return rc;
356 }
357 out->width = convert.width;
358 out->height = convert.height;
359 out->band_height = convert.tile_height;
360 out->reference_work_bytes = jof_work_bytes(out->width, out->height, out->width, 1U);
361 out->banded_work_bytes = convert.work_bytes;
362 out->webp_work_bytes = convert.webp_work_bytes;
363 const uint64_t row = (uint64_t)out->width * out->bpp;
364 const uint64_t band = row * out->band_height;
365 if ((out->reference_work_bytes == 0U) || (band > UINT32_MAX)) {
367 }
368 out->row_bytes = (uint32_t)row;
369 out->band_tile_bytes = (uint32_t)band;
371 return internal_stable(source);
372}
uint32_t jof_stored_bound(uint32_t raw_bytes)
Worst-case stored-tile byte bound for scratch/cell sizing.
Definition jof.c:439
Import-time transcode producer: JPEG/PNG/WebP -> JOF band-tile atlas in bounded RAM (#231,...
uint32_t jof_work_bytes(uint16_t max_width, uint16_t max_height, uint16_t tile_w, uint16_t tile_h)
Compute the work-arena size the producer needs for given caps.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
@ k_ra8_err_not_supported
Requested feature not compiled in, not wired, or not supported by this MCU variant.
Definition ra8_err.h:180
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_null_ptr
Pointer was NULL where a valid pointer was required.
Definition ra8_err.h:478
@ k_ra8_err_protocol_error
Protocol-level error (e.g.
Definition ra8_err.h:429
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
Definition ra8_err.h:167
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
Caller-workspace I/O contracts for portable format-tool engines.
ra8_err_t ra8_fmt_jof_convert_requirements(const ra8_fmt_source_t *source, ra8_fmt_jof_convert_requirements_t *out)
Derive source geometry and exact JOF conversion workspace needs.
static uint16_t internal_be16(const uint8_t bytes[2])
Decode one big-endian uint16 field.
static bool internal_is_sof(uint8_t marker)
Classify a JPEG SOF-range marker without table-marker false positives.
static ra8_err_t internal_stable(const ra8_fmt_source_t *source)
Validate a source through its optional stability callback.
static uint32_t internal_be32(const uint8_t bytes[4])
Decode one big-endian uint32 field.
ra8_err_t ra8_fmt_jof_verify_requirements(const ra8_fmt_source_t *source, ra8_fmt_jof_verify_requirements_t *out)
Derive exact producer and comparison storage for bounded JOF verification.
static ra8_err_t internal_png_bpp(const ra8_fmt_source_t *source, uint8_t color_type, uint8_t *bpp)
Scan pre-IDAT PNG chunks for palette transparency.
static ra8_err_t internal_exact(const ra8_fmt_source_t *source, uint64_t offset, uint8_t *bytes, size_t len)
Read one exact positioned span.
verify_plan_const_t
Bounded encoded-header probe constants.
@ k_verify_jpeg_dac
JPEG arithmetic table code.
@ k_verify_png_head
Signature, IHDR, and colour type.
@ k_verify_jpeg_soi
JPEG start-of-image marker.
@ k_verify_png_color_type
PNG IHDR colour-type offset.
@ k_verify_u32_high_shift
Big-endian uint32 high shift.
@ k_verify_png_idat
PNG IDAT chunk code.
@ k_verify_jpeg_jpg
JPEG reserved SOF-range code.
@ k_verify_jpeg_marker
JPEG marker introducer.
@ k_verify_fourcc_bytes
RIFF/WebP fourCC width.
@ k_verify_jpeg_dht
JPEG table code in SOF range.
@ k_verify_jpeg_sof0
First JPEG SOF code.
@ k_verify_png_chunks
First post-IHDR chunk offset.
@ k_verify_webp_form_offset
RIFF form-type field offset.
@ k_verify_jpeg_sof_last
Last JPEG SOF-range marker.
@ k_verify_png_trns
PNG tRNS chunk code.
@ k_verify_jpeg_components
Component-count offset after len.
@ k_verify_png_chunk_record
PNG chunk framing bytes.
static ra8_err_t internal_jpeg_bpp(const ra8_fmt_source_t *source, uint8_t *bpp)
Determine baseline JPEG output channels from the first SOF.
static ra8_err_t internal_bpp(const ra8_fmt_source_t *source, uint8_t *bpp)
Derive exact producer output channels from source headers.
verify_png_signature_t
Fixed non-ASCII bytes in the PNG signature.
@ k_verify_png_sig_sub
DOS EOF byte.
@ k_verify_png_sig_cr
Carriage return byte.
@ k_verify_png_sig_lf
Line feed byte.
@ k_verify_png_sig_high
High-bit signature byte.
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
Source geometry and exact arenas required by one JOF conversion.
uint16_t width
Declared source width in pixels.
uint32_t webp_work_bytes
Exact WebP whole-frame bytes, or zero.
uint16_t height
Declared source height in pixels.
uint16_t tile_height
Height selected for emitted tiles.
uint32_t work_bytes
Exact streaming-producer arena bytes.
Exact phase-reused storage requirements for JOF verification.
uint32_t row_bytes
One decoded reference row.
uint8_t bpp
Decoder output bytes per pixel.
uint16_t band_height
Subject tile height under test.
uint32_t reference_work_bytes
One-row reference producer arena.
uint16_t width
Source width in pixels.
uint16_t height
Source height in pixels.
uint32_t band_tile_bytes
Largest decoded subject tile.
uint32_t scratch_bytes
Largest stored-tile staging buffer.
uint32_t webp_work_bytes
Whole-frame WebP arena, or zero.
uint32_t banded_work_bytes
Banded subject producer arena.
Immutable, randomly readable input object.
uint64_t size
Exact object byte length.
void * ctx
Backend-owned context.
ra8_fmt_source_validate_fn validate
Optional stability callback.
jof_pread_fn read_at
Positioned-read callback.