ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_jpeg_sw_encode_emit.c File Reference

Software JPEG encoder: byte/bit emitters, Annex K.3.3 tables and JFIF header-segment writers. More...

#include <stdint.h>
#include "ra8_attributes.h"
#include "ra8_err.h"
#include "ra8_jpeg_sw.h"
#include "ra8_jpeg_sw_encode_internal.h"
#include "ra8_jpeg_sw_internal.h"
Include dependency graph for ra8_jpeg_sw_encode_emit.c:

Go to the source code of this file.

Functions

static void internal_enc_emit_u8 (ra8_jpeg_enc_ctx_t *e, uint8_t b)
 Append one byte; sets overflow on capacity exhaustion.
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.
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_flush_bits (ra8_jpeg_enc_ctx_t *e)
 Implementation of priv_jpeg_sw_enc_flush_bits() – 1-fill pad.
static void internal_enc_build_codes (const uint8_t *bits, const uint8_t *vals, uint16_t *codes, uint8_t *sizes, uint16_t total)
 Build the canonical-code table from BITS+HUFFVAL.
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.
static void internal_enc_emit_dqt (ra8_jpeg_enc_ctx_t *e)
 Emit the DQT segment for both luma and chroma tables.
static void internal_enc_emit_dht_one (ra8_jpeg_enc_ctx_t *e, uint8_t tc_th, const uint8_t *bits, const uint8_t *vals, uint16_t total)
 Emit one DHT segment (T.81 sec B.2.4.2).
static void internal_enc_emit_app0_jfif (ra8_jpeg_enc_ctx_t *e)
 Emit the SOI marker plus the 16-byte APP0 JFIF header.
static void internal_enc_emit_sof0 (ra8_jpeg_enc_ctx_t *e, uint16_t w, uint16_t h)
 Emit the SOF0 frame header for a 3-component 4:2:0 stream.
static void internal_enc_emit_sos (ra8_jpeg_enc_ctx_t *e)
 Emit the SOS scan header (T.81 sec B.2.3).
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.

Variables

static const uint8_t s_hbits_dc_luma [16]
 K.3.3 luma DC BITS (number of codes of each length 1..16).
static const uint8_t s_hval_dc_luma [12]
 K.3.3 luma DC HUFFVAL (12 symbols).
static const uint8_t s_hbits_dc_chroma [16]
 K.3.3 chroma DC BITS.
static const uint8_t s_hval_dc_chroma [12]
 K.3.3 chroma DC HUFFVAL.
static const uint8_t s_hbits_ac_luma [16]
 K.3.3 luma AC BITS.
static const uint8_t s_hval_ac_luma [162]
 K.3.3 luma AC HUFFVAL (162 symbols).
static const uint8_t s_hbits_ac_chroma [16]
 K.3.3 chroma AC BITS.
static const uint8_t s_hval_ac_chroma [162]
 K.3.3 chroma AC HUFFVAL.

Detailed Description

Software JPEG encoder: byte/bit emitters, Annex K.3.3 tables and JFIF header-segment writers.

Tag
[Ring 4 / Domain] {World: NS}

Emitter half of the software JPEG encoder: the bounds-checked byte emitter, the MSB-first bit packer with T.81 sec F.1.2.3 stuffing, the canonical Huffman code build over the T.81 Annex K.3.3 reference tables, and the SOI/APP0/DQT/SOF0/DHT/SOS header-segment writers. The pixel pipeline (RGB->YCbCr conversion, sampling, forward DCT, quantization, per-block emission) and the ra8_jpeg_sw_encode() public API live in ra8_jpeg_sw_encode.c; the symbols shared between the two units are declared in ra8_jpeg_sw_encode_internal.h.

The Huffman BITS/HUFFVAL tables in this file are taken verbatim from ITU-T T.81 / ISO 10918-1 Annex K.3.3.

Spec citations are tagged T.81 sec X.Y "..." and refer to ITU-T Recommendation T.81 (1992) | ISO/IEC 10918-1.

Since
0.1.0

Definition in file ra8_jpeg_sw_encode_emit.c.

Function Documentation

◆ internal_enc_build_codes()

void internal_enc_build_codes ( const uint8_t * bits,
const uint8_t * vals,
uint16_t * codes,
uint8_t * sizes,
uint16_t total )
static

Build the canonical-code table from BITS+HUFFVAL.

T.81 Annex C "Generation of size table" + "Generation of code table": expands the BITS list into per-symbol code lengths, assigns consecutive canonical codes within each length, and scatters both into symbol-indexed LUTs so the block encoder can look codes up by symbol value directly.

Parameters
[in]bits16-entry BITS list (codes per length 1..16).
[in]valsHUFFVAL symbol list (total entries).
[out]codes256-entry code LUT indexed by symbol value.
[out]sizes256-entry code-length LUT indexed by symbol value.
[in]totalNumber of symbols in vals.
Precondition
bits/vals come from the static Annex K.3.3 tables.
total equals the sum of bits[0..15] (<= 256).
Postcondition
codes[v]/sizes[v] are valid for every symbol v in vals.
LUT entries for absent symbols are untouched.
Note
Internal helper; not thread-safe.
Since
0.1.0

Definition at line 277 of file ra8_jpeg_sw_encode_emit.c.

References k_ra8_jpeg_huff_lengths, k_ra8_jpeg_huff_max, and RA8_INTERNAL.

Referenced by priv_jpeg_sw_enc_build_huff().

◆ internal_enc_emit_app0_jfif()

void internal_enc_emit_app0_jfif ( ra8_jpeg_enc_ctx_t * e)
static

Emit the SOI marker plus the 16-byte APP0 JFIF header.

Writes the JFIF 1.1 APP0 segment with units=0, aspect ratio 1:1, no embedded thumbnail. The byte sequence is identical to what the pre-split monolithic encoder produced.

Parameters
[in,out]eEncoder context (cursor advances).
Precondition
e is non-NULL with a writable dst buffer.
e->pos is 0 (SOI opens the stream).
Postcondition
e->pos advanced by 22 bytes (SOI + APP0 header + payload).
Byte stream matches the JFIF 1.1 APP0 layout.
Note
Internal helper; not thread-safe.
Since
0.1.0

Definition at line 446 of file ra8_jpeg_sw_encode_emit.c.

References internal_enc_emit_u8(), k_ra8_jpeg_marker_app0, k_ra8_jpeg_marker_soi, priv_jpeg_sw_enc_emit_u16(), and RA8_INTERNAL.

Referenced by priv_jpeg_sw_enc_headers().

◆ internal_enc_emit_dht_one()

void internal_enc_emit_dht_one ( ra8_jpeg_enc_ctx_t * e,
uint8_t tc_th,
const uint8_t * bits,
const uint8_t * vals,
uint16_t total )
static

Emit one DHT segment (T.81 sec B.2.4.2).

Serialises the TcTh selector, the 16-entry BITS list and the HUFFVAL symbol list of one reference Huffman table.

Parameters
[in,out]eEncoder context (cursor advances).
[in]tc_thTable class/id byte (0x00/0x10/0x01/0x11).
[in]bits16-entry BITS list.
[in]valsHUFFVAL list (total entries).
[in]totalSymbol count for the segment length field.
Precondition
bits/vals are the static Annex K.3.3 tables.
total matches the sum of bits[0..15].
Postcondition
One complete DHT segment appended to the stream.
e->pos advanced by the full segment length.
Note
Internal helper; not thread-safe.
Since
0.1.0

Definition at line 411 of file ra8_jpeg_sw_encode_emit.c.

References internal_enc_emit_u8(), k_ra8_jpeg_huff_lengths, k_ra8_jpeg_marker_dht, priv_jpeg_sw_enc_emit_u16(), and RA8_INTERNAL.

Referenced by priv_jpeg_sw_enc_headers().

◆ internal_enc_emit_dqt()

void internal_enc_emit_dqt ( ra8_jpeg_enc_ctx_t * e)
static

Emit the DQT segment for both luma and chroma tables.

One DQT marker carrying two 8-bit-precision tables (PqTq 0,0 then 0,1), each serialised in zig-zag order per T.81 sec B.2.4.1.

Parameters
[in,out]eEncoder context (cursor advances).
Precondition
e->qy/e->qc hold the scaled quantization tables.
e is non-NULL (module-internal call chain).
Postcondition
Both tables emitted inside a single DQT segment.
e->pos advanced by the full segment length.
Note
Internal helper; not thread-safe.
Since
0.1.0

Definition at line 377 of file ra8_jpeg_sw_encode_emit.c.

References internal_enc_emit_u8(), k_ra8_jpeg_block_size, k_ra8_jpeg_marker_dqt, priv_jpeg_sw_enc_emit_u16(), ra8_jpeg_enc_ctx_t::qc, ra8_jpeg_enc_ctx_t::qy, RA8_INTERNAL, and s_zigzag.

Referenced by priv_jpeg_sw_enc_headers().

◆ internal_enc_emit_sof0()

void internal_enc_emit_sof0 ( ra8_jpeg_enc_ctx_t * e,
uint16_t w,
uint16_t h )
static

Emit the SOF0 frame header for a 3-component 4:2:0 stream.

T.81 sec B.2.2: 8-bit precision, the image dimensions, and the three component records (Y 2x2 sampling on quant table 0; Cb/Cr 1x1 on quant table 1).

Parameters
[in,out]eEncoder context (cursor advances).
[in]wImage width in pixels.
[in]hImage height in pixels.
Precondition
e is non-NULL (module-internal call chain).
w and h are non-zero (public API validated them).
Postcondition
One complete SOF0 segment appended to the stream.
e->pos advanced by the full segment length.
Note
Internal helper; not thread-safe.
Since
0.1.0

Definition at line 487 of file ra8_jpeg_sw_encode_emit.c.

References internal_enc_emit_u8(), k_jpeg_samp_1x1, k_jpeg_samp_2x2, k_jpeg_sof_seg_len, k_ra8_jpeg_marker_sof0, priv_jpeg_sw_enc_emit_u16(), and RA8_INTERNAL.

Referenced by priv_jpeg_sw_enc_headers().

◆ internal_enc_emit_sos()

void internal_enc_emit_sos ( ra8_jpeg_enc_ctx_t * e)
static

Emit the SOS scan header (T.81 sec B.2.3).

Binds the three scan components to their Huffman selectors (Y dc=0/ac=0, Cb/Cr dc=1/ac=1) and writes the baseline spectral-range bytes (Ss=0, Se=63, AhAl=0).

Parameters
[in,out]eEncoder context (cursor advances).
Precondition
e is non-NULL (module-internal call chain).
The four DHT segments were already emitted.
Postcondition
One complete SOS segment appended to the stream.
The stream is positioned for entropy-coded data.
Note
Internal helper; not thread-safe.
Since
0.1.0

Definition at line 526 of file ra8_jpeg_sw_encode_emit.c.

References internal_enc_emit_u8(), k_jpeg_sos_seg_len, k_jpeg_sos_sel_chroma, k_jpeg_spectral_end, k_ra8_jpeg_marker_sos, priv_jpeg_sw_enc_emit_u16(), and RA8_INTERNAL.

Referenced by priv_jpeg_sw_enc_headers().

◆ internal_enc_emit_u8()

void internal_enc_emit_u8 ( ra8_jpeg_enc_ctx_t * e,
uint8_t b )
static

Append one byte; sets overflow on capacity exhaustion.

Single choke point for every byte that reaches the output buffer: once e->pos hits e->cap the byte is dropped and e->overflow latches so the driver can fail the encode with k_ra8_err_invalid_size after the fact.

Parameters
[in,out]eEncoder context (cursor advances).
[in]bByte to append.
Precondition
e is non-NULL (module-internal call chain).
e->dst holds e->cap writable bytes.
Postcondition
e->pos advanced by 1, or e->overflow set.
No write ever lands at or past e->cap.
Note
Internal helper; not thread-safe.
Since
0.1.0

Definition at line 205 of file ra8_jpeg_sw_encode_emit.c.

References ra8_jpeg_enc_ctx_t::cap, ra8_jpeg_enc_ctx_t::dst, ra8_jpeg_enc_ctx_t::overflow, ra8_jpeg_enc_ctx_t::pos, and RA8_INTERNAL.

Referenced by internal_enc_emit_app0_jfif(), internal_enc_emit_dht_one(), internal_enc_emit_dqt(), internal_enc_emit_sof0(), internal_enc_emit_sos(), priv_jpeg_sw_enc_emit_u16(), and priv_jpeg_sw_enc_put_bits().

◆ priv_jpeg_sw_enc_build_huff()

◆ priv_jpeg_sw_enc_emit_u16()

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.

Append a 16-bit big-endian word to the JPEG byte stream.

Definition at line 216 of file ra8_jpeg_sw_encode_emit.c.

References internal_enc_emit_u8(), k_jpeg_byte_mask, k_ra8_jpeg_byte_shift, and RA8_PRIV.

Referenced by internal_enc_emit_app0_jfif(), internal_enc_emit_dht_one(), internal_enc_emit_dqt(), internal_enc_emit_sof0(), internal_enc_emit_sos(), and internal_enc_run().

◆ priv_jpeg_sw_enc_flush_bits()

void priv_jpeg_sw_enc_flush_bits ( ra8_jpeg_enc_ctx_t * e)

Implementation of priv_jpeg_sw_enc_flush_bits() – 1-fill pad.

Flush any partial byte at the end of a scan with 1-fill.

Definition at line 238 of file ra8_jpeg_sw_encode_emit.c.

References ra8_jpeg_enc_ctx_t::bit_buf, ra8_jpeg_enc_ctx_t::bit_cnt, k_jpeg_byte_mask, k_ra8_jpeg_byte_shift, priv_jpeg_sw_enc_put_bits(), and RA8_PRIV.

Referenced by internal_enc_run().

◆ priv_jpeg_sw_enc_headers()

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 )

◆ priv_jpeg_sw_enc_put_bits()

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.

Push n bits MSB-first into the entropy stream with stuffing.

Definition at line 223 of file ra8_jpeg_sw_encode_emit.c.

References ra8_jpeg_enc_ctx_t::bit_buf, ra8_jpeg_enc_ctx_t::bit_cnt, internal_enc_emit_u8(), k_ra8_jpeg_byte_shift, k_ra8_jpeg_marker_byte, and RA8_PRIV.

Referenced by internal_enc_block(), internal_enc_block_ac(), and priv_jpeg_sw_enc_flush_bits().

Variable Documentation

◆ s_hbits_ac_chroma

const uint8_t s_hbits_ac_chroma[16]
static
Initial value:
= {
0,
2,
1,
2,
4,
4,
3,
4,
7,
5,
4,
4,
0,
1,
2,
0x77,
}

K.3.3 chroma AC BITS.

Definition at line 148 of file ra8_jpeg_sw_encode_emit.c.

Referenced by priv_jpeg_sw_enc_build_huff(), and priv_jpeg_sw_enc_headers().

◆ s_hbits_ac_luma

const uint8_t s_hbits_ac_luma[16]
static
Initial value:
= {
0,
2,
1,
3,
3,
2,
4,
3,
5,
5,
4,
4,
0,
0,
1,
0x7d,
}

K.3.3 luma AC BITS.

Definition at line 114 of file ra8_jpeg_sw_encode_emit.c.

Referenced by priv_jpeg_sw_enc_build_huff(), and priv_jpeg_sw_enc_headers().

◆ s_hbits_dc_chroma

const uint8_t s_hbits_dc_chroma[16]
static
Initial value:
= {
0,
3,
1,
1,
1,
1,
1,
1,
1,
1,
1,
0,
0,
0,
0,
0,
}

K.3.3 chroma DC BITS.

Definition at line 79 of file ra8_jpeg_sw_encode_emit.c.

Referenced by priv_jpeg_sw_enc_build_huff(), and priv_jpeg_sw_enc_headers().

◆ s_hbits_dc_luma

const uint8_t s_hbits_dc_luma[16]
static
Initial value:
= {
0,
1,
5,
1,
1,
1,
1,
1,
1,
0,
0,
0,
0,
0,
0,
0,
}

K.3.3 luma DC BITS (number of codes of each length 1..16).

Definition at line 44 of file ra8_jpeg_sw_encode_emit.c.

Referenced by priv_jpeg_sw_enc_build_huff(), and priv_jpeg_sw_enc_headers().

◆ s_hval_ac_chroma

const uint8_t s_hval_ac_chroma[162]
static
Initial value:
= {
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61,
0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33,
0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18,
0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44,
0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63,
0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca,
0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
}

K.3.3 chroma AC HUFFVAL.

Definition at line 167 of file ra8_jpeg_sw_encode_emit.c.

Referenced by priv_jpeg_sw_enc_build_huff(), and priv_jpeg_sw_enc_headers().

◆ s_hval_ac_luma

const uint8_t s_hval_ac_luma[162]
static
Initial value:
= {
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52,
0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83,
0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
}

K.3.3 luma AC HUFFVAL (162 symbols).

Definition at line 133 of file ra8_jpeg_sw_encode_emit.c.

Referenced by priv_jpeg_sw_enc_build_huff(), and priv_jpeg_sw_enc_headers().

◆ s_hval_dc_chroma

const uint8_t s_hval_dc_chroma[12]
static
Initial value:
= {
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
}

K.3.3 chroma DC HUFFVAL.

Definition at line 98 of file ra8_jpeg_sw_encode_emit.c.

Referenced by priv_jpeg_sw_enc_build_huff(), and priv_jpeg_sw_enc_headers().

◆ s_hval_dc_luma

const uint8_t s_hval_dc_luma[12]
static
Initial value:
= {
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
}

K.3.3 luma DC HUFFVAL (12 symbols).

Definition at line 63 of file ra8_jpeg_sw_encode_emit.c.

Referenced by priv_jpeg_sw_enc_build_huff(), and priv_jpeg_sw_enc_headers().