ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_jpeg_sw_internal.h
Go to the documentation of this file.
1
37
38#pragma once
39
40#include <stdint.h>
41
42#include "ra8_attributes.h"
43#include "ra8_err.h"
44
45/* ------------------------------------------------------------------ */
46/* Shared constants and look-up tables */
47/* ------------------------------------------------------------------ */
48
61
70
72typedef enum : int16_t {
73 k_cr_r_q15 = 22970,
74 k_cb_b_q15 = 29032,
75 k_cb_g_q15 = -5638,
76 k_cr_g_q15 = -11700,
78
80typedef enum : uint16_t {
87
98
100typedef enum : uint8_t {
106
133
159
185
210
219static const uint8_t s_zigzag[64] = {
220 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48,
221 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23,
222 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
223};
224
235static const int32_t s_dct_cos_q14[8][8] = {
236 {16384, 16384, 16384, 16384, 16384, 16384, 16384, 16384},
237 {16069, 13623, 9102, 3196, -3196, -9102, -13623, -16069},
238 {15137, 6270, -6270, -15137, -15137, -6270, 6270, 15137},
239 {13623, -3196, -16069, -9102, 9102, 16069, 3196, -13623},
240 {11585, -11585, -11585, 11585, 11585, -11585, -11585, 11585},
241 {9102, -16069, 3196, 13623, -13623, -3196, 16069, -9102},
242 {6270, -15137, 15137, -6270, -6270, 15137, -15137, 6270},
243 {3196, -9102, 13623, -16069, 16069, -13623, 9102, -3196},
244};
245
255static const int32_t s_dct_w_q14[8] = {
256 /* sqrt(2/8) = 0.5; * (1/sqrt(2)) for k=0 -> 0.3536068; * 16384 = 5793. */
257 5793,
258 /* sqrt(2/8) = 0.5; * 16384 = 8192 (k=1..7). */
259 8192,
260 8192,
261 8192,
262 8192,
263 8192,
264 8192,
265 8192,
266};
267
268/* ------------------------------------------------------------------ */
269/* Inline byte / pixel helpers */
270/* ------------------------------------------------------------------ */
271
294RA8_INTERNAL static inline uint8_t internal_clamp_u8(int32_t v)
295{
296 if (v < 0) {
297 return 0U;
298 }
299 if (v > (int32_t)k_ra8_jpeg_pixel_max) {
300 return (uint8_t)k_ra8_jpeg_pixel_max;
301 }
302 return (uint8_t)v;
303}
304
325RA8_INTERNAL static inline uint16_t internal_read_be16(const uint8_t* p)
326{
327 return (uint16_t)(((uint16_t)p[0] << k_ra8_jpeg_byte_shift) | (uint16_t)p[1]);
328}
329
348RA8_INTERNAL static inline void internal_write_be16(uint8_t* p, uint16_t v)
349{
350 p[0] = (uint8_t)(v >> k_ra8_jpeg_byte_shift);
351 p[1] = (uint8_t)(v & k_jpeg_byte_mask);
352}
353
354/* ------------------------------------------------------------------ */
355/* Shared entropy / DSP primitive types */
356/* ------------------------------------------------------------------ */
357
367typedef struct {
368 const uint8_t* buf;
369 uint32_t len;
370 uint32_t pos;
371 uint32_t acc;
372 uint8_t nbits;
373 uint8_t had_eoi;
375
395
396/* ------------------------------------------------------------------ */
397/* Decode-primitive prototypes (defined in ra8_jpeg_sw.c, called from */
398/* the parser unit ra8_jpeg_sw_decode.c) */
399/* ------------------------------------------------------------------ */
400
425
447
472
498RA8_PRIV int32_t priv_jpeg_sw_huff_extend(int32_t v, uint8_t n);
499
518RA8_PRIV void priv_jpeg_sw_idct8x8(int32_t* block);
519
544RA8_PRIV void priv_jpeg_sw_ycc_to_rgb(int32_t y,
545 int32_t cb,
546 int32_t cr,
547 uint8_t* out_r,
548 uint8_t* out_g,
549 uint8_t* out_b);
550
551/* ------------------------------------------------------------------ */
552/* Decoder context + marker/scan primitives (defined in */
553/* ra8_jpeg_sw_decode.c, shared with the streaming driver */
554/* ra8_jpeg_sw_stream.c) */
555/* ------------------------------------------------------------------ */
556
595
605
626
646
666
688
709
735 uint8_t ci,
736 int32_t* outblk);
737
752RA8_PRIV void priv_jpeg_sw_idct_into(int32_t* coeffs, uint8_t* tile);
753
776 uint8_t* y_tile,
777 uint16_t mcu_w_px);
778
801 uint8_t* cb_tile,
802 uint8_t* cr_tile);
803
829 bool* got_sof,
Annotation-attribute framework macros for ra8-firmware.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Error Code Definitions for ra8-firmware.
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
ra8_jpeg_marker_t
JPEG segment marker bytes (T.81 sec B.1.1.3 "Marker assignments").
@ k_ra8_jpeg_marker_sof1
Extended sequential DCT.
@ k_ra8_jpeg_marker_dht
Define Huffman table.
@ k_ra8_jpeg_marker_rst7
Restart 7.
@ k_ra8_jpeg_marker_sof_hi
Last SOF marker code.
@ k_ra8_jpeg_marker_eoi
End of image.
@ k_ra8_jpeg_marker_sof0
Baseline DCT, Huffman.
@ k_ra8_jpeg_marker_dac
DAC marker (skipped).
@ k_ra8_jpeg_marker_high
Marker high-byte mask.
@ k_ra8_jpeg_marker_rst0
Restart 0.
@ k_ra8_jpeg_marker_sof3
Lossless.
@ k_ra8_jpeg_marker_sof_lo
First SOF marker code.
@ k_ra8_jpeg_marker_soi
Start of image.
@ k_ra8_jpeg_marker_dri
Define restart interval.
@ k_ra8_jpeg_marker_app0
JFIF APP0.
@ k_ra8_jpeg_marker_pad
Stuffed byte (data 0xFF).
@ k_ra8_jpeg_marker_dqt
Define quantization table.
@ k_ra8_jpeg_marker_sos
Start of scan.
@ k_ra8_jpeg_marker_sof2
Progressive DCT.
@ k_ra8_jpeg_marker_com
Comment.
void priv_jpeg_sw_idct8x8(int32_t *block)
Full 8x8 inverse DCT, in place.
ra8_jpeg_shift_t
Shift amounts and signed-cast helpers.
@ k_ra8_jpeg_signbit_pos16
RA8 JPEG signbit pos16.
@ k_ra8_jpeg_level_offset
RA8 JPEG level offset.
@ k_ra8_jpeg_nibble_shift
RA8 JPEG nibble shift.
@ k_ra8_jpeg_nibble_mask
RA8 JPEG nibble mask.
@ k_ra8_jpeg_byte_shift
RA8 JPEG byte shift.
@ k_ra8_jpeg_dht_ac_class
RA8 JPEG dht ac class.
@ k_ra8_jpeg_zrl_runlen
Zero-Run-Length symbol skip.
@ k_ra8_jpeg_eob_runlen
F0 high-nibble RRRR mask.
@ k_ra8_jpeg_quality_pivot
RA8 JPEG quality pivot.
@ k_ra8_jpeg_be_byte_mask
Low-byte mask.
@ k_ra8_jpeg_rgb_components
RA8 JPEG RGB components.
@ k_ra8_jpeg_c_sampling
SOF0 H/V byte for chroma.
@ k_ra8_jpeg_pixel_max
RA8 JPEG pixel maximum.
@ k_ra8_jpeg_y_sampling_420
SOF0 H/V byte for Y in 4:2:0.
@ k_ra8_jpeg_signbit_pos
RA8 JPEG signbit pos.
@ k_ra8_jpeg_yuv_shift
BT.601 fixed-point shift.
@ k_ra8_jpeg_huff_lengths
16 BITS-list slots.
@ k_ra8_jpeg_zrl_symbol
T.81 K.3.3 ZRL byte.
@ k_ra8_jpeg_dht_dc_class
RA8 JPEG dht dc class.
ra8_err_t priv_jpeg_sw_parse_sof0(ra8_jpeg_dec_ctx_t *d)
Parse the SOF0 frame header (T.81 sec B.2.2).
ra8_err_t priv_jpeg_sw_dispatch(ra8_jpeg_dec_ctx_t *d, bool *got_sof, ra8_jpeg_dec_marker_action_t *action)
Dispatch one JPEG marker segment in a decode driver loop.
static void internal_write_be16(uint8_t *p, uint16_t v)
Write a 16-bit big-endian word into p.
void priv_jpeg_sw_ycc_to_rgb(int32_t y, int32_t cb, int32_t cr, uint8_t *out_r, uint8_t *out_g, uint8_t *out_b)
Convert a YCbCr triple to RGB (BT.601, fixed-point).
static uint16_t internal_read_be16(const uint8_t *p)
Read a 16-bit big-endian word from p.
ra8_err_t priv_jpeg_sw_mcu_y(ra8_jpeg_dec_ctx_t *d, ra8_jpeg_bitreader_t *br, uint8_t *y_tile, uint16_t mcu_w_px)
Decode the hmax*vmax luma blocks of one MCU into the Y tile.
jpeg_ycc_coeff_t
Q15 BT.601 YCbCr->RGB coefficients (== Q16 / 2).
@ k_cr_r_q15
1.40200 / 2 * 2^15.
@ k_cb_g_q15
Cb g q15.
@ k_cb_b_q15
1.77200 / 2 * 2^15.
@ k_cr_g_q15
Cr g q15.
int32_t priv_jpeg_sw_htab_decode(ra8_jpeg_bitreader_t *br, const ra8_jpeg_htab_t *h)
Decode one Huffman symbol from br using table h.
int32_t priv_jpeg_sw_br_get_bits(ra8_jpeg_bitreader_t *br, uint8_t n)
Pop n bits MSB-first; returns -1 on underflow.
jpeg_dsp_shift_t
Fixed-point IDCT / YCbCr shift amounts.
@ k_jpeg_idct_p2_bias_sh
JPEG idct p2 bias sh.
@ k_jpeg_idct_p1_bias_sh
JPEG idct p1 bias sh.
@ k_jpeg_ycc_q15_shift
JPEG ycc q15 shift.
@ k_jpeg_q14_shift
JPEG q14 shift.
@ k_jpeg_idct_p1_sh
JPEG idct p1 sh.
jpeg_enc_t
JPEG encoder quality scaling and segment field values.
@ k_jpeg_q_round_bias
JPEG q round bias.
@ k_jpeg_sos_seg_len
JPEG sos seg length.
@ k_jpeg_q_scale_high
JPEG q scale high.
@ k_jpeg_spectral_end
JPEG spectral end.
@ k_jpeg_q_percent
JPEG q percent.
@ k_jpeg_sof_seg_len
JPEG sof seg length.
@ k_jpeg_q_scale_low
JPEG q scale low.
void priv_jpeg_sw_htab_build(ra8_jpeg_htab_t *h)
Build canonical code/size and mincode/maxcode tables.
ra8_err_t priv_jpeg_sw_parse_dht(ra8_jpeg_dec_ctx_t *d)
Parse a DHT segment (T.81 sec B.2.4.2) and build its tables.
jpeg_misc_t
Misc JPEG masks / offsets.
@ k_jpeg_nibble_mask
JPEG nibble mask.
@ k_jpeg_byte_mask
JPEG byte mask.
@ k_jpeg_zrl_symbol
JPEG zrl symbol.
@ k_jpeg_mcu_align
JPEG mcu align.
@ k_jpeg_sof_dims_off
JPEG sof dims off.
@ k_jpeg_reservoir_lo
JPEG reservoir lo.
ra8_jpeg_const_t
Sizes and indices used throughout the codec.
@ k_ra8_jpeg_app0_len
APP0 (JFIF) segment length.
@ k_ra8_jpeg_huff_ids
Luma + chroma table id.
@ k_ra8_jpeg_dht_hdr
DHT TcTh byte size.
@ k_ra8_jpeg_garbage_len
Garbage-input cutoff.
@ k_ra8_jpeg_enc_max_w
Encoder max image width (px).
@ k_ra8_jpeg_eob_band_max
Last AC zig-zag index.
@ k_ra8_jpeg_mcu_max_dim
4:2:0 MCU is 16x16 luma px.
@ k_ra8_jpeg_min_jpeg_len
Smallest plausible stream.
@ k_ra8_jpeg_block_size
Coefficients per block.
@ k_ra8_jpeg_sos_len
SOS segment length we emit.
@ k_ra8_jpeg_block_dim
8x8 DCT block edge.
@ k_ra8_jpeg_sof0_min_len
Min SOF0 segment length.
@ k_ra8_jpeg_huff_classes
DC + AC.
@ k_ra8_jpeg_marker_byte
RA8 JPEG marker byte.
@ k_ra8_jpeg_max_comps
YCbCr or grayscale only.
@ k_ra8_jpeg_mcu_align
MCU alignment mask.
@ k_ra8_jpeg_sof0_hdr_len
SOF0 segment length we emit.
@ k_ra8_jpeg_quant_tabs
One luma + one chroma table.
@ k_ra8_jpeg_huff_max
Max symbols per Huffman table.
int32_t priv_jpeg_sw_huff_extend(int32_t v, uint8_t n)
Decode a signed n-bit DCT coefficient (T.81 F.1.2.1.3).
jpeg_enc_field_t
JPEG component sampling / table-selector bytes (positional).
@ k_jpeg_sos_sel_chroma
JPEG sos sel chroma.
@ k_jpeg_dht_ac_chroma
JPEG dht ac chroma.
@ k_jpeg_samp_2x2
JPEG samp 2x2.
@ k_jpeg_samp_1x1
JPEG samp 1x1.
static const int32_t s_dct_cos_q14[8][8]
Cosine table cos((2n+1)*k*pi/16) * 2^14, k = row, n = col.
static const uint8_t s_zigzag[64]
Zig-zag de-interleave order (T.81 Figure 5).
void priv_jpeg_sw_idct_into(int32_t *coeffs, uint8_t *tile)
IDCT a coefficient block and emit level-shifted 8-bit samples.
ra8_jpeg_color_coef_t
BT.601 RGB<->YCbCr fixed-point coefficients (Q16).
@ k_ra8_jpeg_cbr
RA8 JPEG CBR.
@ k_ra8_jpeg_crb
RA8 JPEG crb.
@ k_ra8_jpeg_cr_r
1.40200.
@ k_ra8_jpeg_crr
RA8 JPEG crr.
@ k_ra8_jpeg_yg
0.58700 * 65536.
@ k_ra8_jpeg_cb_b
1.77200.
@ k_ra8_jpeg_cbg
RA8 JPEG cbg.
@ k_ra8_jpeg_cb_g
RA8 JPEG cb g.
@ k_ra8_jpeg_cr_g
RA8 JPEG cr g.
@ k_ra8_jpeg_yr
0.29900 * 65536.
@ k_ra8_jpeg_yb
0.11400 * 65536.
@ k_ra8_jpeg_cbb
RA8 JPEG cbb.
@ k_ra8_jpeg_crg
RA8 JPEG crg.
ra8_err_t priv_jpeg_sw_block(ra8_jpeg_dec_ctx_t *d, ra8_jpeg_bitreader_t *br, uint8_t ci, int32_t *outblk)
Entropy-decode one 8x8 block of dequantized coefficients.
ra8_err_t priv_jpeg_sw_skip_segment(ra8_jpeg_dec_ctx_t *d)
Skip an unrecognized variable-length segment.
ra8_err_t priv_jpeg_sw_parse_dqt(ra8_jpeg_dec_ctx_t *d)
Parse a DQT segment (T.81 sec B.2.4.1) into d->qtab.
static uint8_t internal_clamp_u8(int32_t v)
Clamp v to the unsigned 8-bit pixel range.
ra8_err_t priv_jpeg_sw_mcu_chroma(ra8_jpeg_dec_ctx_t *d, ra8_jpeg_bitreader_t *br, uint8_t *cb_tile, uint8_t *cr_tile)
Decode the Cb and Cr 8x8 blocks of one MCU.
ra8_err_t priv_jpeg_sw_parse_sos(ra8_jpeg_dec_ctx_t *d)
Parse the SOS scan header (T.81 sec B.2.3) selectors.
jpeg_marker_range_t
JPEG SOF marker range and stuffing markers.
@ k_jpeg_marker_ff00
JPEG marker ff00.
@ k_jpeg_marker_jpg
JPEG marker jpg.
@ k_jpeg_marker_sof1
JPEG marker sof1.
@ k_jpeg_marker_sof_hi
JPEG marker sof hi.
@ k_jpeg_marker_sof_lo
JPEG marker sof lo.
static const int32_t s_dct_w_q14[8]
sqrt(2/N)*C(k) DCT normalization weights, Q14.
ra8_jpeg_dec_marker_action_t
Result of priv_jpeg_sw_dispatch(): what the driver does next.
@ k_ra8_jpeg_dec_eoi
EOI reached; the driver should stop with an error.
@ k_ra8_jpeg_dec_continue
Keep scanning markers.
@ k_ra8_jpeg_dec_scan
SOS reached; the driver should run its scan loop.
Big-endian bit reader over an entropy-coded segment.
uint32_t acc
Bit accumulator.
uint32_t len
Total bytes.
uint8_t nbits
Bits valid in acc (0..32).
const uint8_t * buf
Byte stream.
uint32_t pos
Read cursor.
uint8_t had_eoi
Set when an end marker is consumed.
Decoder state shared by the whole-buffer and streaming drivers.
int32_t comp_dc_pred[k_ra8_jpeg_max_comps]
DC predictors (scan state).
ra8_jpeg_htab_t hac[k_ra8_jpeg_huff_ids]
AC Huffman tables.
uint16_t qtab[k_ra8_jpeg_quant_tabs][k_ra8_jpeg_block_size]
Dequant tables.
uint8_t comp_qid[k_ra8_jpeg_max_comps]
Quant-table selector.
uint8_t comp_dc_id[k_ra8_jpeg_max_comps]
DC Huffman selector.
uint16_t height
Image height from SOF0, pixels.
uint8_t vmax
Max vertical sampling factor.
uint32_t src_len
Length of src in bytes.
uint8_t comp_v[k_ra8_jpeg_max_comps]
Vertical sampling.
uint32_t cursor
Parse cursor into src.
uint8_t ncomp
1 grayscale, 3 YCbCr.
ra8_jpeg_htab_t hdc[k_ra8_jpeg_huff_ids]
DC Huffman tables.
uint16_t width
Image width from SOF0, pixels.
uint8_t comp_h[k_ra8_jpeg_max_comps]
Horizontal sampling.
uint8_t comp_id[k_ra8_jpeg_max_comps]
Component identifiers.
uint8_t comp_ac_id[k_ra8_jpeg_max_comps]
AC Huffman selector.
uint8_t hmax
Max horizontal sampling factor.
const uint8_t * src
Byte slice the parsers read from.
Decoded Huffman table – 256 entries indexed by symbol order.
uint16_t valptr[k_ra8_jpeg_huff_lengths]
Valptr.
uint16_t huffcode[k_ra8_jpeg_huff_max]
Code per symbol.
int32_t maxcode[k_ra8_jpeg_huff_lengths]
Maxcode.
int32_t mincode[k_ra8_jpeg_huff_lengths]
Mincode.
uint8_t vals[k_ra8_jpeg_huff_max]
Symbol order.
uint8_t bits[k_ra8_jpeg_huff_lengths]
BITS list.
uint8_t huffsize[k_ra8_jpeg_huff_max]
Code length.