ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_webp.c
Go to the documentation of this file.
1
17
18#include "ra8_webp.h"
19
20#include <limits.h>
21#include <stddef.h>
22#include <stdint.h>
23
24#include "ra8_attributes.h"
25#include "ra8_check.h"
26#include "ra8_err.h"
27#include "ra8_webp_arena.h"
28#include "src/webp/decode.h" /* WebPGetInfo, WebPDecodeRGBAInto (vendored libwebp) */
29
31static const char s_ra8_webp_tag[] = "WEBP";
32
33ra8_err_t ra8_webp_get_info(const uint8_t* data, size_t size, uint32_t* out_w, uint32_t* out_h)
34{
36 RA8_CHECK_NULL_PTR(out_w, s_ra8_webp_tag, "out_w");
37 RA8_CHECK_NULL_PTR(out_h, s_ra8_webp_tag, "out_h");
38 if (size == 0U) {
39 ra8_log_error(s_ra8_webp_tag, "empty WebP buffer");
41 }
42
43 int w = 0;
44 int h = 0;
45 /* WebPGetInfo returns 0 for any invalid container or a zero/oversized
46 * dimension, so on success both w and h are already in [1, WEBP_MAX_DIMENSION]
47 * -- no separate non-positive check is needed (it would be dead code). */
48 if (WebPGetInfo(data, size, &w, &h) == 0) {
50 }
51 if (((uint32_t)w > k_ra8_webp_max_dim) || ((uint32_t)h > k_ra8_webp_max_dim)) {
52 ra8_log_error(s_ra8_webp_tag, "WebP dimension exceeds cap");
54 }
55
56 *out_w = (uint32_t)w;
57 *out_h = (uint32_t)h;
58 return k_ra8_ok;
59}
60
92static ra8_err_t internal_webp_check_output(const uint8_t* data,
93 size_t size,
94 size_t out_stride,
95 size_t out_capacity,
96 uint32_t* w,
97 uint32_t* h)
98{
99 RA8_RETURN_ON_ERROR(ra8_webp_get_info(data, size, w, h), s_ra8_webp_tag, "info");
100 const size_t min_stride = (size_t)*w * (size_t)k_ra8_webp_bytes_per_px;
101 if ((out_stride < min_stride) || (((size_t)*h * out_stride) > out_capacity)) {
102 ra8_log_error(s_ra8_webp_tag, "output buffer too small");
104 }
105 if (out_stride > (size_t)INT_MAX) {
106 ra8_log_error(s_ra8_webp_tag, "output stride exceeds INT_MAX");
108 }
109 return k_ra8_ok;
110}
111
145static ra8_err_t internal_webp_decode_impl(const uint8_t* data,
146 size_t size,
147 ra8_webp_arena_t* arena,
148 uint8_t* out_rgba,
149 size_t out_stride,
150 size_t out_capacity,
151 uint32_t* out_w,
152 uint32_t* out_h)
153{
154 uint32_t w = 0U;
155 uint32_t h = 0U;
156 RA8_RETURN_ON_ERROR(internal_webp_check_output(data, size, out_stride, out_capacity, &w, &h),
158 "validate");
159
160 ra8_webp_arena_bind(arena);
161 uint8_t* const result = WebPDecodeRGBAInto(data, size, out_rgba, out_capacity, (int)out_stride);
163 if (result == nullptr) {
165 }
166
167 /*
168 * This facade decodes a whole WebP into a caller RGBA8888 buffer. The #290
169 * normalize-on-import path consumes it in the JOF tile producer
170 * (apps/shared_libs/jof/src/jof_produce.c: priv_webp_transcode), which
171 * bands the decoded canvas into JOF tiles so render time touches one codec
172 * regardless of source. TODO(#289): the ra8_reflow / ra8_img inline raster
173 * dispatch (apps/shared_libs/reflow/src/reflow_image.c) does not yet have a WebP
174 * arm for the small-image (non-tiled) path -- that lands with the longstrip
175 * render work; large WebP pages already normalize through the producer above.
176 */
177
178 if (out_w != nullptr) {
179 *out_w = w;
180 }
181 if (out_h != nullptr) {
182 *out_h = h;
183 }
184 return k_ra8_ok;
185}
186
187ra8_err_t ra8_webp_decode_rgba(const uint8_t* data,
188 size_t size,
189 ra8_webp_arena_t* arena,
190 uint8_t* out_rgba,
191 size_t out_stride,
192 size_t out_capacity,
193 uint32_t* out_w,
194 uint32_t* out_h)
195{
196 RA8_CHECK_NULL_PTR(data, s_ra8_webp_tag, "data");
197 RA8_CHECK_NULL_PTR(arena, s_ra8_webp_tag, "arena");
198 RA8_CHECK_NULL_PTR(out_rgba, s_ra8_webp_tag, "out_rgba");
199 return internal_webp_decode_impl(data,
200 size,
201 arena,
202 out_rgba,
203 out_stride,
204 out_capacity,
205 out_w,
206 out_h);
207}
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_RETURN_ON_ERROR(err, tag, message)
Early return on error, propagating the code upward.
Definition ra8_check.h:184
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ 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_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_err_validation_failed
Validation rule failed (caller-supplied invariant not satisfied).
Definition ra8_err.h:459
@ k_ra8_err_range_check_failed
Value outside range enforced by RA8_CHECK_RANGE / RA8_CHECK_RANGE_TAG.
Definition ra8_err.h:471
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
#define ra8_log_error(tag, message)
RA8 log error.
Definition ra8_log.h:335
static ra8_err_t internal_webp_check_output(const uint8_t *data, size_t size, size_t out_stride, size_t out_capacity, uint32_t *w, uint32_t *h)
Read the WebP header and validate the caller's output geometry.
Definition ra8_webp.c:92
static ra8_err_t internal_webp_decode_impl(const uint8_t *data, size_t size, ra8_webp_arena_t *arena, uint8_t *out_rgba, size_t out_stride, size_t out_capacity, uint32_t *out_w, uint32_t *out_h)
Validate geometry, decode the WebP into out_rgba, and report size.
Definition ra8_webp.c:145
static const char s_ra8_webp_tag[]
Component tag used in validation log lines.
Definition ra8_webp.c:31
ra8_err_t ra8_webp_decode_rgba(const uint8_t *data, size_t size, ra8_webp_arena_t *arena, uint8_t *out_rgba, size_t out_stride, size_t out_capacity, uint32_t *out_w, uint32_t *out_h)
Decode a WebP into a caller-owned RGBA8888 buffer, heap-free.
Definition ra8_webp.c:187
ra8_err_t ra8_webp_get_info(const uint8_t *data, size_t size, uint32_t *out_w, uint32_t *out_h)
Read a WebP header's pixel dimensions without decoding the body.
Definition ra8_webp.c:33
Zero-heap WebP (VP8 / VP8L) decode facade over the vendored libwebp.
@ k_ra8_webp_bytes_per_px
Output bytes per pixel (RGBA8888).
Definition ra8_webp.h:59
@ k_ra8_webp_max_dim
Max accepted width/height, pixels/axis.
Definition ra8_webp.h:58
Heap-free scratch allocator hooks for the vendored libwebp decoder.
void ra8_webp_arena_unbind(void)
Unbind the active arena; subsequent allocation hooks fail with nullptr.
void ra8_webp_arena_bind(ra8_webp_arena_t *arena)
Bind arena as the active scratch for subsequent decode allocations.
Caller-owned bump arena backing a single WebP decode.