47static const char*
s_tag =
"JPEG_SW";
55 16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57,
56 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56, 68, 109, 103, 77, 24, 35, 55, 64,
57 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99,
62 17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99, 99, 99,
63 99, 99, 47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
64 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
231 uint32_t a = (v < 0) ? (uint32_t)(-v) : (uint32_t)v;
264 int32_t q = (int32_t)qv;
269 int32_t a = -v + (q >> 1);
272 return (v + (q >> 1)) / q;
302 const uint16_t* ac_codes,
303 const uint8_t* ac_sizes)
316 run = (uint8_t)(run - 16U);
321 int32_t v = (z[k] < 0) ? (z[k] - 1) : z[k];
356 const int32_t* spatial,
359 const uint16_t* dc_codes,
360 const uint8_t* dc_sizes,
361 const uint16_t* ac_codes,
362 const uint8_t* ac_sizes)
377 int32_t diff = z[0] - e->
dc_pred[comp_idx];
382 int32_t v = (diff < 0) ? (diff - 1) : diff;
418 for (uint16_t i = 0U; i < n; i++) {
420 int32_t r = (int32_t)rgb[px];
421 int32_t g = (int32_t)rgb[px + 1U];
422 int32_t b = (int32_t)rgb[px + 2U];
467 uint16_t sy = (uint16_t)(y0 + r);
469 sy = (uint16_t)(plane_h - 1U);
472 uint16_t sx = (uint16_t)(x0 + c);
474 sx = (uint16_t)(plane_w - 1U);
516 for (uint8_t dy = 0U; dy < 2U; dy++) {
517 for (uint8_t dx = 0U; dx < 2U; dx++) {
518 uint16_t yy = (uint16_t)(sy + dy);
519 uint16_t xx = (uint16_t)(sx + dx);
521 yy = (uint16_t)(plane_h - 1U);
524 xx = (uint16_t)(plane_w - 1U);
526 s += cplane[((uint32_t)yy * plane_w) + xx];
530 return s / (int32_t)n;
564 uint16_t sy = (uint16_t)(y0 + (r * 2U));
565 uint16_t sx = (uint16_t)(x0 + (c * 2U));
611 for (uint8_t r = 0U; r < 16U; r++) {
612 uint16_t src_y = mby + r;
614 src_y = (uint16_t)(h - 1U);
616 for (uint16_t c = 0U; c < pad_w; c++) {
619 src_x = (uint16_t)(w - 1U);
624 tmp_rgb[didx] = rgb[sidx + 0U];
625 tmp_rgb[didx + 1U] = rgb[sidx + 1U];
626 tmp_rgb[didx + 2U] = rgb[sidx + 2U];
630 &y_strip[(
size_t)r * pad_w],
631 &cb_strip[(
size_t)r * pad_w],
632 &cr_strip[(
size_t)r * pad_w]);
662 const int32_t* y_strip,
663 const int32_t* cb_strip,
664 const int32_t* cr_strip)
666 for (uint16_t mbx = 0U; mbx < pad_w; mbx = (uint16_t)(mbx + 16U)) {
669 for (uint8_t by = 0U; by < 2U; by++) {
670 for (uint8_t bx = 0U; bx < 2U; bx++) {
674 (uint16_t)(mbx + (bx * 8U)),
756 for (uint16_t mby = 0U; mby < pad_h; mby = (uint16_t)(mby + 16U)) {
783 uint32_t out_buf_len,
790 if (width == 0U || height == 0U) {
804 e->
cap = out_buf_len;
static const char * s_tag
Logging / check tag.
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_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_invalid_arg
Invalid function argument.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
Pure-software baseline JPEG (ISO/IEC 10918-1 / ITU-T T.81) codec.
@ k_ra8_jpeg_sw_quality_min
Lowest legal quality.
@ k_ra8_jpeg_sw_quality_max
Largest legal quality.
static int32_t internal_enc_avg_2x2(const int32_t *cplane, uint16_t plane_w, uint16_t plane_h, uint16_t sx, uint16_t sy)
Average the 2x2 chroma neighbourhood at (sx, sy), clamped.
static void internal_enc_sample_y_block(const int32_t *yplane, uint16_t plane_w, uint16_t plane_h, uint16_t x0, uint16_t y0, int32_t *out)
Sample one 8x8 luma block from the Y plane (clamp-to-edge).
static int32_t internal_enc_quantize(int32_t v, uint8_t qv)
Quantize one DCT coefficient with symmetric rounding.
static void internal_enc_scale_qtab(const uint8_t *base, uint8_t *dst, uint16_t scale)
Scale a base quantization table by scale/100, clamped to 1..255.
static void internal_enc_rgb_to_ycc_row(const uint8_t *rgb, uint16_t n, int32_t *y, int32_t *cb, int32_t *cr)
Convert one row of RGB into Y, Cb, Cr samples (BT.601 Q16).
static void internal_enc_block_ac(ra8_jpeg_enc_ctx_t *e, const int32_t *z, const uint16_t *ac_codes, const uint8_t *ac_sizes)
Emit the 63 AC coefficients of one quantized block.
static uint16_t internal_enc_quality_scale(uint8_t q)
Compute the IJG quality-scale factor.
static void internal_enc_convert_strip_to_ycc(const uint8_t *rgb, uint16_t w, uint16_t h, uint16_t pad_w, uint16_t mby, int32_t *y_strip, int32_t *cb_strip, int32_t *cr_strip, uint8_t *tmp_rgb)
Convert 16 source RGB rows into Y / Cb / Cr 16-row strips.
static const uint8_t s_quant_chroma[64]
T.81 Annex K.1 chroma quantization table.
static void internal_fdct8x8(int32_t *block)
2-D forward DCT of one 8x8 block, in place.
static void internal_fwd_dct_1d_norm(const int32_t *in, int32_t *out)
1-D forward DCT pass with JPEG normalization folded in.
static uint8_t internal_enc_bits_needed(int32_t v)
Compute the number of significant-magnitude bits of v.
static void internal_enc_sample_c_block_420(const int32_t *cplane, uint16_t plane_w, uint16_t plane_h, uint16_t x0, uint16_t y0, int32_t *out)
Build one 8x8 chroma block by 2x2 averaging from a 16x16 region.
static void internal_enc_block(ra8_jpeg_enc_ctx_t *e, const int32_t *spatial, const uint8_t *qtab, uint8_t comp_idx, const uint16_t *dc_codes, const uint8_t *dc_sizes, const uint16_t *ac_codes, const uint8_t *ac_sizes)
Encode one 8x8 block of source samples.
static const uint8_t s_quant_luma[64]
T.81 Annex K.1 luma quantization table.
static void internal_enc_encode_mcu_row(ra8_jpeg_enc_ctx_t *e, uint16_t pad_w, const int32_t *y_strip, const int32_t *cb_strip, const int32_t *cr_strip)
Encode every 16x16 MCU in a 16-row YCbCr strip.
static ra8_err_t internal_enc_run(ra8_jpeg_enc_ctx_t *e, const uint8_t *rgb, uint16_t w, uint16_t h, uint8_t quality)
Top-level encode driver – emits the full JFIF byte stream.
ra8_err_t ra8_jpeg_sw_encode(const uint8_t *rgb_buf, uint16_t width, uint16_t height, uint8_t quality, uint8_t *out_buf, uint32_t out_buf_len, uint32_t *out_len)
Encode a packed RGB888 frame as a baseline JPEG.
void priv_jpeg_sw_enc_put_bits(ra8_jpeg_enc_ctx_t *e, uint32_t code, uint8_t n)
Implementation of priv_jpeg_sw_enc_put_bits() – T.81 F.1.2.3 stuffing.
void priv_jpeg_sw_enc_headers(ra8_jpeg_enc_ctx_t *e, uint16_t w, uint16_t h, uint16_t total_dc_l, uint16_t total_ac_l, uint16_t total_dc_c, uint16_t total_ac_c)
Implementation of priv_jpeg_sw_enc_headers() – JFIF 1.1 segment order.
void priv_jpeg_sw_enc_flush_bits(ra8_jpeg_enc_ctx_t *e)
Implementation of priv_jpeg_sw_enc_flush_bits() – 1-fill pad.
void priv_jpeg_sw_enc_build_huff(ra8_jpeg_enc_ctx_t *e, uint16_t *total_dc_l, uint16_t *total_ac_l, uint16_t *total_dc_c, uint16_t *total_ac_c)
Implementation of priv_jpeg_sw_enc_build_huff() – four K.3.3 LUT builds.
void priv_jpeg_sw_enc_emit_u16(ra8_jpeg_enc_ctx_t *e, uint16_t v)
Implementation of priv_jpeg_sw_enc_emit_u16() – big-endian byte pair.
Module-private declarations shared by the two encoder translation units of the software JPEG codec.
Module-private declarations shared across the software JPEGcodec translation units.
@ k_ra8_jpeg_marker_eoi
End of image.
@ k_ra8_jpeg_level_offset
RA8 JPEG level offset.
@ k_ra8_jpeg_nibble_shift
RA8 JPEG nibble shift.
@ k_ra8_jpeg_quality_pivot
RA8 JPEG quality pivot.
@ k_ra8_jpeg_rgb_components
RA8 JPEG RGB components.
@ k_ra8_jpeg_pixel_max
RA8 JPEG pixel maximum.
@ k_ra8_jpeg_yuv_shift
BT.601 fixed-point shift.
@ k_jpeg_idct_p1_bias_sh
JPEG idct p1 bias sh.
@ k_jpeg_idct_p1_sh
JPEG idct p1 sh.
@ k_jpeg_q_round_bias
JPEG q round bias.
@ k_jpeg_q_scale_high
JPEG q scale high.
@ k_jpeg_q_percent
JPEG q percent.
@ k_jpeg_q_scale_low
JPEG q scale low.
@ k_jpeg_zrl_symbol
JPEG zrl symbol.
@ k_jpeg_mcu_align
JPEG mcu align.
@ k_ra8_jpeg_enc_max_w
Encoder max image width (px).
@ k_ra8_jpeg_block_size
Coefficients per block.
@ k_ra8_jpeg_block_dim
8x8 DCT block edge.
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).
@ k_ra8_jpeg_cbr
RA8 JPEG CBR.
@ k_ra8_jpeg_crb
RA8 JPEG crb.
@ k_ra8_jpeg_crr
RA8 JPEG crr.
@ k_ra8_jpeg_yg
0.58700 * 65536.
@ k_ra8_jpeg_cbg
RA8 JPEG cbg.
@ 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.
static const int32_t s_dct_w_q14[8]
sqrt(2/N)*C(k) DCT normalization weights, Q14.
Lightweight Logging Interface for ra8-firmware.
Encoder state – buffer cursor, bit accumulator, scaled quant tables, Huffman LUTs and DC predictors.
int32_t dc_pred[k_ra8_jpeg_max_comps]
Per-component DC predictors.
bool overflow
Set on capacity exhaustion.
uint8_t size_ac_l[k_ra8_jpeg_huff_max]
Luma AC code lengths.
uint16_t code_dc_c[k_ra8_jpeg_huff_max]
Chroma DC Huffman codes.
uint8_t size_dc_l[k_ra8_jpeg_huff_max]
Luma DC code lengths.
uint8_t size_ac_c[k_ra8_jpeg_huff_max]
Chroma AC code lengths.
uint8_t * dst
Destination JPEG byte buffer.
uint32_t pos
Write cursor into dst.
uint16_t code_ac_c[k_ra8_jpeg_huff_max]
Chroma AC Huffman codes.
uint16_t code_dc_l[k_ra8_jpeg_huff_max]
Luma DC Huffman codes.
uint8_t qy[k_ra8_jpeg_block_size]
Scaled luma quant table.
uint16_t code_ac_l[k_ra8_jpeg_huff_max]
Luma AC Huffman codes.
uint8_t size_dc_c[k_ra8_jpeg_huff_max]
Chroma DC code lengths.
uint8_t qc[k_ra8_jpeg_block_size]
Scaled chroma quant table.
uint32_t cap
Capacity of dst in bytes.