37 tok/s from Qwen3-Next-80B (48 GB) on a 32 GB laptop, 24 without drafts vs 2.8 for llama.cpp; reading 62 vs 16 tok/s. Faster than llama.cpp on all 13 models compared in Q4_K_M. LLMs bigger than your RAM, pure C, CPU or GPU.
C avx2ccpu-inferenceggufinference-enginelinuxllmlocal-llmmixture-of-expertsmoenvmequantizationqwen3simdspeculative-decodingvulkan
Release 2026-10-29: Qwen3.8-Flash-Next and NVIDIA Nemotron 3.5 Bolt; pictures with Qwen3.5 and Gemma 4; speech to text with Whisper from every format OpenAI accepts, word times, voice activity and who speaks when; the OpenAI API in janas-server almost whole (files, uploads, batches, vector stores, evals, assistants, web search, code interpreter, HTTPS)
camauri70e4aec21364
diff --git a/ChangeLog.md b/ChangeLog.mdindex 828cd52..1468324 100644--- a/ChangeLog.md+++ b/ChangeLog.md@@ -2,7 +2,63 @@> Curated, user-facing summary of completed work, newest first.-## [2026-10-28] - gpu_check asks what the engine asks+## [2026-10-29] - Speech in every sound file OpenAI accepts++- **Word times** (`timestamp_granularities[]=word` in janas-server's transcriptions, `words` in the library's parameters with `janas_llm_speech_words` and `janas_llm_speech_word`, the FreeBASIC header too), found as OpenAI's open-source Whisper finds them: the window's text decoded again keeping the attention of the model's alignment heads over the sound (from the checkpoint; OpenAI's default, the decoder's second half, without them), its softmax standardized and median-filtered, the path of least cost through it by dynamic time warping, tokens joined into words as OpenAI's tokenizer joins them (characters for Chinese, Japanese, Thai, Lao, Burmese and Cantonese), punctuation joined to its word, OpenAI's corrections at pauses and segment ends, and the next window starting at the last word's end. On John F. Kennedy's 11 seconds the words end where whisper.cpp's token times (the same method, in half precision) put them, within 20-40 ms.+- **Who speaks when** (`response_format=diarized_json` with `known_speaker_names[]` and `known_speaker_references[]`, as OpenAI's `gpt-4o-transcribe-diarize`; `diarize` and `janas_llm_speech_turns` in the library): WeSpeaker's ResNet34 speaker embeddings, converted from its ONNX model by a new tool, `wespeaker2jns`, run over Kaldi's filter banks computed as Kaldi computes them (1.6e-4 from kaldi-native-fbank; the embedding at a cosine of 0.9999993 to onnxruntime's), on windows of 1.5 s of the speech; WeSpeaker's spectral clustering (a k-nearest affinity, the Laplacian's eigengap for the number of speakers, k-means; the eigenvectors by Janas's own Householder and QL); each word the speaker of the windows over it, turns of consecutive words, known speakers named by their recordings. On 50 seconds spoken by three synthetic voices: ten turns of ten, the three speakers found, the known ones named; one speaker in a recording of one.+- **Chunking by voice activity** (`chunking_strategy` `auto` or `server_vad` with its threshold, prefix padding and silence, as OpenAI's; `chunking` in the library): Silero VAD v5, converted from its ONNX model by a new tool, `silero2jns` (a protobuf reader of its own, the 16 kHz model's weights in single precision), run as its model computes it (the STFT's basis, four convolutions, an LSTM carried from window to window) and its stretches of speech found as Silero's `get_speech_timestamps` finds them; only those are read, Whisper's windows within each, the text before carried on. Its probabilities agree with onnxruntime's on the same model to 1.7e-6; 46 seconds of Italian transcribed in eleven segments that follow the pauses.+- **Transcriptions streamed, with log-probabilities**: `stream: true` gives server-sent events as OpenAI's transcription models do (a `transcript.text.delta` for each segment as soon as it is final, `transcript.text.done` with the text and the usage), `include[]=logprobs` each text token's log-probability and bytes; in the library a callback for each final segment, a flag that stops a transcription between windows, and each token's log-probability. With the `openai` SDK: an M4A of 46 seconds streamed in ten deltas, words with their times, the logprobs of John F. Kennedy's 25 tokens.+- **A text that repeats itself is found as OpenAI's Whisper finds it**: its compression ratio, its bytes over their zlib-compressed bytes, above 2.4 has the window decoded again at a higher temperature (whisper.cpp's entropy of the tokens was used before); the compressed size from Janas's own deflate (`src/common/deflate.c`), identical to zlib's at its default level on 3,300 texts against Python's zlib (random bytes, words, loops, Italian, up to 32 KiB and over several blocks); `compression_ratio` in verbose_json's segments and in `janas_llm_speech_segment_info`.+- **Faults found by fuzzing the decoders under AddressSanitizer and UndefinedBehaviorSanitizer, fixed**: an Opus packet whose frames were empty concealed a frame of no samples and wrote before the CELT decoder's buffer (now no more is concealed than the packet's TOC says, as libopus does); an MP3 frame with more main data than the bit reservoir holds; ALAC's predictor and SILK's shifts of negative values in wrapping arithmetic made explicit; a FLAC header's skip.++- **Sound files read by Janas's own decoders**, for speech to text: FLAC (every kind RFC 9639 describes), MP3 and the other MPEG audio (MPEG-1, 2 and 2.5, Layers I, II and III: the bit reservoir, joint and intensity stereo, mixed blocks, free format, LAME's gapless trim), Ogg with Vorbis (floors 0 and 1, every residue), FLAC or Opus, and Opus itself - SILK in its exact fixed point with libopus's resampler, CELT, hybrid frames, the changes of mode between them, lost frames concealed as libopus conceals them, multistream with its channel mappings. Against FFmpeg's decoders and the reference streams: FLAC identical on 21 files; MPEG at 114 to 127 dB on 37 files and on ISO 11172-4's conformance streams, within 16-bit rounding of their reference PCM; Vorbis at 130 dB (91-99 dB on floor 0, where FFmpeg computes the line spectrum in single precision); Opus with the range coder's state identical, frame after frame, on every official test vector, SILK identical to the sample and CELT at 86 dB against Debian's libopus (139 dB once its approximate exp2 is taken into account). A Vorbis stream of a single page is now trimmed at its end only, as libvorbisfile does (FFmpeg drops its last 256 samples).+- **MP4, M4A, MOV, AAC and WebM, read by Janas's own decoders too**, so every format OpenAI's transcriptions accept is read: AAC LC, Main (its backward-adaptive prediction rounded as the reference does) and LTP, HE-AAC's spectral band replication (also downsampled, at the core's rate) and HE-AAC v2's parametric stereo, coupling channels, pulses, noise substitution, intensity and mid/side stereo, temporal noise shaping, frames of 1024 and 960; AAC in MP4 (sample tables and movie fragments, the edit list trimming the encoder's delay and padding) and in ADTS frames; ALAC; MP3, Opus, FLAC and PCM in MP4 and MOV; Matroska and WebM with sizes unknown (as browsers record them) and every lacing, with Opus (its codec delay and discard padding), Vorbis, FLAC, AAC, MPEG audio, ALAC and PCM. Against the MPEG-4 conformance streams FFmpeg's tests use (LC, Main, pulses, coupling channels, 96 kHz, 5.1, HE-AAC v1 and v2, downsampled SBR): 127 to 138 dB against FFmpeg, which itself is within 16-bit rounding of the reference PCM; ALAC, FLAC and PCM identical; AAC from FFmpeg's encoder in M4A, ADTS and Matroska 135 dB. Where FFmpeg's command line leaves the encoder's padding at the end (the edit list's length, Matroska's codec delay), Janas gives the source's exact length. AAC's low-delay (ER AAC LD, ELD) and USAC kinds are refused with a reason. Fifteen small clips, one of each format, in `tests/test_audio.c` with FFmpeg's decoding of them, every truncation and thousands of corrupted copies read or refused, clean under the sanitizers.++## [2026-10-28] - Defects found by a code review, fixed; one choice of quality; JSON schemas with bounds and formats++- **Speech to text, with Whisper**: `/v1/audio/transcriptions` and `/v1/audio/translations` in janas-server (`--speech-model`; json, text, srt, vtt and verbose_json, as OpenAI's `whisper-1`), `janas_llm_speech_*` in the library and its FreeBASIC header. A new tool, `hf2jns_whisper`, converts OpenAI's checkpoints from Hugging Face, the weights kept in the half precision they were released in; the encoder runs on the vision encoders' tiled kernels, now in half precision too, the decoder token by token with its keys and values kept, and the transcription goes window by window as OpenAI's does - the language found, timestamps, temperatures raised where the text loops, silence skipped. Against whisper.cpp on the same weights: the logits at a cosine of 1.000000, the same spectrograms, the same segments and times on 46 seconds of Italian but for two words, a comma and one time 40 ms apart; the tokenizer (GPT-2's expression, new in `tokenizer.c`) splits as Hugging Face's. Sound read by Janas's own WAV decoder (8 to 32 bits, float, any channels) and resampled with a windowed sinc (`src/common/audio*.c`, tried in `tests/test_audio.c`); FLAC and MP3 not yet.+- **Pictures in, with Gemma 4 12B**: its vision has no blocks - each patch of 48x48 pixels layer-normed, through one dense layer, normed, plus the embeddings of its column and row, normed, then projected - converted from its 100 MB of the checkpoint and run in `vision_gemma_u.c`, the same as llama.cpp's to a cosine of 1.000000; its pictures attended both ways as the 26B's. It told the shapes, read "JANAS 42" and described an abstract picture.+- **Pictures in, with Gemma 4 26B-A4B**: its larger encoder (27 blocks of 1152, standardized output) converted the same way, at a cosine of 0.999998 against llama.cpp's. In its language model a picture's tokens attend to one another both ways, as it was trained: the attention takes, token by token, the last position it sees (`janas_attn_set_last`, checked against a double-precision reference with a picture across calls and chunks), a prompt is never cut inside a picture, and a model whose encoder says so reads prompts in blocks that hold a picture whole; those passes stay on the CPU's attention. It describes a picture as llama.cpp's does.+- **Pictures in, with Gemma 4 E2B and E4B**: their vision encoder converted from the original checkpoint by `hf2jns_vision` (of Gemma's checkpoint only the vision tower is read: 338 MB for E2B), run as the model's own code does - the picture sized to at most 280 tokens as its processor does, RMS norms, every linear clamped to its bounds, a 2D rotary embedding of the patches' columns and rows, 3 x 3 pooling - and the pictures' tokens given the padding's per-layer inputs as the reference does. Against llama.cpp's encoder on the same pictures: a cosine of 0.999999 on average. Both models told a red circle and a blue square, a green triangle, and read "JANAS 42" off a picture of text.+- **Pictures in, with Qwen3.5**: `janas_llm_chat_image` in the library, `/image` in janas-chat, `image_url` and `input_image` parts (data: URLs, http(s) at public addresses only, files uploaded here) in janas-server. The vision encoder is converted from the original checkpoint by a new tool, `hf2jns_vision`, into a `.vision.jns` beside the model, and runs as the model's own processor and code do: pictures resized as Pillow does (to the byte), Janas's own PNG and JPEG decoders (the same pixels as Pillow on 295 pictures of every kind), the ViT's output against llama.cpp's at a cosine of 0.999997; the pictures' tokens get their row and column as positions (the interleaved multimodal RoPE). With Qwen3.5-0.8B: "a red circle on the left and a blue square on the right", in the library, janas-chat and janas-server alike, and the same answers as llama.cpp's.+- **Code interpreter** (`--code-interpreter`, off by default): the model writes Python and the server runs it in a cage of its own (`src/common/sandbox.c`: user, mount, PID, network, IPC and UTS namespaces without privileges, the system's `/usr` read-only, only `/mnt/data` writable, no network, threads but no new processes, a seccomp filter, limits on memory, file sizes and open files), one Python session per container that keeps its variables like a notebook, figures saved as PNG; `code_interpreter` in the Responses API (with `code_interpreter_call` items and `container_file_citation` annotations) and in the Assistants (steps with logs and images as Files, `file_path` annotations), and the Containers API (nine operations). With the `openai` SDK and Qwen3-4B: computations, a CSV summed, a chart downloaded, variables kept; links planted by the code to the user's files never followed; the cage tried against code that wants out in `tests/test_sandbox.c`.+- **Assistants** (`/v1/assistants`, `/v1/threads`, messages, runs, run steps; 23 operations): assistants with functions and `file_search` over this server's vector stores, threads and their messages (files attached for `file_search` go into the thread's own store), runs in the background through the same code as the Responses API - function calls wait for `submit_tool_outputs`, searches become steps, answers become messages with `file_citation` annotations; `cancel` stops the model at its next token, and a run cut by a stop goes on after the restart. Runs stream their events with `stream: true`, the text as the model writes it. With the `openai` SDK and Qwen3-4B, all of these, polled and streamed. Not yet: `code_interpreter`, images.+- **janas-server stops at once with a request running**: Ctrl+C waited for the whole reply of every request in progress, even when its client had gone (a request without a stream never looks at its connection), so a stop could take minutes. The clients' requests, running and waiting, now end at their next token (or block of the prompt read), new ones are refused, and the server waits at most 2 s for them to answer: a client still there gets "the server is stopping" (as an error, in a stream too); batches and evals go on where they resume at the next start. With Qwen3-4B and a reply of 2,000 tokens running: stopped in 0.1 to 0.2 s, client gone, waiting, or reading a stream.+- **JSON output held to a string's `pattern`**: classes, groups, alternatives and repetitions as a grammar, a quote, a backslash or a control character written escaped, the match written from the string's start (an unanchored pattern let the model write on without ever reaching it); patterns it cannot hold (anchors inside, back references, look-arounds) leave the string free as before. Qwen3-4B at temperature 1, 100 replies to a schema of six patterns (a plate, a code, a phone number, a hashtag, a quoted motto, a Windows path): 100 valid.+- **Evals** (`/v1/evals`, twelve operations): an eval's graders - string checks, text similarity (fuzzy match, BLEU, GLEU, METEOR, ROUGE, cosine) and the server's own model as a judge that labels or scores, held to its answer by a JSON schema - run on rows given in the request or in a JSONL file, the samples written by the model from a template or brought by the rows; runs go on in the background behind every client's request, saved row by row, so they go on after a restart, and can be cancelled. With the `openai` SDK and Qwen3-4B, all four kinds of grader, a cancel and a restart.+- **`file_search` in the Responses API**: the model searches the request's vector stores, answers from the passages and cites them as [n], which become `file_citation` annotations, with a `file_search_call` item holding the query and the results. With Qwen3-4B, the right file found and cited.+- **Vector stores** (`/v1/vector_stores`, sixteen operations, the file batches among them): text files cut into overlapping chunks, embedded in the background with `--embedding-model` and searched by their cosine, or by their words (BM25) without one; attributes and filters, scores from 0 to 1. With the `openai` SDK and Qwen3-Embedding-0.6B, each of three questions found its own file first.+- **HTTPS** (`--tls-cert`, `--tls-key`): the server listens itself, does the TLS handshake on the Mbed TLS it carries, and hands libmicrohttpd the connection in clear through a pair of sockets, so every request and stream is the same; the key is checked against the certificate at start. With `curl` and the `openai` SDK, four requests at once, clean under AddressSanitizer. Linux and macOS.+- **`web_search` in the Responses API**: the model searches Wikipedia in the language it chooses (English when that has nothing), answers from the pages' first sentences and cites them as [n], which become `url_citation` annotations, with a `web_search_call` item listing the query and the sources; `--no-web` refuses it. With Qwen3-4B in English and in Italian, the answers came from the pages and cited the right ones.+- **Batches** (`/v1/batches`, four operations): a JSONL file of requests checked, run one at a time behind every client's request (a chat reply in 0.12 s while a batch ran), each through the same code as the request it stands for, answered in an output file and an error file; saved after every line, so a batch goes on after a restart; cancelled or expired, what was done is kept. Tried with the `openai` SDK.+- **Uploads** (`/v1/uploads`, four operations): a file of up to 8 GiB in parts of up to 64 MiB, joined into a File on completion and checked against its size and MD5 (`src/common/md5.c`); an upload lasts an hour. With the `openai` SDK, 150 MiB in three parts came back the same.+- **Files** (`/v1/files`, five operations): uploads as the SDKs send them, with their purpose and expiry, kept in the store's directory under ids (a client's file name never a path), listed, read back after a restart, downloaded, deleted; 16 GiB in all at most, a gigabyte of disk left free; with `--no-store` in a directory removed at the end. The ground for batches, uploads and vector stores. Tried with the `openai` SDK.+- **JSON output held to a schema's numbers and formats**: integers to `minimum`, `maximum`, their exclusive forms and `multipleOf` (up to 50), exactly - a grammar of their digits that never begins a number it cannot end inside the bounds; other numbers to their bounds, without an exponent and with a fraction only where it cannot cross one; the formats `date`, `time`, `date-time`, `uuid`, `email` and `ipv4` as their grammar, through a small reader of regular expressions (`src/llm/schema_re.c`, there for `pattern` next); string lengths up to 4,096 instead of 64. Every integer from -1,200 to 1,200 checked against five schemas; Qwen3-4B at temperature 1, 100 replies to a schema with all of them: 100 valid.++- **One choice for how much quality the engine may give up for speed: `--quality high|medium|low`** (janas-chat, janas-server; `min_quality` in `janas_llm_params`, `quality` in the Node module, `Quality` in the FreeBASIC classes). With `low`, the default, the engine decides by the machine, as before: where the experts do not all fit in memory it reads them at four bits, and with less than a fifth of them in memory it leaves out the light ones not there rather than wait for the disk (Nemotron 3.5 Bolt with a 2.7 GB cache: 391 of 400 against llama.cpp where 396, 6% faster); with memory for every expert nothing is lowered. `medium` allows the four bits but leaves no expert out; `high` lowers nothing. In janas-chat, `--quality low` asked for also takes the catalog's levers measured faster on that model (the former `--profile fast`; `--profile` stays as the older name, `quality` meaning `high`). The single levers - `--bits`, `--head`, `--attention`, the experts per token - stay, and win over it.++- **janas-server no longer falls over on a text part that is not a string**: a message's `"text"` given as an array or an object (chat completions and responses) had its length taken for its bytes and was copied past its end, and the server crashed; it is now a 400 (`invalid_value`). A refusal part's text is read from its `refusal` field, as OpenAI writes it.+- **Web pages of other sites cannot use the server**: a page you visit could make the browser send requests to `127.0.0.1`, and with a name of its own resolved there (DNS rebinding) read the answers. The browser's `Origin` is now checked on every request: the server's own page passes, programs (which send none) are not concerned, any other page gets a 403 (`origin_not_allowed`) whatever the key; `--allow-origin ORIGIN` lets a web client of another origin in, with the CORS headers it needs.+- **MCP servers named in a request are reached at public addresses only**: every address the name resolves to is checked and the connection goes to one that passed, so loopback, private, link-local (the clouds' metadata), shared, multicast and reserved ranges are refused, IPv4 inside IPv6 judged as itself; `--mcp-allow HOST[:PORT]` names an MCP server of your own on this machine or the network.+- **Stored documents have limits**: at most 64 MiB each (a conversation that would grow past it gets a 400, `conversation_too_large`) and 1 GiB in all, the oldest going first; adding to a conversation joins the texts instead of reading the whole of it back and writing it again.+- **janas-chat no longer lets a model or a tool drive the terminal**: what a model writes and what MCP tools return were printed as they came, so an escape sequence in them could move the cursor, switch screens or set the clipboard (OSC 52); the control characters and the C1 controls are now taken out, and a reply's tabs are four spaces. A third party's tool call is shown whole when you are asked to allow it (it was cut at 400 bytes, and `[a]lways` said yes to what was not shown). `/load-config` freed the settings' text that options still pointed into (`/save-config` then read freed memory); resizing the window no longer counts as "no" to a question or closes a page; two MCP servers whose names make the same prefix (`a.b` and `a_b`) get different ones; the input history is readable by its owner only.+- **janas-get and the tools**: a download goes to `.part` and takes its name only once its SHA-256 is checked (an interrupted one could pass for the model), a file Hugging Face lists no SHA-256 for is said to be unchecked, and a redirect from HTTPS to HTTP is refused; on Windows the arguments of a `.cmd`/`.bat` (npx) are always quoted and those holding `"`, `%`, `!` or a newline refused, as cmd.exe would act on them; hf2gguf past 1,024 tensors, janas-nsm on a file with carriage returns only, and the version and the GPU's reason in build.sh's `-D` strings escaped.+- **CPU kernels**: a row ending inside a group with a shape its type cannot end in pads with zeros instead of copying past the row (the loaders refuse such shapes); the experts' bit planes are refused for any resident tensor (they could be sized but not unpacked); Q2_0's and MXFP4's tiles no longer read minima never written; NVFP4's scale table is filled at start rather than checked once a row.+- **GPU: fixes from the review**: the keep-alive's empty dispatch wrote a float near the end of the stage that carries a card's results back, from another thread and in no order with the copies (a card with memory of its own could see four bytes zeroed): it has a buffer of its own; on a GPU whose subgroups have fewer than 8 lanes the row shaders would step 0 blocks at a time, for ever: they are not made there, the products go the other ways and the whole pass is refused; the attention's workgroups take at most 16 query heads and, with sixteen-bit keys, 2,048 numbers of them; Qwen3.5's gated query under `JANAS_GPU_REPACK` cut every head from the first; `JANAS_GPU_MVID` below the block size left the experts' tables too short for the types that always go by rows; host memory is imported only with a coherent memory type; the rotations of `qk_rope`, `ix_prep` and `ix_pool` are rounded twice as on the CPU, never fused by a driver; the memory a card gives back is counted again; one command buffer freed. `gpu_check` on the laptop's Arc as before (3,980 products, 0 wrong), Qwen3.5-0.8B's whole pass 245 of 256 against llama.cpp.+- **Nemotron's context slides right, and its mixture with a small cache**: sliding the context (a long chat) turned back by the dropped positions keys that had never been turned, since Nemotron's attention has no RoPE: now they only move (`llm_slide`, 400 tokens, 100 dropped: the same most likely token, mean |diff| 0.21, the next 24 tokens agreeing for 22). With an expert cache under a fifth of the experts, the experts left out because they were not in memory had the others' weights brought back to a sum of one, where Nemotron's router gives five; the weights now go back to the router's own sum, and the threshold is a share of it. With 2.7 GB of cache, 400 tokens of C against llama.cpp: 391 of 400 at 6.9 tokens/s with the experts left out, 396 at 6.5 without.+- **A cache smaller than a block's experts**: with fewer than 128 slots a prompt's block asked for more experts at once than the cache holds, and failed; the requests are now cut to the slots there are (Nemotron, 100 MiB of cache, blocks of 64: the same bits as a token at a time, 64 of 64 against llama.cpp). An expert that found no slot because reads ahead held them all was handed over as a null pointer: the reads ahead are settled and the slot sought again, and failing that the request fails instead of crashing.+- **Smaller fixes in the engine**: a saved state with a recurrent log longer than its buffers or a base outside its text is refused; a pass that fails reading Gemma 4's per-layer inputs no longer loses the recurrent state first; a slide that cut the recurrent log went on replaying it from the wrong place, now the state goes on from the log's end; the MTP layer's own router and convolution copies are freed.+- **A model file must be what it says it is**: a JNS file's resident tensors are now refused unless their bytes are what their type and shape make (a crafted file, downloaded, could have the engine read past the region), the experts' gate, up and down matrices must all have the shapes the buffers are made for (only the gate's rows were asked), and a Nemotron layer's router may name only experts present in the file and at least as many as it chooses. A GGUF tensor with an empty dimension is refused, and `gguf2jns` refuses rows longer than 16 MiB (they overran its buffers), more layers than the engine reads, more experts used than there are, and metadata with arrays of arrays; `hf2jns_mtp` reads a safetensors header with every field checked. Every JNS file here opens as before, and one converted again comes out the same to the byte.+- **Smaller fixes in the engine's plumbing**: the expert cache sorted with a variable shared between caches, divided by zero on a file whose every slot is empty, and destroyed locks it had not made yet when an allocation failed; the NPU's graph cache now keys on the device too, is not used when the driver's version cannot be read, and two NPUs of one process write it without sharing a temporary name; a failed keep-awake no longer leaks when asked again; the thread pool returns NULL instead of ending the program when a thread cannot start.+- **Smaller fixes on the server and its clients**: a prompt of token ids with a token longer than 255 bytes read past a buffer; response ids made of zeros if the system's randomness failed; the MCP client's header lines capped at 64, so a call's own pushed the configured `Authorization` out; a redirect from HTTPS to HTTP on the same host kept the credentials (now dropped on any change of scheme, host or port).++## [2026-10-28] - NVIDIA's Nemotron 3.5 Bolt, and gpu_check asks what the engine asks++- **NVIDIA Nemotron-3.5-Bolt-74B-A9B on the CPU** (architecture `nemotron_h_moe`, NVIDIA's own 24GB GGUF: experts in IQ2_XXS trained at two bits, Mamba layers in NVFP4): 88 layers, each one part with its own norm and residual - 40 Mamba-2 mixers, 40 mixtures and 8 attentions without RoPE. Against llama.cpp, 400 tokens of C, the most likely token agrees at 395, mean |diff| 0.183; the tokens the same as llama.cpp's on seven texts. On the laptop's CPU, 12 threads, a token at a time with every expert in RAM: 6.8 tokens/s. What it took: the NVFP4 type (four sub-blocks of 16 with UE4M3 scales; dequantized as ggml to the bit, an AVX2 product the same bits as the reference); Mamba-2 (`model_mamba.c`: the conv with its bias, the scan in float, D, the gate and the grouped norm, on DeltaNet's two buffers, replay and log, so drafts work); the mixture in a latent space (the input brought down to 1,024 before the experts, their weighted sum brought up after), experts with relu^2 and no gate, routed by sigmoids with a bias for the choice, each layer with its own number of experts; Mistral's Tekken pre-tokenizer (`pixtral`). `gguf2jns` writes layers without experts, experts without a gate and a table as wide as the layer with the most, the others' slots absent; the reader takes them only as a tail with zero checksums.++- **The attention's way chosen once, so that a pass gives the same result every time**: the choice between the query as it is and at sixteen bits (26 October) was measured on the calls themselves, a band of positions at a time, and so changed from one pass to the next: the same tokens could come out a few hundredths apart, a block's not the same bits as a token at a time (Nemotron: 0 of 64 positions). Now it is measured once, before the first call, on made-up keys of the model's geometry at 1,024 positions and up (a token's calls to 65,536, a block's to 16,384), and kept in `~/.cache/janas/attn.txt` by processor, threads and geometry: the sixteen-bit way from the shortest context where it wins there and at every longer one. Blocks are again the same bits as a token at a time (Nemotron 64 of 64); Qwen3-4B after 4,096 tokens as before (writing 22.9 tok/s, prompt 98.0). `JANAS_ATTN=float` or `int16` fixes it.++- **Nemotron's NVFP4 on the GPU, and the NPU's graphs kept across sessions**: NVFP4 is b32_matvec.comp's eighth kind (a block of 32 is half an NVFP4 block, its two sub-blocks' dots in two fmas as on the CPU), so a prompt's blocks give the GPU Nemotron's Mamba projections too, the same bits as the CPU (blocks of 64, the fast prompt's tiles apart: those round as they do for every type, and agree with llama.cpp as the CPU does, 254 of 256); its whole pass on the GPU is not there yet. The NPU's compiled graphs are now kept in `~/.cache/janas/npu` by default instead of only where `JANAS_NPU_CACHE` said: their preparation is paid once, not at every session (Nemotron: 36 s the first time, 3.8 s after). The engine's choice of the NPU and janas-bench's rows already leave that preparation out. On Nemotron the NPU takes the Mamba, attention, latent and shared products of the prompt's blocks; on the laptop it reads them a little slower than the CPU (19.2 against 20.2 tok/s), so the engine leaves it off there.- **`gpu_check` no longer runs on the GPU a product the engine would leave to the CPU** (issue #46): its "every row on the GPU" pass called the GPU without asking whether it takes the product, and one of its shapes starts a row into the buffer, two bytes off a 4-byte boundary for the blocks of 110, 210, 82, 50, 66, 74 and 98 bytes (Q3_K, IQ3_S, Q6_K, Q6_K_P, IQ1_S, IQ2_XXS/XS/S, IQ3_XXS). The engine never gives those to the GPU (`janas_gpu_can` wants their weights 4-byte aligned, as model files keep them); the Arc read them right anyway, AMD's driver did not, and gpu_check reported 164 wrong products on a Radeon 890M. Now they are counted as left to the CPU, as the engine computes them: on the laptop's Arc 3,980 products, 0 wrong, 220 left to the CPU.diff --git a/MODELS.md b/MODELS.mdindex 682c510..6a4f8f7 100644--- a/MODELS.md+++ b/MODELS.md@@ -165,6 +165,8 @@ f1b8111d3e49a0238fcaa77ec9ee24eb9805266b5e85bf8e54c12dedb02cf4ff Qwen3-30B-A3B-**Gemma 4: E2B, E4B, 12B and 26B-A4B, and their assistants**, from [`unsloth/gemma-4-E2B-it-GGUF`](https://huggingface.co/unsloth/gemma-4-E2B-it-GGUF), [`unsloth/gemma-4-E4B-it-GGUF`](https://huggingface.co/unsloth/gemma-4-E4B-it-GGUF), [`unsloth/gemma-4-12b-it-GGUF`](https://huggingface.co/unsloth/gemma-4-12b-it-GGUF) and [`unsloth/gemma-4-26B-A4B-it-GGUF`](https://huggingface.co/unsloth/gemma-4-26B-A4B-it-GGUF). Each converted in one step, as it is kept here, and each converted twice on 27 September 2026: the same bytes. The assistant, Gemma's drafting model (the `mtp-` file, given to the chat with `--mtp`), converts with `gguf2jns` like the model. The 26B's feed-forwards are widened with zeros to a multiple of 256, which is why its file is larger than its GGUF.+**NVIDIA Nemotron-3.5-Bolt-74B-A9B (24GB)**, from [`nvidia/NVIDIA-Nemotron-3.5-Bolt-74B-A9B-24GB-GGUF`](https://huggingface.co/nvidia/NVIDIA-Nemotron-3.5-Bolt-74B-A9B-24GB-GGUF) (SHA-256 of the GGUF as published there, 91d63363...99af75), converted on 28 October 2026 by `gguf2jns` into a 22.0 GB file: its layers without experts (Mamba-2 and attention) get an empty slot, its experts no gate, and the table of experts is as wide as the layer with the most (496), the others' extra slots absent. Its licence is NVIDIA's OpenMDW-1.1 (on the model's page): until its terms are read for redistribution, nothing of it is published here; convert it yourself.+```740185b21d22ceb83a11c3aa62ad5842ef32c70f6096d756bbee85a1e4ec34b8 gemma-4-E2B-it-Q4_K_M.gguf1f18e08878df41f377d563b38048d41841c7973ddea845cd9672fcf23cbd864b gemma-4-e2b-it-q4km.jns (gguf2jns)diff --git a/build.sh b/build.shindex a68024d..1db08bb 100755--- a/build.sh+++ b/build.sh@@ -59,8 +59,7 @@ Outputsrc/services/<tool>/main.c becomes janas-<tool>, withthe other .c files of its directory; the libraries of others insrc/third_party/ go into the programs that use them; every other .c- under src/ goes into the library. Janet (src/janet/, Linux x86-64)- gives janet, janet-check, janet-mnist and janet-mnist-deep. The release build+ under src/ goes into the library. The release buildalso links libjanas_llm.so and libjanas_mcp.so, which export the publicAPIs of include/janas/llm.h and include/janas/mcp.h and nothing else.On Linux src/node/ becomes janas.node, the module for Node.js, with the@@ -194,9 +193,15 @@ fiAR="${AR:-ar}"LDSTATIC=()[ "$OS" = windows ] && LDSTATIC=(-static)+# A text as a C string literal for -D: backslashes and quotes escaped,+# anything but printable ASCII dropped (a tag's name, a tool's output)+c_string() {+ printf '"%s"' "$(printf '%s' "$1" | LC_ALL=C tr -cd '[:print:]' |+ sed 's/[\\"]/\\&/g')"+}# the version in reports (janas-bench)VERSION="$(git describe --always --dirty 2>/dev/null || echo unknown)"-CFLAGS+=(-DJANAS_VERSION="\"$VERSION\"")+CFLAGS+=(-DJANAS_VERSION="$(c_string "$VERSION")")# Throw away what this variant made, and only what this variant made: the# objects of the variant and the programs that carry its suffix. Release has@@ -304,7 +309,7 @@ fi[ -z "$GPU_MISSING" ] ||echo "note: building without GPU support: it needs $GPU_MISSING"# the reason, for the engine to give when asked for the GPU-[ -z "$GPU_MISSING" ] || CFLAGS+=(-DJANAS_GPU_WHY="\"$GPU_MISSING\"")+[ -z "$GPU_MISSING" ] || CFLAGS+=(-DJANAS_GPU_WHY="$(c_string "$GPU_MISSING")")[ -z "$GLSLANG" ] || CFLAGS+=(-DJANAS_GPU_SPV -I"$SPV")# Web pages of the programs (src/<tool>/*.html) as C arrays, so that the@@ -329,8 +334,10 @@ for html in src/*/*.html; dodoneCFLAGS+=(-I"$GEN")-# one object, rebuilt when its source or any header is newer-if ! headers_newest=$(find src include "$SPV" "$GEN" -name '*.h' -printf '%T@\n' |+# one object, rebuilt when its source or any header (or .inc, such as the+# server's routes.inc) is newer+if ! headers_newest=$(find src include "$SPV" "$GEN" \( -name '*.h' -o \+ -name '*.inc' \) -printf '%T@\n' |sort -n | tail -1) || [ -z "$headers_newest" ]; thenecho "build.sh: cannot list the headers under src/ and include/" >&2exit 2@@ -605,7 +612,11 @@ for src in tests/*.c tools/*.c src/*/main.c src/services/*/main.c; do[ -n "$c" ] || continueobj=$(objname "$c")own+=("$obj")- compile "$c" "$obj" "${CFLAGS[@]}" "${TP_INC[@]}"+ # the server's HTTPS reads Mbed TLS's headers, as the system's+ extra=()+ [ "$c" = src/server/tls.c ] &&+ extra=("${MBEDTLS_INC[@]/#-I/-isystem}")+ compile "$c" "$obj" "${CFLAGS[@]}" "${TP_INC[@]}" "${extra[@]}"done < <(sort <<<"$own_list")fiecho "LD $exe"diff --git a/docs/chat.md b/docs/chat.mdindex b3cc430..6cc79d6 100644--- a/docs/chat.md+++ b/docs/chat.md@@ -6,7 +6,7 @@ janas-chat [model] [options]The model is a `.jns` file, or the name of a model of the catalog (`janas-get list` shows them): `janas-chat qwen3-30b-a3b` looks for it in `~/.local/share/janas/models` (or `JANAS_MODELS`, or `--models`), and if it is not there yet offers to download and convert it, with its prediction file or draft, through `janas-get`. With no model at all the chat lists the catalog, marks what is already there, and asks which one. Qwen3.5-0.8B is left out of that list: it is a test model of the Qwen3.5 architecture, kept for the engine's checks (it is converted in eight weight types), not for chatting; asked on 8 October 2026 for twenty actions (the weather, aircraft overhead, Wikipedia), it called the right tool 7 times out of 15 and made facts up, where Qwen3.5-2B, the small model for such work, called it 14 times. It still opens by name, with a word of warning. The first download proposes the folder and takes another if you type one; janas-chat then finds the models there only when told (`--models` or `JANAS_MODELS`).-The settings come in layers, each over the one before: the model's profile in the catalog, the measure of the model on this machine, the chat's own file (`~/.config/janas/chat.conf`), the model's file (`~/.config/janas/models/<name>.conf`), the command line. `/save-config` writes the model's file (`/save-config global` the chat's), `/del-config` deletes it, and the catalog itself is never written. A model of the catalog has two profiles: `quality`, the default, where the answers are the ones the file gives; and `fast` (`--profile fast`), which adds the levers measured to make that model faster at a small, measured cost to its answers (see below). A model whose catalog entry names a draft gets it by itself: Qwen3-4B guesses with Qwen3-0.6B.+The settings come in layers, each over the one before: the model's profile in the catalog, the measure of the model on this machine, the chat's own file (`~/.config/janas/chat.conf`), the model's file (`~/.config/janas/models/<name>.conf`), the command line. `/save-config` writes the model's file (`/save-config global` the chat's), `/del-config` deletes it, and the catalog itself is never written. How much quality the engine may give up for speed is one choice, `--quality <high|medium|low>`. With `low`, the default, it decides by the machine: where the experts do not all fit in memory it reads them at four bits (5 to 7 positions of 400 where the most likely token differs), and with less than a fifth of them in memory it also leaves out the light experts that are not there rather than wait for the disk (Nemotron 3.5 Bolt with a 2.7 GB cache: 391 of 400 against llama.cpp where 396, 6% faster); with memory for every expert nothing is lowered. `medium` allows the four bits but leaves no expert out, `high` lowers nothing. Asked for in so many words, `--quality low` also adds the levers measured to make that model faster on any machine, the catalog's fast profile below (`--profile fast`, the older name, is the same; `--profile quality` is `high`). The single levers — `--bits`, `--head`, `--attention`, `/experts` — stay for those who want them, and win over `--quality`. A model whose catalog entry names a draft gets it by itself: Qwen3-4B guesses with Qwen3-0.6B.**The fast profiles, as measured.** Every lever that trades a little accuracy for speed — `--head 4`, `--attention fast`, `--bits 4` where the experts are in planes — was tried on every model of the catalog, alone and in every combination, on 8 October 2026 on the development laptop; a lever is in a model's profile when it adds 2% or more. Speed: 128 tokens after a prompt of 1,900 (`tests/bench_long`, two runs each); for the three models larger than their 16 GiB cache, a fixed text read token by token instead (`tests/llm_eval`), because each lever changes the text a greedy reply takes and with it the experts read from disk. Agreement: the most likely token against llama.cpp, Italian prose / C source, as in the file → with the profile.@@ -27,11 +27,11 @@ The settings come in layers, each over the one before: the model's profile in thQwen3.5-0.8B does not take `--head 4`: with it, it answered "3" to "2+3". Gemma 4 has no fast profile: its keys are sixteen-bit, where `--attention fast` has nothing to do, its heads are not six-bit and its experts are not in planes. `--attention fast` pays where attention is a larger share of a token at 1,900 positions (Qwen3-0.6B +7.5%, Qwen3-30B-A3B +4.0%); on the hybrid Qwen3.5, Qwen3.6 and Qwen3-Next it adds 2% or less.-**The measure on this machine.** What makes a model fastest depends on the machine too, so the first time a model starts in a terminal the chat offers to measure it: the model opened each way that can differ — drafts from its prediction file or draft model, from the conversation, or none; in the `fast` profile also the output head as in the file — a reply to each of three prompts (prose, code, a list) to warm it, then the same three measured at temperature 0, so every way writes the same text; drafting and not drafting are measured prompt by prompt, taking turns at going first, since a mixture of experts is still warming while it is measured (until 8 October 2026 the drafts went first and warmed only on the prose, and Qwen3-30B-A3B looked 10% slower with them: with the order swapped, 9% faster). A way other than the one the model starts with has to be at least 3% faster to be kept (a couple of per cent is the noise of three replies). The result goes to `~/.cache/janas/tune-<name>-<profile>.conf`, with the speeds as a comment, and is used for the levers you did not set yourself; a "no" is kept too, so the question comes once. `--tune` measures again, `--no-tune` neither asks nor uses it. On the development laptop: Qwen3-4B 33.1 tokens/s with Qwen3-0.6B's drafts, 29.2 without drafts, 29.5 with drafts from the conversation; Qwen3.5-2B 77.4 with its MTP block, 41.9 without drafts, 60.6 from the conversation, and with the head at four bits 77.4 against 75.6 at six; Qwen3-0.6B within 1% either way, so it starts as it would have.+**The measure on this machine.** What makes a model fastest depends on the machine too, so the first time a model starts in a terminal the chat offers to measure it: the model opened each way that can differ — drafts from its prediction file or draft model, from the conversation, or none; with `--quality low` asked for, also the output head as in the file — a reply to each of three prompts (prose, code, a list) to warm it, then the same three measured at temperature 0, so every way writes the same text; drafting and not drafting are measured prompt by prompt, taking turns at going first, since a mixture of experts is still warming while it is measured (until 8 October 2026 the drafts went first and warmed only on the prose, and Qwen3-30B-A3B looked 10% slower with them: with the order swapped, 9% faster). A way other than the one the model starts with has to be at least 3% faster to be kept (a couple of per cent is the noise of three replies). The result goes to `~/.cache/janas/tune-<name>-<profile>.conf`, with the speeds as a comment, and is used for the levers you did not set yourself; a "no" is kept too, so the question comes once. `--tune` measures again, `--no-tune` neither asks nor uses it. On the development laptop: Qwen3-4B 33.1 tokens/s with Qwen3-0.6B's drafts, 29.2 without drafts, 29.5 with drafts from the conversation; Qwen3.5-2B 77.4 with its MTP block, 41.9 without drafts, 60.6 from the conversation, and with the head at four bits 77.4 against 75.6 at six; Qwen3-0.6B within 1% either way, so it starts as it would have.| Option | What it does ||---|---|-| `--profile <quality\|fast>` | the catalog's profile for the model (default `quality`) |+| `--quality <high\|medium\|low>` | the lowest quality the engine may choose to be faster where memory is short (default `low`; asked for, `low` adds the catalog's fast profile; see above). `--profile quality\|fast` is the older name for `high` and `low` || `--models <dir>` | where the models are (default: `JANAS_MODELS`, or `~/.local/share/janas/models`) || `--tune`, `--no-tune` | measure again how the model runs fastest on this machine; or neither ask nor use that measure || `--mtp <file>` | the model's multi-token prediction file (an MTP block, or Gemma 4's assistant): faster replies, same text; without it, the one beside the model that fits it |@@ -81,6 +81,7 @@ In the chat, a line starting with `/` is a command, and it turns grey-blue as so| `/reset` | a new conversation, and a clean window || `/system [text\|off]` | show or change the system message || `/temp <t>`, `/spec`, `/think` | sampling, drafts, reasoning (`/think 512`: on, at most 512 tokens of it) |+| `/image <file>` | a picture (PNG or JPEG) for your next message, with a model that sees (Qwen3.5 or Gemma 4 with its `.vision.jns` beside it, [models.md](models.md)) || `/mode [auto\|eco\|max]` | power mode, and what the engine chose || `/experts [n]` | experts per token: fewer is faster and a little less accurate; any number, `0` included, turns off the leaving out of light experts not in memory || `/markdown [on\|off]` | read the model's marks, or print them |@@ -116,7 +117,7 @@ The chat starts by itself the services of Janas it finds beside it - `janas-flig**The system message is read ahead.** It used to be four thousand tokens with the services' tools, and the first reply waited for all of it. `janas-chat` now reads it as soon as it starts, while you write, and says so: a line in the conversation when it begins, and "reading the tools ahead" with how far it is in the status line, where it is the last thing to be dropped on a narrow window; a message sent before the end waits for it, with "waiting for the tools" and the share, and Ctrl-C leaves the rest to the reply. Once read it is kept in `~/.cache/janas` (2 GiB at most for these files), and the next chat on the same model finds it there: "System message and tools ready". Changing the system message or the tools reads them again.-**The NPU made ready, in sight.** The first blocks the NPU reads in a session wait while it is made ready: the weights it takes copied in eight bits and its graphs compiled, 17 s for Qwen3.5-2B on the development laptop, about 50 s for Qwen3-4B (21 October 2026). The status line says so meanwhile, wherever it happens - while the system message is read ahead, or in a reply -: "preparing the NPU 12/25, 8 s", the graphs made of those to make and the seconds gone; after the reply a line says how long it took, once a session. A program on the library has the same from `janas_llm_npu_preparing`, which may be called from another thread while the engine works.+**The NPU made ready, in sight.** The first blocks the NPU reads in a session wait while it is made ready: the weights it takes copied in eight bits and its graphs compiled, 17 s for Qwen3.5-2B on the development laptop, about 50 s for Qwen3-4B (21 October 2026). The status line says so meanwhile, wherever it happens - while the system message is read ahead, or in a reply -: "preparing the NPU 12/25, 8 s", the graphs made of those to make and the seconds gone; after the reply a line says how long it took, once a session. The compiled graphs are kept in `~/.cache/janas/npu` (`JANAS_NPU_CACHE` names another directory, empty for none), so the next sessions load them instead of compiling them again: on Nemotron-3.5-Bolt 36 s the first time, 3.8 s after (28 October 2026). Deleting that directory makes them compiled once more. A program on the library has the same from `janas_llm_npu_preparing`, which may be called from another thread while the engine works.## Tools from MCP serversdiff --git a/docs/features.md b/docs/features.mdindex d7e5f6b..d1fd53d 100644--- a/docs/features.md+++ b/docs/features.md@@ -3,6 +3,7 @@- **Models:** Qwen3.5 and Qwen3.6 (`qwen35moe`, e.g. Qwen3.6-35B-A3B), Qwen3-Next (`qwen3next`, e.g. Qwen3-Next-80B-A3B-Instruct and Qwen3-Coder-Next), Qwen3 MoE (`qwen3moe`, e.g. Qwen3-30B-A3B), the **dense** Qwen3.5 models (`qwen35`) and the **dense** Qwen3 models (`qwen3`), from the usual **Q4_K_M** GGUF files, or **Q3_K**, **IQ3_S**, **IQ4_NL**, **IQ4_XS**, **Q4_0**, **Q4_1** and **Q5_1** ones, and unsloth's "UD" mixes of them (checked on Qwen3.5-0.8B and 9B against llama.cpp), and the "MXFP4_MOE" files, whose experts are MXFP4 (checked on Qwen3.6-35B-A3B against llama.cpp on the same file: 199 of 200 most likely tokens agree). A dense feed-forward is a mixture with one expert and no router, so the same path serves both and there is no second engine to keep in step. Of the dense ones **Qwen3.5-0.8B, 2B and 9B, Qwen3-4B and Qwen3-0.6B are the ones that have been run here**; the Qwen3.5 three were checked against llama.cpp on the same files. **Nearly everything here was measured on Qwen3-Next-80B-A3B-Instruct**: it is the one the engine was shaped around, and the one whose numbers are quoted below. The others have been converted, checked against a reference and used, but far less.- **Gemma 4** (`gemma4`): **Gemma-4-12B-it** (dense) and **Gemma-4-26B-A4B-it** (a mixture of 128 experts beside a dense feed-forward) convert and chat, with their reasoning on or off (`--think`); the prompts are, token for token, the ones the model's own chat template writes, and a full context slides as it does for the other models. Its sliding-window layers keep only the last positions they can see, so a long context costs mostly the full layers: for the 12B about 16 KB a token and half a gigabyte fixed, instead of 175 KB a token. Keys and values are kept at sixteen bits (Gemma is far more sensitive to them than Qwen). Tools work in Gemma's own notation - declarations, calls and the tools' answers inside the model's turn, token for token as its template writes them. The small **Gemma-4-E2B-it** and **E4B-it** run too, with chat and tools: their per-layer embeddings - a table as large as half of E2B's file - are read from the file a row at a time and never loaded, so E2B ran here in 1.5 GB of memory (a 3.1 GB file) and E4B in 3.1 GB (5.0 GB). Each of the four has an **assistant**, a small drafting model of its own that reads the main model's keys and values, taken as its `--mtp` file: on an Italian chat reply of 110 tokens, greedy, it took E2B from 34.7 tokens/s to 51.0, E4B from 18.3 to 26.3, the 12B from 8.5 to 12.9 and the 26B from 16.9 to 24.5 on the development laptop, the same tokens with and without it. Against llama.cpp on 200 tokens of prose the most likely token agrees at 160 positions on the 12B (mean logit difference 0.84) and at 160 on the 26B (0.97); on another 200-token text at 191 on E2B (0.45) and 195 on E4B (0.28). Against an exact float64 computation over the same weights (a NumPy forward pass written to check them), on the prose: the 12B agrees at 167 positions (llama.cpp 173), the 26B at 156 (llama.cpp 160), with mean errors of 0.75 and 0.87 (llama.cpp 0.67 and 0.90). The larger two turn small numerical differences into large ones, and llama.cpp is not a steady reference for them: against itself with an eight-bit cache it agrees at 153 positions on the 12B, and with a 32-bit cache and no flash attention, at 112. The reference has to be computed in batches of at most 32 tokens: with larger ones llama.cpp's logits for the early positions change (it looks ahead), for a reason not yet found. Since 26 October 2026 all four run **whole on the GPU** too - the sliding-window layers' rings, the full layers' heads of 512, the sixteen-bit keys and values, the sandwich norms, the per-layer embeddings' branch, the 26B's two branches and its Q5_1 experts - with the GPU's norms and quantizations giving the CPU's bits: against llama.cpp on 400 tokens the 12B agrees at 367 positions on the GPU (the CPU 362), the 26B at 321 (321), E2B at 386 (391) and E4B at 392 (394).- **Llama 3** (`llama`, since 25 October 2026): Llama 3 checkpoints from Hugging Face, with a PEFT LoRA adapter merged into the weights if one is given, written as a GGUF by `tools/hf2gguf` that llama.cpp's own tools quantize and `gguf2jns` converts. Checked on DeepNSM-1B (Llama-3.2-1B with the adapter of arXiv 2505.11764): the merged weights the same to the bit as an independent merge, the tokens the same as llama.cpp's, the most likely token as llama.cpp's at 466 of 477 positions in Q8_0; on the CPU and the GPU.+- **NVIDIA Nemotron 3.5 Bolt** (`nemotron_h_moe`, since 28 October 2026, on the `nemotron` branch until it is merged): **Nemotron-3.5-Bolt-74B-A9B** from NVIDIA's own 24GB GGUF (22.0 GB: its experts in IQ2_XXS, trained at two bits, the Mamba layers in NVFP4, the embedding and the output head in Q8_0). Its 88 layers each do one thing with a norm and a residual of their own: 40 Mamba-2 mixers, 40 mixtures and 8 attentions without RoPE. The mixtures route by sigmoids with a bias for the choice, each layer with its own number of experts (up to 496, 9 a token), and their experts work in a latent space of 1,024 (the input brought down before them, their weighted sum brought up after), relu^2 without a gate, beside a shared expert. Against llama.cpp on the same file, 400 tokens of C: the most likely token agrees at 395 positions (mean logit difference 0.183); its tokenizer (Mistral's Tekken) gives llama.cpp's tokens on seven texts. On the development laptop's CPU, the same file and 12 threads, it writes at 7.0 tokens/s where llama.cpp writes at 5.92, and reads a prompt of 512 tokens at 15.1 tokens/s (16.5 without the first read of the experts from disk) where llama.cpp reads at 16.7; with all 20 usable threads (janas-bench) 20.2 and 8.3. A prompt's blocks give the GPU its products, NVFP4 included (the same bits as the CPU), and the NPU the products of the weights that stay in memory (on the laptop a little slower than the CPU: the engine leaves it off there); its whole pass on the GPU is the next step.- **Qwen3.8-Flash-Next** (`qwen4exp`, since 22 October 2026): a mixture of 512 experts and 177 billion parameters, from ISTA-DASLab's two-bit GGUF (GSQ-RCO, Q2_0, 66.4 GB), on the CPU, the GPU and the NPU. Its residual runs in four streams (hyper-connections), its n-gram embeddings are read from the file a row at a time, and past 2,051 positions its attention is sparse, an indexer choosing the blocks each token reads. Against llama.cpp on the same file, 2,200 tokens of prose: the most likely token agrees at 2,171 positions (mean logit difference 0.134), and at all 148 positions read by the sparse attention. On 8,192 tokens, its last 512 positions (an Italian table of links and numbers) agree at 482, mean difference 0.191, and the same 512 tokens read after only 512 others agree at 482 too (0.178): the long context costs no agreement. Its multi-token prediction block comes from unsloth's GGUF of the block ([models](models.md)): its draft is the model's own next token at 130 of 158 positions of code, and on a chat reply on the CPU 29 of 40 drafts were kept, the text the same with drafts and without. The chat writes, token for token, the prompt the model's own template writes - the reasoning effort's words at the top of the system message, the earlier replies with their reasoning - and calls tools in its XML notation. `JANAS_HC_MIX=q8` rewrites its mixes of the streams - 1.26 GB of BF16 matrices read at every token, a third of what a token reads besides the experts - as Q8_0 when the model opens, half the bytes, their inputs kept at sixteen bits: against llama.cpp on 400 tokens of C the most likely token agreed at 392 positions (BF16: 390), on 400 of Italian prose at 364 (367), the mean logit difference 0.138 and 0.173 (0.135, 0.166); with the inputs quantized to eight bits as the quantized products take them, 388 and 359, and weights at four bits, 382 and 349. Those products run on the CPU, and in a prompt's blocks on the GPU (below). On the CPU (Core Ultra 9 185H, 12 threads, a 12 GiB expert cache, 1,024 tokens of prose and then 200) a reply runs at 11.9 tokens/s with them and at 10.95 with the BF16 mixes, the prompt at 24.0 either way; the default stays BF16. `--head 4` (`JANAS_HEAD_BITS=4`) takes its output head from Q5_K to Q4_K, 437 MB a token to 358, at a price in quality as on the other models: 387 and 351 positions (0.184, 0.234). A prompt's blocks run whole on a GPU that shares the host's memory (`JANAS_GPU_MOE=1` where the cache holds fewer than half the experts), its sparse attention too, choosing the same blocks as the CPU: on the last 512 of 8,192 tokens the most likely token agreed with llama.cpp at 481 positions (the CPU 482). Against llama.cpp, blocks of 256 tokens of C agree at 247 of 256 positions (the GPU's products beside the CPU: 248), of Italian prose at 230 (235 to 238; the CPU alone 235) with the lowest divergence of the three (KL 0.0172, against 0.0176 and 0.0186); its speed is not measured yet. The weights are under the Qwen Community License 1.0 ([MODELS.md](../MODELS.md#what-this-project-redistributes)); `janas-get qwen3.8-flash-next` downloads it converted, with its MTP block, from [`prabanta-dev/Qwen3.8-Flash-Next-JNS`](https://huggingface.co/prabanta-dev/Qwen3.8-Flash-Next-JNS) (68.4 GB, under the same licence); with `--from-gguf` it downloads ISTA's GGUF and converts it.- **Reasoning models:** the reasoning is shown apart, in grey, and can be turned off.- **`janas-chat`:** a terminal chat with a line editor of its own, history, colours, a status line and a context that slides instead of ending.diff --git a/docs/library.md b/docs/library.mdindex 574ae0d..e3f73b1 100644--- a/docs/library.md+++ b/docs/library.md@@ -27,6 +27,10 @@ cc yours.c -Iinclude -Lbin/x86_64-linux -ljanas_llm -o yoursA program that keeps the conversation itself - an HTTP client does, and sends it whole every time - hands it over with `janas_llm_chat_load`: the messages with their roles, the last one the user's. Whatever the new conversation shares with the one already computed is not read again, so the same messages plus a new one cost only the new one. `janas_llm_chat_prompt` continues raw text, with no chat format around it, and `janas_llm_default_system` gives the system message Janas's own programs use. `janas_llm_chat_stats` says why a reply ended and how much of its prompt was already computed. `janas_llm_chat_keep` keeps a few conversations computed besides the current one, for a program that switches between them: the one that shares most of the next request is copied back instead of being read again. `janas_llm_chat_prepare` reads the system message and the tools before the first message, a block at a time, so that a program can read them while its user writes; with `janas_llm_chat_keep_disk` a system message read once on a model is found on the disk by the next chat.+A model that sees (Qwen3.5 or Gemma 4, with its `.vision.jns` beside it: [models.md](models.md)) takes pictures: `janas_llm_chat_image(chat, bytes, n)` reads a PNG or a JPEG and queues it for the next user message, sent with `janas_llm_chat_send` or added with `janas_llm_chat_add`, before its text. `JANAS_LLM_EMODEL` says the model has no encoder.++Speech to text has a handle of its own, apart from the chat models: `janas_llm_speech_open(path, params, &s)` opens a Whisper model (made by `hf2jns_whisper`: [models.md](models.md)), `janas_llm_speech_transcribe(s, bytes, n, params)` reads a sound file (WAV, FLAC, MP3 and the other MPEG audio, Ogg, MP4/M4A with AAC or ALAC, AAC, WebM/Matroska; any rate and channels) and writes down what is said - in the language it is said in, found out or given (`language`), or translated into English (`translate`), with a `prompt` of names and terms to expect - and then `janas_llm_speech_text`, `janas_llm_speech_language`, `janas_llm_speech_duration`, `janas_llm_speech_segments` and `janas_llm_speech_segment` (each with its start and end in seconds) read it; `janas_llm_speech_segment_info` says how sure the model was; with `words = 1` in the transcription's parameters, `janas_llm_speech_words` and `janas_llm_speech_word` give every word with its start, end and probability, as OpenAI's word timestamps give them; `on_segment` in the parameters is called with each segment as soon as it is final (to show the text while the recording is still being read), `stop` ends a transcription between windows, `janas_llm_speech_segment_logprobs` and `janas_llm_speech_token` give each token's log-probability and bytes. With `chunking` only the speech that Silero VAD finds is read (`silero-vad.jns` beside the model, or `vad_model`); with `diarize`, `janas_llm_speech_turns` and `janas_llm_speech_turn` say who speaks when - each turn's speaker a known one's name (`speaker_names` and a few seconds of each in `speaker_refs`) or `A`, `B`... (`wespeaker-resnet34.jns` beside the model, or `speaker_model`). Opened after a chat model on the same thread, it gets that thread's CPUs, which the chat model pins to one: open it first, or from another thread.+## From FreeBASIC and BASIC MODERN[`include/janas/llm.bi`](../include/janas/llm.bi) declares the same library for FreeBASIC, and so for **BASIC MODERN**, the dialect implemented by Prabanta — the platform Janas is meant to sit inside. The same conversation as above:diff --git a/docs/models.md b/docs/models.mdindex 5c0cd17..250dfba 100644--- a/docs/models.md+++ b/docs/models.md@@ -78,6 +78,44 @@ Both steps are deterministic, so the file can be checked against the fingerprintRoom needed: three copies of the model at the worst moment — the GGUF, the converted file and the one with the planes — so about 145 GB for the largest. The flat file goes as soon as the planes are written, and the GGUF can go too, though keeping it saves the download the day a new version of the converter is worth running.+**The vision encoder** (Qwen3.5 and Gemma 4: E2B, E4B, 12B and 26B-A4B; pictures in janas-chat with `/image`, in janas-server's messages, `janas_llm_chat_image` in the library). Like the MTP block it comes from the original checkpoint, which `hf2jns_vision` reads from the directory a download from Hugging Face makes (`config.json`, `preprocessor_config.json` or `processor_config.json`, and the safetensors; of Gemma's checkpoint, which holds the whole model, only the vision tower is read):++```+bin/x86_64-linux/hf2jns_vision models/hf/Qwen/Qwen3.5-0.8B \+ models/jns/qwen3.5-0.8b.vision.jns+bin/x86_64-linux/hf2jns_vision models/hf/google/gemma-4-E2B-it \+ models/jns/gemma-4-e2b-it.vision.jns+```++The engine finds it beside the model: the `.vision.jns` whose name is the longest beginning of the model's (`qwen3.5-0.8b.vision.jns` serves `qwen3.5-0.8b-q4km.jns` and every other quantization of it). For Qwen3.5-0.8B it is 202 MB, its matrices kept in bfloat16 as the checkpoint has them. Its output against llama.cpp's (an mmproj made from the same checkpoint): cosine 0.999997 on average. Gemma 4 E2B's is 338 MB; a picture is made at most 280 tokens, as its processor does, and its output against llama.cpp's is at a cosine of 0.999999 on average, 0.99999 at worst, on pictures of 256 and 252 tokens. E4B is converted the same way and answers the same questions about the same pictures. The 26B-A4B too (1.1 GB, 27 blocks; cosine 0.999998 on average against llama.cpp): in its language model a picture's tokens see one another, the later ones too, so the engine, finding its encoder beside it, reads prompts in blocks that hold a picture whole (320 tokens). The 12B's has no blocks - a patch of 48x48 pixels normed, through one dense layer, plus its positions - 100 MB, at a cosine of 1.000000 against llama.cpp's, its pictures attended both ways as the 26B's.++**Speech to text: Whisper** (`janas-server --speech-model`, `janas_llm_speech_*` in the library). OpenAI's models from their Hugging Face checkpoints (`openai/whisper-small`, `openai/whisper-large-v3-turbo` and the others: `config.json`, `generation_config.json`, `preprocessor_config.json`, `tokenizer.json` and the safetensors), converted by `hf2jns_whisper`:++```sh+bin/x86_64-linux/hf2jns_whisper models/hf/openai/whisper-small \+ models/jns/whisper-small.jns+```++The matrices are kept in half precision, as OpenAI released them (the checkpoints' floats are their exact copies), laid out as the engine reads them; whisper-small is 489 MB. Against whisper.cpp on the same weights and the same sound, the logits of every token of a transcription agree to a cosine of 1.000000 on average, 0.999996 at worst (whisper.cpp without its half-precision flash attention, which alone takes it to 0.99996), and the spectrograms are the same; the tokenizer splits text as Hugging Face's does. The transcription goes window by window as OpenAI's does (language found, timestamps, temperatures raised where the text loops or is unlikely, silence skipped).++**Voice activity detection: Silero VAD** (chunking a recording, `chunking_strategy` in janas-server, `chunking` in the library), from its ONNX model (`onnx-community/silero-vad`, MIT, 2.2 MB), converted by `silero2jns` into a file beside the speech model, where it is found by itself:++```sh+bin/x86_64-linux/silero2jns models/hf/onnx-community/silero-vad/onnx/model.onnx \+ models/jns/silero-vad.jns+```++The weights of its 16 kHz model in single precision as the ONNX file holds them (read by Janas's own protobuf reader); its probability of speech in each 32 ms agrees with onnxruntime's on the same file to 1.7e-6 over 46 seconds of speech.++**Speaker embeddings: WeSpeaker ResNet34-LM** (who speaks when: `diarized_json` in janas-server, `diarize` in the library), from its ONNX model (`Wespeaker/wespeaker-voxceleb-resnet34-LM`, by the WeSpeaker project, trained on VoxCeleb2, CC-BY-4.0: credit to WeSpeaker, https://github.com/wenet-e2e/wespeaker), converted by `wespeaker2jns` into a file beside the speech model:++```sh+bin/x86_64-linux/wespeaker2jns models/hf/Wespeaker/wespeaker-voxceleb-resnet34-LM/voxceleb_resnet34_LM.onnx \+ models/jns/wespeaker-resnet34.jns+```++Its convolutions in half precision (13 MB); on the same filter banks its embedding agrees with onnxruntime's to a cosine of 0.9999993, and Janas's filter banks with Kaldi's (kaldi-native-fbank) to 1.6e-4.+**The multi-token prediction block** (Qwen3-Next and the dense Qwen3.5; optional, faster replies). Its own model carries it, GGUF files leave it out, so it comes from the original checkpoint. `hf2jns_mtp` can read just the block's tensors from Hugging Face, with range requests, instead of the shards that hold them:```shdiff --git a/docs/serving.md b/docs/serving.mdindex 84f0ef5..90c5f9c 100644--- a/docs/serving.md+++ b/docs/serving.md@@ -27,6 +27,7 @@ bin/x86_64-linux/janas-server qwen3-next.jns| `--port N` | 8080 by default || `--api-key KEY` | ask every client for this key, as `Authorization: Bearer KEY`; `JANAS_API_KEY` sets it too || `--name ID` | the model's name for clients; the file's name without `.jns` by default |+| `--quality <low\|medium\|high>` | the lowest quality the engine may choose to be faster where memory is short: `low` (default) the experts at four bits where they do not all fit and the light ones not in memory left out, `medium` the four bits only, `high` nothing lowered (see [the chat](chat.md)) || `--think on\|off` | reasoning before replying, for models that do it, when a request does not say || `--reasoning-budget N` | the longest reasoning, in tokens, when a request does not say: there it is closed and the model answers (-1: no limit, the default; 0: no reasoning) || `--test` | serve a chat page for trying it out, on `/test` (below) |@@ -34,7 +35,13 @@ bin/x86_64-linux/janas-server qwen3-next.jns| `--warn-input N` | longer inputs are taken, and said on stderr with their parts || `--fim-prefix N`, `--fim-suffix N`, `--fim-max N` | completions with a `suffix`, an editor filling in code at its cursor: the tokens before the cursor read at most (2048; 0: all) and the lines after it (8; 0: all); `--fim-max N` the tokens written there at most, whatever the request asks (256; 0: as asked), since an editor asks for thousands and the requests of the next keystrokes wait for them. Over the budget the text before the cursor is cut at a line to three quarters of it, and the start of the window is kept across keystrokes, also when the editor slides its window, so that a keystroke reads only what it changed (see [code completion](code-completion.md)) || `--metrics FILE`, `--progress S` | JSON lines of what every reply costs, and progress lines while a long prompt is read (see [Watching a long prompt](chat.md#watching-a-long-prompt)); `--verbose` prints progress every 30 s |+| `--tls-cert FILE`, `--tls-key FILE` | serve HTTPS with this certificate and key (PEM; below) |+| `--no-web` | refuse the `web_search` tool in `/v1/responses` (below) |+| `--code-interpreter` | run the code a model writes (`code_interpreter`, below), in a cage; off by default |+| `--code-interpreter-python PATH` | the `python3` inside the cage, a path under `/usr` (the system's by default) || `--no-mcp` | refuse tools of type `mcp` in `/v1/responses`: no connection leaves the server on a request's word (below) |+| `--mcp-allow HOST[:PORT]` | an MCP server at a private or local address that requests may name all the same (again for more; `[::1]` for an IPv6 address): by default only public addresses are reached (below) |+| `--allow-origin ORIGIN` | a web page of another origin that may call the server, written as the browser sends it (`http://localhost:3000`; again for more; `*` for any), answered with the CORS headers it needs; pages of other sites are refused (below) || `--keep N` | conversations kept computed besides the one in use (8; 0: none), so that clients taking turns, or a client's requests on the side for titles and tags, do not have their conversations read again from the start || `--keep-disk GIB` | disk for them below the memory, in `~/.cache/janas`, so that they outlive the server: a long system message and tools are read once (8; 0: none; never more than a quarter of the space left). Written only when a conversation leaves the memory and when the server stops || `--keep-memory GIB` | the memory for them; by default half of what the expert cache and the margin left to other programs leave free at start, at least 256 MiB |@@ -56,7 +63,12 @@ What it answers:| `POST /infill` | llama.cpp's route for filling in code, which editors' extensions made for llama.cpp ask (llama.vscode): `input_prefix`, `prompt`, `input_suffix`, `n_predict` (0: the prompt only, read for the next request), `n_indent`, `t_max_predict_ms`, the answer as llama.cpp's (`content`, `timings`); `input_extra` is not used, and the most likely token is always taken || `POST /v1/responses` and the six operations under it | OpenAI's newer API: items in and out, `previous_response_id`, conversations, streamed events, `background` and `cancel`, `input_items`, `input_tokens`, `compact` || `/v1/conversations` and its seven operations | conversations the server keeps, and their items |+| `/v1/files` and its four operations | files a client uploads (`purpose`, `expires_after`), listed, read, downloaded and deleted (below) |+| `/v1/uploads` and its three operations | a file sent in parts, joined into a File (below) |+| `/v1/batches` and its three operations | a file of requests run in the background (below) |+| `/v1/vector_stores` and its operations | stores of files cut into chunks and searched (below) || `POST /v1/embeddings` | with `--embedding-model FILE`: the vectors of texts (below) |+| `POST /v1/audio/transcriptions`, `/v1/audio/translations` | with `--speech-model FILE`: speech to text with a Whisper model (below) || `POST /v1/moderations` | asked of the chat model (below) || `GET /health` | `{"status":"ok"}` |@@ -80,9 +92,13 @@ A client that lets you set the address of an OpenAI-compatible API should need n**Tools.** `tools`, `tool_choice` (`none`, `auto`, `required`, a function) and `parallel_tool_calls`, and the older `functions`. The tools are written into the system message the way the model's chat template writes them, and a call the model opens is held to a grammar of the functions and their parameters until it closes it, so its arguments are always a JSON object valid against the function's schema. The calls come back in `tool_calls`, whole in one delta of the stream, and the reply ends with `"finish_reason": "tool_calls"`; sent back with the tools' answers, they are found again among the replies the server remembers, so the next turn reads only the answers. Qwen models write calls in one of two ways, and the template says which: as JSON (Qwen3, Qwen3-Next) or as XML (Qwen3.5 and 3.6, Qwen3-Coder). **Both have been run here**: the JSON way on Qwen3-4B and Qwen3-0.6B, the XML way on Qwen3.6-35B-A3B (two calls at once, their answers sent back, 422 of 471 prompt tokens reused on the next turn) and on Qwen3-Coder-Next, which describes the tools in a way of its own (two calls at once with valid arguments, their answers used in the reply, 496 of 562 prompt tokens reused).-**MCP servers in the Responses API.** A tool of type `mcp` (`server_label`, `server_url`, `headers`, `authorization`, `allowed_tools`, `require_approval`) has the server reach that MCP server over HTTP or HTTPS, as OpenAI's does. The response begins with an `mcp_list_tools` item (once per conversation), the model sees the tools as `server_label__tool`, and a call it writes either becomes an `mcp_approval_request` that ends the response - `require_approval` is `always` by default, as at OpenAI - and runs when the next request answers it with an `mcp_approval_response`, or, where no approval is needed, runs at once as an `mcp_call` item with its output, and the model goes on in the same response (up to eight rounds). Streamed, the calls come as `response.mcp_call.*` events. `allowed_tools` and the approval filters take names or `{tool_names, read_only}`; `tool_choice` may name an MCP tool. Connectors and tunnels are not served. A request makes the server open connections to the address it names: `--no-mcp` turns that off for a server others can reach. Tried with Qwen3-4B against the test server of `tests/mcp_fake_server.c`: a call run at once, one approved on the next request, and the same streamed; and through the `openai` SDK against DeepWiki over HTTPS, as OpenAI's own examples do: its tool listed, called, and the model answering from what it returned.+**MCP servers in the Responses API.** A tool of type `mcp` (`server_label`, `server_url`, `headers`, `authorization`, `allowed_tools`, `require_approval`) has the server reach that MCP server over HTTP or HTTPS, as OpenAI's does. The response begins with an `mcp_list_tools` item (once per conversation), the model sees the tools as `server_label__tool`, and a call it writes either becomes an `mcp_approval_request` that ends the response - `require_approval` is `always` by default, as at OpenAI - and runs when the next request answers it with an `mcp_approval_response`, or, where no approval is needed, runs at once as an `mcp_call` item with its output, and the model goes on in the same response (up to eight rounds). Streamed, the calls come as `response.mcp_call.*` events. `allowed_tools` and the approval filters take names or `{tool_names, read_only}`; `tool_choice` may name an MCP tool. Connectors and tunnels are not served. A request makes the server open connections to the address it names, so it reaches only public ones: every address the name resolves to is checked, and the connection goes to one that passed, so a name that resolves to a public address when checked and to a private one when used gains nothing. Refused are loopback (127/8, ::1), private (10/8, 172.16/12, 192.168/16, fc00::/7), shared (100.64/10), link-local (169.254/16, where clouds answer with their machines' credentials, and fe80::/10), multicast, documentation, benchmarking and the other reserved ranges, and IPv4 addresses inside IPv6 ones (mapped, NAT64, 6to4) are judged as themselves; the error says `not a public address`. An MCP server of your own on this machine or the local network is named with `--mcp-allow` (`--mcp-allow localhost:3001`, `--mcp-allow intranet.lan`: the host as the request writes it, with or without a port). `--no-mcp` turns the connections off altogether, for a server others can reach. Tried with Qwen3-4B against the test server of `tests/mcp_fake_server.c`: a call run at once, one approved on the next request, and the same streamed; and through the `openai` SDK against DeepWiki over HTTPS, as OpenAI's own examples do: its tool listed, called, and the model answering from what it returned.-**JSON output.** `response_format` `json_object` or `json_schema` (and `text.format` in the Responses API): the answer is held to a grammar token by token, so it is valid JSON when the model ends it (not when `max_tokens` cuts it). A schema is held to its types, the properties of an object in the schema's order with the required ones always there and no others, array items and bounds up to 32, string lengths up to 64, `enum`, `const`, `anyOf`, `oneOf`, `$ref` within the schema and `nullable`; `pattern`, `format` and numeric bounds are not enforced. The reasoning, where there is one, stays free.+**File search in the Responses API.** A tool of type `file_search` (`vector_store_ids`, `max_num_results` up to 50, six by default since every passage goes into the model's context, `filters`, `ranking_options.score_threshold`) gives the model a search of those vector stores: it writes the query, the server searches every store and gives it the best passages, numbered on from any source found before. The response holds a `file_search_call` item with the query and the results (file, score, text, attributes), and the text `file_citation` annotations (file, name, index) for the `[n]` the model wrote. `tool_choice: {"type": "file_search"}` makes it search first. With Qwen3-4B and three files: "Which cheese goes into spaghetti alla carbonara?" searched, found the right file, answered "pecorino romano ... [1]", [1] a citation of that file.++**Web search in the Responses API.** A tool of type `web_search` (or its dated and preview names) gives the model a search of Wikipedia, the open source the project's services use: the model writes the query and the language, the server asks that Wikipedia (the English one when the first has nothing) and gives the model the best five pages' first sentences, numbered. The response holds a `web_search_call` item with the query and the pages' addresses, and the text the `url_citation` annotations of the `[n]` the model wrote after the facts. `tool_choice: {"type": "web_search"}` makes it search first; `--no-web` refuses the tool. Wikipedia is not news: the tool tells the model so. With Qwen3-4B: "Who designed the Eiffel Tower?" answered "Gustave Eiffel ... 1889 [3]", [3] annotated with the page; in Italian, streamed, "Eleonora d'Arborea" searched on the Italian Wikipedia and answered from it with [1]. The stream gives the search as an output item added and done, not yet as OpenAI's `response.web_search_call.*` events.++**JSON output.** `response_format` `json_object` or `json_schema` (and `text.format` in the Responses API): the answer is held to a grammar token by token, so it is valid JSON when the model ends it (not when `max_tokens` cuts it). A schema is held to its types, the properties of an object in the schema's order with the required ones always there and no others, array items and bounds up to 32, string lengths up to 4,096, `enum`, `const`, `anyOf`, `oneOf`, `$ref` within the schema and `nullable`. Integers are held to `minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum` and `multipleOf` (up to 50) exactly, digit by digit, so a number that cannot end inside the bounds is never begun; other numbers to their bounds without an exponent, with a fraction only where no fraction can cross a bound (up to 10 writes 10, never 10.5; from 1.5 writes 2 and more). The formats `date`, `time`, `date-time`, `uuid`, `email` and `ipv4` are held as their grammar (a date's month 01 to 12 and day 01 to 31, a time with its offset, each octet of an address 0 to 255). A string's `pattern` is held as its grammar too: characters and classes (`[a-z]`, `[^,]`, `\d`, `\w`, `\s`, `.`), groups, alternatives and repetitions (`*`, `+`, `?`, `{m,n}` up to 256), found anywhere in the string unless anchored with `^` and `$` as JSON Schema says; a quote, a backslash or a control character the pattern asks for is written escaped. A pattern with anchors inside, back references, look-arounds or Unicode classes is not enforced (the string is left free), nor are `minLength` and `maxLength` beside a pattern or a format. The other formats and `allOf` of more than one schema are not enforced. The reasoning, where there is one, stays free.**Log-probabilities.** `logprobs` and `top_logprobs` (up to 20) in chat, `logprobs` in completions, `top_logprobs` in responses. They are the model's own: where a grammar forced a token the model found unlikely, its log-probability says so.@@ -90,14 +106,34 @@ A client that lets you set the address of an OpenAI-compatible API should need n**Embeddings** come from a second, small model opened with `--embedding-model`, such as Qwen3-Embedding-0.6B: texts or token ids, one or many, `dimensions` to cut the vector shorter, floats or `base64`. Against llama.cpp on the same file the vectors of the same text agree to a cosine of 0.98 to 0.996, and the similarities between texts come out alike.+**Speech to text** comes from a Whisper model opened with `--speech-model` (converted by `hf2jns_whisper`: [models.md](models.md)), beside the chat model and apart from its queue, one recording at a time: `POST /v1/audio/transcriptions` writes down what is said in the language it is said in (found out, or `language`), `POST /v1/audio/translations` into English, as OpenAI's `whisper-1` answers - `file` (every format OpenAI lists - flac, m4a, mp3, mp4, mpeg, mpga, oga, ogg, wav, webm - and a few more, at any rate and with any channels, read by Janas's own decoders: WAV, FLAC, MP3 and the other MPEG audio, Ogg with Vorbis, Opus or FLAC, MP4/M4A/MOV with AAC (LC, Main, LTP, HE-AAC v1 and v2), ALAC, MP3, Opus, FLAC or PCM, AAC in ADTS frames, WebM/Matroska with Opus, Vorbis, FLAC, AAC, MPEG audio, ALAC or PCM; the encoder's delay and padding trimmed as the container says; AAC's low-delay and USAC kinds are refused with a 400 that says so), `model` (any name: there is one speech model), `prompt` (words and spelling to expect, or the text said before), `temperature`, `response_format` `json`, `text`, `srt`, `vtt` or `verbose_json` (the language, the duration and the segments with their times, tokens, average log-probability, probability of silence and compression ratio - the text's bytes over their zlib-compressed bytes, to the byte as zlib makes them, by Janas's own deflate - which, above 2.4, has the window decoded again at a higher temperature, as OpenAI's Whisper does), `timestamp_granularities[]` `segment` and `word` with `verbose_json` (the words with their start and end, found as OpenAI's open-source Whisper finds them: the alignment heads' attention over the sound, its path by dynamic time warping, punctuation joined to its word, the next window starting at the last word's end), `chunking_strategy` (`auto`, or `{"type": "server_vad", "threshold", "prefix_padding_ms", "silence_duration_ms"}`: only the stretches of speech Silero VAD finds are read, the windows within each of them, the text before carried on; `silero-vad.jns` beside the speech model, or `--vad-model`), `response_format` `diarized_json` with `known_speaker_names[]` and `known_speaker_references[]` (data: URLs of a few seconds of each; see below), `include[]=logprobs` (each text token's log-probability and bytes, with json), `stream` (server-sent events: a `transcript.text.delta` for each segment as soon as it is final, then `transcript.text.done` with the whole text and the usage; a client that goes away stops the transcription at its next window), `usage` in seconds. **Who speaks when** (`diarized_json`, as OpenAI's `gpt-4o-transcribe-diarize`): segments of type `transcript.text.segment`, each a speaker's turn with its start, end, text and `speaker` - a known speaker's name, or `A`, `B`... in the order they first speak - also streamed as `transcript.text.segment` events once the recording is read; the speakers found by WeSpeaker's ResNet34 embeddings over windows of 1.5 s of the speech and WeSpeaker's spectral clustering, each word given the speaker of the windows over it; `wespeaker-resnet34.jns` beside the speech model, or `--speaker-model`. Tried with the `openai` SDK on a meeting of 50 seconds spoken by three synthetic voices: ten turns of ten, the three speakers found, the two known ones named. Tried with `curl` and the `openai` SDK and whisper-small: John F. Kennedy's 11 seconds word for word in one segment, 46 seconds of Italian in ten segments with their times (the same as whisper.cpp's but for two words, a comma and one time 40 ms apart) and translated into English, srt, vtt, a text file and an unknown language refused; with the `openai` SDK again, word times, log-probabilities, chunking by voice activity and an M4A of 46 seconds streamed.+**Moderations** are asked of the chat model: at temperature 0 it answers OpenAI's thirteen categories with a JSON object held to a schema, and each score is the chance it gave `true` against `false` in that place. It is a general model following instructions, not a classifier trained for the task.-**Kept on disk.** Stored completions, responses and conversations are written, one file each, to `~/.local/share/janas/server` (or `--store DIR`), and read back when the server starts again; the oldest go past a thousand of each. The directory and its files are readable by their owner alone, since they hold what the clients said, and one server at a time uses a directory. `--no-store` keeps them in memory only, until the server stops. A response running in the background when the server is killed is found as failed at the next start. Images and sound in messages are refused with a 400 that says so.+**Kept on disk.** Stored completions, responses and conversations are written, one file each, to `~/.local/share/janas/server` (or `--store DIR`), and read back when the server starts again; the oldest go past a thousand of each, and past 1 GiB of them all, whatever their kind; a document is at most 64 MiB, so a conversation that would grow past it gets a 400 (`conversation_too_large`). The directory and its files are readable by their owner alone, since they hold what the clients said, and one server at a time uses a directory. `--no-store` keeps them in memory only, until the server stops. A response running in the background when the server is killed is found as failed at the next start. Sound in messages is refused with a 400 that says so; pictures are read by the models that see (below).++**Files.** `POST /v1/files` takes an upload as the SDKs send it (`multipart/form-data`: `file`, `purpose` - assistants, batch, fine-tune, vision, user_data or evals - and `expires_after` from an hour to 30 days; a batch's files expire after 30 days, as at OpenAI). The bytes go to the store's directory (`.files` in it, readable by its owner alone), each under its id; the name the client gave is shown, never used as a path. Without `--store`, or with `--no-store`, they go to a directory of their own under `TMPDIR`, removed when the server stops. An upload is at most `--max-body` (32 MiB unless raised); all the files together at most 16 GiB, and a gigabyte of disk is always left free. `GET /v1/files` lists them newest first (`purpose`, `order`, `limit`, `after`), `GET /v1/files/{id}/content` gives the bytes back. Tried with the `openai` SDK: an upload with binary bytes read back the same, an expiry, the list by purpose and in pages, a delete then a 404, a wrong purpose a 400.++**Uploads**, for a file larger than one request: `POST /v1/uploads` announces it (`filename`, `purpose`, `bytes` up to 8 GiB, `mime_type`), `POST /v1/uploads/{id}/parts` sends a part of up to 64 MiB (whatever `--max-body` says), `POST /v1/uploads/{id}/complete` names the parts in order and joins them into a File, checked against the size announced and the `md5` when given (a wrong one is a 400, and the upload stays open to try again); `cancel` drops it. An upload lasts an hour. Its parts wait beside the files; a server that restarts forgets what was pending. The `openai` SDK's `uploads.upload_file_chunked` sent 150 MiB in three parts with their MD5 and got the same bytes back.++**Batches.** A JSONL file of requests (uploaded with purpose `batch`, up to 50,000 lines and 200 MB, each `{"custom_id", "method": "POST", "url", "body"}`) to `/v1/chat/completions`, `/v1/responses`, `/v1/completions`, `/v1/embeddings` or `/v1/moderations` is checked first (a line that is not JSON, a url other than the batch's endpoint, a custom_id twice: `failed`, with the lines in `errors`), then run one request at a time behind every request a client is waiting for: a client never waits for more than the one line running (a chat reply took 0.12 s while a batch ran). Each line goes through the same code as the request it stands for; the answers go to the output file, the requests answered with an error to the error file, both Files with purpose `batch_output` once the batch ends (30 days, or `output_expires_after`), and `request_counts` and `usage` follow. A batch is saved after every line: stopped with the server, it goes on from there at the next start. `cancel` stops it after the line running, keeping what was done; past its 24 hours it ends `expired` the same way. A line with `"stream": true` is refused. Tried with the `openai` SDK on Qwen3-0.6B: a batch with two bad lines, an invalid file, a cancel halfway (13 of 30 kept), a server stopped after 3 lines of 12 and started again (12 answers, none twice).-**Every other operation of OpenAI's API** - there are 345 in the version it follows (2.3.0) - has a route that answers 501 and says why: not written yet, needs a model of another kind (sound, images, video), trains models, or belongs to the administration of OpenAI's service. The table is generated from the specification by `tools/openapi_routes`, which also counts what is done.+**Vector stores.** A store holds files uploaded here (text: plain, Markdown, code, JSON, CSV; up to 32 MiB each; a file that is not text is `failed` with `unsupported_file`), each cut into chunks of about the tokens its chunking strategy says (`auto`: 800 tokens overlapping by 400; `static` from 100 to 4,096) at whitespace. With `--embedding-model` each chunk gets its embedding, made in the background behind every client's request, and a search compares a query's (asked as a query, as Qwen3-Embedding wants) with all of them by their cosine; without one, the search reads the words (BM25), so a store works on any machine. `POST /v1/vector_stores/{id}/search` takes a query or several, `max_num_results` (up to 50), `filters` on the files' `attributes` (`eq`, `ne`, `gt`, `gte`, `lt`, `lte`, `in`, `nin`, `and`, `or`) and `ranking_options.score_threshold`, and gives the chunks best first with a score from 0 to 1. Stores, their files and attributes are listed, read, changed and deleted as at OpenAI, and kept beside the files (`.vectors`); a batch of files (`file_batches`, up to 2,000, with `file_ids` or `files` each with its own attributes) is attached at once, followed by its counts, cancelled (the files still waiting are), and listed. Tried with the `openai` SDK, Qwen3-Embedding-0.6B and without: three files of different subjects, each question found its own file first (0.64 to 0.70 with embeddings, the others below 0.23), a binary file refused, the filters, the lists, a rename, a delete.++**Evals.** An eval names the shape of its data (`data_source_config`, `custom` with an `item_schema`) and its graders (`testing_criteria`): `string_check` (`eq`, `ne`, `like`, `ilike`), `text_similarity` (`fuzzy_match`, `bleu`, `gleu`, `meteor`, `rouge_1` to `rouge_5`, `rouge_l`, `cosine` over word counts, with `pass_threshold`), and `label_model` and `score_model`, which the server's own model answers, held by a JSON schema to one of the `labels` or to a number in `range`; `python` graders are refused. A run takes the rows in the request (`file_content`) or in a JSONL file uploaded here (`file_id`), up to 10,000; with a `completions` or `responses` data source the model writes each row's sample from `input_messages` (a template filled with `{{item.field}}`, or an `item_reference`), with a `jsonl` one the rows bring their own `sample`. The graders read `{{item.…}}` and `{{sample.output_text}}`. A run goes on in the background behind every client's request, row by row: its `result_counts` and `per_testing_criteria_results` grow as it goes, each row's output item (sample, each grader's score, pass or fail) is kept beside the files (`.evals`) and listed by status; a run is saved after every row, so it goes on after a restart, and `cancel` stops it after the row it is on. Tried with the `openai` SDK and Qwen3-4B: three questions answered and graded by all four kinds of grader (the model as judge said `correct` and scored 10 of 10); a wrong expected answer judged `incorrect`; a run cancelled after 2 rows of 8; a server stopped after 2 rows of 5 and started again (5 output items, none twice).++**Assistants.** The Assistants API (`/v1/assistants`, `/v1/threads`, their messages, runs and run steps: 23 operations), which OpenAI is retiring in favour of the Responses API, is served for the programs written for it. An assistant keeps its instructions, its tools (functions and `file_search`) and its `tool_resources` (vector stores of this server, given or made of `file_ids`); a thread keeps its messages, and the files attached to a message for `file_search` go into the thread's own vector store. A run goes on in the background: the thread's messages (the last ones only with `truncation_strategy`), the instructions, the tools and the sampling (the run's, else the assistant's) go to the same code as `/v1/responses`; a search becomes a `tool_calls` step with its results, the answer a message of the thread with its citations as `file_citation` annotations, and calls of functions make the run wait (`requires_action`, ten minutes) for `submit_tool_outputs`, after which it goes on. `cancel` stops the model at its next token; a run is saved after every pass, so one cut by a stop goes on at the next start. With `stream: true` (creating a run, a thread and run, or submitting tool outputs) the run's events come as OpenAI sends them - `thread.run.*`, `thread.run.step.*`, `thread.message.created`, `thread.message.delta` as the model writes, `thread.message.completed`, then `done` when the run ends or waits for tool outputs - so the SDKs' event handlers work. With `--code-interpreter`, an assistant's `code_interpreter` runs in the thread's own container, kept from run to run with its variables, with the files of the assistant's and the thread's `code_interpreter` resources (and those attached to messages for it) in `/mnt/data`; a step shows the code, its logs and its images, the images made Files (purpose `assistants_output`), and a file the answer names becomes a `file_path` annotation. Pictures in the messages are read as in the Responses API (below). Tried with the `openai` SDK and Qwen3-4B: a question, a function called with the right arguments and its output used in the answer, a file attached to a message searched and cited, a cancel in 2 s, a run cut by a stop and finished after the restart; streamed, three sentences came in 50 pieces as written, and the function and the search the same way.++**Pictures in messages.** A model that sees - Qwen3.5 and Gemma 4 for now, with its vision encoder beside it (below, and [models.md](models.md)) - reads the pictures of the user's messages: `image_url` parts in chat completions (`{"url": ...}`) and `input_image` parts in the Responses API (an `image_url`, or the `file_id` of a file uploaded here), the url a `data:` URL (base64) or an `http(s)` one, which the server fetches at public addresses only (as the MCP servers, `--mcp-allow` naming the exceptions), 16 MiB at most a picture and 16 a request; PNG and JPEG, read by Janas's own decoders, the same pixels as Pillow. A picture of more than a megapixel is made smaller (Pillow's bicubic, as the model's processor does), so it is at most 1024 tokens (`JANAS_IMAGE_MAX_TOKENS` changes it; Gemma 4 at most 280, as its processor). A model without an encoder answers 400 with the reason; a picture in a message other than the user's, or bytes that are not a picture, get a 400 too. With Qwen3.5-0.8B through the `openai` SDK: a picture of a red circle and a blue square, "I see a red circle and a blue square." (217 tokens of prompt, under 5 s), the same picture by its `file_id` and by a `data:` URL in the Responses API, a URL at 127.0.0.1 refused.++**Code interpreter.** Off unless the server is started with `--code-interpreter`: the code a model writes then runs on this machine, so it is the choice of who runs the server. The code runs in a cage Janas builds itself (`src/common/sandbox.c`, Linux, without privileges: user namespaces, which most distributions allow): no network at all, no other process to see or start (threads only), none of your files - the root holds the system's `/usr` read-only, a few files of `/etc` the programs need, a `/tmp` of 256 MiB in memory and the container's own directory at `/mnt/data` - limits on memory, file sizes and open files, no privileges and a seccomp filter on the system calls that could reach the kernel's less guarded parts; whatever the code leaves in `/mnt/data` (a link to one of your files, say) is never followed by the server. Each container has one Python session, as a notebook: variables and imports stay from one piece of code to the next, the value of the last expression is shown, figures drawn with matplotlib are saved into `/mnt/data` (unless the code saved an image itself); a piece of code that runs past two minutes ends the session. The libraries are the system's (with Debian's packages: numpy, scipy, sympy, matplotlib, Pillow). In `/v1/responses` the tool `code_interpreter` (`container` an id, or `{"type": "auto", "file_ids", "memory_limit"}`) gives the model a function to run Python, run by the server: a `code_interpreter_call` item holds the code and its outputs (logs, and images as the URL of the container's file), and a file the answer names becomes a `container_file_citation`. The Containers API (`/v1/containers`, nine operations) makes containers (`file_ids` copied in, `memory_limit` 1g to 64g, `expires_after` up to 20 minutes since the last use), lists, reads and deletes them, and uploads, lists, reads, downloads and deletes their files; an expired container's session ends and its files go. Tried with the `openai` SDK and Qwen3-4B: the 30th Fibonacci number computed, a CSV uploaded and summed (the model corrected its code after two tracebacks), a chart drawn and downloaded as a PNG, a variable kept from one response to the next, links to `/etc/passwd` and `/home` planted by the code never listed nor served; and in `tests/test_sandbox.c` the cage against code that wants out (your files, writing outside, the network, other processes, a fork, 3 GiB of memory, a 16 MiB file, a full `/tmp`, an endless loop), with mutations of the cage caught.++**Every other operation of OpenAI's API** - there are 345 in the version it follows (2.3.0), 100 of them served - has a route that answers 501 and says why: not written yet, needs a model of another kind (sound, images, video), trains models, or belongs to the administration of OpenAI's service. The table is generated from the specification by `tools/openapi_routes`, which also counts what is done.**One request at a time.** The model holds one sequence, so requests wait in line and run in the order they came; when the line is full the answer is 429. While a streamed request waits, the stream says where it stands in the line (`: queue N`, a comment clients skip) and the test page shows it. A client that closes the connection while a reply streams stops the reply.-**On the network.** It listens on this machine alone unless `--host` says otherwise, and then it warns if there is no key. There is no HTTPS yet: across a network you do not trust, put it behind something that adds it.+**On the network.** It listens on this machine alone unless `--host` says otherwise, and then it warns if there is no key. Across a network, serve HTTPS: `--tls-cert cert.pem --tls-key key.pem` (PEM, the certificate's chain after it, the key not encrypted; RSA or EC) makes the server speak HTTPS alone on its port, TLS 1.2 and 1.3 through the Mbed TLS it carries, and checks at start that the key is the certificate's. Every request, stream and limit is the same as in clear. Not on Windows yet. Tried with a certificate of its own: `curl` and the `openai` SDK (models, a chat, a stream, four requests at once), a request in clear refused, an untrusted certificate refused by the client, a key that is not the certificate's refused at start.++**Web pages.** A page of any site you visit can make your browser send requests to `127.0.0.1`: it cannot read the answers, but the model would still run and a tool of type `mcp` would still connect, and a site that makes its own name resolve to `127.0.0.1` would read them too. The browser says which page is asking, in the `Origin` header, and the server refuses every page but its own (`/test`, at an address or at `localhost`, this machine's name or the `--host` given) with a 403 (`origin_not_allowed`), key or no key. Programs send no `Origin` and are not concerned. A web client served from another origin - a chat on `http://localhost:3000` that calls the server from the browser - is let in with `--allow-origin http://localhost:3000`, and gets the CORS headers it needs (`OPTIONS` answered too); `--allow-origin '*'` lets in any page, which undoes the protection.**The test page.** With `--test`, `http://127.0.0.1:8080/test` is *Janas-Chat Web*: a chat in the browser that talks to the API like any client, with the reasoning in grey, a line under each reply with the context used and the speed (last, lowest, highest and mean of the conversation), and the settings - system message, temperature, reasoning, key - behind *Settings*. It reads the marks a model writes as `janas-chat` does, and also quotes, tables, struck text and links; a link asks before it opens, because links an AI writes can be wrong or unsafe. *Markdown sample* shows a text of ours with every mark it knows, without asking the model. The page is inside the program, loads nothing from anywhere else and may talk only to this server.diff --git a/include/janas/janas_oop.bi b/include/janas/janas_oop.biindex 7a4f1da..3b1b77e 100644--- a/include/janas/janas_oop.bi+++ b/include/janas/janas_oop.bi@@ -40,6 +40,12 @@ enum PowerMode explicitMax = JANAS_LLM_MODE_MAX '' whatever is fastestend enum+enum Quality explicit '' the lowest the engine goes on its own+ Low = JANAS_LLM_QUALITY_LOW '' fastest where memory is short+ Medium = JANAS_LLM_QUALITY_MEDIUM '' experts at four bits, none left out+ High = JANAS_LLM_QUALITY_HIGH '' nothing lowered+end enum+enum AttentionMode explicitAuto = JANAS_LLM_ATTN_AUTOExact = JANAS_LLM_ATTN_EXACT '' the query as it is, summed in floating pointdiff --git a/include/janas/llm.bi b/include/janas/llm.biindex 7dbf77f..c9cdad8 100644--- a/include/janas/llm.bi+++ b/include/janas/llm.bi@@ -41,6 +41,13 @@ const JANAS_LLM_MODE_AUTO = 0const JANAS_LLM_MODE_ECO = 1const JANAS_LLM_MODE_MAX = 2+'' the lowest quality the engine may choose on its own to be faster where+'' memory is short (min_quality): low lowers the experts' bits and leaves+'' out light ones not in memory, medium only the bits, high neither+const JANAS_LLM_QUALITY_LOW = 0+const JANAS_LLM_QUALITY_MEDIUM = 1+const JANAS_LLM_QUALITY_HIGH = 2+'' how the attention scores are computed: the query as it is, or to sixteen'' bits with an exact integer sum (about twice as fast, an error of 3e-5)const JANAS_LLM_ATTN_AUTO = 0@@ -64,6 +71,8 @@ type janas_llm_paramsreserve_bytes as ulongint '' memory left to other programs (0: a fifth)head_bits as long '' 4: a six-bit output head read at four'' (faster, less exact); 0 or 6: as in the file+ min_quality as long '' JANAS_LLM_QUALITY_* (0: low, the engine's+ '' choice); expert_bits and head_bits winend typetype janas_llm_chat_params@@ -278,6 +287,12 @@ declare function janas_llm_chat_system(byval c as janas_llm_chat ptr, _declare function janas_llm_chat_prepare(byval c as janas_llm_chat ptr, _byval done as long ptr, _byval total as long ptr) as long+'' A picture (the bytes of a PNG or a JPEG) for the next user message, sent+'' or added: read now by the model's vision encoder (a <model>.vision.jns+'' beside the model), placed before the message's text.+declare function janas_llm_chat_image(byval c as janas_llm_chat ptr, _+ byval data_ as const any ptr, _+ byval n as longint) as longdeclare function janas_llm_chat_send(byval c as janas_llm_chat ptr, _byval text as const zstring ptr, _byval length as long) as long@@ -419,10 +434,122 @@ declare function janas_llm_chat_reset(byval c as janas_llm_chat ptr) as longdeclare function janas_llm_chat_stats(byval c as const janas_llm_chat ptr, _byval s as janas_llm_chat_stats ptr) as long++'' Speech to text: a Whisper model (a .jns made by tools/hf2jns_whisper),+'' apart from the chat models. A recording - the bytes of a WAV, FLAC,+'' MP3, Ogg, M4A, AAC or WebM file - is transcribed, or translated into+'' English; then its text, language and segments are read. One thread at+'' a time.+type janas_llm_speech as any++type janas_llm_speech_params+ size as ulong '' sizeof(janas_llm_speech_params)+ threads as long '' 0: one a performance core+ vad_model as const zstring ptr '' Silero VAD for chunking; NULL:+ '' silero-vad.jns beside the model+ speaker_model as const zstring ptr '' WeSpeaker for diarization; NULL:+ '' wespeaker-resnet34.jns beside it+end type++type janas_llm_transcribe_params+ size as ulong '' sizeof(janas_llm_transcribe_params)+ translate as long '' 1: into English+ language as const zstring ptr '' "it" or "italian"; NULL: found out+ prompt as const zstring ptr '' text said before, names; or NULL+ seed as ulongint '' of the sampling+ temperature as single '' 0: the likeliest words (a higher one only+ '' where they loop or are unlikely); up to 1+ words as long '' 1: the words' times too+ on_segment as sub cdecl(byval user as any ptr, byval segment as long)+ '' each segment once final; or NULL+ user as any ptr+ stop as long ptr '' nonzero stops it between windows; or NULL+ chunking as long '' 0 whole, 1 auto, 2 the voice activity below+ vad_threshold as single '' 0.5+ vad_prefix_ms as long '' 300+ vad_silence_ms as long '' 200+ diarize as long '' 1: who speaks when (turns)+ n_speakers as long '' known speakers, at most 8+ speaker_names as const zstring const ptr ptr+ speaker_refs as const any const ptr ptr '' their recordings+ speaker_ref_bytes as const longint ptr+end type++declare sub janas_llm_speech_params_default(byval p as janas_llm_speech_params ptr)+declare function janas_llm_speech_open(byval path as const zstring ptr, _+ byval p as const janas_llm_speech_params ptr, _+ byval out as janas_llm_speech ptr ptr) as long+declare sub janas_llm_speech_close(byval s as janas_llm_speech ptr)+declare sub janas_llm_transcribe_params_default(byval p as janas_llm_transcribe_params ptr)+declare function janas_llm_speech_transcribe(byval s as janas_llm_speech ptr, _+ byval dat as const any ptr, _+ byval n as longint, _+ byval p as const janas_llm_transcribe_params ptr) as long+declare function janas_llm_speech_text(byval s as const janas_llm_speech ptr, _+ byval buf as zstring ptr, _+ byval cap as long, _+ byval length as long ptr) as long+declare function janas_llm_speech_language(byval s as const janas_llm_speech ptr, _+ byval buf as zstring ptr, _+ byval cap as long, _+ byval length as long ptr) as long+declare function janas_llm_speech_language_name(byval code as const zstring ptr) _+ as const zstring ptr+declare function janas_llm_speech_duration(byval s as const janas_llm_speech ptr) as double+declare function janas_llm_speech_segments(byval s as const janas_llm_speech ptr) as long+declare function janas_llm_speech_segment(byval s as const janas_llm_speech ptr, _+ byval i as long, _+ byval start as double ptr, _+ byval end_ as double ptr, _+ byval buf as zstring ptr, _+ byval cap as long, _+ byval length as long ptr) as long+declare function janas_llm_speech_segment_info(byval s as const janas_llm_speech ptr, _+ byval i as long, _+ byval avg_logprob as single ptr, _+ byval no_speech_prob as single ptr, _+ byval temperature as single ptr, _+ byval compression_ratio as single ptr, _+ byval ids as long ptr, _+ byval max as long, _+ byval n as long ptr) as long+declare function janas_llm_speech_segment_logprobs(byval s as const janas_llm_speech ptr, _+ byval i as long, _+ byval lp as single ptr, _+ byval max as long, _+ byval n as long ptr) as long+declare function janas_llm_speech_token(byval s as const janas_llm_speech ptr, _+ byval id as long, _+ byval buf as zstring ptr, _+ byval cap as long, _+ byval length as long ptr) as long+declare function janas_llm_speech_token_is_text(byval s as const janas_llm_speech ptr, _+ byval id as long) as long+declare function janas_llm_speech_words(byval s as const janas_llm_speech ptr) as long+declare function janas_llm_speech_turns(byval s as const janas_llm_speech ptr) as long+declare function janas_llm_speech_turn(byval s as const janas_llm_speech ptr, _+ byval i as long, _+ byval start as double ptr, _+ byval end_ as double ptr, _+ byval speaker as zstring ptr, _+ byval speaker_cap as long, _+ byval speaker_len as long ptr, _+ byval buf as zstring ptr, _+ byval cap as long, _+ byval length as long ptr) as long+declare function janas_llm_speech_word(byval s as const janas_llm_speech ptr, _+ byval i as long, _+ byval start as double ptr, _+ byval end_ as double ptr, _+ byval probability as single ptr, _+ byval buf as zstring ptr, _+ byval cap as long, _+ byval length as long ptr) as long+end extern'' The parameter blocks carry their own size, and every call checks it, so a'' header out of step with the library is refused instead of misread. On-'' x86-64 the sizes are 64, 80 and 192 bytes (22 Oct 2026), the C header's-'' field for field; a program can see for itself with sizeof() and with-'' janas_llm_abi_version().+'' x86-64 the sizes are 64, 80 and 192 bytes (22 Oct 2026), 24 and 112 for+'' speech's (29 Oct 2026), the C header's field for field; a program can see+'' for itself with sizeof() and with janas_llm_abi_version().diff --git a/include/janas/llm.h b/include/janas/llm.hindex 6a90e76..76d0000 100644--- a/include/janas/llm.h+++ b/include/janas/llm.h@@ -59,6 +59,11 @@ extern "C" {#define JANAS_LLM_MODE_ECO 1#define JANAS_LLM_MODE_MAX 2+/* The lowest quality the engine may choose on its own (min_quality) */+#define JANAS_LLM_QUALITY_LOW 0+#define JANAS_LLM_QUALITY_MEDIUM 1+#define JANAS_LLM_QUALITY_HIGH 2+typedef struct janas_llm janas_llm; /* an open model */typedef struct janas_llm_chat janas_llm_chat; /* a conversation */@@ -115,6 +120,21 @@ struct janas_llm_params {requantized for both: Qwen3.5-2B 9% faster, 352 where 376. 0 or 6:as in the file (default) */int32_t head_bits;+ /*+ * The lowest quality the engine may go down to on its own, where the+ * machine is short of memory for the experts and would wait for the disk+ * at every token: JANAS_LLM_QUALITY_*. LOW (default): whatever reads+ * fastest there - the experts at four bits when they do not all fit in+ * memory (5 to 7 positions of 400 where the most likely token differs+ * from all six), and with less than a fifth of them in memory the light+ * ones not in RAM left out (Nemotron 3.5 Bolt with 2.7 GB: 391 of 400+ * against llama.cpp where 396, 6% faster). MEDIUM: four bits, no expert+ * left out. HIGH: neither, every bit of every expert the router chose.+ * With the memory to hold them all, all three read the same. The levers+ * a caller sets (expert_bits, head_bits, janas_llm_set_experts) are its+ * own choice and stand whatever this says.+ */+ int32_t min_quality;};/*@@ -376,6 +396,19 @@ JANAS_LLM_API int32_t janas_llm_chat_system(janas_llm_chat *c, const char *text,JANAS_LLM_API int32_t janas_llm_chat_prepare(janas_llm_chat *c, int32_t *done,int32_t *total);+/*+ * A picture (the n bytes of a PNG or a JPEG) for the next user message,+ * sent (janas_llm_chat_send) or added (janas_llm_chat_add): read now by the+ * model's vision encoder - a <model>.vision.jns beside the model, made by+ * tools/hf2jns_vision (Qwen3.5) - and placed before the message's text, in+ * the order given. JANAS_LLM_EMODEL when the model has no encoder,+ * JANAS_LLM_EINVAL when the bytes are not a picture this reads. A picture+ * of more than a megapixel is made smaller (JANAS_IMAGE_MAX_TOKENS: its+ * tokens at most, 1024 by default).+ */+JANAS_LLM_API int32_t janas_llm_chat_image(janas_llm_chat *c, const void *data,+ int64_t n);+/** Sends a user message; the reply follows through janas_llm_chat_next. A* reply still being read is cut where it is and closed first.@@ -766,6 +799,152 @@ struct janas_llm_chat_stats {JANAS_LLM_API int32_t janas_llm_chat_stats(const janas_llm_chat *c,struct janas_llm_chat_stats *s);+/*+ * Speech to text: a Whisper model (a .jns made by tools/hf2jns_whisper from+ * a Hugging Face checkpoint, openai/whisper-*), apart from the chat models.+ * A recording - the bytes of a WAV, FLAC, MP3 (or other MPEG audio), Ogg,+ * MP4/M4A, AAC or WebM file, any rate and channels - is+ * transcribed in the language it is spoken in, or translated into English;+ * then its text, its language and its segments with their times are read.+ * A speech model is used from one thread at a time.+ */+typedef struct janas_llm_speech janas_llm_speech;++struct janas_llm_speech_params {+ uint32_t size; /* sizeof(struct janas_llm_speech_params) */+ int32_t threads; /* 0: one a performance core (default) */+ const char *vad_model; /* a voice activity detector (Silero VAD, made+ by tools/silero2jns) for chunking; NULL:+ silero-vad.jns beside the speech model, if+ there is one (default) */+ const char *speaker_model; /* speaker embeddings (WeSpeaker ResNet34,+ made by tools/wespeaker2jns) for+ diarization; NULL: wespeaker-resnet34.jns+ beside the speech model, if there is one+ (default) */+};+JANAS_LLM_API void+janas_llm_speech_params_default(struct janas_llm_speech_params *p);+/* p may be NULL; *out gets the model. JANAS_LLM_EOPEN when the file is not+ a Whisper model this reads. */+JANAS_LLM_API int32_t+janas_llm_speech_open(const char *path, const struct janas_llm_speech_params *p,+ janas_llm_speech **out);+JANAS_LLM_API void janas_llm_speech_close(janas_llm_speech *s);++struct janas_llm_transcribe_params {+ uint32_t size; /* sizeof(struct janas_llm_transcribe_params) */+ int32_t translate; /* 1: into English (default 0) */+ const char *language; /* "it", or "italian"; NULL: found out (default) */+ const char *prompt; /* text said before, or the words and spelling+ expected (names, terms); NULL: none */+ uint64_t seed; /* of the sampling */+ float temperature; /* 0: the likeliest words, a higher temperature+ only where they go round in a loop or are+ unlikely (default); up to 1: sampled from the+ start at that temperature */+ int32_t words; /* 1: the words' times too, as OpenAI's finds+ them (default 0) */+ /* called during the transcription with each segment once it is final+ (janas_llm_speech_segment and the others read it), in order; NULL:+ none */+ void (*on_segment)(void *user, int32_t segment);+ void *user;+ /* set to nonzero (from another thread) to stop it between windows;+ NULL: none */+ volatile int32_t *stop;+ /* how the recording is cut, as OpenAI's chunking_strategy: 0 whole+ (default); 1 auto (loudness normalized, then voice activity+ detection with the defaults below); 2 voice activity detection with+ vad_threshold, vad_prefix_ms and vad_silence_ms. Only the speech is+ read, its windows within each stretch of it. */+ int32_t chunking;+ float vad_threshold; /* 0.5 */+ int32_t vad_prefix_ms; /* 300: kept before speech */+ int32_t vad_silence_ms; /* 200: that ends a stretch of speech */+ /* 1: who speaks when (janas_llm_speech_turns), as OpenAI's+ gpt-4o-transcribe-diarize; known speakers, at most 8: their names and+ a short recording of each (the bytes of a sound file, 2-10 s) */+ int32_t diarize;+ int32_t n_speakers;+ const char *const *speaker_names;+ const void *const *speaker_refs;+ const int64_t *speaker_ref_bytes;+};+JANAS_LLM_API void+janas_llm_transcribe_params_default(struct janas_llm_transcribe_params *p);+/* The n bytes of a sound file (WAV, FLAC, MP3, Ogg, M4A, WebM; any rate and+ channels; up to four hours at 48 kHz) transcribed; p may be NULL.+ JANAS_LLM_EINVAL when the bytes are not a sound this reads, or the+ language is unknown. */+JANAS_LLM_API int32_t+janas_llm_speech_transcribe(janas_llm_speech *s, const void *data, int64_t n,+ const struct janas_llm_transcribe_params *p);+/* Of the last transcription: its text (as janas_llm_describe writes+ strings), its language's code ("it"), its length in seconds, its+ segments. */+JANAS_LLM_API int32_t janas_llm_speech_text(const janas_llm_speech *s,+ char *buf, int32_t cap,+ int32_t *len);+JANAS_LLM_API int32_t janas_llm_speech_language(const janas_llm_speech *s,+ char *buf, int32_t cap,+ int32_t *len);+/* The English name of a language, in lower case ("italian"), from its+ code; NULL for a code Whisper does not know. */+JANAS_LLM_API const char *janas_llm_speech_language_name(const char *code);+JANAS_LLM_API double janas_llm_speech_duration(const janas_llm_speech *s);+JANAS_LLM_API int32_t janas_llm_speech_segments(const janas_llm_speech *s);+/* Segment i: start and end in seconds, its text (beginning with a space,+ as the model writes it). */+JANAS_LLM_API int32_t janas_llm_speech_segment(const janas_llm_speech *s,+ int32_t i, double *start,+ double *end, char *buf,+ int32_t cap, int32_t *len);+/* How sure the model was of segment i: the mean log-probability of its+ tokens, the probability that its window held no speech, the+ temperature it was decoded at, its window's compression ratio (its+ text's bytes over their zlib-compressed bytes, as OpenAI's: high when it+ repeats itself); and its tokens (ids of the model's vocabulary, at most+ max into ids, *n the count; ids may be NULL). */+JANAS_LLM_API int32_t janas_llm_speech_segment_info(+ const janas_llm_speech *s, int32_t i, float *avg_logprob,+ float *no_speech_prob, float *temperature, float *compression_ratio,+ int32_t *ids, int32_t max, int32_t *n);+/* The words of the last transcription asked with words = 1, all segments'+ in order: their count, and word i's start and end in seconds, its+ probability and its text (with its leading space, punctuation joined,+ as OpenAI's word timestamps give them). */+/* The log-probability of each of segment i's tokens (at most max into+ lp, *n the count), as janas_llm_speech_segment_info gives its tokens. */+JANAS_LLM_API int32_t janas_llm_speech_segment_logprobs(+ const janas_llm_speech *s, int32_t i, float *lp, int32_t max, int32_t *n);+/* The bytes a token of the speech model stands for (as+ janas_llm_describe writes strings; a token may end inside a UTF-8+ character: join them before reading them as text). */+JANAS_LLM_API int32_t janas_llm_speech_token(const janas_llm_speech *s,+ int32_t id, char *buf, int32_t cap,+ int32_t *len);+/* 1 when the token is text, 0 when it is a special one (a timestamp, the+ end of text...). */+JANAS_LLM_API int32_t janas_llm_speech_token_is_text(const janas_llm_speech *s,+ int32_t id);+JANAS_LLM_API int32_t janas_llm_speech_words(const janas_llm_speech *s);+/* The turns of the last transcription asked with diarize = 1: their count,+ and turn i's start and end in seconds, its speaker (a known speaker's+ name, or "A", "B"... in the order they first speak) and its text. */+JANAS_LLM_API int32_t janas_llm_speech_turns(const janas_llm_speech *s);+JANAS_LLM_API int32_t janas_llm_speech_turn(const janas_llm_speech *s,+ int32_t i, double *start,+ double *end, char *speaker,+ int32_t speaker_cap,+ int32_t *speaker_len, char *buf,+ int32_t cap, int32_t *len);+JANAS_LLM_API int32_t janas_llm_speech_word(const janas_llm_speech *s,+ int32_t i, double *start,+ double *end, float *probability,+ char *buf, int32_t cap,+ int32_t *len);+#ifdef __cplusplus}#endifdiff --git a/src/chat/edit.h b/src/chat/edit.hindex 958e333..bf60171 100644--- a/src/chat/edit.h+++ b/src/chat/edit.h@@ -14,10 +14,12 @@#define JANAS_CHAT_EDIT_H#include <errno.h>+#include <fcntl.h>#include <poll.h>#include <stdio.h>#include <stdlib.h>#include <string.h>+#include <sys/stat.h>#include <unistd.h>#include "scroll.h"@@ -65,9 +67,18 @@ static void hist_save(const struct janas_hist *h){if (!h->path[0])return;- FILE *f = fopen(h->path, "w");- if (!f)+ /* the owner's alone: what was typed to the model */+ int fd = open(h->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);+ if (fd < 0)+ return;+#ifndef _WIN32+ fchmod(fd, 0600); /* a file an older version made 0644 */+#endif+ FILE *f = fdopen(fd, "w");+ if (!f) {+ close(fd);return;+ }for (int i = 0; i < h->n; i++)fprintf(f, "%s\n", h->line[i]);fclose(f);diff --git a/src/chat/main.c b/src/chat/main.cindex 029b530..ebe59c1 100644--- a/src/chat/main.c+++ b/src/chat/main.c@@ -61,7 +61,7 @@ static double sess_tokens, sess_seconds;static const char *const commands[] = {"/help", "/about", "/stats", "/reset", "/system", "/temp","/spec", "/think", "/mode", "/experts", "/markdown", "/mcp",- "/context", "/quit", "/exit", NULL};+ "/context", "/image", "/quit", "/exit", NULL};static void on_sigwinch(int sig){@@ -302,10 +302,17 @@ static void usage(void)" model a .jns file, or a name of the catalog (none: the\n"" list, to choose one; a model not here yet is\n"" downloaded, after asking)\n"- " --profile <quality|fast>\n"- " quality (default): answers as the file gives\n"- " them; fast: also what was measured faster on\n"- " this model at a small cost to its answers\n"+ " --quality <high|medium|low>\n"+ " the lowest quality the engine may choose to\n"+ " be faster where memory is short: low (the\n"+ " default) the experts at four bits, the light\n"+ " ones not in memory left out; medium: four\n"+ " bits, nothing left out; high: nothing\n"+ " lowered. With memory for every expert all\n"+ " three read alike. Asked for, low also takes\n"+ " what was measured faster on this model at a\n"+ " small cost (the head at four bits). The\n"+ " levers below (--bits, --head...) win over it\n"" --models <dir> where the models are (default: JANAS_MODELS,\n"" or ~/.local/share/janas/models)\n"" --tune measure again how this model runs fastest on\n"@@ -416,6 +423,9 @@ static const struct {{"/system <text>", "new conversation with this system message"},{"/system off", "new conversation with no system message"},{"/temp <t>", "temperature (0: greedy)"},+ {"/image <file>",+ "a picture (PNG or JPEG) for your next message, for models that see "+ "(their vision file beside them)"},{"/spec [on|off]", "speculative decoding (toggles)"},{"/mode [auto|eco|max]", "power mode, and what the engine chose"},{"/experts [n]",@@ -965,7 +975,7 @@ static const char *const OPTIONS[] = {"--system", "--mcp-config", "--progress", "--temp","--top-k", "--top-p", "--min-p", "--seed","--max", "--head", "--profile", "--models",- "--prompt-on", "--drafts-on", "--reply-on"};+ "--quality", "--prompt-on", "--drafts-on", "--reply-on"};#define N_OPTIONS (sizeof OPTIONS / sizeof *OPTIONS)/* v of option a as a whole number or a number within limits; on a@@ -1012,6 +1022,7 @@ static int take_options(int n, char **w, struct chat_opts *o, char *why,static const char *const BITS[] = {"2", "4", "6"};static const char *const HEAD[] = {"4", "6"};static const char *const PROFILES[] = {"quality", "fast"};+ static const char *const QUALITY[] = {"high", "medium", "low"};for (int i = 0; i < n; i++) {const char *a = w[i], *v = i + 1 < n ? w[i + 1] : NULL;long long x;@@ -1129,9 +1140,17 @@ static int take_options(int n, char **w, struct chat_opts *o, char *why,if ((k = opt_choice(a, v, BITS, 3, why, why_len)) < 0)return -1;o->mp->expert_bits = 2 + 2 * k;+ } else if (strcmp(a, "--quality") == 0) {+ if ((k = opt_choice(a, v, QUALITY, 3, why, why_len)) < 0)+ return -1;+ o->mp->min_quality = 2 - k; /* JANAS_LLM_QUALITY_* */+ *o->profile = k == 2; /* the catalog's choices: low */} else if (strcmp(a, "--profile") == 0) {+ /* the older name: quality is high, fast is low */if ((k = opt_choice(a, v, PROFILES, 2, why, why_len)) < 0)return -1;+ o->mp->min_quality =+ k ? JANAS_LLM_QUALITY_LOW : JANAS_LLM_QUALITY_HIGH;*o->profile = k;} else if (strcmp(a, "--models") == 0) {setenv("JANAS_MODELS", v, 1); /* read when the model is found */@@ -1195,6 +1214,18 @@ static int take_options(int n, char **w, struct chat_opts *o, char *why,*//* A question answered with one key. Piped in, nobody is there to answer:the caller asked for it in writing and that is the answer. */+/* A key for a question: a window resized meanwhile (SIGWINCH, no+ SA_RESTART) is not an answer, Ctrl+C is (no) */+static int answer_byte(unsigned char *k)+{+ for (;;) {+ int r = tty_byte(k);+ if (r < 0 && errno == EINTR && !interrupted)+ continue;+ return r;+ }+}+static int ask_yes(const char *question){if (!term.tty)@@ -1202,7 +1233,7 @@ static int ask_yes(const char *question)term_printf(&term, "%s [y/N] ", question);term_raw(&term, 1);unsigned char k = 0;- if (tty_byte(&k) < 0)+ if (answer_byte(&k) < 0)k = 0;term_raw(&term, 0);int yes = k == 'y' || k == 'Y';@@ -1368,8 +1399,12 @@ static void conf_write(const char *path, const struct chat_opts *o)fprintf(f, "--mcp-auto\n");if (*o->mcp_instructions)fprintf(f, "--mcp-instructions\n");- if (*o->profile)- fprintf(f, "--profile fast\n");+ if (m->min_quality == JANAS_LLM_QUALITY_HIGH)+ fprintf(f, "--quality high\n");+ else if (m->min_quality == JANAS_LLM_QUALITY_MEDIUM)+ fprintf(f, "--quality medium\n");+ else if (*o->profile) /* asked for: with the catalog's choices */+ fprintf(f, "--quality low\n");fclose(f);term_printf(&term, "written to %s (the system message is not kept)\n",path);@@ -1408,7 +1443,7 @@ static int ask_key(const char *question)term_printf(&term, "%s ", question);term_raw(&term, 1);unsigned char k = 0;- if (tty_byte(&k) < 0)+ if (answer_byte(&k) < 0)k = 0;term_raw(&term, 0);if (k >= 'A' && k <= 'Z')@@ -1503,12 +1538,18 @@ static int run_calls(janas_llm_chat *c, int autorun)for every time, even with --mcp-auto: never "always"; shownwhole, since the user says yes to what it holds */int writes = mcpc_writes(name);- int cut = writes ? INT_MAX : 400;- term_printf(&term, "%s→ %s%s %s%.*s%s%s\n",- mcpc_own(name) && !writes ? T_DIM(&term) : T_CMD(&term),- name, T_DIM(&term), "", cut, args,- strlen(args) > (size_t)cut ? " ..." : "", T_RESET(&term));int go = !writes && (autorun || mcpc_always(name) || mcpc_own(name));+ /* whole where the user is asked: a yes (or an always) is to all of+ it, not to its first 400 bytes */+ int cut = go ? 400 : INT_MAX;+ char *sname = term_cleaned(name), *sargs = term_cleaned(args);+ term_printf(+ &term, "%s→ %s%s %s%.*s%s%s\n",+ mcpc_own(name) && !writes ? T_DIM(&term) : T_CMD(&term),+ sname ? sname : "?", T_DIM(&term), "", cut, sargs ? sargs : "?",+ sargs && strlen(sargs) > (size_t)cut ? " ..." : "", T_RESET(&term));+ free(sname);+ free(sargs);char *key = call_key(name, args);if (failed_before(key)) {go = 0;@@ -1557,16 +1598,20 @@ static int run_calls(janas_llm_chat *c, int autorun)failed.call[failed.n++] = key;key = NULL;}- const char *t = texts[i] ? texts[i] : "";- size_t line = strcspn(t, "\n");+ char *t = term_cleaned(texts[i] ? texts[i] : "");+ const char *tt = t ? t : "";+ size_t line = strcspn(tt, "\n");term_printf(&term, "%s%s%.*s%s%s\n", rc ? T_WARN(&term) : T_DIM(&term),- rc > 0 ? "tool error: " : "", (int)(line < 200 ? line : 200), t,- line < strlen(t) || line > 200 ? " ..." : "", T_RESET(&term));+ rc > 0 ? "tool error: " : "", (int)(line < 200 ? line : 200),+ tt, line < strlen(tt) || line > 200 ? " ..." : "",+ T_RESET(&term));+ free(t);if (shown && *shown) {/* the user's, as the tool wrote it (a page): the modelhas only a note of it */term_gap(&term);+ shown[term_clean(shown, strlen(shown))] = 0;term_printf(&term, "%s\n", shown);}free(shown);@@ -1634,9 +1679,19 @@ static int show_reply(const janas_llm *llm, janas_llm_chat *c, int markdown,if (conv.back)scroll_draw(&term, &conv); /* re-wrapped to the new width */}- md_write(&md, piece, (size_t)len);if (len > 0)tail = piece[len - 1];+ /* what the model writes drives no terminal; its tabs (code) four+ spaces */+ char wide[4 * sizeof(piece)];+ size_t wn = 0;+ for (int32_t i = 0; i < len; i++)+ if (piece[i] == '\t')+ for (int k = 0; k < 4; k++)+ wide[wn++] = ' ';+ else+ wide[wn++] = piece[i];+ md_write(&md, wide, term_clean(wide, wn));fflush(stdout);struct janas_llm_chat_stats rs = {.size = sizeof(rs)};if (len == 0 && janas_llm_chat_stats(c, &rs) == JANAS_LLM_OK &&@@ -1757,6 +1812,7 @@ int main(int argc, char **argv)int gpu = 1; /* the GPU may be given work, if there is one */const char *mcp_config = NULL; /* NULL: the default file */int mcp = 1, mcp_auto = 0, mcp_instructions = 0, services = 1, no_mtp = 0;+ /* profile: the catalog's faster choices, at --quality low asked */int all_tools = 0, profile = 0, tune = 0, npu_int8 = 0;int devices[JANAS_DEVICES_OPTIONS] = {0, 0, 0};static char mtp_found[4096];@@ -1773,6 +1829,8 @@ int main(int argc, char **argv)*/char cpath[1024];char *conf_text = NULL, *mconf_text = NULL;+ char **old_texts = NULL; /* those /load-config replaced, freed at the end */+ int n_old_texts = 0;int cn = 0, mn = 0;char **cw = conf_path(cpath, sizeof(cpath), 0, NULL) == 0? conf_read(cpath, &cn, &conf_text)@@ -1795,18 +1853,32 @@ int main(int argc, char **argv)char **mw = conf_path(mpath, sizeof(mpath), 0, cm.key) == 0? conf_read(mpath, &mn, &mconf_text): NULL;- const char *pv = chat_words_last(argc - oi, argv + oi, "--profile");- if (!pv)- pv = chat_words_last(mn, mw, "--profile");- if (!pv)- pv = chat_words_last(cn, cw, "--profile");+ /* the quality asked (--quality, or --profile its older name): typed,+ else the model's file, else the chat's; low when none says */+ const char *qs[3][2] = {+ {chat_words_last(argc - oi, argv + oi, "--quality"),+ chat_words_last(argc - oi, argv + oi, "--profile")},+ {chat_words_last(mn, mw, "--quality"),+ chat_words_last(mn, mw, "--profile")},+ {chat_words_last(cn, cw, "--quality"),+ chat_words_last(cn, cw, "--profile")}};+ const char *qv = NULL;+ int asked = 0;+ for (int i = 0; i < 3 && !qv; i++)+ if ((qv = qs[i][0] ? qs[i][0] : qs[i][1]) != NULL)+ asked = 1;+ /* low by default lets the engine choose by the machine (memory short:+ faster, plenty: the file's quality); asked for, it adds the+ catalog's choices measured faster on this model whatever the+ machine (the head at four bits, say) */+ int low = asked && (!strcmp(qv, "low") || !strcmp(qv, "fast"));char why[400];- if (pv && strcmp(pv, "fast") == 0) {+ if (low) {const char *fw = cm.entry ? cm.entry->fast : NULL;- if (!fw)- fprintf(stderr, "profile fast: nothing measured faster on this "- "model yet, so the same as quality\n");- else {+ if (!fw) {+ fprintf(stderr, "quality low: nothing measured faster on this "+ "model beyond the engine's own choices\n");+ } else {/* the catalog's words, split on spaces */static char fbuf[256];char *pw[16];@@ -1819,7 +1891,10 @@ int main(int argc, char **argv)fprintf(stderr, "janas-chat: catalog: %s\n", why);return 2;}- fprintf(stderr, "profile fast: %s\n", fw);+ fprintf(stderr,+ "quality low: %s (measured faster on this "+ "model; --quality high keeps the file's)\n",+ fw);}}/* the levers the user set, in any file or typed: the measure leaves@@ -2101,7 +2176,15 @@ int main(int argc, char **argv)if (take_options(cn, cw, &o, bad, sizeof bad) != 0)term_printf(&term, "%s: %s\n", cpath, bad);free(cw);- free(conf_text); /* the older words are let go */+ /* the older words stay: options not in the new file+ (an MTP path, the MCP configuration) still point+ into them, and /save-config reads those */+ char **ot = realloc(old_texts, (size_t)(n_old_texts + 1) *+ sizeof(*ot));+ if (ot) {+ old_texts = ot;+ old_texts[n_old_texts++] = conf_text;+ }conf_text = text;if (janas_llm_chat_set_params(c, &cp) != JANAS_LLM_OK)term_printf(&term, "%s\n", janas_llm_last_error());@@ -2177,6 +2260,30 @@ int main(int argc, char **argv)}janas_llm_experts(llm, &used, &most);term_printf(&term, "%d experts per token of %d\n", used, most);+ } else if (strcmp(msg, "/image") == 0) {+ /* a picture for the next message, read now */+ FILE *f = *arg ? fopen(arg, "rb") : NULL;+ long fn = -1;+ char *data = NULL;+ if (f && fseek(f, 0, SEEK_END) == 0 && (fn = ftell(f)) > 0 &&+ fn <= (32L << 20) && fseek(f, 0, SEEK_SET) == 0 &&+ (data = malloc((size_t)fn)) &&+ fread(data, 1, (size_t)fn, f) != (size_t)fn) {+ free(data);+ data = NULL;+ }+ if (f)+ fclose(f);+ if (!*arg)+ term_printf(&term, "/image <file>: a PNG or a JPEG\n");+ else if (!data)+ term_printf(&term, "cannot read %s\n", arg);+ else if (janas_llm_chat_image(c, data, fn) != JANAS_LLM_OK)+ term_printf(&term, "%s\n", janas_llm_last_error());+ else+ term_printf(&term, "the picture goes with your next "+ "message\n");+ free(data);} else if (strcmp(msg, "/temp") == 0 && *arg) {cp.temperature = (float)atof(arg);if (janas_llm_chat_set_params(c, &cp) != JANAS_LLM_OK)@@ -2255,6 +2362,9 @@ int main(int argc, char **argv)janas_llm_close(llm);scroll_free(&conv);free(conf_text);+ for (int i = 0; i < n_old_texts; i++)+ free(old_texts[i]);+ free(old_texts);if (term.tty) {hist_save(&history);hist_free(&history);diff --git a/src/chat/mcp_chat.c b/src/chat/mcp_chat.cindex f84cae7..94c808e 100644--- a/src/chat/mcp_chat.c+++ b/src/chat/mcp_chat.c@@ -349,10 +349,42 @@ void mcpc_close_services(void)opened_prefix = NULL;}+/* Does p clash with a server's prefix: the same, or one the start of the+ other ("a__" and "a__b__"), so that a tool name would go to the wrong+ server? */+static int prefix_clash(const char *p)+{+ for (size_t i = 0; i < n_servers; i++) {+ size_t a = strlen(p), b = strlen(servers[i].prefix);+ if (strncmp(p, servers[i].prefix, a < b ? a : b) == 0)+ return 1;+ }+ return 0;+}++/* The name's prefix, made unique among the servers': a.b and a_b both+ come out a_b__, the second a_b_2__ */+static char *unique_prefix(const char *name)+{+ char *base = prefix_of(name);+ if (!base || !prefix_clash(base))+ return base;+ size_t n = strlen(base) - 2; /* without its "__" */+ char *p = malloc(n + 16);+ for (int k = 2; p && k < 100000; k++) {+ snprintf(p, n + 16, "%.*s_%d__", (int)n, base, k);+ if (!prefix_clash(p))+ break;+ }+ free(base);+ return p;+}+static int add_server(const char *line, janas_mcp *m, int own){+ /* the prefix against the servers as they are, before the table moves */+ char *name = strdup(line), *prefix = unique_prefix(line);struct server *g = realloc(servers, (n_servers + 1) * sizeof(*g));- char *name = strdup(line), *prefix = prefix_of(line);if (!g || !name || !prefix) {if (g)servers = g;diff --git a/src/chat/term.h b/src/chat/term.hindex 48cad11..a8abc90 100644--- a/src/chat/term.h+++ b/src/chat/term.h@@ -19,6 +19,7 @@#include <stdio.h>#include <stdlib.h>#include <string.h>+#include <errno.h>#include <stdarg.h>#include <stdint.h>#include <wchar.h>@@ -250,6 +251,46 @@ static void term_puts(struct janas_term *t, const char *s)term_write(t, s, strlen(s));}+/*+ * Text from outside - a model's reply, a tool's arguments and results - with+ * the bytes that drive the terminal taken out, in place: a tab becomes a+ * space (the width the wrapping counts; the terminal would jump to its own+ * stops), and the control characters but newline go (ESC, which starts the+ * sequences that move the cursor, set the clipboard (OSC 52) or switch screens;+ * carriage return; backspace) and DEL, and the C1 controls written in UTF-8+ * (U+0080 to U+009F: 0xC2 0x80-0x9F, CSI among them). The new length.+ */+static size_t term_clean(char *s, size_t n)+{+ size_t w = 0;+ for (size_t i = 0; i < n; i++) {+ unsigned char c = (unsigned char)s[i];+ if (c == '\t')+ c = ' ';+ if ((c < 0x20 && c != '\n') || c == 0x7f)+ continue;+ if (c == 0xc2 && i + 1 < n && (unsigned char)s[i + 1] >= 0x80 &&+ (unsigned char)s[i + 1] <= 0x9f) {+ i++;+ continue;+ }+ s[w++] = (char)c;+ }+ return w;+}++/* A copy cleaned (term_clean), NUL-terminated; NULL on memory. */+static char *term_cleaned(const char *s)+{+ size_t n = strlen(s);+ char *d = malloc(n + 1);+ if (!d)+ return NULL;+ memcpy(d, s, n);+ d[term_clean(d, n)] = 0;+ return d;+}+__attribute__((format(gnu_printf, 2, 3))) static voidterm_printf(struct janas_term *t, const char *fmt, ...){@@ -678,8 +719,9 @@ enum {static int term_key(void){unsigned char c;- if (tty_byte(&c) != 1)- return TERM_KEY_ESC; /* the input ended: let the page close */+ if (tty_byte(&c) != 1) /* the input ended: let the page close; a+ resize (EINTR) only redraws it */+ return errno == EINTR ? TERM_KEY_OTHER : TERM_KEY_ESC;if (c != 27)return c;unsigned char lead = 0, final = 0;diff --git a/src/common/audio.c b/src/common/audio.cnew file mode 100644index 0000000..f43f58d--- /dev/null+++ b/src/common/audio.c@@ -0,0 +1,162 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio.c - a sound file recognized by its first bytes and decoded+ * (audio.h); resampling.+ */+#include "audio.h"+#include "audio_ogg.h"++#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++int janas_audio_decode(const void *data, size_t n, struct janas_audio *a,+ char *why, size_t wn)+{+ memset(a, 0, sizeof *a);+ const uint8_t *d = data;+ /* an ID3v2 tag before the sound (MP3's habit, FLAC's sometimes) */+ while (n >= 10 && !memcmp(d, "ID3", 3) && d[3] < 0xFF && d[4] < 0xFF &&+ !((d[6] | d[7] | d[8] | d[9]) & 0x80)) {+ size_t len = 10 ++ ((size_t)d[6] << 21 | (size_t)d[7] << 14 |+ (size_t)d[8] << 7 | d[9]) ++ (d[5] & 0x10 ? 10 : 0); /* a footer */+ if (len > n)+ break;+ d += len;+ n -= len;+ }+ int rc = janas_audio_wav(d, n, a, why, wn);+ if (rc <= 0)+ return rc;+ if ((rc = janas_audio_flac(d, n, a, why, wn)) <= 0)+ return rc;+ if ((rc = janas_audio_ogg(d, n, a, why, wn)) <= 0)+ return rc;+ if ((rc = janas_audio_mp4(d, n, a, why, wn)) <= 0)+ return rc;+ if ((rc = janas_audio_adts(d, n, a, why, wn)) <= 0)+ return rc;+ if ((rc = janas_audio_mkv(d, n, a, why, wn)) <= 0)+ return rc;+ if ((rc = janas_audio_mpeg(d, n, a, why, wn)) <= 0)+ return rc;+ snprintf(why, wn,+ "not a sound file this reads (WAV, FLAC, MP3 and MPEG audio, "+ "Ogg, MP4/M4A, AAC, WebM/Matroska)");+ return -1;+}++void audio_edit_apply(const struct audio_edit *e, uint32_t rate, size_t *skip,+ size_t *total)+{+ size_t got = *total;+ *skip = 0;+ if (!e || e->media_time < 0 || !e->timescale)+ return;+ uint64_t s = (uint64_t)e->media_time * rate / e->timescale;+ *skip = s < got ? (size_t)s : got;+ *total = got - *skip;+ if (e->length && e->movie_timescale) {+ uint64_t k = e->length * rate / e->movie_timescale;+ if (k < *total)+ *total = (size_t)k;+ }+}++void janas_audio_free(struct janas_audio *a)+{+ free(a->pcm);+ memset(a, 0, sizeof *a);+}++static uint32_t gcd(uint32_t a, uint32_t b)+{+ while (b) {+ uint32_t t = a % b;+ a = b;+ b = t;+ }+ return a;+}++/* the zeroth-order modified Bessel function, for the Kaiser window */+static double bessel_i0(double x)+{+ double s = 1, t = 1;+ for (int k = 1; k < 50; k++) {+ t *= (x / (2 * k)) * (x / (2 * k));+ s += t;+ if (t < 1e-12 * s)+ break;+ }+ return s;+}++#define ZEROS 32 /* zero crossings of the sinc on either side */+#define BETA 8.6 /* the Kaiser window's: about 90 dB of rejection */+#define ROLLOFF 0.95 /* the cut-off, of the lower Nyquist frequency */++/* the filter at distance x (in input samples), cut-off fc (cycles a+ sample), half-width hw samples */+static double kernel(double x, double fc, double hw)+{+ if (fabs(x) >= hw)+ return 0;+ double s = x == 0 ? 1 : sin(M_PI * 2 * fc * x) / (M_PI * 2 * fc * x);+ double r = x / hw;+ return 2 * fc * s * bessel_i0(BETA * sqrt(1 - r * r)) / bessel_i0(BETA);+}++int janas_audio_resample(struct janas_audio *a, uint32_t rate)+{+ if (a->rate == rate || !a->n)+ return a->rate = rate, 0;+ double ratio = (double)rate / a->rate;+ double fc = 0.5 * ROLLOFF * (ratio < 1 ? ratio : 1);+ double hw = ZEROS / (2 * fc); /* in input samples */+ size_t out_n = (size_t)floor(a->n * ratio);+ float *out = malloc(sizeof(float) * (out_n ? out_n : 1));+ if (!out)+ return -1;+ /* output i sits at input i * in / out: with in / g and out / g the+ rates' reduced ratio the phases repeat every L = out / g outputs */+ uint32_t g = gcd(a->rate, rate), L = rate / g, M = a->rate / g;+ int taps = (int)ceil(2 * hw) + 1;+ float *table = NULL;+ if ((size_t)L * (size_t)taps <= ((size_t)1 << 22))+ table = malloc(sizeof(float) * L * (size_t)taps);+ if (table)+ for (uint32_t p = 0; p < L; p++) {+ double frac = (double)((uint64_t)p * M % L) / L;+ for (int t = 0; t < taps; t++) {+ long k = t - taps / 2;+ table[(size_t)p * taps + t] =+ (float)kernel((double)k - frac, fc, hw);+ }+ }+ for (size_t i = 0; i < out_n; i++) {+ uint64_t num = (uint64_t)i * M;+ long base = (long)(num / L);+ uint32_t p = (uint32_t)(i % L);+ double frac = (double)(num % L) / L, s = 0;+ for (int t = 0; t < taps; t++) {+ long k = base + t - taps / 2;+ if (k < 0 || (size_t)k >= a->n)+ continue;+ double h = table ? table[(size_t)p * taps + t]+ : kernel((double)(t - taps / 2) - frac, fc, hw);+ s += h * a->pcm[k];+ }+ out[i] = (float)s;+ }+ free(table);+ free(a->pcm);+ a->pcm = out;+ a->n = out_n;+ a->rate = rate;+ return 0;+}diff --git a/src/common/audio.h b/src/common/audio.hnew file mode 100644index 0000000..5c30ab9--- /dev/null+++ b/src/common/audio.h@@ -0,0 +1,63 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio.h - sound read from its files into mono float samples in [-1, 1],+ * as the speech models take it: the channels averaged, then resampled to+ * the model's rate. Janas's own decoders (audio_wav.c, audio_flac.c,+ * audio_mp3.c and audio_mp2.c, audio_ogg.c with audio_vorbis.c and+ * audio_opus*.c), with bounds on sizes so a file cannot make them take+ * more memory than its sound.+ */+#ifndef JANAS_COMMON_AUDIO_H+#define JANAS_COMMON_AUDIO_H++#include <stddef.h>+#include <stdint.h>++/* at most four hours at 48 kHz, after mixing */+#define JANAS_AUDIO_MAX_SAMPLES ((size_t)48000 * 3600 * 4)++struct janas_audio {+ float *pcm; /* n samples, mono */+ size_t n;+ uint32_t rate; /* samples a second */+};++/* n bytes of a sound file (WAV, FLAC, MP3 and MPEG audio, Ogg with+ Vorbis, Opus or FLAC), the kind told by its bytes: 0 with *a filled (free it+ with janas_audio_free), -1 with the reason in why. */+int janas_audio_decode(const void *data, size_t n, struct janas_audio *a,+ char *why, size_t wn);+/* the sound resampled to rate, in place (a windowed sinc, its cut-off+ below the lower of the two Nyquist frequencies): 0, or -1 when out of+ memory */+int janas_audio_resample(struct janas_audio *a, uint32_t rate);+void janas_audio_free(struct janas_audio *a);++/* audio_wav.c: RIFF WAVE, PCM of 8 to 32 bits or float of 32 or 64,+ plain or extensible; 1 when the bytes are not a WAV file */+int janas_audio_wav(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn);+/* audio_mp3.c (and audio_mp2.c): MPEG-1, 2 and 2.5 audio, Layers I, II+ and III; 1 when no frames are found in the first 64 KiB */+int janas_audio_mpeg(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn);+/* audio_ogg.c: Ogg with Vorbis, Opus or FLAC; 1 when not Ogg */+int janas_audio_ogg(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn);+/* audio_flac.c: FLAC, every kind RFC 9639 describes; 1 when not FLAC */+int janas_audio_flac(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn);+/* audio_mp4.c: MP4, M4A, MOV with AAC, ALAC, MP3, Opus, FLAC or PCM; 1+ when not MP4 */+int janas_audio_mp4(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn);+/* audio_mkv.c: Matroska and WebM with Opus, Vorbis, FLAC, AAC, MPEG+ audio, ALAC or PCM; 1 when not EBML */+int janas_audio_mkv(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn);+/* audio_aac.c: AAC in ADTS frames (.aac); 1 when not ADTS */+int janas_audio_adts(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn);++#endifdiff --git a/src/common/audio_aac.c b/src/common/audio_aac.cnew file mode 100644index 0000000..57131c7--- /dev/null+++ b/src/common/audio_aac.c@@ -0,0 +1,1699 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_aac.c - AAC's core decoder (ISO/IEC 14496-3, 4.4-4.6): the+ * AudioSpecificConfig, raw data blocks of single, pair and LFE channel+ * elements, the scalefactors and the Huffman-coded spectrum, pulses,+ * perceptual noise substitution, mid/side and intensity stereo, temporal+ * noise shaping, AAC Main's backward-adaptive prediction and AAC LTP's+ * long-term prediction, the inverse MDCT with sine and Kaiser-Bessel+ * windows, long and short. Fill elements carry the SBR data+ * (audio_aac_sbr.c) that doubles the rate of HE-AAC. Frames of 1024 and+ * of 960 samples. The rounding of AAC Main's predictor is libfaad's and+ * FFmpeg's, the noise's generator FFmpeg's, so the output can be compared+ * with theirs. The decoder of a stream (aac_stream) mixes the channels to+ * mono and trims the start and the end as the container says.+ */+#include "audio_aac.h"++#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "audio_aac_tab.h"+#include "audio_fft.h"++/* ---- Huffman codes ---- */++static int vlc_fill(struct vlc *v, int bits, const uint32_t *code,+ const uint8_t *len, const uint8_t *sym, int n)+{+ size_t size = (size_t)1 << bits;+ /* the subtables: one for each prefix of bits bits that longer codes+ share, as wide as the longest of them */+ int sub[1 << 12];+ if (bits > 12)+ return -1;+ memset(sub, 0, sizeof(int) * size);+ for (int i = 0; i < n; i++) {+ if (len[i] > bits) {+ int extra = len[i] - bits;+ if (len[i] > 32 || extra > 16)+ return -1;+ uint32_t pre = code[i] >> extra;+ if (extra > sub[pre])+ sub[pre] = extra;+ }+ }+ size_t total = size;+ for (size_t p = 0; p < size; p++)+ total += sub[p] ? (size_t)1 << sub[p] : 0;+ if (total > 32767)+ return -1;+ v->t = calloc(total, sizeof *v->t);+ if (!v->t)+ return -1;+ v->bits = bits;+ size_t at = size;+ for (size_t p = 0; p < size; p++) {+ if (sub[p]) {+ v->t[p].sym = (int16_t)at;+ v->t[p].len = (int8_t)-sub[p];+ at += (size_t)1 << sub[p];+ }+ }+ for (int i = 0; i < n; i++) {+ int s = sym ? sym[i] : i;+ if (!len[i])+ continue;+ if (len[i] <= bits) {+ uint32_t first = code[i] << (bits - len[i]);+ uint32_t cnt = 1u << (bits - len[i]);+ for (uint32_t k = 0; k < cnt; k++) {+ struct vlc_e *e = &v->t[first + k];+ if (e->len)+ return -1;+ e->sym = (int16_t)s;+ e->len = (int8_t)len[i];+ }+ } else {+ int extra = len[i] - bits;+ uint32_t pre = code[i] >> extra;+ int w = -v->t[pre].len;+ uint32_t low = code[i] & ((1u << extra) - 1);+ uint32_t first = low << (w - extra);+ uint32_t cnt = 1u << (w - extra);+ for (uint32_t k = 0; k < cnt; k++) {+ struct vlc_e *e = &v->t[v->t[pre].sym + (int)(first + k)];+ if (e->len)+ return -1;+ e->sym = (int16_t)s;+ e->len = (int8_t)extra;+ }+ }+ }+ return 0;+}++int vlc_build(struct vlc *v, int bits, const uint32_t *code, const uint8_t *len,+ const uint8_t *sym, int n)+{+ v->t = NULL;+ if (vlc_fill(v, bits, code, len, sym, n) == 0)+ return 0;+ vlc_free(v);+ return -1;+}++void vlc_free(struct vlc *v)+{+ free(v->t);+ v->t = NULL;+}++/* ---- the configuration ---- */++static const int RATES[13] = {96000, 88200, 64000, 48000, 44100, 32000, 24000,+ 22050, 16000, 12000, 11025, 8000, 7350};++int aac_index_rate(int index)+{+ return index >= 0 && index < 13 ? RATES[index] : 0;+}++/* the index of the nearest rate's tables (14496-3, table 4.82) */+int aac_rate_index(int rate)+{+ static const int AT[12] = {92017, 75132, 55426, 46009, 37566, 27713,+ 23004, 18783, 13856, 11502, 9391, 0};+ for (int i = 0; i < 12; i++)+ if (rate >= AT[i])+ return i;+ return 11;+}++static int read_aot(struct abr *b)+{+ int aot = (int)abr_bits(b, 5);+ return aot == 31 ? 32 + (int)abr_bits(b, 6) : aot;+}++static int read_rate(struct abr *b, int *index)+{+ *index = (int)abr_bits(b, 4);+ if (*index == 15) {+ int rate = (int)abr_bits(b, 24);+ *index = aac_rate_index(rate);+ return rate;+ }+ return aac_index_rate(*index);+}++/* a program_config_element (4.4.1.1): the channels it makes, or -1 */+static int read_pce(struct abr *b)+{+ abr_bits(b, 4); /* element_instance_tag */+ abr_bits(b, 2); /* object_type */+ abr_bits(b, 4); /* sampling_frequency_index */+ int front = (int)abr_bits(b, 4), side = (int)abr_bits(b, 4);+ int back = (int)abr_bits(b, 4), lfe = (int)abr_bits(b, 2);+ int assoc = (int)abr_bits(b, 3), cc = (int)abr_bits(b, 4);+ if (abr_bit(b))+ abr_bits(b, 4); /* mono_mixdown */+ if (abr_bit(b))+ abr_bits(b, 4); /* stereo_mixdown */+ if (abr_bit(b))+ abr_bits(b, 3); /* matrix_mixdown */+ int ch = lfe;+ for (int i = 0; i < front + side + back; i++) {+ ch += abr_bit(b) ? 2 : 1;+ abr_bits(b, 4);+ }+ abr_bits(b, 4 * lfe);+ abr_bits(b, 4 * assoc);+ for (int i = 0; i < cc; i++)+ abr_bits(b, 5);+ b->pos = (b->pos + 7) & ~(size_t)7;+ b->pos += 8 * (size_t)abr_bits(b, 8); /* the comment */+ return abr_over(b) ? -1 : ch;+}++static const int CONFIG_CHANNELS[16] = {0, 1, 2, 3, 4, 5, 6, 8,+ 0, 0, 0, 7, 8, 24, 8, 0};++int aac_config(const uint8_t *p, size_t n, struct aac_cfg *c, char *why,+ size_t wn)+{+ struct abr b = {.p = p, .n = n};+ memset(c, 0, sizeof *c);+ c->sbr = c->ps = -1;+ int aot = read_aot(&b);+ c->rate = read_rate(&b, &c->rate_index);+ c->chan_config = (int)abr_bits(&b, 4);+ if (aot == AOT_SBR || aot == AOT_PS) {+ c->sbr = 1;+ c->ps = aot == AOT_PS;+ int idx;+ c->sbr_rate = read_rate(&b, &idx);+ aot = read_aot(&b);+ if (aot == 22)+ abr_bits(&b, 4); /* extensionChannelConfiguration */+ }+ if (aot != AOT_MAIN && aot != AOT_LC && aot != AOT_LTP) {+ static const char *const NAMES[] = {[3] = "AAC SSR",+ [6] = "AAC Scalable",+ [8] = "CELP",+ [9] = "HVXC",+ [17] = "ER AAC LC",+ [19] = "ER AAC LTP",+ [20] = "ER AAC Scalable",+ [22] = "ER BSAC",+ [23] = "ER AAC LD",+ [34] = "MPEG-1/2 Layer 1",+ [35] = "MPEG-1/2 Layer 2",+ [36] = "ALS",+ [39] = "ER AAC ELD",+ [42] = "USAC (xHE-AAC)"};+ const char *name =+ aot >= 0 && aot < (int)(sizeof NAMES / sizeof *NAMES) && NAMES[aot]+ ? NAMES[aot]+ : NULL;+ if (name)+ snprintf(why, wn, "an AAC of a kind not read: %s", name);+ else+ snprintf(why, wn, "an AAC of a kind not read: object type %d", aot);+ return -1;+ }+ c->aot = aot;+ if (!c->rate || c->rate > 96000) {+ snprintf(why, wn, "an AAC configuration without a rate");+ return -1;+ }+ /* GASpecificConfig */+ c->frame = abr_bit(&b) ? 960 : 1024;+ if (abr_bit(&b))+ abr_bits(&b, 14); /* coreCoderDelay */+ int ext = abr_bit(&b);+ if (c->chan_config == 0) {+ int ch = read_pce(&b);+ if (ch <= 0) {+ snprintf(why, wn, "an AAC program configuration that is none");+ return -1;+ }+ c->channels = ch;+ } else {+ c->channels = CONFIG_CHANNELS[c->chan_config];+ }+ if (ext)+ abr_bit(&b); /* extensionFlag3 */+ /* the explicit, backward compatible signal of SBR and PS */+ if (c->sbr < 0 && abr_left(&b) >= 16) {+ if (abr_bits(&b, 11) == 0x2b7) {+ int e = read_aot(&b);+ if (e == AOT_SBR) {+ c->sbr = abr_bit(&b);+ if (c->sbr) {+ int idx;+ c->sbr_rate = read_rate(&b, &idx);+ if (abr_left(&b) >= 12 && abr_bits(&b, 11) == 0x548)+ c->ps = abr_bit(&b);+ }+ }+ }+ }+ if (c->sbr == 1 && !c->sbr_rate)+ c->sbr_rate = 2 * c->rate;+ if (abr_over(&b)) {+ snprintf(why, wn, "an AAC configuration cut short");+ return -1;+ }+ return 0;+}++/* ---- the decoder ---- */++enum { ONLY_LONG, LONG_START, EIGHT_SHORT, LONG_STOP };+enum { ZERO_BT = 0, NOISE_BT = 13, INTENSITY_BT2 = 14, INTENSITY_BT = 15 };++#define MAX_ELEMENTS 16+#define MAX_CHANNELS 48+#define MAX_PRED 672+#define MAX_LTP_SFB 40++struct pred {+ float r0, r1, cor0, cor1, var0, var1;+};++struct ics {+ int seq, shape, prev_shape, max_sfb;+ int groups, group_len[8];+ int num_swb, num_windows;+ const uint16_t *swb; /* num_swb + 1 offsets */+ int pred_present, pred_reset, pred_used[41];+ int ltp_present, ltp_lag;+ float ltp_coef;+ int ltp_used[MAX_LTP_SFB];+};++struct tns {+ int present;+ int n_filt[8];+ int length[8][4], order[8][4], dir[8][4];+ float coef[8][4][20];+};++/* a channel's state */+struct chan {+ struct ics ics;+ struct tns tns;+ uint8_t cb[128]; /* the codebook of each window group x band */+ int sf[128]; /* scalefactors, positions, noise energies */+ float coef[1024]; /* the spectrum */+ float overlap[1024];+ float ltp_state[3072];+ struct pred pred[MAX_PRED];+ int pred_init;+ float out[2048];+};++enum { BEFORE_TNS, BETWEEN_TNS_AND_IMDCT, AFTER_IMDCT = 3 };++/* a coupling channel element's targets and gains (4.4.2.5, 4.6.8.3) */+struct cce {+ int point, num_coupled;+ int type[8], id[8], ch_select[8];+ float gain[16][128]; /* [list][band], [list][0] after the IMDCT */+ float out[2048]; /* its samples, for coupling after the IMDCT */+};++struct element {+ int type; /* 0 SCE, 1 CPE, 2 CCE, 3 LFE */+ int tag;+ int first; /* its first channel */+ int ch;+ struct sbr *sbr;+ struct cce *cce;+};++struct aac_dec {+ struct aac_cfg cfg;+ struct vlc sf_vlc, spec_vlc[11];+ struct aimdct *mdct_long, *mdct_short;+ float sine_long[1024], sine_short[128];+ float kbd_long[1024], kbd_short[128];+ float pow43[8192];+ float buf[2048];+ float fwd[2048];+ uint32_t noise_state;+ int frame, short_len;+ struct element el[MAX_ELEMENTS];+ int n_el;+ struct chan *ch[MAX_CHANNELS];+ int n_ch;+ int sbr_on; /* -1 until the first frame says */+ int sbr_down; /* the output at the core's rate */+ float *outp[MAX_CHANNELS];+ int out_ch;+};++static double bessel_i0(double x)+{+ double s = 1, t = 1;+ for (int k = 1; k < 50; k++) {+ t *= (x / (2 * k)) * (x / (2 * k));+ s += t;+ }+ return s;+}++/* FFmpeg's ff_kbd_window_init: the left half, n samples */+static void kbd_window(float *w, double alpha, int n)+{+ double temp[1025], scale = 0, sum = 0;+ double a2 = 4 * (alpha * M_PI / n) * (alpha * M_PI / n);+ for (int i = 0; i <= n / 2; i++) {+ temp[i] = bessel_i0(sqrt((double)i * (n - i) * a2));+ scale += temp[i] * (1 + (i && i < n / 2));+ }+ scale = 1.0 / (scale + 1);+ int i;+ for (i = 0; i <= n / 2; i++) {+ sum += temp[i];+ w[i] = (float)sqrt(sum * scale);+ }+ for (; i < n; i++) {+ sum += temp[n - i];+ w[i] = (float)sqrt(sum * scale);+ }+}++static void sine_window(float *w, int n)+{+ for (int i = 0; i < n; i++)+ w[i] = (float)sin((i + 0.5) * (M_PI / (2.0 * n)));+}++void aac_free(struct aac_dec *d)+{+ if (!d)+ return;+ vlc_free(&d->sf_vlc);+ for (int i = 0; i < 11; i++)+ vlc_free(&d->spec_vlc[i]);+ aimdct_free(d->mdct_long);+ aimdct_free(d->mdct_short);+ for (int i = 0; i < d->n_el; i++) {+ sbr_free(d->el[i].sbr);+ free(d->el[i].cce);+ }+ for (int i = 0; i < MAX_CHANNELS; i++) {+ free(d->ch[i]);+ free(d->outp[i]);+ }+ free(d);+}++struct aac_dec *aac_new(const struct aac_cfg *c)+{+ struct aac_dec *d = calloc(1, sizeof *d);+ if (!d)+ return NULL;+ d->cfg = *c;+ d->frame = c->frame;+ d->short_len = c->frame / 8;+ d->noise_state = 0x1f2e3d4c;+ d->sbr_on = c->sbr == 0 ? 0 : -1;+ d->sbr_down = c->sbr == 1 && c->sbr_rate && c->sbr_rate < 2 * c->rate;+ static const uint16_t *const CODES[11] = {+ AAC_CODES1, AAC_CODES2, AAC_CODES3, AAC_CODES4, AAC_CODES5, AAC_CODES6,+ AAC_CODES7, AAC_CODES8, AAC_CODES9, AAC_CODES10, AAC_CODES11};+ static const uint8_t *const BITS[11] = {+ AAC_BITS1, AAC_BITS2, AAC_BITS3, AAC_BITS4, AAC_BITS5, AAC_BITS6,+ AAC_BITS7, AAC_BITS8, AAC_BITS9, AAC_BITS10, AAC_BITS11};+ static const int SIZES[11] = {81, 81, 81, 81, 81, 81,+ 64, 64, 169, 169, 289};+ int bad = vlc_build(&d->sf_vlc, 9, AAC_SF_CODE, AAC_SF_BITS, NULL, 121);+ for (int i = 0; i < 11; i++) {+ uint32_t code[289];+ for (int k = 0; k < SIZES[i]; k++)+ code[k] = CODES[i][k];+ if (bad ||+ vlc_build(&d->spec_vlc[i], 9, code, BITS[i], NULL, SIZES[i]) != 0) {+ bad = 1;+ /* the sbr and vlc fields are zero: aac_free copes */+ }+ }+ d->mdct_long = aimdct_new(2 * d->frame);+ d->mdct_short = aimdct_new(2 * d->short_len);+ if (bad || !d->mdct_long || !d->mdct_short) {+ aac_free(d);+ return NULL;+ }+ sine_window(d->sine_long, d->frame);+ sine_window(d->sine_short, d->short_len);+ kbd_window(d->kbd_long, 4.0, d->frame);+ kbd_window(d->kbd_short, 6.0, d->short_len);+ for (int i = 0; i < 8192; i++)+ d->pow43[i] = (float)(cbrt((double)i) * i);+ return d;+}++int aac_out_rate(const struct aac_dec *d)+{+ return d->sbr_on == 1 && !d->sbr_down ? 2 * d->cfg.rate : d->cfg.rate;+}++int aac_out(const struct aac_dec *d, float ***pcm)+{+ *pcm = (float **)d->outp;+ return d->out_ch;+}++/* ---- the syntax ---- */++static const uint16_t *swb_table(int frame, int index, int *num)+{+ const uint16_t *t;+ switch (frame) {+ case 1024:+ *num = AAC_NUM_SWB_1024[index];+ t = AAC_SWB_1024 + AAC_SWB_1024_AT[index];+ break;+ case 960:+ *num = AAC_NUM_SWB_960[index];+ t = AAC_SWB_960 + AAC_SWB_960_AT[index];+ break;+ case 128:+ *num = AAC_NUM_SWB_128[index];+ t = AAC_SWB_128 + AAC_SWB_128_AT[index];+ break;+ default:+ *num = AAC_NUM_SWB_120[index];+ t = AAC_SWB_120 + AAC_SWB_120_AT[index];+ break;+ }+ return t;+}++static int read_ltp(struct abr *b, struct ics *ics)+{+ ics->ltp_lag = (int)abr_bits(b, 11);+ ics->ltp_coef = AAC_LTP_COEF[abr_bits(b, 3)];+ int n = ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB;+ for (int i = 0; i < MAX_LTP_SFB; i++)+ ics->ltp_used[i] = i < n ? abr_bit(b) : 0;+ return 0;+}++/* ics_info (4.4.2.1): 0, or -1 */+static int read_ics_info(struct aac_dec *d, struct abr *b, struct ics *ics)+{+ if (abr_bit(b))+ return -1; /* reserved */+ ics->prev_shape = ics->shape;+ ics->seq = (int)abr_bits(b, 2);+ ics->shape = abr_bit(b);+ ics->pred_present = ics->ltp_present = 0;+ ics->pred_reset = 0;+ if (ics->seq == EIGHT_SHORT) {+ ics->max_sfb = (int)abr_bits(b, 4);+ int grouping = (int)abr_bits(b, 7);+ ics->groups = 1;+ ics->group_len[0] = 1;+ for (int i = 6; i >= 0; i--) {+ if (grouping >> i & 1)+ ics->group_len[ics->groups - 1]++;+ else+ ics->group_len[ics->groups++] = 1;+ }+ ics->num_windows = 8;+ ics->swb = swb_table(d->short_len, d->cfg.rate_index, &ics->num_swb);+ } else {+ ics->max_sfb = (int)abr_bits(b, 6);+ ics->groups = 1;+ ics->group_len[0] = 1;+ ics->num_windows = 1;+ ics->swb = swb_table(d->frame, d->cfg.rate_index, &ics->num_swb);+ if ((ics->pred_present = abr_bit(b))) {+ if (d->cfg.aot == AOT_MAIN) {+ if (abr_bit(b)) {+ ics->pred_reset = (int)abr_bits(b, 5);+ if (ics->pred_reset == 0 || ics->pred_reset > 30)+ return -1;+ }+ int max = AAC_PRED_SFB_MAX[d->cfg.rate_index];+ int n = ics->max_sfb < max ? ics->max_sfb : max;+ for (int i = 0; i < 41; i++)+ ics->pred_used[i] = i < n ? abr_bit(b) : 0;+ } else if (d->cfg.aot == AOT_LTP) {+ if ((ics->ltp_present = abr_bit(b)))+ read_ltp(b, ics);+ } else {+ return -1; /* prediction in AAC LC */+ }+ }+ }+ return ics->max_sfb > ics->num_swb ? -1 : 0;+}++static int read_section_data(struct abr *b, struct ics *ics, uint8_t *cb)+{+ int bits = ics->seq == EIGHT_SHORT ? 3 : 5;+ int esc = (1 << bits) - 1;+ for (int g = 0; g < ics->groups; g++) {+ int k = 0;+ while (k < ics->max_sfb) {+ int c = (int)abr_bits(b, 4);+ if (c == 12)+ return -1;+ int len = 0, inc;+ do {+ inc = (int)abr_bits(b, bits);+ len += inc;+ if (abr_over(b))+ return -1;+ } while (inc == esc);+ if (k + len > ics->max_sfb)+ return -1;+ for (int i = 0; i < len; i++)+ cb[g * 16 + k + i] = (uint8_t)c;+ k += len;+ }+ for (; k < (ics->seq == EIGHT_SHORT ? 16 : 64); k++)+ cb[g * (ics->seq == EIGHT_SHORT ? 16 : 0) + k] = ZERO_BT;+ }+ return 0;+}++/* the scalefactors (4.6.2.3): 0, or -1 */+static int read_scalefactors(struct aac_dec *d, struct abr *b, struct ics *ics,+ const uint8_t *cb, int *sf, int global_gain)+{+ int offset[3] = {global_gain, 0, global_gain - 90};+ int noise_first = 1;+ int stride = ics->seq == EIGHT_SHORT ? 16 : 0;+ for (int g = 0; g < ics->groups; g++) {+ for (int i = 0; i < ics->max_sfb; i++) {+ int idx = g * stride + i, v;+ switch (cb[idx]) {+ case ZERO_BT:+ sf[idx] = 0;+ break;+ case INTENSITY_BT:+ case INTENSITY_BT2:+ if ((v = vlc_get(b, &d->sf_vlc)) < 0)+ return -1;+ offset[1] += v - 60;+ sf[idx] = offset[1] < -155 ? -155+ : offset[1] > 100 ? 100+ : offset[1];+ break;+ case NOISE_BT:+ if (noise_first) {+ offset[2] += (int)abr_bits(b, 9) - 256;+ noise_first = 0;+ } else {+ if ((v = vlc_get(b, &d->sf_vlc)) < 0)+ return -1;+ offset[2] += v - 60;+ }+ sf[idx] = offset[2] < -100 ? -100+ : offset[2] > 155 ? 155+ : offset[2];+ break;+ default:+ if ((v = vlc_get(b, &d->sf_vlc)) < 0)+ return -1;+ offset[0] += v - 60;+ if (offset[0] < 0 || offset[0] > 255)+ return -1;+ sf[idx] = offset[0];+ break;+ }+ }+ }+ return 0;+}++static int read_tns(struct abr *b, const struct ics *ics, struct tns *t)+{+ int is8 = ics->seq == EIGHT_SHORT;+ for (int w = 0; w < ics->num_windows; w++) {+ t->n_filt[w] = (int)abr_bits(b, 2 - is8);+ if (!t->n_filt[w])+ continue;+ int res = abr_bit(b);+ for (int f = 0; f < t->n_filt[w]; f++) {+ t->length[w][f] = (int)abr_bits(b, 6 - 2 * is8);+ int order = (int)abr_bits(b, 5 - 2 * is8);+ if (order > (is8 ? 7 : 20))+ return -1;+ t->order[w][f] = order;+ if (!order)+ continue;+ t->dir[w][f] = abr_bit(b);+ int compress = abr_bit(b);+ int bits = res + 3 - compress;+ const float *map = compress ? (res ? AAC_TNS_1_4 : AAC_TNS_1_3)+ : (res ? AAC_TNS_0_4 : AAC_TNS_0_3);+ for (int i = 0; i < order; i++)+ t->coef[w][f][i] = map[abr_bits(b, bits)];+ }+ }+ return 0;+}++static uint32_t noise_next(uint32_t *s)+{+ *s = *s * 1664525u + 1013904223u;+ return *s;+}++/* the spectral data, dequantized, with noise in the noise bands (4.6.3,+ 4.6.13): 0, or -1 */+static int read_spectrum(struct aac_dec *d, struct abr *b, struct chan *c,+ const int *pulse)+{+ struct ics *ics = &c->ics;+ float *coef = c->coef;+ int stride = ics->seq == EIGHT_SHORT ? 16 : 0;+ int wlen = ics->seq == EIGHT_SHORT ? d->short_len : d->frame;+ memset(coef, 0, sizeof(float) * (size_t)d->frame);+ int win = 0;+ for (int g = 0; g < ics->groups; g++) {+ int glen = ics->group_len[g];+ for (int i = 0; i < ics->max_sfb; i++) {+ int idx = g * stride + i, cbt = c->cb[idx];+ int lo = ics->swb[i], hi = ics->swb[i + 1];+ if (cbt == ZERO_BT || cbt >= INTENSITY_BT2)+ continue;+ if (cbt == NOISE_BT) {+ for (int w = 0; w < glen; w++) {+ float *x = coef + (win + w) * wlen;+ float energy = 0;+ for (int k = lo; k < hi; k++) {+ x[k] = (float)(int32_t)noise_next(&d->noise_state);+ energy += x[k] * x[k];+ }+ float scale =+ (float)pow(2.0, 0.25 * c->sf[idx]) / sqrtf(energy);+ for (int k = lo; k < hi; k++)+ x[k] *= scale;+ }+ continue;+ }+ const struct vlc *v = &d->spec_vlc[cbt - 1];+ int dim4 = cbt < 5, unsign = cbt == 3 || cbt == 4 || cbt >= 7;+ int mod = cbt < 5 ? 3+ : cbt < 7 ? 9+ : cbt < 9 ? 8+ : cbt < 11 ? 13+ : 17;+ int off = unsign ? 0 : mod / 2;+ for (int w = 0; w < glen; w++) {+ float *x = coef + (win + w) * wlen;+ for (int k = lo; k < hi; k += dim4 ? 4 : 2) {+ int s = vlc_get(b, v);+ if (s < 0)+ return -1;+ int q[4], n = dim4 ? 4 : 2;+ if (dim4) {+ q[0] = s / 27 - off;+ q[1] = s / 9 % 3 - off;+ q[2] = s / 3 % 3 - off;+ q[3] = s % 3 - off;+ } else {+ q[0] = s / mod - off;+ q[1] = s % mod - off;+ }+ if (unsign) {+ for (int j = 0; j < n; j++)+ if (q[j] && abr_bit(b))+ q[j] = -q[j];+ if (cbt == 11) {+ for (int j = 0; j < 2; j++) {+ if (q[j] != 16 && q[j] != -16)+ continue;+ int nb = 4;+ while (abr_bit(b)) {+ if (++nb > 12)+ return -1;+ }+ int e = (1 << nb) + (int)abr_bits(b, nb);+ q[j] = q[j] < 0 ? -e : e;+ }+ }+ }+ for (int j = 0; j < n && k + j < hi; j++)+ x[k + j] = q[j];+ }+ if (abr_over(b))+ return -1;+ }+ }+ win += glen;+ }+ /* pulses, on the quantized values of coded bands (4.6.4) */+ if (pulse) {+ int at = ics->swb[pulse[1]], sfb = pulse[1];+ for (int i = 0; i < pulse[0]; i++) {+ at += pulse[2 + 2 * i];+ if (at >= d->frame)+ return -1;+ while (sfb < ics->num_swb && ics->swb[sfb + 1] <= at)+ sfb++;+ if (sfb >= ics->max_sfb || c->cb[sfb] == ZERO_BT ||+ c->cb[sfb] >= NOISE_BT)+ continue;+ float q = coef[at];+ int amp = pulse[3 + 2 * i];+ coef[at] = q > 0 ? q + amp : q - amp;+ }+ }+ /* dequantized */+ win = 0;+ for (int g = 0; g < ics->groups; g++) {+ int glen = ics->group_len[g];+ for (int i = 0; i < ics->max_sfb; i++) {+ int idx = g * stride + i, cbt = c->cb[idx];+ if (cbt == ZERO_BT || cbt >= NOISE_BT)+ continue;+ float scale = (float)pow(2.0, 0.25 * (c->sf[idx] - 100));+ for (int w = 0; w < glen; w++) {+ float *x = coef + (win + w) * wlen;+ for (int k = ics->swb[i]; k < ics->swb[i + 1]; k++) {+ float q = x[k];+ int a = (int)fabsf(q);+ if (a >= 8192)+ return -1;+ x[k] = q < 0 ? -d->pow43[a] * scale : d->pow43[a] * scale;+ }+ }+ }+ win += glen;+ }+ return 0;+}++/* individual_channel_stream (4.4.2.7): 0, or -1 */+static int read_ics(struct aac_dec *d, struct abr *b, struct chan *c,+ int common_window)+{+ int global_gain = (int)abr_bits(b, 8);+ if (!common_window && read_ics_info(d, b, &c->ics) != 0)+ return -1;+ if (read_section_data(b, &c->ics, c->cb) != 0)+ return -1;+ if (read_scalefactors(d, b, &c->ics, c->cb, c->sf, global_gain) != 0)+ return -1;+ int pulse[2 + 8], *pp = NULL;+ if (abr_bit(b)) {+ if (c->ics.seq == EIGHT_SHORT)+ return -1;+ pulse[0] = (int)abr_bits(b, 2) + 1;+ pulse[1] = (int)abr_bits(b, 6);+ if (pulse[1] >= c->ics.num_swb)+ return -1;+ for (int i = 0; i < pulse[0]; i++) {+ pulse[2 + 2 * i] = (int)abr_bits(b, 5);+ pulse[3 + 2 * i] = (int)abr_bits(b, 4);+ }+ pp = pulse;+ }+ c->tns.present = abr_bit(b);+ if (c->tns.present && read_tns(b, &c->ics, &c->tns) != 0)+ return -1;+ if (abr_bit(b))+ return -1; /* gain control: AAC SSR only */+ if (read_spectrum(d, b, c, pp) != 0)+ return -1;+ return abr_over(b) ? -1 : 0;+}++/* ---- the tools ---- */++static float flt16_round(float f)+{+ uint32_t i;+ memcpy(&i, &f, 4);+ i = (i + 0x00008000u) & 0xFFFF0000u;+ memcpy(&f, &i, 4);+ return f;+}++static float flt16_even(float f)+{+ uint32_t i;+ memcpy(&i, &f, 4);+ i = (i + 0x00007FFFu + (i & 0x00010000u >> 16)) & 0xFFFF0000u;+ memcpy(&f, &i, 4);+ return f;+}++static float flt16_trunc(float f)+{+ uint32_t i;+ memcpy(&i, &f, 4);+ i &= 0xFFFF0000u;+ memcpy(&f, &i, 4);+ return f;+}++static void pred_reset(struct pred *p)+{+ p->r0 = p->r1 = p->cor0 = p->cor1 = 0;+ p->var0 = p->var1 = 1;+}++static void predict(struct pred *ps, float *coef, int output)+{+ const float a = 0.953125f, alpha = 0.90625f;+ float r0 = ps->r0, r1 = ps->r1;+ float cor0 = ps->cor0, cor1 = ps->cor1;+ float var0 = ps->var0, var1 = ps->var1;+ float k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;+ float k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;+ float pv = flt16_round(k1 * r0 + k2 * r1);+ if (output)+ *coef += pv;+ float e0 = *coef, e1 = e0 - k1 * r0;+ ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);+ ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));+ ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);+ ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));+ ps->r1 = flt16_trunc(a * (r0 - k1 * e0));+ ps->r0 = flt16_trunc(a * e0);+}++/* AAC Main's prediction (4.6.6) */+static void apply_prediction(struct aac_dec *d, struct chan *c)+{+ struct ics *ics = &c->ics;+ if (!c->pred_init) {+ for (int i = 0; i < MAX_PRED; i++)+ pred_reset(&c->pred[i]);+ c->pred_init = 1;+ }+ if (ics->seq == EIGHT_SHORT) {+ for (int i = 0; i < MAX_PRED; i++)+ pred_reset(&c->pred[i]);+ return;+ }+ int max = AAC_PRED_SFB_MAX[d->cfg.rate_index];+ for (int sfb = 0; sfb < max; sfb++)+ for (int k = ics->swb[sfb]; k < ics->swb[sfb + 1] && k < MAX_PRED; k++)+ predict(&c->pred[k], &c->coef[k],+ ics->pred_present && ics->pred_used[sfb]);+ if (ics->pred_reset)+ for (int i = ics->pred_reset - 1; i < MAX_PRED; i += 30)+ pred_reset(&c->pred[i]);+}++/* mid/side (4.6.8.1), on the bands that are neither noise nor intensity */+static void apply_ms(struct aac_dec *d, struct chan *l, struct chan *r,+ const uint8_t *ms)+{+ const struct ics *ics = &l->ics;+ int stride = ics->seq == EIGHT_SHORT ? 16 : 0;+ int wlen = ics->seq == EIGHT_SHORT ? d->short_len : d->frame;+ int win = 0;+ for (int g = 0; g < ics->groups; g++) {+ for (int i = 0; i < ics->max_sfb; i++) {+ int idx = g * stride + i;+ if (!ms[idx] || l->cb[idx] >= NOISE_BT || r->cb[idx] >= NOISE_BT)+ continue;+ for (int w = 0; w < ics->group_len[g]; w++) {+ float *a = l->coef + (win + w) * wlen;+ float *b = r->coef + (win + w) * wlen;+ for (int k = ics->swb[i]; k < ics->swb[i + 1]; k++) {+ float m = a[k], s = b[k];+ a[k] = m + s;+ b[k] = m - s;+ }+ }+ }+ win += ics->group_len[g];+ }+}++/* intensity stereo (4.6.8.2) */+static void apply_intensity(struct aac_dec *d, struct chan *l, struct chan *r,+ const uint8_t *ms, int ms_present)+{+ const struct ics *ics = &r->ics;+ int stride = ics->seq == EIGHT_SHORT ? 16 : 0;+ int wlen = ics->seq == EIGHT_SHORT ? d->short_len : d->frame;+ int win = 0;+ for (int g = 0; g < ics->groups; g++) {+ for (int i = 0; i < ics->max_sfb; i++) {+ int idx = g * stride + i, bt = r->cb[idx];+ if (bt != INTENSITY_BT && bt != INTENSITY_BT2)+ continue;+ float c = bt == INTENSITY_BT ? 1.0f : -1.0f;+ if (ms_present)+ c *= ms[idx] ? -1.0f : 1.0f;+ float scale = c * (float)pow(2.0, -0.25 * r->sf[idx]);+ for (int w = 0; w < ics->group_len[g]; w++) {+ const float *a = l->coef + (win + w) * wlen;+ float *b = r->coef + (win + w) * wlen;+ for (int k = ics->swb[i]; k < ics->swb[i + 1]; k++)+ b[k] = a[k] * scale;+ }+ }+ win += ics->group_len[g];+ }+}++/* temporal noise shaping (4.6.9): the all-pole filter, or with inverse+ the all-zero one LTP's prediction takes */+static void apply_tns(struct aac_dec *d, float *coef, const struct tns *t,+ const struct ics *ics, int inverse)+{+ int is8 = ics->seq == EIGHT_SHORT;+ int wlen = is8 ? d->short_len : d->frame;+ int mmm = is8 ? AAC_TNS_MAX_BANDS_128[d->cfg.rate_index]+ : AAC_TNS_MAX_BANDS_1024[d->cfg.rate_index];+ if (mmm > ics->max_sfb)+ mmm = ics->max_sfb;+ if (!mmm)+ return;+ for (int w = 0; w < ics->num_windows; w++) {+ int bottom = ics->num_swb;+ for (int f = 0; f < t->n_filt[w]; f++) {+ int top = bottom;+ bottom = top - t->length[w][f];+ if (bottom < 0)+ bottom = 0;+ int order = t->order[w][f];+ if (!order)+ continue;+ /* the reflection coefficients to the LPC ones */+ float lpc[20];+ for (int i = 0; i < order; i++) {+ float r = -t->coef[w][f][i];+ lpc[i] = r;+ for (int j = 0; j < (i + 1) >> 1; j++) {+ float fj = lpc[j], bj = lpc[i - 1 - j];+ lpc[j] = fj + r * bj;+ lpc[i - 1 - j] = bj + r * fj;+ }+ }+ int start = ics->swb[bottom < mmm ? bottom : mmm];+ int end = ics->swb[top < mmm ? top : mmm];+ int size = end - start;+ if (size <= 0)+ continue;+ int inc = 1;+ if (t->dir[w][f]) {+ inc = -1;+ start = end - 1;+ }+ float *x = coef + w * wlen;+ if (!inverse) {+ for (int m = 0; m < size; m++, start += inc)+ for (int i = 1; i <= (m < order ? m : order); i++)+ x[start] -= x[start - i * inc] * lpc[i - 1];+ } else {+ float tmp[1024];+ for (int m = 0; m < size; m++, start += inc) {+ tmp[m] = x[start];+ for (int i = 1; i <= (m < order ? m : order); i++)+ x[start] += tmp[m - i] * lpc[i - 1];+ }+ }+ }+ }+}++/* the window of a long frame (or the left of the first short): left+ half by the previous shape, right half by this one */+static void long_window(struct aac_dec *d, const struct ics *ics, float *w)+{+ int n = d->frame, s = d->short_len;+ const float *lp = ics->prev_shape ? d->kbd_long : d->sine_long;+ const float *lc = ics->shape ? d->kbd_long : d->sine_long;+ const float *sp = ics->prev_shape ? d->kbd_short : d->sine_short;+ const float *sc = ics->shape ? d->kbd_short : d->sine_short;+ int flat = (n - s) / 2; /* 448 */+ for (int i = 0; i < n; i++) {+ if (ics->seq == LONG_STOP)+ w[i] = i < flat ? 0 : i < flat + s ? sp[i - flat] : 1;+ else+ w[i] = lp[i];+ }+ for (int i = 0; i < n; i++) {+ float v;+ if (ics->seq == LONG_START)+ v = i < flat ? 1 : i < flat + s ? sc[s - 1 - (i - flat)] : 0;+ else+ v = lc[n - 1 - i];+ w[n + i] = v;+ }+}++/* the forward MDCT of 2n windowed samples to n coefficients, as an+ encoder makes them (LTP's prediction): X[k] = 2 sum x[i] cos(...) */+static void mdct_forward(struct aac_dec *d, const float *x, float *X)+{+ int n = d->frame, h = n / 2;+ float *u = d->fwd;+ for (int i = 0; i < h; i++) {+ u[i] = -x[3 * h - 1 - i] - x[3 * h + i];+ u[h + i] = x[i] - x[n - 1 - i];+ }+ aimdct_dct4(d->mdct_long, u, 1, X);+}++/* AAC LTP's prediction (4.6.7), added to the spectrum's bands */++static void apply_ltp(struct aac_dec *d, struct chan *c)+{+ struct ics *ics = &c->ics;+ if (!ics->ltp_present || ics->seq == EIGHT_SHORT)+ return;+ int n = d->frame;+ float *pt = d->buf, w[2048], pf[1024];+ int num = ics->ltp_lag < n ? ics->ltp_lag + n : 2 * n;+ for (int i = 0; i < 2 * n; i++)+ pt[i] = i < num && i + 2 * n - ics->ltp_lag >= 0+ ? c->ltp_state[i + 2 * n - ics->ltp_lag] * ics->ltp_coef+ : 0;+ long_window(d, ics, w);+ for (int i = 0; i < 2 * n; i++)+ pt[i] *= w[i] * 32768.0f * 2.0f;+ mdct_forward(d, pt, pf);+ if (c->tns.present)+ apply_tns(d, pf, &c->tns, ics, 1);+ int max = ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB;+ for (int sfb = 0; sfb < max; sfb++)+ if (ics->ltp_used[sfb])+ for (int k = ics->swb[sfb]; k < ics->swb[sfb + 1]; k++)+ c->coef[k] += pf[k];+}++/* the inverse MDCT, windows and overlap (4.6.11, 4.6.18), into c->out+ (n samples) and c->overlap; z: the 2n samples windowed */+static void synthesize(struct aac_dec *d, struct chan *c)+{+ struct ics *ics = &c->ics;+ int n = d->frame, s = d->short_len;+ float *z = d->buf;+ float y[2048];+ if (ics->seq == EIGHT_SHORT) {+ const float *sp = ics->prev_shape ? d->kbd_short : d->sine_short;+ const float *sc = ics->shape ? d->kbd_short : d->sine_short;+ int flat = (n - s) / 2;+ memset(z, 0, sizeof(float) * 2 * (size_t)n);+ for (int w = 0; w < 8; w++) {+ aimdct_run(d->mdct_short, c->coef + w * s, y);+ const float *left = w ? sc : sp;+ float *o = z + flat + w * s;+ float g = 1.0f / (float)s / 32768.0f;+ for (int i = 0; i < s; i++) {+ o[i] += y[i] * left[i] * g;+ o[s + i] += y[s + i] * sc[s - 1 - i] * g;+ }+ }+ } else {+ float w[2048];+ aimdct_run(d->mdct_long, c->coef, y);+ long_window(d, ics, w);+ float g = 1.0f / (float)n / 32768.0f;+ for (int i = 0; i < 2 * n; i++)+ z[i] = y[i] * w[i] * g;+ }+ for (int i = 0; i < n; i++) {+ c->out[i] = z[i] + c->overlap[i];+ c->overlap[i] = z[n + i];+ }+ if (d->cfg.aot == AOT_LTP) {+ memmove(c->ltp_state, c->ltp_state + n, sizeof(float) * (size_t)n);+ memcpy(c->ltp_state + n, c->out, sizeof(float) * (size_t)n);+ memcpy(c->ltp_state + 2 * n, z + n, sizeof(float) * (size_t)n);+ }+}++/* ---- the elements ---- */++/* the element of type and tag, made at its first sight */+static struct element *element(struct aac_dec *d, int type, int tag)+{+ for (int i = 0; i < d->n_el; i++)+ if (d->el[i].type == type && d->el[i].tag == tag)+ return &d->el[i];+ int ch = type == 1 ? 2 : 1;+ if (d->n_el == MAX_ELEMENTS || d->n_ch + ch > MAX_CHANNELS)+ return NULL;+ struct element *e = &d->el[d->n_el];+ for (int k = 0; k < ch; k++) {+ if (!d->ch[d->n_ch + k] &&+ !(d->ch[d->n_ch + k] = calloc(1, sizeof(struct chan))))+ return NULL;+ if (!d->outp[d->n_ch + k] &&+ !(d->outp[d->n_ch + k] = calloc(2 * 2048, sizeof(float))))+ return NULL;+ }+ e->type = type;+ e->tag = tag;+ e->first = d->n_ch;+ e->ch = ch;+ e->sbr = NULL;+ e->cce = NULL;+ d->n_ch += ch;+ d->n_el++;+ return e;+}++static int read_sce(struct aac_dec *d, struct abr *b, struct element *e)+{+ struct chan *c = d->ch[e->first];+ if (read_ics(d, b, c, 0) != 0)+ return -1;+ if (d->cfg.aot == AOT_MAIN)+ apply_prediction(d, c);+ return 0;+}++static int read_cpe(struct aac_dec *d, struct abr *b, struct element *e)+{+ struct chan *l = d->ch[e->first], *r = d->ch[e->first + 1];+ int common = abr_bit(b), ms_present = 0;+ uint8_t ms[128] = {0};+ if (common) {+ if (read_ics_info(d, b, &l->ics) != 0)+ return -1;+ int shape = r->ics.shape;+ r->ics = l->ics;+ r->ics.prev_shape = shape;+ if (r->ics.pred_present && d->cfg.aot == AOT_LTP)+ if ((r->ics.ltp_present = abr_bit(b)))+ read_ltp(b, &r->ics);+ ms_present = (int)abr_bits(b, 2);+ if (ms_present == 3)+ return -1;+ int stride = l->ics.seq == EIGHT_SHORT ? 16 : 0;+ for (int g = 0; g < l->ics.groups; g++)+ for (int i = 0; i < l->ics.max_sfb; i++)+ ms[g * stride + i] = ms_present == 1 ? (uint8_t)abr_bit(b) : 1;+ if (!ms_present)+ memset(ms, 0, sizeof ms);+ }+ if (read_ics(d, b, l, common) != 0 || read_ics(d, b, r, common) != 0)+ return -1;+ if (!common && d->cfg.aot == AOT_MAIN) {+ apply_prediction(d, l);+ apply_prediction(d, r);+ }+ if (common) {+ if (ms_present)+ apply_ms(d, l, r, ms);+ if (d->cfg.aot == AOT_MAIN) {+ apply_prediction(d, l);+ apply_prediction(d, r);+ }+ }+ apply_intensity(d, l, r, ms, ms_present);+ return 0;+}++static int read_cce(struct aac_dec *d, struct abr *b, struct element *e)+{+ static const float CCE_SCALE[4] = {+ 1.09050773266525765921f, 1.18920711500272106672f, (float)M_SQRT2, 2.0f};+ if (!e->cce && !(e->cce = calloc(1, sizeof *e->cce)))+ return -1;+ struct cce *cc = e->cce;+ int num_gain = 0;+ cc->point = 2 * abr_bit(b);+ cc->num_coupled = (int)abr_bits(b, 3);+ for (int c = 0; c <= cc->num_coupled; c++) {+ num_gain++;+ cc->type[c] = abr_bit(b);+ cc->id[c] = (int)abr_bits(b, 4);+ if (cc->type[c] == 1) {+ cc->ch_select[c] = (int)abr_bits(b, 2);+ if (cc->ch_select[c] == 3)+ num_gain++;+ } else {+ cc->ch_select[c] = 2;+ }+ }+ cc->point += abr_bit(b) || (cc->point >> 1);+ int sign = abr_bit(b);+ float scale = CCE_SCALE[abr_bits(b, 2)];+ if (read_sce(d, b, e) != 0)+ return -1;+ const struct chan *ch = d->ch[e->first];+ const struct ics *ics = &ch->ics;+ int stride = ics->seq == EIGHT_SHORT ? 16 : 0;+ for (int c = 0; c < num_gain; c++) {+ int cge = 1, gain = 0;+ float cache = 1.0f;+ if (c) {+ cge = cc->point == AFTER_IMDCT ? 1 : abr_bit(b);+ if (cge) {+ int v = vlc_get(b, &d->sf_vlc);+ if (v < 0)+ return -1;+ gain = v - 60;+ }+ cache = powf(scale, (float)-gain);+ }+ if (cc->point == AFTER_IMDCT) {+ cc->gain[c][0] = cache;+ continue;+ }+ for (int g = 0; g < ics->groups; g++)+ for (int sfb = 0; sfb < ics->max_sfb; sfb++) {+ int idx = g * stride + sfb;+ if (ch->cb[idx] == ZERO_BT)+ continue;+ if (!cge) {+ int v = vlc_get(b, &d->sf_vlc);+ if (v < 0)+ return -1;+ int t = v - 60;+ if (t) {+ int s1 = 1;+ t = gain += t;+ if (sign) {+ s1 -= 2 * (t & 1);+ t >>= 1;+ }+ cache = powf(scale, (float)-t) * (float)s1;+ }+ }+ cc->gain[c][idx] = cache;+ }+ }+ return abr_over(b) ? -1 : 0;+}++/* the coupling channels' spectra added to a target's, before the IMDCT */+static void couple_dependent(struct aac_dec *d, struct chan *t,+ const struct chan *src, const float *gain)+{+ const struct ics *ics = &src->ics;+ int stride = ics->seq == EIGHT_SHORT ? 16 : 0;+ int wlen = ics->seq == EIGHT_SHORT ? d->short_len : d->frame;+ int win = 0;+ for (int g = 0; g < ics->groups; g++) {+ for (int i = 0; i < ics->max_sfb; i++) {+ int idx = g * stride + i;+ if (src->cb[idx] == ZERO_BT)+ continue;+ for (int w = 0; w < ics->group_len[g]; w++) {+ float *dst = t->coef + (win + w) * wlen;+ const float *s = src->coef + (win + w) * wlen;+ for (int k = ics->swb[i]; k < ics->swb[i + 1]; k++)+ dst[k] += gain[idx] * s[k];+ }+ }+ win += ics->group_len[g];+ }+}++/* the coupling at point of the frame's CCEs into element e: its+ spectra (before the IMDCT), or its outputs out[] of n samples */+static void couple(struct aac_dec *d, struct element *const *seen, int n_seen,+ const struct element *e, int point, float **out, int n)+{+ for (int k = 0; k < n_seen; k++) {+ const struct element *x = seen[k];+ if (x->type != 2 || !x->cce || x->cce->point != point)+ continue;+ const struct cce *cc = x->cce;+ int index = 0;+ for (int c = 0; c <= cc->num_coupled; c++) {+ if (cc->type[c] != e->type || cc->id[c] != e->tag) {+ index += 1 + (cc->ch_select[c] == 3);+ continue;+ }+ for (int j = 0; j < 2 && j < e->ch; j++) {+ int use =+ j == 0 ? cc->ch_select[c] != 1 : cc->ch_select[c] != 2;+ if (!use)+ continue;+ if (point == AFTER_IMDCT) {+ float gain = cc->gain[index][0];+ for (int i = 0; i < n; i++)+ out[j][i] += gain * cc->out[i];+ } else {+ couple_dependent(d, d->ch[e->first + j], d->ch[x->first],+ cc->gain[index]);+ }+ if (j == 0 && cc->ch_select[c] != 0)+ index++;+ else if (j == 1)+ index++;+ }+ }+ }+}++/* a fill element's payloads: SBR for the element before it */+static int read_fil(struct aac_dec *d, struct abr *b, struct element *prev)+{+ int cnt = (int)abr_bits(b, 4);+ if (cnt == 15)+ cnt += (int)abr_bits(b, 8) - 1;+ size_t end = b->pos + 8 * (size_t)cnt;+ if (cnt > 0) {+ int type = (int)abr_bits(b, 4);+ if ((type == 13 || type == 14) && prev && d->sbr_on != 0) {+ if (!prev->sbr) {+ prev->sbr =+ sbr_new(d->cfg.rate, prev->type == 1 ? 2 : 1, d->sbr_down);+ if (!prev->sbr)+ return -1;+ }+ if (d->sbr_on < 0)+ d->sbr_on = 1;+ sbr_read(prev->sbr, b, type == 14, cnt);+ }+ }+ b->pos = end;+ return abr_over(b) ? -1 : 0;+}++static int read_dse(struct abr *b)+{+ abr_bits(b, 4);+ int align = abr_bit(b);+ int cnt = (int)abr_bits(b, 8);+ if (cnt == 255)+ cnt += (int)abr_bits(b, 8);+ if (align)+ b->pos = (b->pos + 7) & ~(size_t)7;+ b->pos += 8 * (size_t)cnt;+ return abr_over(b) ? -1 : 0;+}++int aac_frame(struct aac_dec *d, const uint8_t *p, size_t n, char *why,+ size_t wn)+{+ struct abr b = {.p = p, .n = n};+ struct element *prev = NULL;+ struct element *seen[MAX_ELEMENTS];+ int n_seen = 0;+ for (;;) {+ int id = (int)abr_bits(&b, 3);+ if (abr_over(&b)) {+ snprintf(why, wn, "an AAC frame cut short");+ return -1;+ }+ if (id == 7)+ break;+ int rc = 0;+ switch (id) {+ case 0:+ case 1:+ case 2:+ case 3: {+ int tag = (int)abr_bits(&b, 4);+ struct element *e = element(d, id, tag);+ if (!e) {+ snprintf(why, wn, "an AAC stream of too many channels");+ return -1;+ }+ rc = id == 1 ? read_cpe(d, &b, e)+ : id == 2 ? read_cce(d, &b, e)+ : read_sce(d, &b, e);+ prev = e;+ if (n_seen < MAX_ELEMENTS)+ seen[n_seen++] = e;+ break;+ }+ case 4:+ rc = read_dse(&b);+ break;+ case 5:+ rc = read_pce(&b) < 0 ? -1 : 0;+ break;+ case 6:+ rc = read_fil(d, &b, prev);+ break;+ }+ if (rc != 0) {+ snprintf(why, wn, "an AAC frame that is none");+ return -1;+ }+ }+ if (d->sbr_on < 0)+ d->sbr_on = 0;+ int nsamp = d->sbr_on == 1 && !d->sbr_down ? 2 * d->frame : d->frame;+ /* the coupling channels first: their spectra with their TNS, and+ their samples when they are added after the IMDCT */+ for (int k = 0; k < n_seen; k++) {+ struct element *e = seen[k];+ if (e->type != 2)+ continue;+ struct chan *c = d->ch[e->first];+ if (c->tns.present)+ apply_tns(d, c->coef, &c->tns, &c->ics, 0);+ if (e->cce->point != AFTER_IMDCT)+ continue;+ synthesize(d, c);+ float *core[1] = {c->out}, *o[2] = {e->cce->out, NULL};+ if (d->sbr_on == 1) {+ if (!e->sbr && !(e->sbr = sbr_new(d->cfg.rate, 1, d->sbr_down)))+ return -1;+ float tmp[2048];+ o[1] = tmp;+ sbr_apply(e->sbr, 1, core, o);+ } else {+ memcpy(e->cce->out, c->out, sizeof(float) * (size_t)d->frame);+ }+ }+ /* the spectra to samples, element by element */+ int out = 0;+ for (int k = 0; k < n_seen; k++) {+ struct element *e = seen[k];+ if (e->type == 2)+ continue;+ float *core[2];+ int coupled = e->type <= 1;+ if (coupled)+ couple(d, seen, n_seen, e, BEFORE_TNS, NULL, 0);+ for (int j = 0; j < e->ch; j++) {+ struct chan *c = d->ch[e->first + j];+ if (d->cfg.aot == AOT_LTP)+ apply_ltp(d, c);+ if (c->tns.present)+ apply_tns(d, c->coef, &c->tns, &c->ics, 0);+ }+ if (coupled)+ couple(d, seen, n_seen, e, BETWEEN_TNS_AND_IMDCT, NULL, 0);+ for (int j = 0; j < e->ch; j++) {+ struct chan *c = d->ch[e->first + j];+ synthesize(d, c);+ core[j] = c->out;+ }+ int first_out = out;+ if (d->sbr_on == 1) {+ int ps = e->ch == 1 && e->sbr && sbr_has_ps(e->sbr);+ int och = ps ? 2 : e->ch;+ if (out + och > MAX_CHANNELS)+ break;+ /* outputs, not channels: PS makes two of a mono element */+ int ok = 1;+ for (int j = 0; j < och; j++)+ if (!d->outp[out + j] &&+ !(d->outp[out + j] = calloc(2 * 2048, sizeof(float))))+ ok = 0;+ if (!ok)+ break;+ float *o[2] = {d->outp[out], och > 1 ? d->outp[out + 1] : NULL};+ if (e->sbr) {+ sbr_apply(e->sbr, e->ch, core, o);+ } else {+ /* no SBR data for this element yet: the core upsampled+ by the same filter bank, as an empty SBR frame */+ if (!(e->sbr = sbr_new(d->cfg.rate, e->ch, d->sbr_down)))+ break;+ sbr_apply(e->sbr, e->ch, core, o);+ }+ out += och;+ } else {+ for (int j = 0; j < e->ch && out < MAX_CHANNELS; j++)+ memcpy(d->outp[out++], core[j],+ sizeof(float) * (size_t)d->frame);+ }+ if (coupled && out - first_out >= e->ch)+ couple(d, seen, n_seen, e, AFTER_IMDCT, d->outp + first_out, nsamp);+ }+ d->out_ch = out;+ return nsamp;+}++/* ---- a stream ---- */++int aac_stream(const struct aac_cfg *c, const uint8_t *data, const size_t *off,+ const size_t *len, size_t count, const struct audio_edit *edit,+ struct janas_audio *a, char *why, size_t wn)+{+ struct aac_dec *d = aac_new(c);+ if (!d) {+ snprintf(why, wn, "out of memory");+ return -1;+ }+ float *pcm = NULL;+ size_t got = 0, cap = 0;+ int rc = -1, rate = 0, bad = 0;+ for (size_t i = 0; i < count; i++) {+ char w[160];+ int ns = aac_frame(d, data + off[i], len[i], w, sizeof w);+ if (ns < 0) {+ /* a frame that cannot be read is silence, as a decoder+ conceals it; a stream of nothing else is refused */+ if (++bad > 8 && got == 0) {+ snprintf(why, wn, "%s", w);+ goto end;+ }+ if (!rate)+ continue;+ ns = rate == 2 * c->rate ? 2 * d->frame : d->frame;+ d->out_ch = 0;+ }+ if (!rate)+ rate = aac_out_rate(d);+ if ((size_t)ns > JANAS_AUDIO_MAX_SAMPLES - got) {+ snprintf(why, wn, "a sound longer than this reads");+ goto end;+ }+ if (got + (size_t)ns > cap) {+ size_t nc = cap ? cap * 2 : 65536;+ while (nc < got + (size_t)ns)+ nc *= 2;+ float *t = realloc(pcm, sizeof(float) * nc);+ if (!t) {+ snprintf(why, wn, "out of memory");+ goto end;+ }+ pcm = t;+ cap = nc;+ }+ float **o;+ int nch = aac_out(d, &o);+ for (int k = 0; k < ns; k++) {+ float s = 0;+ for (int j = 0; j < nch; j++)+ s += o[j][k];+ pcm[got + (size_t)k] = nch ? s / (float)nch : 0;+ }+ got += (size_t)ns;+ }+ size_t skip = 0, total = got;+ audio_edit_apply(edit, (uint32_t)rate, &skip, &total);+ if (!total) {+ snprintf(why, wn, "an AAC stream without sound");+ goto end;+ }+ memmove(pcm, pcm + skip, sizeof(float) * total);+ a->pcm = pcm;+ a->n = total;+ a->rate = (uint32_t)rate;+ pcm = NULL;+ rc = 0;+end:+ free(pcm);+ aac_free(d);+ return rc;+}++/* ---- ADTS ---- */++int janas_audio_adts(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn)+{+ /* two headers in a row, or one that fills the file */+ if (n < 7 || d[0] != 0xFF || (d[1] & 0xF6) != 0xF0)+ return 1;+ size_t flen = (size_t)(d[3] & 3) << 11 | (size_t)d[4] << 3 | d[5] >> 5;+ if (flen < 7 || (flen < n && (flen + 2 > n || d[flen] != 0xFF ||+ (d[flen + 1] & 0xF6) != 0xF0)))+ return 1;+ struct aac_cfg c;+ memset(&c, 0, sizeof c);+ c.aot = (d[2] >> 6) + 1;+ c.rate_index = d[2] >> 2 & 15;+ c.rate = aac_index_rate(c.rate_index);+ c.chan_config = (d[2] & 1) << 2 | d[3] >> 6;+ c.channels = CONFIG_CHANNELS[c.chan_config];+ c.frame = 1024;+ c.sbr = c.ps = -1;+ if (!c.rate || (c.aot != AOT_MAIN && c.aot != AOT_LC && c.aot != AOT_LTP)) {+ snprintf(why, wn, "an ADTS stream of a kind not read (profile %d)",+ c.aot - 1);+ return -1;+ }+ size_t cap = 1024, count = 0;+ size_t *off = malloc(sizeof(size_t) * cap),+ *len = malloc(sizeof(size_t) * cap);+ int rc = -1;+ if (!off || !len) {+ snprintf(why, wn, "out of memory");+ goto end;+ }+ for (size_t at = 0; at + 7 <= n;) {+ const uint8_t *h = d + at;+ if (h[0] != 0xFF || (h[1] & 0xF6) != 0xF0)+ break; /* a tag, or the end of the sound */+ size_t fl = (size_t)(h[3] & 3) << 11 | (size_t)h[4] << 3 | h[5] >> 5;+ int crc = !(h[1] & 1), blocks = (h[6] & 3) + 1;+ size_t hl = 7 + (crc ? 2 * (size_t)blocks : 0);+ if (fl < hl || (h[2] >> 2 & 15) != c.rate_index)+ break;+ if (fl > n - at)+ fl = n - at; /* the last frame cut short */+ /* the blocks: after one header, each with its CRC when there are+ several and they are protected */+ size_t start = at + 7 + (crc && blocks == 1 ? 2 : 0);+ size_t posn[4] = {0};+ if (crc && blocks > 1)+ for (int i = 1; i < blocks; i++)+ posn[i] = (size_t)h[7 + 2 * (i - 1)] << 8 | h[8 + 2 * (i - 1)];+ if (crc && blocks > 1)+ start = at + 7 + 2 * (size_t)blocks;+ for (int i = 0; i < blocks; i++) {+ size_t bs = start, be = at + fl;+ if (blocks > 1) {+ if (crc) {+ bs = i ? at + posn[i] : start;+ be = i + 1 < blocks ? at + posn[i + 1] : at + fl;+ if (be < bs + 2 || be > at + fl)+ break;+ be -= 2;+ } else {+ /* unprotected blocks are read one after another:+ the decoder tells where each ends */+ be = at + fl;+ }+ }+ if (count == cap) {+ cap *= 2;+ size_t *o2 = realloc(off, sizeof(size_t) * cap);+ if (o2)+ off = o2;+ size_t *l2 = realloc(len, sizeof(size_t) * cap);+ if (l2)+ len = l2;+ if (!o2 || !l2) {+ snprintf(why, wn, "out of memory");+ goto end;+ }+ }+ off[count] = bs;+ len[count++] = be > bs ? be - bs : 0;+ if (blocks > 1 && !crc)+ break; /* not separable: the first block only */+ }+ at += fl;+ }+ rc = aac_stream(&c, d, off, len, count, NULL, a, why, wn);+end:+ free(off);+ free(len);+ return rc;+}diff --git a/src/common/audio_aac.h b/src/common/audio_aac.hnew file mode 100644index 0000000..0513904--- /dev/null+++ b/src/common/audio_aac.h@@ -0,0 +1,172 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_aac.h - what the parts of the AAC decoder share (ISO/IEC 14496-3):+ * the bits, read most significant first, the Huffman tables, the core+ * (audio_aac.c: AAC Main, LC and LTP), spectral band replication+ * (audio_aac_sbr.c) and parametric stereo (audio_aac_ps.c). Inside+ * src/common.+ */+#ifndef JANAS_COMMON_AUDIO_AAC_H+#define JANAS_COMMON_AUDIO_AAC_H++#include <stddef.h>+#include <stdint.h>++#include "audio_ogg.h"++/* ---- bits ---- */++struct abr {+ const uint8_t *p;+ size_t n; /* bytes */+ size_t pos; /* bits */+};++/* the next 32 bits, zeros past the end, without taking them */+static inline uint32_t abr_peek(const struct abr *b)+{+ size_t at = b->pos >> 3;+ uint64_t w = 0;+ for (int i = 0; i < 8; i++)+ w = w << 8 | (at + (size_t)i < b->n ? b->p[at + (size_t)i] : 0);+ return (uint32_t)((w << (b->pos & 7)) >> 32);+}++/* k bits, 0 to 32 */+static inline uint32_t abr_bits(struct abr *b, int k)+{+ if (!k)+ return 0;+ uint32_t v = abr_peek(b) >> (32 - k);+ b->pos += (size_t)k;+ return v;+}++static inline int abr_bit(struct abr *b)+{+ return (int)abr_bits(b, 1);+}++/* read past the end */+static inline int abr_over(const struct abr *b)+{+ return b->pos > b->n * 8;+}++static inline size_t abr_left(const struct abr *b)+{+ return b->pos < b->n * 8 ? b->n * 8 - b->pos : 0;+}++/* ---- Huffman codes ---- */++struct vlc_e {+ int16_t sym; /* or the subtable's start */+ int8_t len; /* bits taken; < 0: a subtable of -len bits; 0: no code */+};++struct vlc {+ struct vlc_e *t;+ int bits;+};++/* n codes (code[i] of len[i] bits, MSB first) for the symbols sym[i] (or+ i when sym is NULL): 0, or -1 when out of memory or not a prefix code */+int vlc_build(struct vlc *v, int bits, const uint32_t *code, const uint8_t *len,+ const uint8_t *sym, int n);+void vlc_free(struct vlc *v);++/* the next symbol, or -1 (a code that is none) */+static inline int vlc_get(struct abr *b, const struct vlc *v)+{+ uint32_t w = abr_peek(b);+ const struct vlc_e *e = &v->t[w >> (32 - v->bits)];+ int used = 0;+ if (e->len < 0) {+ used = v->bits;+ e = &v->t[e->sym + (int)((w << used) >> (32 + e->len))];+ }+ if (e->len <= 0)+ return -1;+ b->pos += (size_t)(used + e->len);+ return e->sym;+}++/* ---- the configuration ---- */++enum {+ AOT_MAIN = 1,+ AOT_LC = 2,+ AOT_SSR = 3,+ AOT_LTP = 4,+ AOT_SBR = 5,+ AOT_PS = 29,+};++struct aac_cfg {+ int aot; /* the core's: 1, 2 or 4 */+ int rate_index; /* the core's sampling frequency index, 0 to 12 */+ int rate;+ int chan_config; /* 0: a program config element says */+ int frame; /* 1024 or 960 */+ int sbr; /* 1 said present, 0 said absent, -1 not said */+ int ps; /* the same */+ int sbr_rate; /* the output's rate with SBR */+ int channels; /* from the configuration, 0 when unknown */+};++/* the AudioSpecificConfig of an MP4 file or of Matroska: 0, or -1 with+ why */+int aac_config(const uint8_t *p, size_t n, struct aac_cfg *c, char *why,+ size_t wn);+/* the rate of a sampling frequency index, 0 when none */+int aac_index_rate(int index);+int aac_rate_index(int rate);++struct aac_dec;+struct aac_dec *aac_new(const struct aac_cfg *c);+void aac_free(struct aac_dec *d);+/* one raw_data_block of n bytes: its samples a channel (the frame, or+ twice it with SBR), or -1 with why; the channels and their samples in+ aac_out */+int aac_frame(struct aac_dec *d, const uint8_t *p, size_t n, char *why,+ size_t wn);+int aac_out(const struct aac_dec *d, float ***pcm);+/* the output's rate: the core's, or twice it once SBR was found */+int aac_out_rate(const struct aac_dec *d);++/* a whole stream: the packets (raw data blocks, data + off[i], len[i]),+ trimmed as the edit says (NULL: not at all); mixed to mono into a */+int aac_stream(const struct aac_cfg *c, const uint8_t *data, const size_t *off,+ const size_t *len, size_t count, const struct audio_edit *edit,+ struct janas_audio *a, char *why, size_t wn);++/* ---- spectral band replication (audio_aac_sbr.c) ---- */++struct sbr;+/* for an element of ch channels (1, 2) of a core at rate; down: the+ output at the core's rate (downsampled SBR) */+struct sbr *sbr_new(int rate, int ch, int down);+void sbr_free(struct sbr *s);+/* the sbr_extension_data of an extension payload of cnt bytes at b (its+ type, 13 or 14, read): 0, or -1 */+int sbr_read(struct sbr *s, struct abr *b, int crc, int cnt);+/* 1024 core samples of each channel to 2048 (1024 downsampled): in[ch],+ out[ch]; with PS a mono element makes two channels (out[0], out[1]) */+void sbr_apply(struct sbr *s, int ch, float *const *in, float **out);+/* parametric stereo was found in the SBR data */+int sbr_has_ps(const struct sbr *s);++/* ---- parametric stereo (audio_aac_ps.c) ---- */++struct ps;+struct ps *ps_new(void);+void ps_free(struct ps *p);+/* ps_data from an SBR extension of bits bits: the bits read, or -1 */+int ps_read(struct ps *p, struct abr *b, int bits);+/* the QMF subbands of a mono signal (l: re and im planes of 38 slots of+ 64 bands) into two, l and r; top: the bands SBR fills */+void ps_apply(struct ps *p, float (*l)[38][64], float (*r)[38][64], int top);++#endifdiff --git a/src/common/audio_aac_ps.c b/src/common/audio_aac_ps.cnew file mode 100644index 0000000..9bceec6--- /dev/null+++ b/src/common/audio_aac_ps.c@@ -0,0 +1,1022 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_aac_ps.c - parametric stereo (ISO/IEC 14496-3, 8.6.4), HE-AAC+ * v2's two channels made from one: the ps_data in SBR's extension (the+ * intensity differences, coherences and, beyond the baseline, the phase+ * differences, in 10, 20 or 34 bands, Huffman-coded in frequency or in+ * time), the hybrid analysis that splits the lowest QMF bands, the+ * decorrelated signal (all-pass links and delays, transients attenuated),+ * the mixing matrices interpolated between the envelopes' borders, and the+ * hybrid synthesis back to the QMF bands that SBR's synthesis turns into+ * samples. Single precision, FFmpeg's order of operations.+ */+#include "audio_aac.h"++#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "audio_aac_tab.h"++#define MAX_ENV 5+#define MAX_PAR 34+#define MAX_SSB 91+#define MAX_AP_BANDS 50+#define SLOTS 32+#define MAX_DELAY 14+#define AP_LINKS 3+#define MAX_AP_DELAY 5++enum {+ IID_DF1,+ IID_DT1,+ IID_DF0,+ IID_DT0,+ ICC_DF,+ ICC_DT,+ IPD_DF,+ IPD_DT,+ OPD_DF,+ OPD_DT,+};++static const int NR_PAR_BANDS[2] = {20, 34};+static const int NR_IPDOPD_BANDS[2] = {11, 17};+static const int NR_BANDS[2] = {71, 91};+static const int DECAY_CUTOFF[2] = {10, 32};+static const int NR_ALLPASS_BANDS[2] = {30, 50};+static const int SHORT_DELAY_BAND[2] = {42, 62};++struct ps_tables {+ float pd_re[512], pd_im[512];+ float HA[46][8][4], HB[46][8][4];+ float f20_0_8[8][8][2], f34_0_12[12][8][2], f34_1_8[8][8][2],+ f34_2_4[4][8][2];+ float q_fract[2][50][3][2];+ float phi_fract[2][50][2];+};++struct ps {+ /* the bitstream's */+ int start;+ int enable_iid, iid_quant, nr_iid_par, nr_ipdopd_par;+ int enable_icc, icc_mode, nr_icc_par;+ int enable_ext, frame_class, num_env_old, num_env;+ int enable_ipdopd;+ int border[MAX_ENV + 1];+ int8_t iid[MAX_ENV][MAX_PAR], icc[MAX_ENV][MAX_PAR];+ int8_t ipd[MAX_ENV][MAX_PAR], opd[MAX_ENV][MAX_PAR];+ int is34, is34_old;+ /* the processing's */+ float in_buf[5][44][2];+ float delay[MAX_SSB][SLOTS + MAX_DELAY][2];+ float ap_delay[MAX_AP_BANDS][AP_LINKS][SLOTS + MAX_AP_DELAY][2];+ float peak_decay_nrg[34], power_smooth[34], peak_decay_diff_smooth[34];+ float H11[2][MAX_ENV + 1][MAX_PAR], H12[2][MAX_ENV + 1][MAX_PAR];+ float H21[2][MAX_ENV + 1][MAX_PAR], H22[2][MAX_ENV + 1][MAX_PAR];+ float Lbuf[91][32][2], Rbuf[91][32][2];+ int8_t opd_hist[MAX_PAR], ipd_hist[MAX_PAR];+ struct vlc vlc[10];+ struct ps_tables t;+};++/* ---- the tables (FFmpeg's ps_tableinit) ---- */++static void make_filters(float (*filter)[8][2], const float *proto, int bands)+{+ for (int q = 0; q < bands; q++)+ for (int n = 0; n < 7; n++) {+ double theta = 2 * M_PI * (q + 0.5) * (n - 6) / bands;+ filter[q][n][0] = (float)(proto[n] * cos(theta));+ filter[q][n][1] = (float)(proto[n] * -sin(theta));+ }+}++static void tables(struct ps_tables *t)+{+ static const float SIN[] = {0, (float)M_SQRT1_2, 1, (float)M_SQRT1_2,+ 0, -(float)M_SQRT1_2, -1, -(float)M_SQRT1_2};+ static const float COS[] = {1, (float)M_SQRT1_2, 0, -(float)M_SQRT1_2,+ -1, -(float)M_SQRT1_2, 0, (float)M_SQRT1_2};+ static const float IID_DEQ[] = {+ 0.05623413251903f,+ 0.12589254117942f,+ 0.19952623149689f,+ 0.31622776601684f,+ 0.44668359215096f,+ 0.63095734448019f,+ 0.79432823472428f,+ 1,+ 1.25892541179417f,+ 1.58489319246111f,+ 2.23872113856834f,+ 3.16227766016838f,+ 5.01187233627272f,+ 7.94328234724282f,+ 17.7827941003892f,+ 0.00316227766017f,+ 0.00562341325190f,+ 0.01f,+ 0.01778279410039f,+ 0.03162277660168f,+ 0.05623413251903f,+ 0.07943282347243f,+ 0.11220184543020f,+ 0.15848931924611f,+ 0.22387211385683f,+ 0.31622776601684f,+ 0.39810717055350f,+ 0.50118723362727f,+ 0.63095734448019f,+ 0.79432823472428f,+ 1,+ 1.25892541179417f,+ 1.58489319246111f,+ 1.99526231496888f,+ 2.51188643150958f,+ 3.16227766016838f,+ 4.46683592150963f,+ 6.30957344480193f,+ 8.91250938133745f,+ 12.5892541179417f,+ 17.7827941003892f,+ 31.6227766016838f,+ 56.2341325190349f,+ 100,+ 177.827941003892f,+ 316.227766016837f,+ };+ static const float ICC_INVQ[] = {1, 0.937f, 0.84118f, 0.60092f,+ 0.36764f, 0, -0.589f, -1};+ static const float ACOS_ICC_INVQ[] = {+ 0, 0.35685527f, 0.57133466f, 0.92614472f,+ 1.1943263f, (float)(M_PI / 2), 2.2006171f, (float)M_PI};+ static const int8_t F_CENTER_20[] = {-3, -1, 1, 3, 5, 7, 10, 14, 18, 22};+ static const int8_t F_CENTER_34[] = {+ 2, 6, 10, 14, 18, 22, 26, 30, 34, -10, -6, -2, 51, 57, 15, 21,+ 27, 33, 39, 45, 54, 66, 78, 42, 102, 66, 78, 90, 102, 114, 126, 90};+ static const float LINKS[] = {0.43f, 0.75f, 0.347f};+ const float gain = 0.39f;+ for (int p0 = 0; p0 < 8; p0++)+ for (int p1 = 0; p1 < 8; p1++)+ for (int p2 = 0; p2 < 8; p2++) {+ float re = 0.25f * COS[p0] + 0.5f * COS[p1] + COS[p2];+ float im = 0.25f * SIN[p0] + 0.5f * SIN[p1] + SIN[p2];+ float mag = (float)(1 / hypot(im, re));+ t->pd_re[p0 * 64 + p1 * 8 + p2] = re * mag;+ t->pd_im[p0 * 64 + p1 * 8 + p2] = im * mag;+ }+ for (int iid = 0; iid < 46; iid++) {+ float c = IID_DEQ[iid];+ float c1 = (float)M_SQRT2 / sqrtf(1.0f + c * c);+ float c2 = c * c1;+ for (int icc = 0; icc < 8; icc++) {+ float alpha = 0.5f * ACOS_ICC_INVQ[icc];+ float beta = alpha * (c1 - c2) * (float)M_SQRT1_2;+ t->HA[iid][icc][0] = c2 * cosf(beta + alpha);+ t->HA[iid][icc][1] = c1 * cosf(beta - alpha);+ t->HA[iid][icc][2] = c2 * sinf(beta + alpha);+ t->HA[iid][icc][3] = c1 * sinf(beta - alpha);+ float rho = ICC_INVQ[icc] > 0.05f ? ICC_INVQ[icc] : 0.05f;+ alpha = 0.5f * atan2f(2.0f * c * rho, c * c - 1.0f);+ float mu = c + 1.0f / c;+ mu = sqrtf(1 + (4 * rho * rho - 4) / (mu * mu));+ float gamma = atanf(sqrtf((1.0f - mu) / (1.0f + mu)));+ if (alpha < 0)+ alpha += (float)(M_PI / 2);+ float ac = cosf(alpha), as = sinf(alpha);+ float gc = cosf(gamma), gs = sinf(gamma);+ t->HB[iid][icc][0] = (float)(M_SQRT2 * ac * gc);+ t->HB[iid][icc][1] = (float)(M_SQRT2 * as * gc);+ t->HB[iid][icc][2] = (float)(-M_SQRT2 * as * gs);+ t->HB[iid][icc][3] = (float)(M_SQRT2 * ac * gs);+ }+ }+ for (int k = 0; k < 30; k++) {+ double fc = k < 10 ? F_CENTER_20[k] * 0.125 : k - 6.5f;+ for (int m = 0; m < AP_LINKS; m++) {+ double th = -M_PI * LINKS[m] * fc;+ t->q_fract[0][k][m][0] = (float)cos(th);+ t->q_fract[0][k][m][1] = (float)sin(th);+ }+ double th = -M_PI * gain * fc;+ t->phi_fract[0][k][0] = (float)cos(th);+ t->phi_fract[0][k][1] = (float)sin(th);+ }+ for (int k = 0; k < 50; k++) {+ double fc = k < 32 ? F_CENTER_34[k] / 24.0 : k - 26.5f;+ for (int m = 0; m < AP_LINKS; m++) {+ double th = -M_PI * LINKS[m] * fc;+ t->q_fract[1][k][m][0] = (float)cos(th);+ t->q_fract[1][k][m][1] = (float)sin(th);+ }+ double th = -M_PI * gain * fc;+ t->phi_fract[1][k][0] = (float)cos(th);+ t->phi_fract[1][k][1] = (float)sin(th);+ }+ make_filters(t->f20_0_8, PS_G0_Q8, 8);+ make_filters(t->f34_0_12, PS_G0_Q12, 12);+ make_filters(t->f34_1_8, PS_G1_Q8, 8);+ make_filters(t->f34_2_4, PS_G2_Q4, 4);+}++struct ps *ps_new(void)+{+ struct ps *p = calloc(1, sizeof *p);+ if (!p)+ return NULL;+ for (int t = 0; t < 10; t++) {+ int at = PS_HUFF_AT[t], n = PS_HUFF_AT[t + 1] - at;+ if (vlc_build(&p->vlc[t], 9, PS_HUFF_CODE + at, PS_HUFF_LEN + at,+ PS_HUFF_SYM + at, n) != 0) {+ ps_free(p);+ return NULL;+ }+ }+ tables(&p->t);+ return p;+}++void ps_free(struct ps *p)+{+ if (!p)+ return;+ for (int t = 0; t < 10; t++)+ vlc_free(&p->vlc[t]);+ free(p);+}++/* ---- the data ---- */++static int huff(struct ps *p, struct abr *b, int t)+{+ int v = vlc_get(b, &p->vlc[t]);+ return v < 0 ? 1000 : v + PS_HUFF_OFFSET[t];+}++/* kind 0 iid, 1 icc, 2 ipd/opd: 0, or -1 */+static int read_par(struct ps *p, struct abr *b, int8_t (*par)[MAX_PAR],+ int table, int e, int dt, int num, int kind)+{+ if (dt) {+ int ep = e ? e - 1 : p->num_env_old - 1;+ if (ep < 0)+ ep = 0;+ for (int k = 0; k < num; k++) {+ int v = par[ep][k] + huff(p, b, table);+ if (kind == 2)+ v &= 7;+ par[e][k] = (int8_t)v;+ if ((kind == 0 && abs(v) > 7 + 8 * p->iid_quant) ||+ (kind == 1 && (unsigned)v > 7))+ return -1;+ }+ } else {+ int v = 0;+ for (int k = 0; k < num; k++) {+ v += huff(p, b, table);+ if (kind == 2)+ v &= 7;+ par[e][k] = (int8_t)v;+ if ((kind == 0 && abs(v) > 7 + 8 * p->iid_quant) ||+ (kind == 1 && (unsigned)v > 7))+ return -1;+ }+ }+ return 0;+}++static int read_ext(struct ps *p, struct abr *b, int id)+{+ size_t start = b->pos;+ if (id)+ return 0;+ p->enable_ipdopd = abr_bit(b);+ if (p->enable_ipdopd) {+ for (int e = 0; e < p->num_env; e++) {+ int dt = abr_bit(b);+ read_par(p, b, p->ipd, dt ? IPD_DT : IPD_DF, e, dt,+ p->nr_ipdopd_par, 2);+ dt = abr_bit(b);+ read_par(p, b, p->opd, dt ? OPD_DT : OPD_DF, e, dt,+ p->nr_ipdopd_par, 2);+ }+ }+ abr_bit(b);+ return (int)(b->pos - start);+}++int ps_read(struct ps *p, struct abr *host, int bits)+{+ static const int8_t NUM_ENV[2][4] = {{0, 1, 2, 4}, {1, 2, 3, 4}};+ static const int8_t NR_IIDICC[] = {10, 20, 34, 10, 20, 34};+ static const int8_t NR_IPDOPD[] = {5, 11, 17, 5, 11, 17};+ struct abr g = *host, *b = &g;+ size_t start = b->pos;+ int header = abr_bit(b);+ if (header) {+ p->enable_iid = abr_bit(b);+ if (p->enable_iid) {+ int mode = (int)abr_bits(b, 3);+ if (mode > 5)+ goto err;+ p->nr_iid_par = NR_IIDICC[mode];+ p->iid_quant = mode > 2;+ p->nr_ipdopd_par = NR_IPDOPD[mode];+ }+ p->enable_icc = abr_bit(b);+ if (p->enable_icc) {+ p->icc_mode = (int)abr_bits(b, 3);+ if (p->icc_mode > 5)+ goto err;+ p->nr_icc_par = NR_IIDICC[p->icc_mode];+ }+ p->enable_ext = abr_bit(b);+ }+ p->frame_class = abr_bit(b);+ p->num_env_old = p->num_env;+ p->num_env = NUM_ENV[p->frame_class][abr_bits(b, 2)];+ p->border[0] = -1;+ if (p->frame_class) {+ for (int e = 1; e <= p->num_env; e++) {+ p->border[e] = (int)abr_bits(b, 5);+ if (p->border[e] < p->border[e - 1])+ goto err;+ }+ } else {+ static const int LOG2[5] = {0, 0, 1, 1, 2};+ for (int e = 1; e <= p->num_env; e++)+ p->border[e] = (e * SLOTS >> LOG2[p->num_env]) - 1;+ }+ if (p->enable_iid) {+ for (int e = 0; e < p->num_env; e++) {+ int dt = abr_bit(b);+ static const int HUFF_IID[4] = {IID_DF0, IID_DF1, IID_DT0, IID_DT1};+ if (read_par(p, b, p->iid, HUFF_IID[2 * dt + p->iid_quant], e, dt,+ p->nr_iid_par, 0) != 0)+ goto err;+ }+ } else {+ memset(p->iid, 0, sizeof p->iid);+ }+ if (p->enable_icc) {+ for (int e = 0; e < p->num_env; e++) {+ int dt = abr_bit(b);+ if (read_par(p, b, p->icc, dt ? ICC_DT : ICC_DF, e, dt,+ p->nr_icc_par, 1) != 0)+ goto err;+ }+ } else {+ memset(p->icc, 0, sizeof p->icc);+ }+ if (p->enable_ext) {+ int cnt = (int)abr_bits(b, 4);+ if (cnt == 15)+ cnt += (int)abr_bits(b, 8);+ cnt *= 8;+ while (cnt > 7) {+ int id = (int)abr_bits(b, 2);+ cnt -= 2 + read_ext(p, b, id);+ }+ if (cnt < 0)+ goto err;+ b->pos += (size_t)cnt;+ }+ /* an envelope that reaches the frame's end */+ if (!p->num_env || p->border[p->num_env] < SLOTS - 1) {+ int source = p->num_env ? p->num_env - 1 : p->num_env_old - 1;+ if (source >= 0 && source != p->num_env) {+ if (p->enable_iid)+ memcpy(p->iid[p->num_env], p->iid[source], sizeof p->iid[0]);+ if (p->enable_icc)+ memcpy(p->icc[p->num_env], p->icc[source], sizeof p->icc[0]);+ if (p->enable_ipdopd) {+ memcpy(p->ipd[p->num_env], p->ipd[source], sizeof p->ipd[0]);+ memcpy(p->opd[p->num_env], p->opd[source], sizeof p->opd[0]);+ }+ }+ if (p->enable_iid)+ for (int k = 0; k < p->nr_iid_par; k++)+ if (abs(p->iid[p->num_env][k]) > 7 + 8 * p->iid_quant)+ goto err;+ if (p->enable_icc)+ for (int k = 0; k < p->nr_iid_par; k++)+ if ((unsigned)p->icc[p->num_env][k] > 7)+ goto err;+ p->num_env++;+ p->border[p->num_env] = SLOTS - 1;+ }+ p->is34_old = p->is34;+ if (p->enable_iid || p->enable_icc)+ p->is34 = (p->enable_iid && p->nr_iid_par == 34) ||+ (p->enable_icc && p->nr_icc_par == 34);+ if (!p->enable_ipdopd) {+ memset(p->ipd, 0, sizeof p->ipd);+ memset(p->opd, 0, sizeof p->opd);+ }+ if (header)+ p->start = 1;+ {+ int used = (int)(b->pos - start);+ if (used <= bits) {+ host->pos += (size_t)used;+ return used;+ }+ }+err:+ p->start = 0;+ host->pos += (size_t)bits;+ memset(p->iid, 0, sizeof p->iid);+ memset(p->icc, 0, sizeof p->icc);+ memset(p->ipd, 0, sizeof p->ipd);+ memset(p->opd, 0, sizeof p->opd);+ return bits;+}++/* ---- the processing ---- */++static void hybrid_filter(float (*out)[2], float (*in)[2],+ const float (*filter)[8][2], ptrdiff_t stride, int n)+{+ float inre0[6], inre1[6], inim0[6], inim1[6];+ for (int j = 0; j < 6; j++) {+ inre0[j] = in[j][0] + in[12 - j][0];+ inre1[j] = in[j][1] - in[12 - j][1];+ inim0[j] = in[j][1] + in[12 - j][1];+ inim1[j] = in[j][0] - in[12 - j][0];+ }+ for (int i = 0; i < n; i++) {+ float re = filter[i][6][0] * in[6][0];+ float im = filter[i][6][0] * in[6][1];+ for (int j = 0; j < 6; j++) {+ re += filter[i][j][0] * inre0[j] - filter[i][j][1] * inre1[j];+ im += filter[i][j][0] * inim0[j] + filter[i][j][1] * inim1[j];+ }+ out[i * stride][0] = re;+ out[i * stride][1] = im;+ }+}++static void hybrid2_re(float (*in)[2], float (*out)[32][2], int len,+ int reverse)+{+ static const float G1_Q2[] = {+ 0.0f, 0.01899487526049f, 0.0f, -0.07293139167538f,+ 0.0f, 0.30596630545168f, 0.5f};+ for (int i = 0; i < len; i++, in++) {+ float re_in = G1_Q2[6] * in[6][0], re_op = 0;+ float im_in = G1_Q2[6] * in[6][1], im_op = 0;+ for (int j = 0; j < 6; j += 2) {+ re_op += G1_Q2[j + 1] * (in[j + 1][0] + in[12 - j - 1][0]);+ im_op += G1_Q2[j + 1] * (in[j + 1][1] + in[12 - j - 1][1]);+ }+ out[reverse][i][0] = re_in + re_op;+ out[reverse][i][1] = im_in + im_op;+ out[!reverse][i][0] = re_in - re_op;+ out[!reverse][i][1] = im_in - im_op;+ }+}++static void hybrid_analysis(struct ps *p, float (*out)[32][2],+ float (*L)[38][64], int is34)+{+ for (int i = 0; i < 5; i++)+ for (int j = 0; j < 38; j++) {+ p->in_buf[i][j + 6][0] = L[0][j][i];+ p->in_buf[i][j + 6][1] = L[1][j][i];+ }+ const int len = 32;+ if (is34) {+ for (int i = 0; i < len; i++) {+ hybrid_filter(out[0] + i, p->in_buf[0] + i,+ (const float(*)[8][2])p->t.f34_0_12, 32, 12);+ hybrid_filter(out[12] + i, p->in_buf[1] + i,+ (const float(*)[8][2])p->t.f34_1_8, 32, 8);+ hybrid_filter(out[20] + i, p->in_buf[2] + i,+ (const float(*)[8][2])p->t.f34_2_4, 32, 4);+ hybrid_filter(out[24] + i, p->in_buf[3] + i,+ (const float(*)[8][2])p->t.f34_2_4, 32, 4);+ hybrid_filter(out[28] + i, p->in_buf[4] + i,+ (const float(*)[8][2])p->t.f34_2_4, 32, 4);+ }+ for (int i = 5; i < 64; i++)+ for (int j = 0; j < len; j++) {+ out[27 + i][j][0] = L[0][j][i];+ out[27 + i][j][1] = L[1][j][i];+ }+ } else {+ for (int i = 0; i < len; i++) {+ float temp[8][2];+ hybrid_filter(temp, p->in_buf[0] + i,+ (const float(*)[8][2])p->t.f20_0_8, 1, 8);+ out[0][i][0] = temp[6][0];+ out[0][i][1] = temp[6][1];+ out[1][i][0] = temp[7][0];+ out[1][i][1] = temp[7][1];+ out[2][i][0] = temp[0][0];+ out[2][i][1] = temp[0][1];+ out[3][i][0] = temp[1][0];+ out[3][i][1] = temp[1][1];+ out[4][i][0] = temp[2][0] + temp[5][0];+ out[4][i][1] = temp[2][1] + temp[5][1];+ out[5][i][0] = temp[3][0] + temp[4][0];+ out[5][i][1] = temp[3][1] + temp[4][1];+ }+ hybrid2_re(p->in_buf[1], out + 6, len, 1);+ hybrid2_re(p->in_buf[2], out + 8, len, 0);+ for (int i = 3; i < 64; i++)+ for (int j = 0; j < len; j++) {+ out[7 + i][j][0] = L[0][j][i];+ out[7 + i][j][1] = L[1][j][i];+ }+ }+ for (int i = 0; i < 5; i++)+ memcpy(p->in_buf[i], p->in_buf[i] + 32, sizeof(p->in_buf[i][0]) * 6);+}++static void hybrid_synthesis(float (*out)[38][64], float (*in)[32][2], int is34)+{+ const int len = 32;+ if (is34) {+ for (int n = 0; n < len; n++) {+ for (int c = 0; c < 2; c++)+ for (int i = 0; i < 5; i++)+ out[c][n][i] = 0;+ for (int i = 0; i < 12; i++) {+ out[0][n][0] += in[i][n][0];+ out[1][n][0] += in[i][n][1];+ }+ for (int i = 0; i < 8; i++) {+ out[0][n][1] += in[12 + i][n][0];+ out[1][n][1] += in[12 + i][n][1];+ }+ for (int i = 0; i < 4; i++) {+ out[0][n][2] += in[20 + i][n][0];+ out[1][n][2] += in[20 + i][n][1];+ out[0][n][3] += in[24 + i][n][0];+ out[1][n][3] += in[24 + i][n][1];+ out[0][n][4] += in[28 + i][n][0];+ out[1][n][4] += in[28 + i][n][1];+ }+ }+ for (int i = 5; i < 64; i++)+ for (int n = 0; n < len; n++) {+ out[0][n][i] = in[27 + i][n][0];+ out[1][n][i] = in[27 + i][n][1];+ }+ } else {+ for (int n = 0; n < len; n++) {+ out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] ++ in[3][n][0] + in[4][n][0] + in[5][n][0];+ out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] ++ in[3][n][1] + in[4][n][1] + in[5][n][1];+ out[0][n][1] = in[6][n][0] + in[7][n][0];+ out[1][n][1] = in[6][n][1] + in[7][n][1];+ out[0][n][2] = in[8][n][0] + in[9][n][0];+ out[1][n][2] = in[8][n][1] + in[9][n][1];+ }+ for (int i = 3; i < 64; i++)+ for (int n = 0; n < len; n++) {+ out[0][n][i] = in[7 + i][n][0];+ out[1][n][i] = in[7 + i][n][1];+ }+ }+}++static void decorrelate(float (*out)[2], float (*delay)[2],+ float (*ap_delay)[SLOTS + MAX_AP_DELAY][2],+ const float phi_fract[2], const float (*q_fract)[2],+ const float *transient_gain, float g_decay_slope,+ int len)+{+ static const float A[] = {0.65143905753106f, 0.56471812200776f,+ 0.48954165955695f};+ float ag[AP_LINKS];+ for (int m = 0; m < AP_LINKS; m++)+ ag[m] = A[m] * g_decay_slope;+ for (int n = 0; n < len; n++) {+ float in_re = delay[n][0] * phi_fract[0] - delay[n][1] * phi_fract[1];+ float in_im = delay[n][0] * phi_fract[1] + delay[n][1] * phi_fract[0];+ for (int m = 0; m < AP_LINKS; m++) {+ float a_re = ag[m] * in_re, a_im = ag[m] * in_im;+ float ld_re = ap_delay[m][n + 2 - m][0];+ float ld_im = ap_delay[m][n + 2 - m][1];+ float fd_re = q_fract[m][0], fd_im = q_fract[m][1];+ float apd_re = in_re, apd_im = in_im;+ in_re = ld_re * fd_re - ld_im * fd_im;+ in_re -= a_re;+ in_im = ld_re * fd_im + ld_im * fd_re;+ in_im -= a_im;+ ap_delay[m][n + 5][0] = apd_re + ag[m] * in_re;+ ap_delay[m][n + 5][1] = apd_im + ag[m] * in_im;+ }+ out[n][0] = transient_gain[n] * in_re;+ out[n][1] = transient_gain[n] * in_im;+ }+}++static void decorrelation(struct ps *p, float (*out)[32][2],+ const float (*s)[32][2], int is34)+{+ float power[34][SLOTS], transient_gain[34][SLOTS];+ const int8_t *k_to_i = is34 ? PS_K_TO_I_34 : PS_K_TO_I_20;+ const float peak_decay_factor = 0.76592833836465f;+ const float transient_impact = 1.5f, a_smooth = 0.25f;+ memset(power, 0, sizeof power);+ if (is34 != p->is34_old) {+ memset(p->peak_decay_nrg, 0, sizeof p->peak_decay_nrg);+ memset(p->power_smooth, 0, sizeof p->power_smooth);+ memset(p->peak_decay_diff_smooth, 0, sizeof p->peak_decay_diff_smooth);+ memset(p->delay, 0, sizeof p->delay);+ memset(p->ap_delay, 0, sizeof p->ap_delay);+ }+ for (int k = 0; k < NR_BANDS[is34]; k++) {+ int i = k_to_i[k];+ for (int n = 0; n < SLOTS; n++)+ power[i][n] += s[k][n][0] * s[k][n][0] + s[k][n][1] * s[k][n][1];+ }+ for (int i = 0; i < NR_PAR_BANDS[is34]; i++) {+ for (int n = 0; n < SLOTS; n++) {+ float decayed = peak_decay_factor * p->peak_decay_nrg[i];+ p->peak_decay_nrg[i] =+ decayed > power[i][n] ? decayed : power[i][n];+ p->power_smooth[i] += a_smooth * (power[i][n] - p->power_smooth[i]);+ p->peak_decay_diff_smooth[i] +=+ a_smooth * (p->peak_decay_nrg[i] - power[i][n] -+ p->peak_decay_diff_smooth[i]);+ float denom = transient_impact * p->peak_decay_diff_smooth[i];+ transient_gain[i][n] =+ denom > p->power_smooth[i] ? p->power_smooth[i] / denom : 1.0f;+ }+ }+ int k;+ for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {+ int b = k_to_i[k];+ float slope = 1.f - 0.05f * (float)(k - DECAY_CUTOFF[is34]);+ slope = slope < 0 ? 0 : slope > 1 ? 1 : slope;+ memcpy(p->delay[k], p->delay[k] + SLOTS,+ sizeof(p->delay[k][0]) * MAX_DELAY);+ memcpy(p->delay[k] + MAX_DELAY, s[k], sizeof(p->delay[k][0]) * SLOTS);+ for (int m = 0; m < AP_LINKS; m++)+ memcpy(p->ap_delay[k][m], p->ap_delay[k][m] + SLOTS,+ sizeof(p->ap_delay[k][m][0]) * 5);+ decorrelate(out[k], p->delay[k] + MAX_DELAY - 2, p->ap_delay[k],+ p->t.phi_fract[is34][k],+ (const float(*)[2])p->t.q_fract[is34][k], transient_gain[b],+ slope, SLOTS);+ }+ for (; k < NR_BANDS[is34]; k++) {+ int i = k_to_i[k];+ int d = k < SHORT_DELAY_BAND[is34] ? 14 : 1;+ memcpy(p->delay[k], p->delay[k] + SLOTS,+ sizeof(p->delay[k][0]) * MAX_DELAY);+ memcpy(p->delay[k] + MAX_DELAY, s[k], sizeof(p->delay[k][0]) * SLOTS);+ const float(*src)[2] = (const float(*)[2])p->delay[k] + MAX_DELAY - d;+ for (int n = 0; n < SLOTS; n++) {+ out[k][n][0] = src[n][0] * transient_gain[i][n];+ out[k][n][1] = src[n][1] * transient_gain[i][n];+ }+ }+}++static void map_idx_10_to_20(int8_t *pm, const int8_t *par, int full)+{+ int b;+ if (full) {+ b = 9;+ } else {+ b = 4;+ pm[10] = 0;+ }+ for (; b >= 0; b--)+ pm[2 * b + 1] = pm[2 * b] = par[b];+}++static void map_idx_34_to_20(int8_t *pm, const int8_t *par, int full)+{+ pm[0] = (int8_t)((2 * par[0] + par[1]) / 3);+ pm[1] = (int8_t)((par[1] + 2 * par[2]) / 3);+ pm[2] = (int8_t)((2 * par[3] + par[4]) / 3);+ pm[3] = (int8_t)((par[4] + 2 * par[5]) / 3);+ pm[4] = (int8_t)((par[6] + par[7]) / 2);+ pm[5] = (int8_t)((par[8] + par[9]) / 2);+ pm[6] = par[10];+ pm[7] = par[11];+ pm[8] = (int8_t)((par[12] + par[13]) / 2);+ pm[9] = (int8_t)((par[14] + par[15]) / 2);+ pm[10] = par[16];+ if (full) {+ pm[11] = par[17];+ pm[12] = par[18];+ pm[13] = par[19];+ pm[14] = (int8_t)((par[20] + par[21]) / 2);+ pm[15] = (int8_t)((par[22] + par[23]) / 2);+ pm[16] = (int8_t)((par[24] + par[25]) / 2);+ pm[17] = (int8_t)((par[26] + par[27]) / 2);+ pm[18] = (int8_t)((par[28] + par[29] + par[30] + par[31]) / 4);+ pm[19] = (int8_t)((par[32] + par[33]) / 2);+ }+}++static void map_val_34_to_20(float *par)+{+ par[0] = (2 * par[0] + par[1]) * 0.33333333f;+ par[1] = (par[1] + 2 * par[2]) * 0.33333333f;+ par[2] = (2 * par[3] + par[4]) * 0.33333333f;+ par[3] = (par[4] + 2 * par[5]) * 0.33333333f;+ par[4] = (par[6] + par[7]) * 0.5f;+ par[5] = (par[8] + par[9]) * 0.5f;+ par[6] = par[10];+ par[7] = par[11];+ par[8] = (par[12] + par[13]) * 0.5f;+ par[9] = (par[14] + par[15]) * 0.5f;+ par[10] = par[16];+ par[11] = par[17];+ par[12] = par[18];+ par[13] = par[19];+ par[14] = (par[20] + par[21]) * 0.5f;+ par[15] = (par[22] + par[23]) * 0.5f;+ par[16] = (par[24] + par[25]) * 0.5f;+ par[17] = (par[26] + par[27]) * 0.5f;+ par[18] = (par[28] + par[29] + par[30] + par[31]) * 0.25f;+ par[19] = (par[32] + par[33]) * 0.5f;+}++static void map_idx_10_to_34(int8_t *pm, const int8_t *par, int full)+{+ static const int8_t FULL[34] = {0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3,+ 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 7, 7,+ 8, 8, 8, 8, 9, 9, 9, 9, 9, 9};+ int n = full ? 34 : 16;+ for (int i = 0; i < n; i++)+ pm[i] = par[FULL[i]];+ if (!full)+ pm[16] = 0;+}++static void map_idx_20_to_34(int8_t *pm, const int8_t *par, int full)+{+ if (full) {+ pm[33] = par[19];+ pm[32] = par[19];+ pm[31] = par[18];+ pm[30] = par[18];+ pm[29] = par[18];+ pm[28] = par[18];+ pm[27] = par[17];+ pm[26] = par[17];+ pm[25] = par[16];+ pm[24] = par[16];+ pm[23] = par[15];+ pm[22] = par[15];+ pm[21] = par[14];+ pm[20] = par[14];+ pm[19] = par[13];+ pm[18] = par[12];+ pm[17] = par[11];+ }+ pm[16] = par[10];+ pm[15] = par[9];+ pm[14] = par[9];+ pm[13] = par[8];+ pm[12] = par[8];+ pm[11] = par[7];+ pm[10] = par[6];+ pm[9] = par[5];+ pm[8] = par[5];+ pm[7] = par[4];+ pm[6] = par[4];+ pm[5] = par[3];+ pm[4] = (int8_t)((par[2] + par[3]) / 2);+ pm[3] = par[2];+ pm[2] = par[1];+ pm[1] = (int8_t)((par[0] + par[1]) / 2);+ pm[0] = par[0];+}++static void map_val_20_to_34(float *par)+{+ par[33] = par[19];+ par[32] = par[19];+ par[31] = par[18];+ par[30] = par[18];+ par[29] = par[18];+ par[28] = par[18];+ par[27] = par[17];+ par[26] = par[17];+ par[25] = par[16];+ par[24] = par[16];+ par[23] = par[15];+ par[22] = par[15];+ par[21] = par[14];+ par[20] = par[14];+ par[19] = par[13];+ par[18] = par[12];+ par[17] = par[11];+ par[16] = par[10];+ par[15] = par[9];+ par[14] = par[9];+ par[13] = par[8];+ par[12] = par[8];+ par[11] = par[7];+ par[10] = par[6];+ par[9] = par[5];+ par[8] = par[5];+ par[7] = par[4];+ par[6] = par[4];+ par[5] = par[3];+ par[4] = (par[2] + par[3]) * 0.5f;+ par[3] = par[2];+ par[2] = par[1];+ par[1] = (par[0] + par[1]) * 0.5f;+}++/* the parameters in the bands the processing runs in: par itself, or+ mapped into buf */+static int8_t (*remap(int8_t (*buf)[MAX_PAR], int8_t (*par)[MAX_PAR],+ int num_par, int num_env, int full, int is34))[MAX_PAR]+{+ if (is34) {+ if (num_par == 20 || num_par == 11) {+ for (int e = 0; e < num_env; e++)+ map_idx_20_to_34(buf[e], par[e], full);+ return buf;+ }+ if (num_par == 10 || num_par == 5) {+ for (int e = 0; e < num_env; e++)+ map_idx_10_to_34(buf[e], par[e], full);+ return buf;+ }+ } else {+ if (num_par == 34 || num_par == 17) {+ for (int e = 0; e < num_env; e++)+ map_idx_34_to_20(buf[e], par[e], full);+ return buf;+ }+ if (num_par == 10 || num_par == 5) {+ for (int e = 0; e < num_env; e++)+ map_idx_10_to_20(buf[e], par[e], full);+ return buf;+ }+ }+ return par;+}++static void stereo_processing(struct ps *p, float (*l)[32][2],+ float (*r)[32][2], int is34)+{+ int8_t iid_buf[MAX_ENV][MAX_PAR], icc_buf[MAX_ENV][MAX_PAR];+ int8_t ipd_buf[MAX_ENV][MAX_PAR], opd_buf[MAX_ENV][MAX_PAR];+ int8_t(*iid_m)[MAX_PAR] = iid_buf, (*icc_m)[MAX_PAR] = icc_buf;+ int8_t(*ipd_m)[MAX_PAR] = ipd_buf, (*opd_m)[MAX_PAR] = opd_buf;+ const int8_t *k_to_i = is34 ? PS_K_TO_I_34 : PS_K_TO_I_20;+ const float(*H_LUT)[8][4] = p->icc_mode < 3 ? (const float(*)[8][4])p->t.HA+ : (const float(*)[8][4])p->t.HB;+ float(*H[4])[MAX_ENV + 1][MAX_PAR] = {p->H11, p->H12, p->H21, p->H22};+ if (p->num_env_old)+ for (int j = 0; j < 4; j++)+ for (int c = 0; c < 2; c++)+ memcpy(H[j][c][0], H[j][c][p->num_env_old], sizeof(H[j][c][0]));+ iid_m = remap(iid_buf, p->iid, p->nr_iid_par, p->num_env, 1, is34);+ icc_m = remap(icc_buf, p->icc, p->nr_icc_par, p->num_env, 1, is34);+ if (p->enable_ipdopd) {+ ipd_m = remap(ipd_buf, p->ipd, p->nr_ipdopd_par, p->num_env, 0, is34);+ opd_m = remap(opd_buf, p->opd, p->nr_ipdopd_par, p->num_env, 0, is34);+ }+ if (is34 && !p->is34_old) {+ for (int j = 0; j < 4; j++)+ for (int c = 0; c < 2; c++)+ map_val_20_to_34(H[j][c][0]);+ memset(p->ipd_hist, 0, sizeof p->ipd_hist);+ memset(p->opd_hist, 0, sizeof p->opd_hist);+ } else if (!is34 && p->is34_old) {+ for (int j = 0; j < 4; j++)+ for (int c = 0; c < 2; c++)+ map_val_34_to_20(H[j][c][0]);+ memset(p->ipd_hist, 0, sizeof p->ipd_hist);+ memset(p->opd_hist, 0, sizeof p->opd_hist);+ }+ for (int e = 0; e < p->num_env; e++) {+ for (int b = 0; b < NR_PAR_BANDS[is34]; b++) {+ const float *h =+ H_LUT[iid_m[e][b] + 7 + 23 * p->iid_quant][icc_m[e][b]];+ float h11 = h[0], h12 = h[1], h21 = h[2], h22 = h[3];+ if (p->enable_ipdopd && b < NR_IPDOPD_BANDS[is34]) {+ int opd_idx = p->opd_hist[b] * 8 + opd_m[e][b];+ int ipd_idx = p->ipd_hist[b] * 8 + ipd_m[e][b];+ float opd_re = p->t.pd_re[opd_idx],+ opd_im = p->t.pd_im[opd_idx];+ float ipd_re = p->t.pd_re[ipd_idx],+ ipd_im = p->t.pd_im[ipd_idx];+ p->opd_hist[b] = (int8_t)(opd_idx & 0x3F);+ p->ipd_hist[b] = (int8_t)(ipd_idx & 0x3F);+ float adj_re = opd_re * ipd_re + opd_im * ipd_im;+ float adj_im = opd_im * ipd_re - opd_re * ipd_im;+ float h11i = h11 * opd_im;+ h11 = h11 * opd_re;+ float h12i = h12 * adj_im;+ h12 = h12 * adj_re;+ float h21i = h21 * opd_im;+ h21 = h21 * opd_re;+ float h22i = h22 * adj_im;+ h22 = h22 * adj_re;+ p->H11[1][e + 1][b] = h11i;+ p->H12[1][e + 1][b] = h12i;+ p->H21[1][e + 1][b] = h21i;+ p->H22[1][e + 1][b] = h22i;+ }+ p->H11[0][e + 1][b] = h11;+ p->H12[0][e + 1][b] = h12;+ p->H21[0][e + 1][b] = h21;+ p->H22[0][e + 1][b] = h22;+ }+ for (int k = 0; k < NR_BANDS[is34]; k++) {+ float h[2][4], hs[2][4];+ int start = p->border[e], stop = p->border[e + 1];+ float width = 1.0f / (float)(stop - start ? stop - start : 1);+ int b = k_to_i[k];+ for (int j = 0; j < 4; j++)+ h[0][j] = H[j][0][e][b];+ if (p->enable_ipdopd) {+ float sign = (is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)+ ? -1.0f+ : 1.0f;+ for (int j = 0; j < 4; j++)+ h[1][j] = sign * H[j][1][e][b];+ }+ for (int j = 0; j < 4; j++)+ hs[0][j] = (H[j][0][e + 1][b] - h[0][j]) * width;+ if (p->enable_ipdopd)+ for (int j = 0; j < 4; j++)+ hs[1][j] = (H[j][1][e + 1][b] - h[1][j]) * width;+ if (stop - start <= 0)+ continue;+ float(*lp)[2] = l[k] + 1 + start, (*rp)[2] = r[k] + 1 + start;+ int len = stop - start;+ if (!p->enable_ipdopd) {+ float h0 = h[0][0], h1 = h[0][1], h2 = h[0][2], h3 = h[0][3];+ for (int n = 0; n < len; n++) {+ float l_re = lp[n][0], l_im = lp[n][1];+ float r_re = rp[n][0], r_im = rp[n][1];+ h0 += hs[0][0];+ h1 += hs[0][1];+ h2 += hs[0][2];+ h3 += hs[0][3];+ lp[n][0] = h0 * l_re + h2 * r_re;+ lp[n][1] = h0 * l_im + h2 * r_im;+ rp[n][0] = h1 * l_re + h3 * r_re;+ rp[n][1] = h1 * l_im + h3 * r_im;+ }+ } else {+ float h00 = h[0][0], h01 = h[0][1], h02 = h[0][2],+ h03 = h[0][3];+ float h10 = h[1][0], h11 = h[1][1], h12 = h[1][2],+ h13 = h[1][3];+ for (int n = 0; n < len; n++) {+ float l_re = lp[n][0], l_im = lp[n][1];+ float r_re = rp[n][0], r_im = rp[n][1];+ h00 += hs[0][0];+ h01 += hs[0][1];+ h02 += hs[0][2];+ h03 += hs[0][3];+ h10 += hs[1][0];+ h11 += hs[1][1];+ h12 += hs[1][2];+ h13 += hs[1][3];+ lp[n][0] =+ h00 * l_re + h02 * r_re - h10 * l_im - h12 * r_im;+ lp[n][1] =+ h00 * l_im + h02 * r_im + h10 * l_re + h12 * r_re;+ rp[n][0] =+ h01 * l_re + h03 * r_re - h11 * l_im - h13 * r_im;+ rp[n][1] =+ h01 * l_im + h03 * r_im + h11 * l_re + h13 * r_re;+ }+ }+ }+ }+}++void ps_apply(struct ps *p, float (*L)[38][64], float (*R)[38][64], int top)+{+ int is34 = p->is34;+ if (!p->start) {+ memcpy(R, L, sizeof(float) * 2 * 38 * 64);+ return;+ }+ top += NR_BANDS[is34] - 64;+ if (top < 0)+ top = 0;+ if (top < NR_BANDS[is34])+ memset(p->delay + top, 0,+ sizeof(p->delay[0]) * (size_t)(NR_BANDS[is34] - top));+ if (top < NR_ALLPASS_BANDS[is34])+ memset(p->ap_delay + top, 0,+ sizeof(p->ap_delay[0]) * (size_t)(NR_ALLPASS_BANDS[is34] - top));+ hybrid_analysis(p, p->Lbuf, L, is34);+ decorrelation(p, p->Rbuf, (const float(*)[32][2])p->Lbuf, is34);+ stereo_processing(p, p->Lbuf, p->Rbuf, is34);+ hybrid_synthesis(L, p->Lbuf, is34);+ hybrid_synthesis(R, p->Rbuf, is34);+}diff --git a/src/common/audio_aac_sbr.c b/src/common/audio_aac_sbr.cnew file mode 100644index 0000000..54390da--- /dev/null+++ b/src/common/audio_aac_sbr.c@@ -0,0 +1,1459 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_aac_sbr.c - spectral band replication (ISO/IEC 14496-3, 4.6.18),+ * the upper half of HE-AAC's band made from the lower: the header and the+ * frequency band tables (master, high and low resolution, noise floor,+ * limiter, the patches), the grid of envelopes and noise floors, their+ * Huffman-coded factors, the 32-band analysis QMF bank of the core's+ * samples, the inverse filtering and the high band generated from the+ * low, its envelope adjusted to the factors with noise and sinusoids+ * added, limited and smoothed, and the 64-band synthesis QMF bank at twice+ * the core's rate. Parametric stereo (audio_aac_ps.c) comes in its+ * extension. In single precision as FFmpeg's decoder, whose order of+ * operations it keeps where it changes the rounding.+ */+#include "audio_aac.h"++#include <float.h>+#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "audio_aac_tab.h"+#include "audio_fft.h"++enum { FIXFIX, FIXVAR, VARFIX, VARVAR };+enum {+ T_ENV_1_5,+ F_ENV_1_5,+ T_ENV_BAL_1_5,+ F_ENV_BAL_1_5,+ T_ENV_3_0,+ F_ENV_3_0,+ T_ENV_BAL_3_0,+ F_ENV_BAL_3_0,+ T_NOISE_3_0,+ T_NOISE_BAL_3_0,+};++#define ENV_OFF 2 /* ENVELOPE_ADJUSTMENT_OFFSET: X_high's slots lead */+#define NOISE_FLOOR_OFFSET 6++/* the state of a channel */+struct sbr_ch {+ float x_ana[320 + 1024]; /* the analysis' input, oldest first */+ float W[2][32][32][2]; /* its output, this frame's and the last */+ int ypos;+ float Y[2][38][64][2]; /* the adjusted high band, two frames */+ float v[1280]; /* the synthesis' state, newest first */+ float bw[5];+ int invf[2][5];+ /* the grid */+ int num_env, num_noise, frame_class, amp_res;+ int freq_res[6]; /* [0]: the last of the frame before */+ int t_env[6], t_q[3], t_env_old;+ int e_a[2];+ int df_env[5], df_noise[2];+ int env_q[6][48], noise_q[3][5];+ float env[6][48], noise[3][5];+ int add_harm_flag, add_harm[48];+ int s_index[7][48];+ float g_temp[42][48], q_temp[42][48];+ int index_noise, index_sine;+};++struct sbr {+ int rate; /* SBR's, twice the core's */+ int ch, down;+ int start, reset, ready;+ /* the header */+ int amp_res_header;+ int params[6]; /* start, stop, xover, freq_scale, alter_scale,+ noise_bands */+ int limiter_bands, limiter_gains, interpol_freq, smoothing;+ /* the tables */+ int k[3], kx[2], m[2], pushed;+ int n_master, n[2], n_q, n_lim;+ int f_master[49], f_high[49], f_low[25], f_noise[6], f_lim[30];+ int num_patches, patch_num[6], patch_start[6];+ int coupling;+ struct sbr_ch c[2];+ /* the frame's work */+ float X_low[32][40][2];+ float X_high[64][40][2];+ float alpha0[64][2], alpha1[64][2];+ float e_orig[7][48], q_mapped[7][48], e_curr[7][48];+ float q_m[7][48], s_m[7][48], gain[7][48];+ int s_mapped[7][48];+ float X[2][2][38][64]; /* the synthesis' input: channel, re/im */+ /* the QMF banks: DCT-IVs of 32 (analysis), inverse MDCTs of 128+ (synthesis), the rotations of the analysis' bands; the downsampled+ synthesis' kernel */+ struct aimdct *m64, *m128;+ float rot[32][2];+ float ds_cos[64][32], ds_sin[64][32];+ struct vlc vlc[10];+ struct ps *ps;+ int ps_found;+};++static float exp2fi(int x)+{+ return ldexpf(1.0f, x);+}++/* ---- the header and the frequency band tables ---- */++static void turnoff(struct sbr *s)+{+ s->start = 0;+ s->ready = 0;+ s->kx[1] = 32;+ s->m[1] = 0;+ s->c[0].e_a[1] = s->c[1].e_a[1] = -1;+ for (int i = 0; i < 6; i++)+ s->params[i] = -1;+}++static int cmp_int(const void *a, const void *b)+{+ return *(const int *)a - *(const int *)b;+}++static void make_bands(int *bands, int start, int stop, int num)+{+ float base = powf((float)stop / (float)start, 1.0f / (float)num);+ float prod = (float)start;+ int previous = start;+ for (int k = 0; k < num - 1; k++) {+ prod *= base;+ int present = (int)lrintf(prod);+ bands[k] = present - previous;+ previous = present;+ }+ bands[num - 1] = stop - previous;+}++static int in_table(const int *t, int last, int v)+{+ for (int i = 0; i <= last; i++)+ if (t[i] == v)+ return 1;+ return 0;+}++static void make_f_lim(struct sbr *s)+{+ if (s->limiter_bands > 0) {+ static const float WARP[3] = {1.32715174233856803909f,+ 1.18509277094158210129f,+ 1.11987160404675912501f};+ float lim = WARP[s->limiter_bands - 1];+ int patch_borders[7];+ patch_borders[0] = s->kx[1];+ for (int k = 1; k <= s->num_patches; k++)+ patch_borders[k] = patch_borders[k - 1] + s->patch_num[k - 1];+ memcpy(s->f_lim, s->f_low, sizeof(int) * (size_t)(s->n[0] + 1));+ if (s->num_patches > 1)+ memcpy(s->f_lim + s->n[0] + 1, patch_borders + 1,+ sizeof(int) * (size_t)(s->num_patches - 1));+ qsort(s->f_lim, (size_t)(s->num_patches + s->n[0]), sizeof(int),+ cmp_int);+ s->n_lim = s->n[0] + s->num_patches - 1;+ int *in = s->f_lim + 1, *out = s->f_lim;+ while (out < s->f_lim + s->n_lim) {+ if ((float)*in >= (float)*out * lim) {+ *++out = *in++;+ } else if (*in == *out ||+ !in_table(patch_borders, s->num_patches, *in)) {+ in++;+ s->n_lim--;+ } else if (!in_table(patch_borders, s->num_patches, *out)) {+ *out = *in++;+ s->n_lim--;+ } else {+ *++out = *in++;+ }+ }+ } else {+ s->f_lim[0] = s->f_low[0];+ s->f_lim[1] = s->f_low[s->n[0]];+ s->n_lim = 1;+ }+}++static int read_header(struct sbr *s, struct abr *b)+{+ int old[6], old_limiter = s->limiter_bands;+ memcpy(old, s->params, sizeof old);+ s->start = 1;+ s->ready = 0;+ s->amp_res_header = abr_bit(b);+ s->params[0] = (int)abr_bits(b, 4);+ s->params[1] = (int)abr_bits(b, 4);+ s->params[2] = (int)abr_bits(b, 3);+ abr_bits(b, 2);+ int extra1 = abr_bit(b), extra2 = abr_bit(b);+ if (extra1) {+ s->params[3] = (int)abr_bits(b, 2);+ s->params[4] = abr_bit(b);+ s->params[5] = (int)abr_bits(b, 2);+ } else {+ s->params[3] = 2;+ s->params[4] = 1;+ s->params[5] = 2;+ }+ if (memcmp(old, s->params, sizeof old))+ s->reset = 1;+ if (extra2) {+ s->limiter_bands = (int)abr_bits(b, 2);+ s->limiter_gains = (int)abr_bits(b, 2);+ s->interpol_freq = abr_bit(b);+ s->smoothing = abr_bit(b);+ } else {+ s->limiter_bands = 2;+ s->limiter_gains = 2;+ s->interpol_freq = 1;+ s->smoothing = 1;+ }+ if (s->limiter_bands != old_limiter && !s->reset)+ make_f_lim(s);+ return 0;+}++static int make_f_master(struct sbr *s)+{+ const int8_t *offset;+ switch (s->rate) {+ case 16000:+ offset = SBR_OFFSET;+ break;+ case 22050:+ offset = SBR_OFFSET + 16;+ break;+ case 24000:+ offset = SBR_OFFSET + 32;+ break;+ case 32000:+ offset = SBR_OFFSET + 48;+ break;+ case 44100:+ case 48000:+ case 64000:+ offset = SBR_OFFSET + 64;+ break;+ case 88200:+ case 96000:+ case 128000:+ case 176400:+ case 192000:+ offset = SBR_OFFSET + 80;+ break;+ default:+ return -1;+ }+ unsigned temp = s->rate < 32000 ? 3000 : s->rate < 64000 ? 4000 : 5000;+ unsigned rate = (unsigned)s->rate;+ int start_min = (int)(((temp << 7) + (rate >> 1)) / rate);+ int stop_min = (int)(((temp << 8) + (rate >> 1)) / rate);+ s->k[0] = start_min + offset[s->params[0]];+ if (s->params[1] < 14) {+ int stop_dk[13];+ s->k[2] = stop_min;+ make_bands(stop_dk, stop_min, 64, 13);+ qsort(stop_dk, 13, sizeof(int), cmp_int);+ for (int k = 0; k < s->params[1]; k++)+ s->k[2] += stop_dk[k];+ } else if (s->params[1] == 14) {+ s->k[2] = 2 * s->k[0];+ } else {+ s->k[2] = 3 * s->k[0];+ }+ if (s->k[2] > 64)+ s->k[2] = 64;+ int max_qmf = s->rate <= 32000 ? 48 : s->rate == 44100 ? 35 : 32;+ if (s->k[2] - s->k[0] > max_qmf || s->k[0] <= 0)+ return -1;+ int xover = s->params[2];+ if (!s->params[3]) {+ int dk = s->params[4] + 1;+ s->n_master = ((s->k[2] - s->k[0] + (dk & 2)) >> dk) << 1;+ if (s->n_master <= 0 || xover >= s->n_master || s->n_master > 48)+ return -1;+ for (int k = 1; k <= s->n_master; k++)+ s->f_master[k] = dk;+ int k2diff = s->k[2] - s->k[0] - s->n_master * dk;+ if (k2diff < 0) {+ s->f_master[1]--;+ s->f_master[2] -= (k2diff < -1);+ } else if (k2diff) {+ s->f_master[s->n_master]++;+ }+ s->f_master[0] = s->k[0];+ for (int k = 1; k <= s->n_master; k++)+ s->f_master[k] += s->f_master[k - 1];+ } else {+ int half_bands = 7 - s->params[3];+ int two_regions, vk0[49];+ if (49 * s->k[2] > 110 * s->k[0]) {+ two_regions = 1;+ s->k[1] = 2 * s->k[0];+ } else {+ two_regions = 0;+ s->k[1] = s->k[2];+ }+ int num_bands_0 = (int)lrintf((float)half_bands *+ log2f((float)s->k[1] / (float)s->k[0])) *+ 2;+ if (num_bands_0 <= 0 || num_bands_0 > 48)+ return -1;+ vk0[0] = 0;+ make_bands(vk0 + 1, s->k[0], s->k[1], num_bands_0);+ qsort(vk0 + 1, (size_t)num_bands_0, sizeof(int), cmp_int);+ int vdk0_max = vk0[num_bands_0];+ vk0[0] = s->k[0];+ for (int k = 1; k <= num_bands_0; k++) {+ if (vk0[k] <= 0)+ return -1;+ vk0[k] += vk0[k - 1];+ }+ if (two_regions) {+ int vk1[49];+ float invwarp = s->params[4] ? 0.76923076923076923077f : 1.0f;+ int num_bands_1 =+ (int)lrintf((float)half_bands * invwarp *+ log2f((float)s->k[2] / (float)s->k[1])) *+ 2;+ if (num_bands_1 <= 0 || num_bands_0 + num_bands_1 > 48)+ return -1;+ make_bands(vk1 + 1, s->k[1], s->k[2], num_bands_1);+ int vdk1_min = vk1[1];+ for (int i = 2; i <= num_bands_1; i++)+ if (vk1[i] < vdk1_min)+ vdk1_min = vk1[i];+ if (vdk1_min < vdk0_max) {+ qsort(vk1 + 1, (size_t)num_bands_1, sizeof(int), cmp_int);+ int change = vdk0_max - vk1[1];+ int half = (vk1[num_bands_1] - vk1[1]) >> 1;+ if (half < change)+ change = half;+ vk1[1] += change;+ vk1[num_bands_1] -= change;+ }+ qsort(vk1 + 1, (size_t)num_bands_1, sizeof(int), cmp_int);+ vk1[0] = s->k[1];+ for (int k = 1; k <= num_bands_1; k++) {+ if (vk1[k] <= 0)+ return -1;+ vk1[k] += vk1[k - 1];+ }+ s->n_master = num_bands_0 + num_bands_1;+ if (xover >= s->n_master)+ return -1;+ memcpy(s->f_master, vk0, sizeof(int) * (size_t)(num_bands_0 + 1));+ memcpy(s->f_master + num_bands_0 + 1, vk1 + 1,+ sizeof(int) * (size_t)num_bands_1);+ } else {+ s->n_master = num_bands_0;+ if (xover >= s->n_master)+ return -1;+ memcpy(s->f_master, vk0, sizeof(int) * (size_t)(num_bands_0 + 1));+ }+ }+ return 0;+}++static int calc_patches(struct sbr *s)+{+ int last_k = -1, last_msb = -1, sb = 0, k;+ int msb = s->k[0], usb = s->kx[1];+ int goal_sb =+ (int)(((1000u << 11) + ((unsigned)s->rate >> 1)) / (unsigned)s->rate);+ s->num_patches = 0;+ if (goal_sb < s->kx[1] + s->m[1]) {+ for (k = 0; s->f_master[k] < goal_sb; k++)+ ;+ } else {+ k = s->n_master;+ }+ do {+ int odd = 0;+ if (k == last_k && msb == last_msb)+ return -1;+ last_k = k;+ last_msb = msb;+ for (int i = k; i == k || sb > (s->k[0] - 1 + msb - odd); i--) {+ if (i < 0)+ return -1;+ sb = s->f_master[i];+ odd = (sb + s->k[0]) & 1;+ }+ if (s->num_patches > 5)+ return -1;+ s->patch_num[s->num_patches] = sb - usb > 0 ? sb - usb : 0;+ s->patch_start[s->num_patches] =+ s->k[0] - odd - s->patch_num[s->num_patches];+ if (s->patch_num[s->num_patches] > 0) {+ usb = sb;+ msb = sb;+ s->num_patches++;+ } else {+ msb = s->kx[1];+ }+ if (s->f_master[k] - sb < 3)+ k = s->n_master;+ } while (sb != s->kx[1] + s->m[1]);+ if (s->num_patches > 1 && s->patch_num[s->num_patches - 1] < 3)+ s->num_patches--;+ for (int i = 0; i < s->num_patches; i++)+ if (s->patch_start[i] < 0 || s->patch_start[i] + s->patch_num[i] > 32)+ return -1;+ return 0;+}++static int make_f_derived(struct sbr *s)+{+ int xover = s->params[2];+ s->n[1] = s->n_master - xover;+ s->n[0] = (s->n[1] + 1) >> 1;+ memcpy(s->f_high, s->f_master + xover, sizeof(int) * (size_t)(s->n[1] + 1));+ s->m[1] = s->f_high[s->n[1]] - s->f_high[0];+ s->kx[1] = s->f_high[0];+ if (s->kx[1] + s->m[1] > 64 || s->kx[1] > 32 || s->m[1] > 48 ||+ s->m[1] <= 0)+ return -1;+ s->f_low[0] = s->f_high[0];+ int temp = s->n[1] & 1;+ for (int k = 1; k <= s->n[0]; k++)+ s->f_low[k] = s->f_high[2 * k - temp];+ s->n_q = (int)lrintf((float)s->params[5] *+ log2f((float)s->k[2] / (float)s->kx[1]));+ if (s->n_q < 1)+ s->n_q = 1;+ if (s->n_q > 5)+ return -1;+ s->f_noise[0] = s->f_low[0];+ temp = 0;+ for (int k = 1; k <= s->n_q; k++) {+ temp += (s->n[0] - temp) / (s->n_q + 1 - k);+ s->f_noise[k] = s->f_low[temp];+ }+ if (calc_patches(s) != 0)+ return -1;+ make_f_lim(s);+ s->c[0].index_noise = s->c[1].index_noise = 0;+ return 0;+}++static void sbr_reset(struct sbr *s)+{+ if (make_f_master(s) != 0 || make_f_derived(s) != 0)+ turnoff(s);+}++/* ---- the data ---- */++static int read_grid(struct sbr *s, struct abr *b, struct sbr_ch *c)+{+ static const int CEIL_LOG2[] = {0, 1, 2, 2, 3, 3};+ int pointer = 0, trail = 16, num_old = c->num_env;+ int t_env[6], num_env = 0, freq_res[6];+ memcpy(freq_res, c->freq_res, sizeof freq_res);+ freq_res[0] = c->freq_res[c->num_env];+ int amp_res = s->amp_res_header;+ int cls = (int)abr_bits(b, 2);+ switch (cls) {+ case FIXFIX: {+ num_env = 1 << abr_bits(b, 2);+ if (num_env > 5)+ return -1;+ if (num_env == 1)+ amp_res = 0;+ t_env[0] = 0;+ t_env[num_env] = trail;+ int step = (trail + (num_env >> 1)) / num_env;+ for (int i = 0; i < num_env - 1; i++)+ t_env[i + 1] = t_env[i] + step;+ freq_res[1] = abr_bit(b);+ for (int i = 1; i < num_env; i++)+ freq_res[i + 1] = freq_res[1];+ break;+ }+ case FIXVAR: {+ trail += (int)abr_bits(b, 2);+ int rel = (int)abr_bits(b, 2);+ num_env = rel + 1;+ t_env[0] = 0;+ t_env[num_env] = trail;+ for (int i = 0; i < rel; i++)+ t_env[num_env - 1 - i] =+ t_env[num_env - i] - 2 * (int)abr_bits(b, 2) - 2;+ pointer = (int)abr_bits(b, CEIL_LOG2[num_env]);+ for (int i = 0; i < num_env; i++)+ freq_res[num_env - i] = abr_bit(b);+ break;+ }+ case VARFIX: {+ t_env[0] = (int)abr_bits(b, 2);+ int rel = (int)abr_bits(b, 2);+ num_env = rel + 1;+ t_env[num_env] = trail;+ for (int i = 0; i < rel; i++)+ t_env[i + 1] = t_env[i] + 2 * (int)abr_bits(b, 2) + 2;+ pointer = (int)abr_bits(b, CEIL_LOG2[num_env]);+ for (int i = 0; i < num_env; i++)+ freq_res[i + 1] = abr_bit(b);+ break;+ }+ default: {+ t_env[0] = (int)abr_bits(b, 2);+ trail += (int)abr_bits(b, 2);+ int lead = (int)abr_bits(b, 2), rel = (int)abr_bits(b, 2);+ num_env = lead + rel + 1;+ if (num_env > 5)+ return -1;+ t_env[num_env] = trail;+ for (int i = 0; i < lead; i++)+ t_env[i + 1] = t_env[i] + 2 * (int)abr_bits(b, 2) + 2;+ for (int i = 0; i < rel; i++)+ t_env[num_env - 1 - i] =+ t_env[num_env - i] - 2 * (int)abr_bits(b, 2) - 2;+ pointer = (int)abr_bits(b, CEIL_LOG2[num_env]);+ for (int i = 0; i < num_env; i++)+ freq_res[i + 1] = abr_bit(b);+ break;+ }+ }+ if (pointer > num_env + 1)+ return -1;+ for (int i = 1; i <= num_env; i++)+ if (t_env[i - 1] >= t_env[i] || t_env[i - 1] < 0)+ return -1;+ /* accepted: into the channel */+ c->freq_res[0] = freq_res[0];+ for (int i = 1; i <= num_env; i++)+ c->freq_res[i] = freq_res[i];+ c->amp_res = amp_res;+ c->t_env_old = c->t_env[num_old];+ memcpy(c->t_env, t_env, sizeof(int) * (size_t)(num_env + 1));+ c->num_env = num_env;+ c->frame_class = cls;+ c->num_noise = (num_env > 1) + 1;+ c->t_q[0] = c->t_env[0];+ c->t_q[c->num_noise] = c->t_env[num_env];+ if (c->num_noise > 1) {+ int idx;+ if (cls == FIXFIX)+ idx = num_env >> 1;+ else if (cls & 1)+ idx = num_env - (pointer - 1 > 1 ? pointer - 1 : 1);+ else if (!pointer)+ idx = 1;+ else if (pointer == 1)+ idx = num_env - 1;+ else+ idx = pointer - 1;+ c->t_q[1] = c->t_env[idx];+ }+ c->e_a[0] = -(c->e_a[1] != num_old);+ c->e_a[1] = -1;+ if ((cls & 1) && pointer)+ c->e_a[1] = num_env + 1 - pointer;+ else if (cls == VARFIX && pointer > 1)+ c->e_a[1] = pointer - 1;+ return 0;+}++static void copy_grid(struct sbr_ch *d, const struct sbr_ch *src)+{+ d->freq_res[0] = d->freq_res[d->num_env];+ d->t_env_old = d->t_env[d->num_env];+ d->e_a[0] = -(d->e_a[1] != d->num_env);+ memcpy(d->freq_res + 1, src->freq_res + 1, sizeof(int) * 5);+ memcpy(d->t_env, src->t_env, sizeof d->t_env);+ memcpy(d->t_q, src->t_q, sizeof d->t_q);+ d->num_env = src->num_env;+ d->amp_res = src->amp_res;+ d->num_noise = src->num_noise;+ d->frame_class = src->frame_class;+ d->e_a[1] = src->e_a[1];+}++static void read_dtdf(struct abr *b, struct sbr_ch *c)+{+ for (int i = 0; i < c->num_env; i++)+ c->df_env[i] = abr_bit(b);+ for (int i = 0; i < c->num_noise; i++)+ c->df_noise[i] = abr_bit(b);+}++static void read_invf(struct sbr *s, struct abr *b, struct sbr_ch *c)+{+ memcpy(c->invf[1], c->invf[0], sizeof c->invf[0]);+ for (int i = 0; i < s->n_q; i++)+ c->invf[0][i] = (int)abr_bits(b, 2);+}++static int huff(struct sbr *s, struct abr *b, int t)+{+ int v = vlc_get(b, &s->vlc[t]);+ return v < 0 ? 1000 : v + SBR_HUFF_OFFSET[t];+}++static int read_envelope(struct sbr *s, struct abr *b, struct sbr_ch *c, int ch)+{+ int bits, t, f;+ int delta = (ch == 1 && s->coupling == 1) + 1;+ int odd = s->n[1] & 1;+ if (s->coupling && ch) {+ if (c->amp_res) {+ bits = 5;+ t = T_ENV_BAL_3_0;+ f = F_ENV_BAL_3_0;+ } else {+ bits = 6;+ t = T_ENV_BAL_1_5;+ f = F_ENV_BAL_1_5;+ }+ } else {+ if (c->amp_res) {+ bits = 6;+ t = T_ENV_3_0;+ f = F_ENV_3_0;+ } else {+ bits = 7;+ t = T_ENV_1_5;+ f = F_ENV_1_5;+ }+ }+ for (int i = 0; i < c->num_env; i++) {+ int nb = s->n[c->freq_res[i + 1]];+ if (c->df_env[i]) {+ for (int j = 0; j < nb; j++) {+ int k;+ if (c->freq_res[i + 1] == c->freq_res[i])+ k = j;+ else if (c->freq_res[i + 1])+ k = (j + odd) >> 1;+ else+ k = j ? 2 * j - odd : 0;+ int v = c->env_q[i][k] + delta * huff(s, b, t);+ if ((unsigned)v > 127)+ return -1;+ c->env_q[i + 1][j] = v;+ }+ } else {+ c->env_q[i + 1][0] = delta * (int)abr_bits(b, bits);+ for (int j = 1; j < nb; j++) {+ int v = c->env_q[i + 1][j - 1] + delta * huff(s, b, f);+ if ((unsigned)v > 127)+ return -1;+ c->env_q[i + 1][j] = v;+ }+ }+ }+ memcpy(c->env_q[0], c->env_q[c->num_env], sizeof c->env_q[0]);+ return 0;+}++static int read_noise(struct sbr *s, struct abr *b, struct sbr_ch *c, int ch)+{+ int delta = (ch == 1 && s->coupling == 1) + 1, t, f;+ if (s->coupling && ch) {+ t = T_NOISE_BAL_3_0;+ f = F_ENV_BAL_3_0;+ } else {+ t = T_NOISE_3_0;+ f = F_ENV_3_0;+ }+ for (int i = 0; i < c->num_noise; i++) {+ if (c->df_noise[i]) {+ for (int j = 0; j < s->n_q; j++) {+ int v = c->noise_q[i][j] + delta * huff(s, b, t);+ if ((unsigned)v > 30)+ return -1;+ c->noise_q[i + 1][j] = v;+ }+ } else {+ c->noise_q[i + 1][0] = delta * (int)abr_bits(b, 5);+ for (int j = 1; j < s->n_q; j++) {+ int v = c->noise_q[i + 1][j - 1] + delta * huff(s, b, f);+ if ((unsigned)v > 30)+ return -1;+ c->noise_q[i + 1][j] = v;+ }+ }+ }+ memcpy(c->noise_q[0], c->noise_q[c->num_noise], sizeof c->noise_q[0]);+ return 0;+}++static void read_harmonics(struct sbr *s, struct abr *b, struct sbr_ch *c)+{+ if ((c->add_harm_flag = abr_bit(b)))+ for (int i = 0; i < s->n[1]; i++)+ c->add_harm[i] = abr_bit(b);+}++static int read_data(struct sbr *s, struct abr *b)+{+ struct sbr_ch *c0 = &s->c[0], *c1 = &s->c[1];+ s->ready = 1;+ if (s->ch == 1) {+ if (abr_bit(b))+ abr_bits(b, 4);+ if (read_grid(s, b, c0) != 0)+ return -1;+ read_dtdf(b, c0);+ read_invf(s, b, c0);+ if (read_envelope(s, b, c0, 0) != 0 || read_noise(s, b, c0, 0) != 0)+ return -1;+ read_harmonics(s, b, c0);+ } else {+ if (abr_bit(b))+ abr_bits(b, 8);+ if ((s->coupling = abr_bit(b))) {+ if (read_grid(s, b, c0) != 0)+ return -1;+ copy_grid(c1, c0);+ read_dtdf(b, c0);+ read_dtdf(b, c1);+ read_invf(s, b, c0);+ memcpy(c1->invf[1], c1->invf[0], sizeof c1->invf[0]);+ memcpy(c1->invf[0], c0->invf[0], sizeof c1->invf[0]);+ if (read_envelope(s, b, c0, 0) != 0 ||+ read_noise(s, b, c0, 0) != 0 ||+ read_envelope(s, b, c1, 1) != 0 || read_noise(s, b, c1, 1) != 0)+ return -1;+ } else {+ if (read_grid(s, b, c0) != 0 || read_grid(s, b, c1) != 0)+ return -1;+ read_dtdf(b, c0);+ read_dtdf(b, c1);+ read_invf(s, b, c0);+ read_invf(s, b, c1);+ if (read_envelope(s, b, c0, 0) != 0 ||+ read_envelope(s, b, c1, 1) != 0 ||+ read_noise(s, b, c0, 0) != 0 || read_noise(s, b, c1, 1) != 0)+ return -1;+ }+ read_harmonics(s, b, c0);+ read_harmonics(s, b, c1);+ }+ if (abr_bit(b)) { /* bs_extended_data */+ int left = (int)abr_bits(b, 4);+ if (left == 15)+ left += (int)abr_bits(b, 8);+ left *= 8;+ while (left > 7) {+ int id = (int)abr_bits(b, 2);+ left -= 2;+ if (id == 2 && s->ch == 1) { /* EXTENSION_ID_PS */+ if (!s->ps && !(s->ps = ps_new())) {+ b->pos += (size_t)left;+ left = 0;+ break;+ }+ int used = ps_read(s->ps, b, left);+ if (used < 0) {+ b->pos += (size_t)left;+ left = 0;+ } else {+ s->ps_found = 1;+ left -= used;+ }+ } else {+ b->pos += (size_t)left;+ left = 0;+ }+ }+ if (left > 0)+ b->pos += (size_t)left;+ }+ return 0;+}++int sbr_read(struct sbr *s, struct abr *b, int crc, int cnt)+{+ size_t end = b->pos + 8 * (size_t)cnt - 4;+ s->reset = 0;+ if (crc)+ abr_bits(b, 10);+ s->kx[0] = s->kx[1];+ s->m[0] = s->m[1];+ s->pushed = 1;+ if (abr_bit(b))+ read_header(s, b);+ if (s->reset)+ sbr_reset(s);+ if (s->start && read_data(s, b) != 0)+ turnoff(s);+ if (b->pos > end)+ turnoff(s);+ b->pos = end;+ return 0;+}++int sbr_has_ps(const struct sbr *s)+{+ return s->ps_found;+}++/* ---- dequantization ---- */++static void dequant(struct sbr *s)+{+ static const float EXP2_TAB[2] = {1.0f, (float)M_SQRT2};+ if (s->ch == 2 && s->coupling) {+ struct sbr_ch *a = &s->c[0], *b = &s->c[1];+ int pan_offset = a->amp_res ? 12 : 24;+ for (int e = 1; e <= a->num_env; e++) {+ for (int k = 0; k < s->n[a->freq_res[e]]; k++) {+ float t1, t2;+ if (a->amp_res) {+ t1 = exp2fi(a->env_q[e][k] + 7);+ t2 = exp2fi(pan_offset - b->env_q[e][k]);+ } else {+ t1 = exp2fi((a->env_q[e][k] >> 1) + 7) *+ EXP2_TAB[a->env_q[e][k] & 1];+ t2 = exp2fi((pan_offset - b->env_q[e][k]) >> 1) *+ EXP2_TAB[(pan_offset - b->env_q[e][k]) & 1];+ }+ if (t1 > 1E20f)+ t1 = 1;+ float fac = t1 / (1.0f + t2);+ a->env[e][k] = fac;+ b->env[e][k] = fac * t2;+ }+ }+ for (int e = 1; e <= a->num_noise; e++) {+ for (int k = 0; k < s->n_q; k++) {+ float t1 = exp2fi(NOISE_FLOOR_OFFSET - a->noise_q[e][k] + 1);+ float t2 = exp2fi(12 - b->noise_q[e][k]);+ float fac = t1 / (1.0f + t2);+ a->noise[e][k] = fac;+ b->noise[e][k] = fac * t2;+ }+ }+ } else {+ for (int ch = 0; ch < s->ch; ch++) {+ struct sbr_ch *c = &s->c[ch];+ for (int e = 1; e <= c->num_env; e++)+ for (int k = 0; k < s->n[c->freq_res[e]]; k++) {+ float v;+ if (c->amp_res)+ v = exp2fi(c->env_q[e][k] + 6);+ else+ v = exp2fi((c->env_q[e][k] >> 1) + 6) *+ EXP2_TAB[c->env_q[e][k] & 1];+ c->env[e][k] = v > 1E20f ? 1 : v;+ }+ for (int e = 1; e <= c->num_noise; e++)+ for (int k = 0; k < s->n_q; k++)+ c->noise[e][k] =+ exp2fi(NOISE_FLOOR_OFFSET - c->noise_q[e][k]);+ }+ }+}++/* ---- the QMF banks ---- */++/* 32 slots of 32 new samples each into W[slot][band] (4.6.18.4.1) */+static void qmf_analysis(struct sbr *s, struct sbr_ch *c, const float *in,+ float (*W)[32][2])+{+ float *x = c->x_ana;+ memmove(x, x + 1024, sizeof(float) * 288);+ for (int i = 0; i < 1024; i++)+ x[288 + i] = in[i] * 32768.0f;+ for (int l = 0; l < 32; l++) {+ const float *xs = x + 32 * l; /* x[319 - n] is the spec's x[n] */+ float u[64];+ for (int n = 0; n < 64; n++) {+ float sum = 0;+ for (int j = 0; j < 5; j++) {+ int i = n + 64 * j;+ sum += xs[319 - i] * SBR_QMF_WINDOW[2 * i];+ }+ u[n] = sum;+ }+ /* W[k] = 2 sum u[n] e^(i pi / 64 (k + 1/2)(2n - 1/2)): the+ halves of u through DCT-IVs (sines by reversed inputs), then+ each band turned by e^(-i 3 pi (k + 1/2) / 128) */+ float lo[32], hi[32], rlo[32], rhi[32], c1[32], s1[32], c2[32], s2[32];+ for (int j = 0; j < 32; j++) {+ lo[j] = u[j];+ hi[j] = u[32 + j];+ rlo[j] = u[31 - j];+ rhi[j] = u[63 - j];+ }+ aimdct_dct4(s->m64, lo, 1, c1);+ aimdct_dct4(s->m64, rlo, 1, s1);+ aimdct_dct4(s->m64, hi, 1, c2);+ aimdct_dct4(s->m64, rhi, 1, s2);+ for (int k = 0; k < 32; k++) {+ float sg = k & 1 ? -1.0f : 1.0f;+ float re = c1[k] - s2[k], im = sg * s1[k] + sg * c2[k];+ W[l][k][0] = re * s->rot[k][0] - im * s->rot[k][1];+ W[l][k][1] = re * s->rot[k][1] + im * s->rot[k][0];+ }+ }+}++/* 32 slots of 64 bands to 2048 samples (4.6.18.4.2) */+static void qmf_synthesis(struct sbr *s, struct sbr_ch *c, float (*X)[38][64],+ float *out)+{+ float *v = c->v;+ for (int l = 0; l < 32; l++) {+ memmove(v + 128, v, sizeof(float) * (1280 - 128));+ /* v[n] = 1/64 sum Re(X[k] e^(i pi / 128 (k + 1/2)(2n - 255))):+ the real parts' inverse MDCT and the imaginary parts', reversed+ (the sines), shifted by 32 and signed */+ float rev[64], y1[128], y2[128];+ for (int k = 0; k < 64; k++)+ rev[k] = X[1][l][63 - k];+ aimdct_run(s->m128, X[0][l], y1);+ aimdct_run(s->m128, rev, y2);+ for (int n = 0; n < 128; n++) {+ int i = n >= 32 ? n - 32 : n + 96;+ float c = y1[i], sn = i & 1 ? -y2[i] : y2[i];+ v[n] = (n >= 32 ? sn - c : c - sn) * (1.0f / 64.0f);+ }+ float *o = out + 64 * l;+ for (int k = 0; k < 64; k++) {+ float sum = 0;+ for (int n = 0; n < 5; n++) {+ sum += v[256 * n + k] * SBR_QMF_WINDOW[128 * n + k];+ sum += v[256 * n + 192 + k] * SBR_QMF_WINDOW[128 * n + 64 + k];+ }+ o[k] = sum * (1.0f / 32768.0f);+ }+ }+}++/* the downsampled bank: 32 slots of the lower 32 bands to 1024 samples+ (4.6.18.4.3), its phase (2n - 127.5) the one the reference decoder's+ output and FFmpeg's agree on */+static void qmf_synthesis_ds(struct sbr *s, struct sbr_ch *c,+ float (*X)[38][64], float *out)+{+ float *v = c->v;+ for (int l = 0; l < 32; l++) {+ memmove(v + 64, v, sizeof(float) * (640 - 64));+ for (int n = 0; n < 64; n++) {+ float sum = 0;+ for (int k = 0; k < 32; k++)+ sum +=+ X[0][l][k] * s->ds_cos[n][k] - X[1][l][k] * s->ds_sin[n][k];+ v[n] = sum * (1.0f / 64.0f);+ }+ float *o = out + 32 * l;+ for (int k = 0; k < 32; k++) {+ float sum = 0;+ for (int n = 0; n < 5; n++) {+ sum += v[128 * n + k] * SBR_QMF_WINDOW[2 * (64 * n + k)];+ sum +=+ v[128 * n + 96 + k] * SBR_QMF_WINDOW[2 * (64 * n + 32 + k)];+ }+ o[k] = sum * (1.0f / 32768.0f);+ }+ }+}++/* ---- the high band ---- */++static void lf_gen(struct sbr *s, struct sbr_ch *c)+{+ memset(s->X_low, 0, sizeof s->X_low);+ for (int k = 0; k < s->kx[1]; k++)+ for (int i = 8; i < 40; i++) {+ s->X_low[k][i][0] = c->W[c->ypos][i - 8][k][0];+ s->X_low[k][i][1] = c->W[c->ypos][i - 8][k][1];+ }+ int prev = 1 - c->ypos;+ for (int k = 0; k < s->kx[0]; k++)+ for (int i = 0; i < 8; i++) {+ s->X_low[k][i][0] = c->W[prev][i + 24][k][0];+ s->X_low[k][i][1] = c->W[prev][i + 24][k][1];+ }+}++static void autocorrelate(const float x[40][2], float phi[3][2][2])+{+ float real2 = x[0][0] * x[2][0] + x[0][1] * x[2][1];+ float imag2 = x[0][0] * x[2][1] - x[0][1] * x[2][0];+ float real1 = 0, imag1 = 0, real0 = 0;+ for (int i = 1; i < 38; i++) {+ real0 += x[i][0] * x[i][0] + x[i][1] * x[i][1];+ real1 += x[i][0] * x[i + 1][0] + x[i][1] * x[i + 1][1];+ imag1 += x[i][0] * x[i + 1][1] - x[i][1] * x[i + 1][0];+ real2 += x[i][0] * x[i + 2][0] + x[i][1] * x[i + 2][1];+ imag2 += x[i][0] * x[i + 2][1] - x[i][1] * x[i + 2][0];+ }+ phi[0][1][0] = real2;+ phi[0][1][1] = imag2;+ phi[2][1][0] = real0 + x[0][0] * x[0][0] + x[0][1] * x[0][1];+ phi[1][0][0] = real0 + x[38][0] * x[38][0] + x[38][1] * x[38][1];+ phi[1][1][0] = real1 + x[0][0] * x[1][0] + x[0][1] * x[1][1];+ phi[1][1][1] = imag1 + x[0][0] * x[1][1] - x[0][1] * x[1][0];+ phi[0][0][0] = real1 + x[38][0] * x[39][0] + x[38][1] * x[39][1];+ phi[0][0][1] = imag1 + x[38][0] * x[39][1] - x[38][1] * x[39][0];+}++static void inverse_filter(struct sbr *s)+{+ for (int k = 0; k < s->k[0]; k++) {+ float phi[3][2][2];+ float *a0 = s->alpha0[k], *a1 = s->alpha1[k];+ autocorrelate((const float(*)[2])s->X_low[k], phi);+ float dk = phi[2][1][0] * phi[1][0][0] -+ (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) /+ 1.000001f;+ if (!dk) {+ a1[0] = a1[1] = 0;+ } else {+ float re = phi[0][0][0] * phi[1][1][0] -+ phi[0][0][1] * phi[1][1][1] -+ phi[0][1][0] * phi[1][0][0];+ float im = phi[0][0][0] * phi[1][1][1] ++ phi[0][0][1] * phi[1][1][0] -+ phi[0][1][1] * phi[1][0][0];+ a1[0] = re / dk;+ a1[1] = im / dk;+ }+ if (!phi[1][0][0]) {+ a0[0] = a0[1] = 0;+ } else {+ float re =+ phi[0][0][0] + a1[0] * phi[1][1][0] + a1[1] * phi[1][1][1];+ float im =+ phi[0][0][1] + a1[1] * phi[1][1][0] - a1[0] * phi[1][1][1];+ a0[0] = -re / phi[1][0][0];+ a0[1] = -im / phi[1][0][0];+ }+ if (a1[0] * a1[0] + a1[1] * a1[1] >= 16.0f ||+ a0[0] * a0[0] + a0[1] * a0[1] >= 16.0f)+ a0[0] = a0[1] = a1[0] = a1[1] = 0;+ }+}++static void chirp(struct sbr *s, struct sbr_ch *c)+{+ static const float BW[] = {0.0f, 0.75f, 0.9f, 0.98f};+ for (int i = 0; i < s->n_q; i++) {+ float nb;+ if (c->invf[0][i] + c->invf[1][i] == 1)+ nb = 0.6f;+ else+ nb = BW[c->invf[0][i]];+ if (nb < c->bw[i])+ nb = 0.75f * nb + 0.25f * c->bw[i];+ else+ nb = 0.90625f * nb + 0.09375f * c->bw[i];+ c->bw[i] = nb < 0.015625f ? 0.0f : nb;+ }+}++static int hf_gen(struct sbr *s, struct sbr_ch *c)+{+ int g = 0, k = s->kx[1];+ for (int j = 0; j < s->num_patches; j++) {+ for (int x = 0; x < s->patch_num[j]; x++, k++) {+ int p = s->patch_start[j] + x;+ while (g <= s->n_q && k >= s->f_noise[g])+ g++;+ g--;+ if (g < 0 || k >= 64)+ return -1;+ float bw = c->bw[g];+ float a[4] = {s->alpha1[p][0] * bw * bw, s->alpha1[p][1] * bw * bw,+ s->alpha0[p][0] * bw, s->alpha0[p][1] * bw};+ float(*hi)[2] = s->X_high[k] + ENV_OFF;+ const float(*lo)[2] = (const float(*)[2])s->X_low[p] + ENV_OFF;+ for (int i = 2 * c->t_env[0]; i < 2 * c->t_env[c->num_env]; i++) {+ hi[i][0] = lo[i - 2][0] * a[0] - lo[i - 2][1] * a[1] ++ lo[i - 1][0] * a[2] - lo[i - 1][1] * a[3] + lo[i][0];+ hi[i][1] = lo[i - 2][1] * a[0] + lo[i - 2][0] * a[1] ++ lo[i - 1][1] * a[2] + lo[i - 1][0] * a[3] + lo[i][1];+ }+ }+ }+ if (k < s->m[1] + s->kx[1])+ memset(s->X_high + k, 0,+ sizeof(s->X_high[0]) * (size_t)(s->m[1] + s->kx[1] - k));+ return 0;+}++static int mapping(struct sbr *s, struct sbr_ch *c)+{+ int kx = s->kx[1];+ memset(c->s_index[1], 0, sizeof(c->s_index[1]) * 6);+ for (int e = 0; e < c->num_env; e++) {+ int ilim = s->n[c->freq_res[e + 1]];+ const int *table = c->freq_res[e + 1] ? s->f_high : s->f_low;+ if (kx != table[0])+ return -1;+ for (int i = 0; i < ilim; i++)+ for (int m = table[i]; m < table[i + 1]; m++)+ s->e_orig[e][m - kx] = c->env[e + 1][i];+ int k = c->num_noise > 1 && c->t_env[e] >= c->t_q[1];+ for (int i = 0; i < s->n_q; i++)+ for (int m = s->f_noise[i]; m < s->f_noise[i + 1]; m++)+ s->q_mapped[e][m - kx] = c->noise[k + 1][i];+ for (int i = 0; i < s->n[1]; i++) {+ if (c->add_harm_flag) {+ int mid = (s->f_high[i] + s->f_high[i + 1]) >> 1;+ c->s_index[e + 1][mid - kx] =+ c->add_harm[i] *+ (e >= c->e_a[1] || c->s_index[0][mid - kx] == 1);+ }+ }+ for (int i = 0; i < ilim; i++) {+ int present = 0;+ for (int m = table[i]; m < table[i + 1]; m++)+ if (c->s_index[e + 1][m - kx]) {+ present = 1;+ break;+ }+ for (int m = table[i]; m < table[i + 1]; m++)+ s->s_mapped[e][m - kx] = present;+ }+ }+ memcpy(c->s_index[0], c->s_index[c->num_env], sizeof c->s_index[0]);+ return 0;+}++static void env_estimate(struct sbr *s, struct sbr_ch *c)+{+ int kx = s->kx[1];+ for (int e = 0; e < c->num_env; e++) {+ int ilb = c->t_env[e] * 2 + ENV_OFF,+ iub = c->t_env[e + 1] * 2 + ENV_OFF;+ if (s->interpol_freq) {+ float recip = 0.5f / (float)(c->t_env[e + 1] - c->t_env[e]);+ for (int m = 0; m < s->m[1]; m++) {+ float sum = 0;+ for (int i = ilb; i < iub; i++)+ sum += s->X_high[m + kx][i][0] * s->X_high[m + kx][i][0] ++ s->X_high[m + kx][i][1] * s->X_high[m + kx][i][1];+ s->e_curr[e][m] = sum * recip;+ }+ } else {+ int size = 2 * (c->t_env[e + 1] - c->t_env[e]);+ const int *table = c->freq_res[e + 1] ? s->f_high : s->f_low;+ for (int p = 0; p < s->n[c->freq_res[e + 1]]; p++) {+ float sum = 0;+ int den = size * (table[p + 1] - table[p]);+ for (int k = table[p]; k < table[p + 1]; k++) {+ float sq = 0;+ for (int i = ilb; i < iub; i++)+ sq += s->X_high[k][i][0] * s->X_high[k][i][0] ++ s->X_high[k][i][1] * s->X_high[k][i][1];+ sum += sq;+ }+ sum /= (float)den;+ for (int k = table[p]; k < table[p + 1]; k++)+ s->e_curr[e][k - kx] = sum;+ }+ }+ }+}++static void gain_calc(struct sbr *s, struct sbr_ch *c)+{+ static const float LIMGAIN[4] = {0.70795f, 1.0f, 1.41254f, 10000000000.0f};+ int kx = s->kx[1];+ for (int e = 0; e < c->num_env; e++) {+ int delta = !(e == c->e_a[1] || e == c->e_a[0]);+ for (int k = 0; k < s->n_lim; k++) {+ int lo = s->f_lim[k] - kx, hi = s->f_lim[k + 1] - kx;+ float sum[2] = {0, 0};+ for (int m = lo; m < hi; m++) {+ float temp = s->e_orig[e][m] / (1.0f + s->q_mapped[e][m]);+ s->q_m[e][m] = sqrtf(temp * s->q_mapped[e][m]);+ s->s_m[e][m] = sqrtf(temp * (float)c->s_index[e + 1][m]);+ if (!s->s_mapped[e][m])+ s->gain[e][m] =+ sqrtf(s->e_orig[e][m] /+ ((1.0f + s->e_curr[e][m]) *+ (1.0f + s->q_mapped[e][m] * (float)delta)));+ else+ s->gain[e][m] = sqrtf(s->e_orig[e][m] * s->q_mapped[e][m] /+ ((1.0f + s->e_curr[e][m]) *+ (1.0f + s->q_mapped[e][m])));+ s->gain[e][m] += FLT_MIN;+ }+ for (int m = lo; m < hi; m++) {+ sum[0] += s->e_orig[e][m];+ sum[1] += s->e_curr[e][m];+ }+ float gain_max =+ LIMGAIN[s->limiter_gains] *+ sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));+ if (gain_max > 100000.f)+ gain_max = 100000.f;+ for (int m = lo; m < hi; m++) {+ float q_m_max = s->q_m[e][m] * gain_max / s->gain[e][m];+ if (q_m_max < s->q_m[e][m])+ s->q_m[e][m] = q_m_max;+ if (gain_max < s->gain[e][m])+ s->gain[e][m] = gain_max;+ }+ sum[0] = sum[1] = 0;+ for (int m = lo; m < hi; m++) {+ sum[0] += s->e_orig[e][m];+ sum[1] += s->e_curr[e][m] * s->gain[e][m] * s->gain[e][m] ++ s->s_m[e][m] * s->s_m[e][m] ++ (float)(delta && !s->s_m[e][m]) * s->q_m[e][m] *+ s->q_m[e][m];+ }+ float boost =+ sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));+ if (boost > 1.584893192f)+ boost = 1.584893192f;+ for (int m = lo; m < hi; m++) {+ s->gain[e][m] *= boost;+ s->q_m[e][m] *= boost;+ s->s_m[e][m] *= boost;+ }+ }+ }+}++static void hf_assemble(struct sbr *s, struct sbr_ch *c, float (*Y1)[64][2])+{+ static const float H_SMOOTH[5] = {0.33333333333333f, 0.30150283239582f,+ 0.21816949906249f, 0.11516383427084f,+ 0.03183050093751f};+ int h_sl = 4 * !s->smoothing;+ int kx = s->kx[1], m_max = s->m[1];+ float(*g_temp)[48] = c->g_temp, (*q_temp)[48] = c->q_temp;+ int index_noise = c->index_noise, index_sine = c->index_sine;+ if (s->reset) {+ for (int i = 0; i < h_sl; i++) {+ memcpy(g_temp[i + 2 * c->t_env[0]], s->gain[0],+ sizeof(float) * (size_t)m_max);+ memcpy(q_temp[i + 2 * c->t_env[0]], s->q_m[0],+ sizeof(float) * (size_t)m_max);+ }+ } else if (h_sl) {+ for (int i = 0; i < 4; i++) {+ memcpy(g_temp[i + 2 * c->t_env[0]], g_temp[i + 2 * c->t_env_old],+ sizeof g_temp[0]);+ memcpy(q_temp[i + 2 * c->t_env[0]], q_temp[i + 2 * c->t_env_old],+ sizeof q_temp[0]);+ }+ }+ for (int e = 0; e < c->num_env; e++)+ for (int i = 2 * c->t_env[e]; i < 2 * c->t_env[e + 1]; i++) {+ memcpy(g_temp[h_sl + i], s->gain[e], sizeof(float) * (size_t)m_max);+ memcpy(q_temp[h_sl + i], s->q_m[e], sizeof(float) * (size_t)m_max);+ }+ for (int e = 0; e < c->num_env; e++) {+ for (int i = 2 * c->t_env[e]; i < 2 * c->t_env[e + 1]; i++) {+ float g_tab[48], q_tab[48];+ const float *g_filt, *q_filt;+ if (h_sl && e != c->e_a[0] && e != c->e_a[1]) {+ for (int m = 0; m < m_max; m++) {+ int idx1 = i + h_sl;+ g_tab[m] = 0;+ q_tab[m] = 0;+ for (int j = 0; j <= h_sl; j++) {+ g_tab[m] += g_temp[idx1 - j][m] * H_SMOOTH[j];+ q_tab[m] += q_temp[idx1 - j][m] * H_SMOOTH[j];+ }+ }+ g_filt = g_tab;+ q_filt = q_tab;+ } else {+ g_filt = g_temp[i + h_sl];+ q_filt = q_temp[i];+ }+ float(*Y)[2] = Y1[i] + kx;+ for (int m = 0; m < m_max; m++) {+ Y[m][0] = s->X_high[kx + m][i + ENV_OFF][0] * g_filt[m];+ Y[m][1] = s->X_high[kx + m][i + ENV_OFF][1] * g_filt[m];+ }+ const float *s_m = s->s_m[e];+ if (e != c->e_a[0] && e != c->e_a[1]) {+ float sign0, sign1;+ int ks = 1 - 2 * (kx & 1);+ switch (index_sine) {+ case 0:+ sign0 = 1;+ sign1 = 0;+ break;+ case 1:+ sign0 = 0;+ sign1 = (float)ks;+ break;+ case 2:+ sign0 = -1;+ sign1 = 0;+ break;+ default:+ sign0 = 0;+ sign1 = (float)-ks;+ break;+ }+ int noise = index_noise;+ for (int m = 0; m < m_max; m++) {+ float y0 = Y[m][0], y1 = Y[m][1];+ noise = (noise + 1) & 0x1ff;+ if (s_m[m]) {+ y0 += s_m[m] * sign0;+ y1 += s_m[m] * sign1;+ } else {+ y0 += q_filt[m] * SBR_NOISE[2 * noise];+ y1 += q_filt[m] * SBR_NOISE[2 * noise + 1];+ }+ Y[m][0] = y0;+ Y[m][1] = y1;+ sign1 = -sign1;+ }+ } else {+ int idx = index_sine & 1;+ int A = 1 - ((index_sine + (kx & 1)) & 2);+ int B = (A ^ (-idx)) + idx;+ for (int m = 0; m < m_max; m++)+ Y[m][idx] += s_m[m] * (float)(m & 1 ? B : A);+ }+ index_noise = (index_noise + m_max) & 0x1ff;+ index_sine = (index_sine + 1) & 3;+ }+ }+ c->index_noise = index_noise;+ c->index_sine = index_sine;+}++/* the synthesis' input: the low band, the high band of the frame before+ that runs into this one, this frame's */+static void x_gen(struct sbr *s, struct sbr_ch *c, float (*X)[38][64])+{+ const float(*Y0)[64][2] = (const float(*)[64][2])c->Y[1 - c->ypos];+ const float(*Y1)[64][2] = (const float(*)[64][2])c->Y[c->ypos];+ int i_temp = 2 * c->t_env_old - 32;+ if (i_temp < 0)+ i_temp = 0;+ memset(X, 0, sizeof(float) * 2 * 38 * 64);+ int k;+ for (k = 0; k < s->kx[0]; k++)+ for (int i = 0; i < i_temp; i++) {+ X[0][i][k] = s->X_low[k][i + ENV_OFF][0];+ X[1][i][k] = s->X_low[k][i + ENV_OFF][1];+ }+ for (; k < s->kx[0] + s->m[0]; k++)+ for (int i = 0; i < i_temp; i++) {+ X[0][i][k] = Y0[i + 32][k][0];+ X[1][i][k] = Y0[i + 32][k][1];+ }+ for (k = 0; k < s->kx[1]; k++)+ for (int i = i_temp; i < 38; i++) {+ X[0][i][k] = s->X_low[k][i + ENV_OFF][0];+ X[1][i][k] = s->X_low[k][i + ENV_OFF][1];+ }+ for (; k < s->kx[1] + s->m[1]; k++)+ for (int i = i_temp; i < 32; i++) {+ X[0][i][k] = Y1[i][k][0];+ X[1][i][k] = Y1[i][k][1];+ }+}++void sbr_apply(struct sbr *s, int ch, float *const *in, float **out)+{+ if (ch != s->ch)+ turnoff(s);+ if (s->start && !s->ready)+ turnoff(s);+ if (!s->pushed) {+ s->kx[0] = s->kx[1];+ s->m[0] = s->m[1];+ } else {+ s->pushed = 0;+ }+ if (s->start) {+ dequant(s);+ s->ready = 0;+ }+ for (int k = 0; k < ch; k++) {+ struct sbr_ch *c = &s->c[k];+ qmf_analysis(s, c, in[k], c->W[c->ypos]);+ lf_gen(s, c);+ c->ypos ^= 1;+ if (s->start) {+ inverse_filter(s);+ chirp(s, c);+ if (c->num_env > 0 && hf_gen(s, c) == 0 && mapping(s, c) == 0) {+ env_estimate(s, c);+ gain_calc(s, c);+ hf_assemble(s, c, c->Y[c->ypos]);+ }+ }+ x_gen(s, c, s->X[k]);+ }+ int nch = ch;+ if (ch == 1 && s->ps_found) {+ ps_apply(s->ps, s->X[0], s->X[1], s->kx[1] + s->m[1]);+ nch = 2;+ }+ for (int k = 0; k < nch; k++) {+ if (s->down)+ qmf_synthesis_ds(s, &s->c[k], s->X[k], out[k]);+ else+ qmf_synthesis(s, &s->c[k], s->X[k], out[k]);+ }+}++/* ---- life ---- */++struct sbr *sbr_new(int rate, int ch, int down)+{+ struct sbr *s = calloc(1, sizeof *s);+ if (!s)+ return NULL;+ s->rate = 2 * rate;+ s->ch = ch;+ s->down = down;+ s->kx[1] = 32;+ turnoff(s);+ for (int t = 0; t < 10; t++) {+ int at = SBR_HUFF_AT[t], n = SBR_HUFF_AT[t + 1] - at;+ if (vlc_build(&s->vlc[t], 9, SBR_HUFF_CODE + at, SBR_HUFF_LEN + at,+ SBR_HUFF_SYM + at, n) != 0) {+ sbr_free(s);+ return NULL;+ }+ }+ s->m64 = aimdct_new(64);+ s->m128 = aimdct_new(128);+ if (!s->m64 || !s->m128) {+ sbr_free(s);+ return NULL;+ }+ for (int k = 0; k < 32; k++) {+ double a = -3 * M_PI * (k + 0.5) / 128;+ s->rot[k][0] = (float)(2 * cos(a));+ s->rot[k][1] = (float)(2 * sin(a));+ }+ for (int n = 0; n < 64; n++)+ for (int k = 0; k < 32; k++) {+ double a = M_PI / 64.0 * (k + 0.5) * (2 * n - 127.5);+ s->ds_cos[n][k] = (float)cos(a);+ s->ds_sin[n][k] = (float)sin(a);+ }+ return s;+}++void sbr_free(struct sbr *s)+{+ if (!s)+ return;+ for (int t = 0; t < 10; t++)+ vlc_free(&s->vlc[t]);+ ps_free(s->ps);+ aimdct_free(s->m64);+ aimdct_free(s->m128);+ free(s);+}diff --git a/src/common/audio_aac_tab.h b/src/common/audio_aac_tab.hnew file mode 100644index 0000000..14e5d4f--- /dev/null+++ b/src/common/audio_aac_tab.h@@ -0,0 +1,930 @@+/* audio_aac_tab.h - generated by gen_aac_tab.py from FFmpeg's tables+ (n7.1): do not edit (no clang-format). The AAC decoder's Huffman+ codes, scalefactor bands and filter windows, its SBR's and PS's. */+#ifndef JANAS_COMMON_AUDIO_AAC_TAB_H+#define JANAS_COMMON_AUDIO_AAC_TAB_H++#include <stdint.h>++static const uint32_t AAC_SF_CODE[121] = {+ 0x3ffe8, 0x3ffe6, 0x3ffe7, 0x3ffe5, 0x7fff5, 0x7fff1, 0x7ffed, 0x7fff6,+ 0x7ffee, 0x7ffef, 0x7fff0, 0x7fffc, 0x7fffd, 0x7ffff, 0x7fffe, 0x7fff7,+ 0x7fff8, 0x7fffb, 0x7fff9, 0x3ffe4, 0x7fffa, 0x3ffe3, 0x1ffef, 0x1fff0,+ 0xfff5, 0x1ffee, 0xfff2, 0xfff3, 0xfff4, 0xfff1, 0x7ff6, 0x7ff7,+ 0x3ff9, 0x3ff5, 0x3ff7, 0x3ff3, 0x3ff6, 0x3ff2, 0x1ff7, 0x1ff5,+ 0xff9, 0xff7, 0xff6, 0x7f9, 0xff4, 0x7f8, 0x3f9, 0x3f7,+ 0x3f5, 0x1f8, 0x1f7, 0xfa, 0xf8, 0xf6, 0x79, 0x3a,+ 0x38, 0x1a, 0xb, 0x4, 0x0, 0xa, 0xc, 0x1b,+ 0x39, 0x3b, 0x78, 0x7a, 0xf7, 0xf9, 0x1f6, 0x1f9,+ 0x3f4, 0x3f6, 0x3f8, 0x7f5, 0x7f4, 0x7f6, 0x7f7, 0xff5,+ 0xff8, 0x1ff4, 0x1ff6, 0x1ff8, 0x3ff8, 0x3ff4, 0xfff0, 0x7ff4,+ 0xfff6, 0x7ff5, 0x3ffe2, 0x7ffd9, 0x7ffda, 0x7ffdb, 0x7ffdc, 0x7ffdd,+ 0x7ffde, 0x7ffd8, 0x7ffd2, 0x7ffd3, 0x7ffd4, 0x7ffd5, 0x7ffd6, 0x7fff2,+ 0x7ffdf, 0x7ffe7, 0x7ffe8, 0x7ffe9, 0x7ffea, 0x7ffeb, 0x7ffe6, 0x7ffe0,+ 0x7ffe1, 0x7ffe2, 0x7ffe3, 0x7ffe4, 0x7ffe5, 0x7ffd7, 0x7ffec, 0x7fff4,+ 0x7fff3,+};+static const uint8_t AAC_SF_BITS[121] = {+ 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 18,+ 19, 18, 17, 17, 16, 17, 16, 16, 16, 16, 15, 15, 14, 14, 14, 14, 14, 14, 13, 13,+ 12, 12, 12, 11, 12, 11, 10, 10, 10, 9, 9, 8, 8, 8, 7, 6, 6, 5, 4, 3,+ 1, 4, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 11, 12,+ 12, 13, 13, 13, 14, 14, 16, 15, 16, 15, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19,+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,+ 19,+};+static const uint16_t AAC_CODES1[81] = {+ 0x7f8, 0x1f1, 0x7fd, 0x3f5, 0x68, 0x3f0, 0x7f7, 0x1ec, 0x7f5, 0x3f1,+ 0x72, 0x3f4, 0x74, 0x11, 0x76, 0x1eb, 0x6c, 0x3f6, 0x7fc, 0x1e1,+ 0x7f1, 0x1f0, 0x61, 0x1f6, 0x7f2, 0x1ea, 0x7fb, 0x1f2, 0x69, 0x1ed,+ 0x77, 0x17, 0x6f, 0x1e6, 0x64, 0x1e5, 0x67, 0x15, 0x62, 0x12,+ 0x0, 0x14, 0x65, 0x16, 0x6d, 0x1e9, 0x63, 0x1e4, 0x6b, 0x13,+ 0x71, 0x1e3, 0x70, 0x1f3, 0x7fe, 0x1e7, 0x7f3, 0x1ef, 0x60, 0x1ee,+ 0x7f0, 0x1e2, 0x7fa, 0x3f3, 0x6a, 0x1e8, 0x75, 0x10, 0x73, 0x1f4,+ 0x6e, 0x3f7, 0x7f6, 0x1e0, 0x7f9, 0x3f2, 0x66, 0x1f5, 0x7ff, 0x1f7,+ 0x7f4,+};+static const uint8_t AAC_BITS1[81] = {+ 11, 9, 11, 10, 7, 10, 11, 9, 11, 10, 7, 10, 7, 5, 7, 9, 7, 10, 11, 9,+ 11, 9, 7, 9, 11, 9, 11, 9, 7, 9, 7, 5, 7, 9, 7, 9, 7, 5, 7, 5,+ 1, 5, 7, 5, 7, 9, 7, 9, 7, 5, 7, 9, 7, 9, 11, 9, 11, 9, 7, 9,+ 11, 9, 11, 10, 7, 9, 7, 5, 7, 9, 7, 10, 11, 9, 11, 10, 7, 9, 11, 9,+ 11,+};+static const uint16_t AAC_CODES2[81] = {+ 0x1f3, 0x6f, 0x1fd, 0xeb, 0x23, 0xea, 0x1f7, 0xe8, 0x1fa, 0xf2,+ 0x2d, 0x70, 0x20, 0x6, 0x2b, 0x6e, 0x28, 0xe9, 0x1f9, 0x66,+ 0xf8, 0xe7, 0x1b, 0xf1, 0x1f4, 0x6b, 0x1f5, 0xec, 0x2a, 0x6c,+ 0x2c, 0xa, 0x27, 0x67, 0x1a, 0xf5, 0x24, 0x8, 0x1f, 0x9,+ 0x0, 0x7, 0x1d, 0xb, 0x30, 0xef, 0x1c, 0x64, 0x1e, 0xc,+ 0x29, 0xf3, 0x2f, 0xf0, 0x1fc, 0x71, 0x1f2, 0xf4, 0x21, 0xe6,+ 0xf7, 0x68, 0x1f8, 0xee, 0x22, 0x65, 0x31, 0x2, 0x26, 0xed,+ 0x25, 0x6a, 0x1fb, 0x72, 0x1fe, 0x69, 0x2e, 0xf6, 0x1ff, 0x6d,+ 0x1f6,+};+static const uint8_t AAC_BITS2[81] = {+ 9, 7, 9, 8, 6, 8, 9, 8, 9, 8, 6, 7, 6, 5, 6, 7, 6, 8, 9, 7,+ 8, 8, 6, 8, 9, 7, 9, 8, 6, 7, 6, 5, 6, 7, 6, 8, 6, 5, 6, 5,+ 3, 5, 6, 5, 6, 8, 6, 7, 6, 5, 6, 8, 6, 8, 9, 7, 9, 8, 6, 8,+ 8, 7, 9, 8, 6, 7, 6, 4, 6, 8, 6, 7, 9, 7, 9, 7, 6, 8, 9, 7,+ 9,+};+static const uint16_t AAC_CODES3[81] = {+ 0x0, 0x9, 0xef, 0xb, 0x19, 0xf0, 0x1eb, 0x1e6, 0x3f2, 0xa,+ 0x35, 0x1ef, 0x34, 0x37, 0x1e9, 0x1ed, 0x1e7, 0x3f3, 0x1ee, 0x3ed,+ 0x1ffa, 0x1ec, 0x1f2, 0x7f9, 0x7f8, 0x3f8, 0xff8, 0x8, 0x38, 0x3f6,+ 0x36, 0x75, 0x3f1, 0x3eb, 0x3ec, 0xff4, 0x18, 0x76, 0x7f4, 0x39,+ 0x74, 0x3ef, 0x1f3, 0x1f4, 0x7f6, 0x1e8, 0x3ea, 0x1ffc, 0xf2, 0x1f1,+ 0xffb, 0x3f5, 0x7f3, 0xffc, 0xee, 0x3f7, 0x7ffe, 0x1f0, 0x7f5, 0x7ffd,+ 0x1ffb, 0x3ffa, 0xffff, 0xf1, 0x3f0, 0x3ffc, 0x1ea, 0x3ee, 0x3ffb, 0xff6,+ 0xffa, 0x7ffc, 0x7f2, 0xff5, 0xfffe, 0x3f4, 0x7f7, 0x7ffb, 0xff7, 0xff9,+ 0x7ffa,+};+static const uint8_t AAC_BITS3[81] = {+ 1, 4, 8, 4, 5, 8, 9, 9, 10, 4, 6, 9, 6, 6, 9, 9, 9, 10, 9, 10,+ 13, 9, 9, 11, 11, 10, 12, 4, 6, 10, 6, 7, 10, 10, 10, 12, 5, 7, 11, 6,+ 7, 10, 9, 9, 11, 9, 10, 13, 8, 9, 12, 10, 11, 12, 8, 10, 15, 9, 11, 15,+ 13, 14, 16, 8, 10, 14, 9, 10, 14, 12, 12, 15, 11, 12, 16, 10, 11, 15, 12, 12,+ 15,+};+static const uint16_t AAC_CODES4[81] = {+ 0x7, 0x16, 0xf6, 0x18, 0x8, 0xef, 0x1ef, 0xf3, 0x7f8, 0x19,+ 0x17, 0xed, 0x15, 0x1, 0xe2, 0xf0, 0x70, 0x3f0, 0x1ee, 0xf1,+ 0x7fa, 0xee, 0xe4, 0x3f2, 0x7f6, 0x3ef, 0x7fd, 0x5, 0x14, 0xf2,+ 0x9, 0x4, 0xe5, 0xf4, 0xe8, 0x3f4, 0x6, 0x2, 0xe7, 0x3,+ 0x0, 0x6b, 0xe3, 0x69, 0x1f3, 0xeb, 0xe6, 0x3f6, 0x6e, 0x6a,+ 0x1f4, 0x3ec, 0x1f0, 0x3f9, 0xf5, 0xec, 0x7fb, 0xea, 0x6f, 0x3f7,+ 0x7f9, 0x3f3, 0xfff, 0xe9, 0x6d, 0x3f8, 0x6c, 0x68, 0x1f5, 0x3ee,+ 0x1f2, 0x7f4, 0x7f7, 0x3f1, 0xffe, 0x3ed, 0x1f1, 0x7f5, 0x7fe, 0x3f5,+ 0x7fc,+};+static const uint8_t AAC_BITS4[81] = {+ 4, 5, 8, 5, 4, 8, 9, 8, 11, 5, 5, 8, 5, 4, 8, 8, 7, 10, 9, 8,+ 11, 8, 8, 10, 11, 10, 11, 4, 5, 8, 4, 4, 8, 8, 8, 10, 4, 4, 8, 4,+ 4, 7, 8, 7, 9, 8, 8, 10, 7, 7, 9, 10, 9, 10, 8, 8, 11, 8, 7, 10,+ 11, 10, 12, 8, 7, 10, 7, 7, 9, 10, 9, 11, 11, 10, 12, 10, 9, 11, 11, 10,+ 11,+};+static const uint16_t AAC_CODES5[81] = {+ 0x1fff, 0xff7, 0x7f4, 0x7e8, 0x3f1, 0x7ee, 0x7f9, 0xff8, 0x1ffd, 0xffd,+ 0x7f1, 0x3e8, 0x1e8, 0xf0, 0x1ec, 0x3ee, 0x7f2, 0xffa, 0xff4, 0x3ef,+ 0x1f2, 0xe8, 0x70, 0xec, 0x1f0, 0x3ea, 0x7f3, 0x7eb, 0x1eb, 0xea,+ 0x1a, 0x8, 0x19, 0xee, 0x1ef, 0x7ed, 0x3f0, 0xf2, 0x73, 0xb,+ 0x0, 0xa, 0x71, 0xf3, 0x7e9, 0x7ef, 0x1ee, 0xef, 0x18, 0x9,+ 0x1b, 0xeb, 0x1e9, 0x7ec, 0x7f6, 0x3eb, 0x1f3, 0xed, 0x72, 0xe9,+ 0x1f1, 0x3ed, 0x7f7, 0xff6, 0x7f0, 0x3e9, 0x1ed, 0xf1, 0x1ea, 0x3ec,+ 0x7f8, 0xff9, 0x1ffc, 0xffc, 0xff5, 0x7ea, 0x3f3, 0x3f2, 0x7f5, 0xffb,+ 0x1ffe,+};+static const uint8_t AAC_BITS5[81] = {+ 13, 12, 11, 11, 10, 11, 11, 12, 13, 12, 11, 10, 9, 8, 9, 10, 11, 12, 12, 10,+ 9, 8, 7, 8, 9, 10, 11, 11, 9, 8, 5, 4, 5, 8, 9, 11, 10, 8, 7, 4,+ 1, 4, 7, 8, 11, 11, 9, 8, 5, 4, 5, 8, 9, 11, 11, 10, 9, 8, 7, 8,+ 9, 10, 11, 12, 11, 10, 9, 8, 9, 10, 11, 12, 13, 12, 12, 11, 10, 10, 11, 12,+ 13,+};+static const uint16_t AAC_CODES6[81] = {+ 0x7fe, 0x3fd, 0x1f1, 0x1eb, 0x1f4, 0x1ea, 0x1f0, 0x3fc, 0x7fd, 0x3f6,+ 0x1e5, 0xea, 0x6c, 0x71, 0x68, 0xf0, 0x1e6, 0x3f7, 0x1f3, 0xef,+ 0x32, 0x27, 0x28, 0x26, 0x31, 0xeb, 0x1f7, 0x1e8, 0x6f, 0x2e,+ 0x8, 0x4, 0x6, 0x29, 0x6b, 0x1ee, 0x1ef, 0x72, 0x2d, 0x2,+ 0x0, 0x3, 0x2f, 0x73, 0x1fa, 0x1e7, 0x6e, 0x2b, 0x7, 0x1,+ 0x5, 0x2c, 0x6d, 0x1ec, 0x1f9, 0xee, 0x30, 0x24, 0x2a, 0x25,+ 0x33, 0xec, 0x1f2, 0x3f8, 0x1e4, 0xed, 0x6a, 0x70, 0x69, 0x74,+ 0xf1, 0x3fa, 0x7ff, 0x3f9, 0x1f6, 0x1ed, 0x1f8, 0x1e9, 0x1f5, 0x3fb,+ 0x7fc,+};+static const uint8_t AAC_BITS6[81] = {+ 11, 10, 9, 9, 9, 9, 9, 10, 11, 10, 9, 8, 7, 7, 7, 8, 9, 10, 9, 8,+ 6, 6, 6, 6, 6, 8, 9, 9, 7, 6, 4, 4, 4, 6, 7, 9, 9, 7, 6, 4,+ 4, 4, 6, 7, 9, 9, 7, 6, 4, 4, 4, 6, 7, 9, 9, 8, 6, 6, 6, 6,+ 6, 8, 9, 10, 9, 8, 7, 7, 7, 7, 8, 10, 11, 10, 9, 9, 9, 9, 9, 10,+ 11,+};+static const uint16_t AAC_CODES7[64] = {+ 0x0, 0x5, 0x37, 0x74, 0xf2, 0x1eb, 0x3ed, 0x7f7, 0x4, 0xc,+ 0x35, 0x71, 0xec, 0xee, 0x1ee, 0x1f5, 0x36, 0x34, 0x72, 0xea,+ 0xf1, 0x1e9, 0x1f3, 0x3f5, 0x73, 0x70, 0xeb, 0xf0, 0x1f1, 0x1f0,+ 0x3ec, 0x3fa, 0xf3, 0xed, 0x1e8, 0x1ef, 0x3ef, 0x3f1, 0x3f9, 0x7fb,+ 0x1ed, 0xef, 0x1ea, 0x1f2, 0x3f3, 0x3f8, 0x7f9, 0x7fc, 0x3ee, 0x1ec,+ 0x1f4, 0x3f4, 0x3f7, 0x7f8, 0xffd, 0xffe, 0x7f6, 0x3f0, 0x3f2, 0x3f6,+ 0x7fa, 0x7fd, 0xffc, 0xfff,+};+static const uint8_t AAC_BITS7[64] = {+ 1, 3, 6, 7, 8, 9, 10, 11, 3, 4, 6, 7, 8, 8, 9, 9, 6, 6, 7, 8,+ 8, 9, 9, 10, 7, 7, 8, 8, 9, 9, 10, 10, 8, 8, 9, 9, 10, 10, 10, 11,+ 9, 8, 9, 9, 10, 10, 11, 11, 10, 9, 9, 10, 10, 11, 12, 12, 11, 10, 10, 10,+ 11, 11, 12, 12,+};+static const uint16_t AAC_CODES8[64] = {+ 0xe, 0x5, 0x10, 0x30, 0x6f, 0xf1, 0x1fa, 0x3fe, 0x3, 0x0,+ 0x4, 0x12, 0x2c, 0x6a, 0x75, 0xf8, 0xf, 0x2, 0x6, 0x14,+ 0x2e, 0x69, 0x72, 0xf5, 0x2f, 0x11, 0x13, 0x2a, 0x32, 0x6c,+ 0xec, 0xfa, 0x71, 0x2b, 0x2d, 0x31, 0x6d, 0x70, 0xf2, 0x1f9,+ 0xef, 0x68, 0x33, 0x6b, 0x6e, 0xee, 0xf9, 0x3fc, 0x1f8, 0x74,+ 0x73, 0xed, 0xf0, 0xf6, 0x1f6, 0x1fd, 0x3fd, 0xf3, 0xf4, 0xf7,+ 0x1f7, 0x1fb, 0x1fc, 0x3ff,+};+static const uint8_t AAC_BITS8[64] = {+ 5, 4, 5, 6, 7, 8, 9, 10, 4, 3, 4, 5, 6, 7, 7, 8, 5, 4, 4, 5,+ 6, 7, 7, 8, 6, 5, 5, 6, 6, 7, 8, 8, 7, 6, 6, 6, 7, 7, 8, 9,+ 8, 7, 6, 7, 7, 8, 8, 10, 9, 7, 7, 8, 8, 8, 9, 9, 10, 8, 8, 8,+ 9, 9, 9, 10,+};+static const uint16_t AAC_CODES9[169] = {+ 0x0, 0x5, 0x37, 0xe7, 0x1de, 0x3ce, 0x3d9, 0x7c8, 0x7cd, 0xfc8,+ 0xfdd, 0x1fe4, 0x1fec, 0x4, 0xc, 0x35, 0x72, 0xea, 0xed, 0x1e2,+ 0x3d1, 0x3d3, 0x3e0, 0x7d8, 0xfcf, 0xfd5, 0x36, 0x34, 0x71, 0xe8,+ 0xec, 0x1e1, 0x3cf, 0x3dd, 0x3db, 0x7d0, 0xfc7, 0xfd4, 0xfe4, 0xe6,+ 0x70, 0xe9, 0x1dd, 0x1e3, 0x3d2, 0x3dc, 0x7cc, 0x7ca, 0x7de, 0xfd8,+ 0xfea, 0x1fdb, 0x1df, 0xeb, 0x1dc, 0x1e6, 0x3d5, 0x3de, 0x7cb, 0x7dd,+ 0x7dc, 0xfcd, 0xfe2, 0xfe7, 0x1fe1, 0x3d0, 0x1e0, 0x1e4, 0x3d6, 0x7c5,+ 0x7d1, 0x7db, 0xfd2, 0x7e0, 0xfd9, 0xfeb, 0x1fe3, 0x1fe9, 0x7c4, 0x1e5,+ 0x3d7, 0x7c6, 0x7cf, 0x7da, 0xfcb, 0xfda, 0xfe3, 0xfe9, 0x1fe6, 0x1ff3,+ 0x1ff7, 0x7d3, 0x3d8, 0x3e1, 0x7d4, 0x7d9, 0xfd3, 0xfde, 0x1fdd, 0x1fd9,+ 0x1fe2, 0x1fea, 0x1ff1, 0x1ff6, 0x7d2, 0x3d4, 0x3da, 0x7c7, 0x7d7, 0x7e2,+ 0xfce, 0xfdb, 0x1fd8, 0x1fee, 0x3ff0, 0x1ff4, 0x3ff2, 0x7e1, 0x3df, 0x7c9,+ 0x7d6, 0xfca, 0xfd0, 0xfe5, 0xfe6, 0x1feb, 0x1fef, 0x3ff3, 0x3ff4, 0x3ff5,+ 0xfe0, 0x7ce, 0x7d5, 0xfc6, 0xfd1, 0xfe1, 0x1fe0, 0x1fe8, 0x1ff0, 0x3ff1,+ 0x3ff8, 0x3ff6, 0x7ffc, 0xfe8, 0x7df, 0xfc9, 0xfd7, 0xfdc, 0x1fdc, 0x1fdf,+ 0x1fed, 0x1ff5, 0x3ff9, 0x3ffb, 0x7ffd, 0x7ffe, 0x1fe7, 0xfcc, 0xfd6, 0xfdf,+ 0x1fde, 0x1fda, 0x1fe5, 0x1ff2, 0x3ffa, 0x3ff7, 0x3ffc, 0x3ffd, 0x7fff,+};+static const uint8_t AAC_BITS9[169] = {+ 1, 3, 6, 8, 9, 10, 10, 11, 11, 12, 12, 13, 13, 3, 4, 6, 7, 8, 8, 9,+ 10, 10, 10, 11, 12, 12, 6, 6, 7, 8, 8, 9, 10, 10, 10, 11, 12, 12, 12, 8,+ 7, 8, 9, 9, 10, 10, 11, 11, 11, 12, 12, 13, 9, 8, 9, 9, 10, 10, 11, 11,+ 11, 12, 12, 12, 13, 10, 9, 9, 10, 11, 11, 11, 12, 11, 12, 12, 13, 13, 11, 9,+ 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 11, 10, 10, 11, 11, 12, 12, 13, 13,+ 13, 13, 13, 13, 11, 10, 10, 11, 11, 11, 12, 12, 13, 13, 14, 13, 14, 11, 10, 11,+ 11, 12, 12, 12, 12, 13, 13, 14, 14, 14, 12, 11, 11, 12, 12, 12, 13, 13, 13, 14,+ 14, 14, 15, 12, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 15, 15, 13, 12, 12, 12,+ 13, 13, 13, 13, 14, 14, 14, 14, 15,+};+static const uint16_t AAC_CODES10[169] = {+ 0x22, 0x8, 0x1d, 0x26, 0x5f, 0xd3, 0x1cf, 0x3d0, 0x3d7, 0x3ed,+ 0x7f0, 0x7f6, 0xffd, 0x7, 0x0, 0x1, 0x9, 0x20, 0x54, 0x60,+ 0xd5, 0xdc, 0x1d4, 0x3cd, 0x3de, 0x7e7, 0x1c, 0x2, 0x6, 0xc,+ 0x1e, 0x28, 0x5b, 0xcd, 0xd9, 0x1ce, 0x1dc, 0x3d9, 0x3f1, 0x25,+ 0xb, 0xa, 0xd, 0x24, 0x57, 0x61, 0xcc, 0xdd, 0x1cc, 0x1de,+ 0x3d3, 0x3e7, 0x5d, 0x21, 0x1f, 0x23, 0x27, 0x59, 0x64, 0xd8,+ 0xdf, 0x1d2, 0x1e2, 0x3dd, 0x3ee, 0xd1, 0x55, 0x29, 0x56, 0x58,+ 0x62, 0xce, 0xe0, 0xe2, 0x1da, 0x3d4, 0x3e3, 0x7eb, 0x1c9, 0x5e,+ 0x5a, 0x5c, 0x63, 0xca, 0xda, 0x1c7, 0x1ca, 0x1e0, 0x3db, 0x3e8,+ 0x7ec, 0x1e3, 0xd2, 0xcb, 0xd0, 0xd7, 0xdb, 0x1c6, 0x1d5, 0x1d8,+ 0x3ca, 0x3da, 0x7ea, 0x7f1, 0x1e1, 0xd4, 0xcf, 0xd6, 0xde, 0xe1,+ 0x1d0, 0x1d6, 0x3d1, 0x3d5, 0x3f2, 0x7ee, 0x7fb, 0x3e9, 0x1cd, 0x1c8,+ 0x1cb, 0x1d1, 0x1d7, 0x1df, 0x3cf, 0x3e0, 0x3ef, 0x7e6, 0x7f8, 0xffa,+ 0x3eb, 0x1dd, 0x1d3, 0x1d9, 0x1db, 0x3d2, 0x3cc, 0x3dc, 0x3ea, 0x7ed,+ 0x7f3, 0x7f9, 0xff9, 0x7f2, 0x3ce, 0x1e4, 0x3cb, 0x3d8, 0x3d6, 0x3e2,+ 0x3e5, 0x7e8, 0x7f4, 0x7f5, 0x7f7, 0xffb, 0x7fa, 0x3ec, 0x3df, 0x3e1,+ 0x3e4, 0x3e6, 0x3f0, 0x7e9, 0x7ef, 0xff8, 0xffe, 0xffc, 0xfff,+};+static const uint8_t AAC_BITS10[169] = {+ 6, 5, 6, 6, 7, 8, 9, 10, 10, 10, 11, 11, 12, 5, 4, 4, 5, 6, 7, 7,+ 8, 8, 9, 10, 10, 11, 6, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 10, 6,+ 5, 5, 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 7, 6, 6, 6, 6, 7, 7, 8,+ 8, 9, 9, 10, 10, 8, 7, 6, 7, 7, 7, 8, 8, 8, 9, 10, 10, 11, 9, 7,+ 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 11, 9, 8, 8, 8, 8, 8, 9, 9, 9,+ 10, 10, 11, 11, 9, 8, 8, 8, 8, 8, 9, 9, 10, 10, 10, 11, 11, 10, 9, 9,+ 9, 9, 9, 9, 10, 10, 10, 11, 11, 12, 10, 9, 9, 9, 9, 10, 10, 10, 10, 11,+ 11, 11, 12, 11, 10, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 12, 11, 10, 10, 10,+ 10, 10, 10, 11, 11, 12, 12, 12, 12,+};+static const uint16_t AAC_CODES11[289] = {+ 0x0, 0x6, 0x19, 0x3d, 0x9c, 0xc6, 0x1a7, 0x390, 0x3c2, 0x3df,+ 0x7e6, 0x7f3, 0xffb, 0x7ec, 0xffa, 0xffe, 0x38e, 0x5, 0x1, 0x8,+ 0x14, 0x37, 0x42, 0x92, 0xaf, 0x191, 0x1a5, 0x1b5, 0x39e, 0x3c0,+ 0x3a2, 0x3cd, 0x7d6, 0xae, 0x17, 0x7, 0x9, 0x18, 0x39, 0x40,+ 0x8e, 0xa3, 0xb8, 0x199, 0x1ac, 0x1c1, 0x3b1, 0x396, 0x3be, 0x3ca,+ 0x9d, 0x3c, 0x15, 0x16, 0x1a, 0x3b, 0x44, 0x91, 0xa5, 0xbe,+ 0x196, 0x1ae, 0x1b9, 0x3a1, 0x391, 0x3a5, 0x3d5, 0x94, 0x9a, 0x36,+ 0x38, 0x3a, 0x41, 0x8c, 0x9b, 0xb0, 0xc3, 0x19e, 0x1ab, 0x1bc,+ 0x39f, 0x38f, 0x3a9, 0x3cf, 0x93, 0xbf, 0x3e, 0x3f, 0x43, 0x45,+ 0x9e, 0xa7, 0xb9, 0x194, 0x1a2, 0x1ba, 0x1c3, 0x3a6, 0x3a7, 0x3bb,+ 0x3d4, 0x9f, 0x1a0, 0x8f, 0x8d, 0x90, 0x98, 0xa6, 0xb6, 0xc4,+ 0x19f, 0x1af, 0x1bf, 0x399, 0x3bf, 0x3b4, 0x3c9, 0x3e7, 0xa8, 0x1b6,+ 0xab, 0xa4, 0xaa, 0xb2, 0xc2, 0xc5, 0x198, 0x1a4, 0x1b8, 0x38c,+ 0x3a4, 0x3c4, 0x3c6, 0x3dd, 0x3e8, 0xad, 0x3af, 0x192, 0xbd, 0xbc,+ 0x18e, 0x197, 0x19a, 0x1a3, 0x1b1, 0x38d, 0x398, 0x3b7, 0x3d3, 0x3d1,+ 0x3db, 0x7dd, 0xb4, 0x3de, 0x1a9, 0x19b, 0x19c, 0x1a1, 0x1aa, 0x1ad,+ 0x1b3, 0x38b, 0x3b2, 0x3b8, 0x3ce, 0x3e1, 0x3e0, 0x7d2, 0x7e5, 0xb7,+ 0x7e3, 0x1bb, 0x1a8, 0x1a6, 0x1b0, 0x1b2, 0x1b7, 0x39b, 0x39a, 0x3ba,+ 0x3b5, 0x3d6, 0x7d7, 0x3e4, 0x7d8, 0x7ea, 0xba, 0x7e8, 0x3a0, 0x1bd,+ 0x1b4, 0x38a, 0x1c4, 0x392, 0x3aa, 0x3b0, 0x3bc, 0x3d7, 0x7d4, 0x7dc,+ 0x7db, 0x7d5, 0x7f0, 0xc1, 0x7fb, 0x3c8, 0x3a3, 0x395, 0x39d, 0x3ac,+ 0x3ae, 0x3c5, 0x3d8, 0x3e2, 0x3e6, 0x7e4, 0x7e7, 0x7e0, 0x7e9, 0x7f7,+ 0x190, 0x7f2, 0x393, 0x1be, 0x1c0, 0x394, 0x397, 0x3ad, 0x3c3, 0x3c1,+ 0x3d2, 0x7da, 0x7d9, 0x7df, 0x7eb, 0x7f4, 0x7fa, 0x195, 0x7f8, 0x3bd,+ 0x39c, 0x3ab, 0x3a8, 0x3b3, 0x3b9, 0x3d0, 0x3e3, 0x3e5, 0x7e2, 0x7de,+ 0x7ed, 0x7f1, 0x7f9, 0x7fc, 0x193, 0xffd, 0x3dc, 0x3b6, 0x3c7, 0x3cc,+ 0x3cb, 0x3d9, 0x3da, 0x7d3, 0x7e1, 0x7ee, 0x7ef, 0x7f5, 0x7f6, 0xffc,+ 0xfff, 0x19d, 0x1c2, 0xb5, 0xa1, 0x96, 0x97, 0x95, 0x99, 0xa0,+ 0xa2, 0xac, 0xa9, 0xb1, 0xb3, 0xbb, 0xc0, 0x18f, 0x4,+};+static const uint8_t AAC_BITS11[289] = {+ 4, 5, 6, 7, 8, 8, 9, 10, 10, 10, 11, 11, 12, 11, 12, 12, 10, 5, 4, 5,+ 6, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 8, 6, 5, 5, 6, 7, 7,+ 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 8, 7, 6, 6, 6, 7, 7, 8, 8, 8,+ 9, 9, 9, 10, 10, 10, 10, 8, 8, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9,+ 10, 10, 10, 10, 8, 8, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10,+ 10, 8, 9, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 10, 8, 9,+ 8, 8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 10, 10, 8, 10, 9, 8, 8,+ 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 8, 10, 9, 9, 9, 9, 9, 9,+ 9, 10, 10, 10, 10, 10, 10, 11, 11, 8, 11, 9, 9, 9, 9, 9, 9, 10, 10, 10,+ 10, 10, 11, 10, 11, 11, 8, 11, 10, 9, 9, 10, 9, 10, 10, 10, 10, 10, 11, 11,+ 11, 11, 11, 8, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11,+ 9, 11, 10, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 9, 11, 10,+ 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 9, 12, 10, 10, 10, 10,+ 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12, 9, 9, 8, 8, 8, 8, 8, 8, 8,+ 8, 8, 8, 8, 8, 8, 8, 9, 5,+};+static const uint16_t AAC_SWB_1024[325] = {+ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64,+ 72, 80, 88, 96, 108, 120, 132, 144, 156, 172, 188, 212, 240, 276, 320, 384,+ 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024, 0, 4, 8, 12, 16, 20,+ 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 100, 112, 124,+ 140, 156, 172, 192, 216, 240, 268, 304, 344, 384, 424, 464, 504, 544, 584, 624,+ 664, 704, 744, 784, 824, 864, 904, 944, 984, 1024, 0, 4, 8, 12, 16, 20,+ 24, 28, 32, 36, 40, 48, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144,+ 160, 176, 196, 216, 240, 264, 292, 320, 352, 384, 416, 448, 480, 512, 544, 576,+ 608, 640, 672, 704, 736, 768, 800, 832, 864, 896, 928, 1024, 0, 4, 8, 12,+ 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72, 80, 88, 96, 108, 120,+ 132, 144, 160, 176, 196, 216, 240, 264, 292, 320, 352, 384, 416, 448, 480, 512,+ 544, 576, 608, 640, 672, 704, 736, 768, 800, 832, 864, 896, 928, 960, 992, 1024,+ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 76,+ 84, 92, 100, 108, 116, 124, 136, 148, 160, 172, 188, 204, 220, 240, 260, 284,+ 308, 336, 364, 396, 432, 468, 508, 552, 600, 652, 704, 768, 832, 896, 960, 1024,+ 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 100, 112, 124, 136,+ 148, 160, 172, 184, 196, 212, 228, 244, 260, 280, 300, 320, 344, 368, 396, 424,+ 456, 492, 532, 572, 616, 664, 716, 772, 832, 896, 960, 1024, 0, 12, 24, 36,+ 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 172, 188, 204, 220, 236, 252,+ 268, 288, 308, 328, 348, 372, 396, 420, 448, 476, 508, 544, 580, 620, 664, 712,+ 764, 820, 880, 944, 1024,+};+static const uint16_t AAC_SWB_1024_AT[13] = {+ 0, 0, 42, 90, 90, 140, 192, 192, 240, 240, 240, 284, 284,+};+static const uint8_t AAC_NUM_SWB_1024[13] = {+ 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40, 40,+};+static const uint16_t AAC_SWB_960[269] = {+ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64,+ 72, 80, 88, 96, 108, 120, 132, 144, 156, 172, 188, 212, 240, 276, 320, 384,+ 448, 512, 576, 640, 704, 768, 832, 896, 960, 0, 4, 8, 12, 16, 20, 24,+ 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 100, 112, 124, 140,+ 156, 172, 192, 216, 240, 268, 304, 344, 384, 424, 464, 504, 544, 584, 624, 664,+ 704, 744, 784, 824, 864, 904, 944, 960, 0, 4, 8, 12, 16, 20, 24, 28,+ 32, 36, 40, 48, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 160, 176,+ 196, 216, 240, 264, 292, 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640,+ 672, 704, 736, 768, 800, 832, 864, 896, 928, 960, 0, 4, 8, 12, 16, 20,+ 24, 28, 32, 36, 40, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124,+ 136, 148, 160, 172, 188, 204, 220, 240, 260, 284, 308, 336, 364, 396, 432, 468,+ 508, 552, 600, 652, 704, 768, 832, 896, 960, 0, 8, 16, 24, 32, 40, 48,+ 56, 64, 72, 80, 88, 100, 112, 124, 136, 148, 160, 172, 184, 196, 212, 228,+ 244, 260, 280, 300, 320, 344, 368, 396, 424, 456, 492, 532, 572, 616, 664, 716,+ 772, 832, 896, 960, 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132,+ 144, 156, 172, 188, 204, 220, 236, 252, 268, 288, 308, 328, 348, 372, 396, 420,+ 448, 476, 508, 544, 580, 620, 664, 712, 764, 820, 880, 944, 960,+};+static const uint16_t AAC_SWB_960_AT[13] = {+ 0, 0, 41, 88, 88, 88, 138, 138, 185, 185, 185, 228, 228,+};+static const uint8_t AAC_NUM_SWB_960[13] = {+ 40, 40, 46, 49, 49, 49, 46, 46, 42, 42, 42, 40, 40,+};+static const uint16_t AAC_SWB_128[76] = {+ 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128, 0, 4, 8,+ 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128, 0, 4, 8, 12,+ 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128, 0, 4, 8, 12,+ 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128, 0, 4, 8, 12,+ 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128,+};+static const uint16_t AAC_SWB_128_AT[13] = {+ 0, 0, 0, 13, 13, 13, 28, 28, 44, 44, 44, 60, 60,+};+static const uint8_t AAC_NUM_SWB_128[13] = {+ 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15,+};+static const uint16_t AAC_SWB_120[76] = {+ 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 120, 0, 4, 8,+ 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 120, 0, 4, 8, 12,+ 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 120, 0, 4, 8, 12,+ 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 120, 0, 4, 8, 12,+ 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 120,+};+static const uint16_t AAC_SWB_120_AT[13] = {+ 0, 0, 0, 13, 13, 13, 28, 28, 44, 44, 44, 60, 60,+};+static const uint8_t AAC_NUM_SWB_120[13] = {+ 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15,+};+static const uint8_t AAC_TNS_MAX_BANDS_1024[13] = {+ 31, 31, 34, 40, 42, 51, 46, 46, 42, 42, 42, 39, 39,+};+static const uint8_t AAC_TNS_MAX_BANDS_128[13] = {+ 9, 9, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,+};+static const uint8_t AAC_PRED_SFB_MAX[13] = {+ 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34, 34,+};+static const float AAC_TNS_1_3[4] = {+ 0.0f, -0.43388373f, 0.64278758f, 0.34202015f,+};+static const float AAC_TNS_0_3[8] = {+ 0.0f, -0.43388373f, -0.7818315f, -0.9749279f,+ 0.98480773f, 0.86602539f, 0.64278758f, 0.34202015f,+};+static const float AAC_TNS_1_4[8] = {+ 0.0f, -0.2079117f, -0.40673664f, -0.58778524f,+ 0.67369562f, 0.52643216f, 0.36124167f, 0.18374951f,+};+static const float AAC_TNS_0_4[16] = {+ 0.0f, -0.2079117f, -0.40673664f, -0.58778524f,+ -0.74314481f, -0.86602539f, -0.95105654f, -0.99452192f,+ 0.99573416f, 0.96182561f, 0.8951633f, 0.7980172f,+ 0.67369562f, 0.52643216f, 0.36124167f, 0.18374951f,+};+static const float AAC_LTP_COEF[8] = {+ 0.570829f, 0.696616f, 0.813004f, 0.911304f, 0.9849f, 1.067894f, 1.194601f, 1.369533f,+};+static const uint8_t SBR_HUFF_SYM[604] = {+ 60, 59, 61, 58, 62, 57, 63, 56, 64, 55, 65, 54, 66, 53, 67, 52, 51, 68, 50, 69,+ 49, 70, 48, 47, 71, 46, 72, 45, 44, 73, 41, 42, 43, 74, 36, 40, 76, 34, 39, 75,+ 37, 35, 38, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,+ 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 77, 78, 79,+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,+ 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,+ 120, 60, 59, 61, 58, 57, 62, 56, 63, 55, 64, 54, 65, 53, 66, 52, 67, 51, 68, 50,+ 69, 49, 70, 71, 48, 72, 47, 73, 74, 46, 45, 75, 76, 77, 44, 43, 42, 41, 78, 79,+ 40, 39, 80, 81, 36, 37, 38, 34, 32, 82, 83, 85, 19, 35, 86, 87, 30, 33, 84, 88,+ 104, 9, 14, 16, 17, 23, 27, 29, 31, 90, 97, 102, 107, 108, 0, 1, 2, 3, 4, 5,+ 6, 7, 8, 10, 11, 12, 13, 15, 18, 20, 21, 22, 24, 25, 26, 28, 89, 91, 92, 93,+ 94, 95, 96, 98, 99, 100, 101, 103, 105, 106, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,+ 119, 120, 24, 25, 23, 26, 22, 27, 21, 28, 20, 19, 29, 18, 30, 31, 17, 32, 0, 1,+ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 33, 34, 35, 36, 37,+ 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 24, 23, 25, 22, 26, 27, 21, 20, 28,+ 19, 29, 18, 30, 17, 31, 32, 15, 16, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,+ 11, 12, 13, 14, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,+ 31, 30, 32, 29, 33, 28, 34, 27, 35, 26, 36, 25, 24, 37, 23, 38, 22, 21, 39, 40,+ 41, 18, 20, 19, 17, 42, 43, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,+ 13, 14, 15, 16, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,+ 60, 61, 62, 31, 30, 32, 29, 33, 28, 34, 27, 35, 26, 36, 25, 37, 24, 38, 23, 39,+ 40, 22, 21, 41, 42, 20, 19, 43, 44, 18, 16, 45, 46, 17, 49, 13, 7, 12, 47, 48,+ 9, 10, 15, 51, 52, 53, 56, 8, 11, 55, 0, 1, 2, 3, 4, 5, 6, 14, 50, 54,+ 57, 58, 59, 60, 61, 62, 12, 13, 11, 10, 14, 15, 9, 8, 16, 7, 0, 1, 2, 3,+ 4, 5, 6, 17, 18, 19, 20, 21, 22, 23, 24, 12, 11, 13, 10, 14, 15, 9, 8, 16,+ 7, 17, 18, 0, 1, 2, 3, 4, 5, 6, 19, 20, 21, 22, 23, 24, 31, 32, 30, 29,+ 33, 28, 34, 27, 35, 26, 36, 42, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,+ 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 37, 38, 39, 40, 41, 43,+ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 12,+ 11, 13, 10, 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 18, 19, 20,+ 21, 22, 23, 24,+};+static const uint32_t SBR_HUFF_CODE[604] = {+ 0x0, 0x1, 0x4, 0x5, 0xc, 0xd, 0x1c, 0x1d,+ 0x3c, 0x3d, 0x7c, 0x7d, 0xfc, 0xfd, 0x1fc, 0x1fd,+ 0x3fc, 0x3fd, 0x7fc, 0xffa, 0xffb, 0x1ff8, 0x1ff9, 0x1ffa,+ 0x3ff6, 0x3ff7, 0x3ff8, 0x3ff9, 0x7ff4, 0x7ff5, 0xffec, 0xffed,+ 0xffee, 0xffef, 0xfff0, 0xfff1, 0xfff2, 0x1ffe6, 0x1ffe7, 0x1ffe8,+ 0x1ffe9, 0x3ffd4, 0x3ffd5, 0x3ffd6, 0x3ffd7, 0x3ffd8, 0x3ffd9, 0x3ffda,+ 0x3ffdb, 0x7ffb8, 0x7ffb9, 0x7ffba, 0x7ffbb, 0x7ffbc, 0x7ffbd, 0x7ffbe,+ 0x7ffbf, 0x7ffc0, 0x7ffc1, 0x7ffc2, 0x7ffc3, 0x7ffc4, 0x7ffc5, 0x7ffc6,+ 0x7ffc7, 0x7ffc8, 0x7ffc9, 0x7ffca, 0x7ffcb, 0x7ffcc, 0x7ffcd, 0x7ffce,+ 0x7ffcf, 0x7ffd0, 0x7ffd1, 0x7ffd2, 0x7ffd3, 0x7ffd4, 0x7ffd5, 0x7ffd6,+ 0x7ffd7, 0x7ffd8, 0x7ffd9, 0x7ffda, 0x7ffdb, 0x7ffdc, 0x7ffdd, 0x7ffde,+ 0x7ffdf, 0x7ffe0, 0x7ffe1, 0x7ffe2, 0x7ffe3, 0x7ffe4, 0x7ffe5, 0x7ffe6,+ 0x7ffe7, 0x7ffe8, 0x7ffe9, 0x7ffea, 0x7ffeb, 0x7ffec, 0x7ffed, 0x7ffee,+ 0x7ffef, 0x7fff0, 0x7fff1, 0x7fff2, 0x7fff3, 0x7fff4, 0x7fff5, 0x7fff6,+ 0x7fff7, 0x7fff8, 0x7fff9, 0x7fffa, 0x7fffb, 0x7fffc, 0x7fffd, 0x7fffe,+ 0x7ffff, 0x0, 0x1, 0x4, 0x5, 0xc, 0xd, 0x1c,+ 0x1d, 0x3c, 0x3d, 0x7c, 0xfa, 0xfb, 0xfc, 0x1fa,+ 0x1fb, 0x1fc, 0x3fa, 0x3fb, 0x7f8, 0x7f9, 0x7fa, 0x7fb,+ 0xff8, 0xff9, 0xffa, 0xffb, 0x1ff8, 0x1ff9, 0x1ffa, 0x1ffb,+ 0x3ff8, 0x3ff9, 0x3ffa, 0x7ff6, 0x7ff7, 0xfff0, 0xfff1, 0xfff2,+ 0xfff3, 0xfff4, 0x1ffea, 0x1ffeb, 0x1ffec, 0x1ffed, 0x1ffee, 0x1ffef,+ 0x3ffe0, 0x3ffe1, 0x3ffe2, 0x3ffe3, 0x3ffe4, 0x3ffe5, 0x3ffe6, 0x3ffe7,+ 0x3ffe8, 0x3ffe9, 0x3ffea, 0x3ffeb, 0x3ffec, 0x7ffda, 0x7ffdb, 0x7ffdc,+ 0x7ffdd, 0x7ffde, 0x7ffdf, 0x7ffe0, 0x7ffe1, 0x7ffe2, 0x7ffe3, 0x7ffe4,+ 0x7ffe5, 0x7ffe6, 0x7ffe7, 0x7ffe8, 0xfffd2, 0xfffd3, 0xfffd4, 0xfffd5,+ 0xfffd6, 0xfffd7, 0xfffd8, 0xfffd9, 0xfffda, 0xfffdb, 0xfffdc, 0xfffdd,+ 0xfffde, 0xfffdf, 0xfffe0, 0xfffe1, 0xfffe2, 0xfffe3, 0xfffe4, 0xfffe5,+ 0xfffe6, 0xfffe7, 0xfffe8, 0xfffe9, 0xfffea, 0xfffeb, 0xfffec, 0xfffed,+ 0xfffee, 0xfffef, 0xffff0, 0xffff1, 0xffff2, 0xffff3, 0xffff4, 0xffff5,+ 0xffff6, 0xffff7, 0xffff8, 0xffff9, 0xffffa, 0xffffb, 0xffffc, 0xffffd,+ 0xffffe, 0xfffff, 0x0, 0x2, 0x6, 0xe, 0x1e, 0x3e,+ 0x7e, 0xfe, 0x1fe, 0x7fc, 0x7fd, 0xffc, 0xffd, 0x7ff0,+ 0xffe2, 0xffe3, 0xffe4, 0xffe5, 0xffe6, 0xffe7, 0xffe8, 0xffe9,+ 0xffea, 0xffeb, 0xffec, 0xffed, 0xffee, 0xffef, 0xfff0, 0xfff1,+ 0xfff2, 0xfff3, 0xfff4, 0xfff5, 0xfff6, 0xfff7, 0xfff8, 0xfff9,+ 0xfffa, 0x1fff6, 0x1fff7, 0x1fff8, 0x1fff9, 0x1fffa, 0x1fffb, 0x1fffc,+ 0x1fffd, 0x1fffe, 0x1ffff, 0x0, 0x2, 0x6, 0xe, 0x1e,+ 0x3e, 0x7e, 0xfe, 0x1fe, 0x7fc, 0x7fd, 0x7fe, 0xffe,+ 0x3ffc, 0x7ffa, 0xfff6, 0xfff7, 0x1fff0, 0x3ffe2, 0x3ffe3, 0x3ffe4,+ 0x3ffe5, 0x3ffe6, 0x3ffe7, 0x3ffe8, 0x3ffe9, 0x3ffea, 0x3ffeb, 0x3ffec,+ 0x3ffed, 0x3ffee, 0x3ffef, 0x3fff0, 0x3fff1, 0x3fff2, 0x3fff3, 0x3fff4,+ 0x3fff5, 0x3fff6, 0x3fff7, 0x3fff8, 0x3fff9, 0x3fffa, 0x3fffb, 0x3fffc,+ 0x3fffd, 0x3fffe, 0x7fffe, 0x7ffff, 0x0, 0x2, 0x6, 0xe,+ 0x1e, 0x3e, 0x7e, 0xfe, 0x1fe, 0x7fc, 0x7fd, 0xffc,+ 0x1ffa, 0x1ffb, 0x3ff8, 0x3ff9, 0x3ffa, 0x3ffb, 0x3ffc, 0x7ffa,+ 0xfff6, 0xfff7, 0xfff8, 0xfff9, 0x1fff4, 0x1fff5, 0x3ffec, 0x3ffed,+ 0x3ffee, 0x7ffde, 0x7ffdf, 0x7ffe0, 0x7ffe1, 0x7ffe2, 0x7ffe3, 0x7ffe4,+ 0x7ffe5, 0x7ffe6, 0x7ffe7, 0x7ffe8, 0x7ffe9, 0x7ffea, 0x7ffeb, 0x7ffec,+ 0x7ffed, 0x7ffee, 0x7ffef, 0x7fff0, 0x7fff1, 0x7fff2, 0x7fff3, 0x7fff4,+ 0x7fff5, 0x7fff6, 0x7fff7, 0x7fff8, 0x7fff9, 0x7fffa, 0x7fffb, 0x7fffc,+ 0x7fffd, 0x7fffe, 0x7ffff, 0x0, 0x2, 0x6, 0xe, 0x1e,+ 0x3e, 0xfc, 0xfd, 0x1fc, 0x1fd, 0x3fc, 0x3fd, 0x7fc,+ 0x7fd, 0xffc, 0xffd, 0x1ffc, 0x3ffa, 0x3ffb, 0x7ff8, 0x7ff9,+ 0x7ffa, 0xfff6, 0xfff7, 0xfff8, 0xfff9, 0x1fff4, 0x1fff5, 0x1fff6,+ 0x1fff7, 0x3fff0, 0x3fff1, 0x3fff2, 0x3fff3, 0x3fff4, 0x3fff5, 0x3fff6,+ 0x7ffee, 0x7ffef, 0x7fff0, 0x7fff1, 0x7fff2, 0x7fff3, 0x7fff4, 0x7fff5,+ 0x7fff6, 0x7fff7, 0xffff0, 0xffff1, 0xffff2, 0xffff3, 0xffff4, 0xffff5,+ 0xffff6, 0xffff7, 0xffff8, 0xffff9, 0xffffa, 0xffffb, 0xffffc, 0xffffd,+ 0xffffe, 0xfffff, 0x0, 0x2, 0x6, 0xe, 0x1e, 0x3e,+ 0x7e, 0xfe, 0x1fe, 0xff8, 0x1ff2, 0x1ff3, 0x1ff4, 0x1ff5,+ 0x1ff6, 0x1ff7, 0x1ff8, 0x1ff9, 0x1ffa, 0x1ffb, 0x1ffc, 0x1ffd,+ 0x1ffe, 0x3ffe, 0x3fff, 0x0, 0x2, 0x6, 0xe, 0x1e,+ 0x3e, 0x7e, 0xfe, 0x1fe, 0x7fc, 0xffa, 0x1ff6, 0x1ff7,+ 0x1ff8, 0x1ff9, 0x1ffa, 0x1ffb, 0x3ff8, 0x3ff9, 0x3ffa, 0x3ffb,+ 0x3ffc, 0x3ffd, 0x3ffe, 0x3fff, 0x0, 0x2, 0x6, 0xe,+ 0x1e, 0x3e, 0xfc, 0xfd, 0x3f8, 0x7f2, 0x1fcc, 0x1fcd,+ 0x1fce, 0x1fcf, 0x1fd0, 0x1fd1, 0x1fd2, 0x1fd3, 0x1fd4, 0x1fd5,+ 0x1fd6, 0x1fd7, 0x1fd8, 0x1fd9, 0x1fda, 0x1fdb, 0x1fdc, 0x1fdd,+ 0x1fde, 0x1fdf, 0x1fe0, 0x1fe1, 0x1fe2, 0x1fe3, 0x1fe4, 0x1fe5,+ 0x1fe6, 0x1fe7, 0x1fe8, 0x1fe9, 0x1fea, 0x1feb, 0x1fec, 0x1fed,+ 0x1fee, 0x1fef, 0x1ff0, 0x1ff1, 0x1ff2, 0x1ff3, 0x1ff4, 0x1ff5,+ 0x1ff6, 0x1ff7, 0x1ff8, 0x1ff9, 0x1ffa, 0x1ffb, 0x1ffc, 0x1ffd,+ 0x1ffe, 0x3ffe, 0x3fff, 0x0, 0x2, 0x6, 0x1c, 0x3a,+ 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3,+ 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,+ 0xfc, 0xfd, 0xfe, 0xff,+};+static const uint8_t SBR_HUFF_LEN[604] = {+ 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 12,+ 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17,+ 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,+ 19, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 9, 9, 9, 10, 10,+ 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16, 16,+ 16, 16, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,+ 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20,+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,+ 20, 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 11, 12, 12, 15, 16, 16, 16, 16,+ 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,+ 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 1, 2, 3, 4, 5, 6, 7, 8, 9,+ 11, 11, 11, 12, 14, 15, 16, 16, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19,+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 11, 12, 13, 13, 14, 14, 14, 14, 14, 15,+ 16, 16, 16, 16, 17, 17, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,+ 19, 19, 19, 1, 2, 3, 4, 5, 6, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13,+ 14, 14, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18,+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,+ 20, 20, 20, 20, 20, 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 13, 13, 13,+ 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9,+ 11, 12, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 1, 2, 3, 4,+ 5, 6, 8, 8, 10, 11, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,+ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,+ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 1,+ 2, 3, 5, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,+ 8, 8, 8, 8,+};+static const uint16_t SBR_HUFF_AT[11] = {+ 0, 121, 242, 291, 340, 403, 466, 491, 516, 579, 604,+};+static const int8_t SBR_HUFF_OFFSET[10] = {+ -60, -60, -24, -24, -31, -31, -12, -12, -31, -12,+};+static const int8_t SBR_OFFSET[96] = {+ -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7,+ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13,+ -5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16,+ -6, -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16,+ -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20,+ -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24,+};+static const float SBR_QMF_WINDOW[640] = {+ 0.0000000000f, -0.0005525286f, -0.0005617692f, -0.0004947518f, -0.0004875227f, -0.0004893791f,+ -0.0005040714f, -0.0005226564f, -0.0005466565f, -0.0005677802f, -0.0005870930f, -0.0006132747f,+ -0.0006312493f, -0.0006540333f, -0.0006777690f, -0.0006941614f, -0.0007157736f, -0.0007255043f,+ -0.0007440941f, -0.0007490598f, -0.0007681371f, -0.0007724848f, -0.0007834332f, -0.0007779869f,+ -0.0007803664f, -0.0007801449f, -0.0007757977f, -0.0007630793f, -0.0007530001f, -0.0007319357f,+ -0.0007215391f, -0.0006917937f, -0.0006650415f, -0.0006341594f, -0.0005946118f, -0.0005564576f,+ -0.0005145572f, -0.0004606325f, -0.0004095121f, -0.0003501175f, -0.0002896981f, -0.0002098337f,+ -0.0001446380f, -0.0000617334f, 0.0000134949f, 0.0001094383f, 0.0002043017f, 0.0002949531f,+ 0.0004026540f, 0.0005107388f, 0.0006239376f, 0.0007458025f, 0.0008608443f, 0.0009885988f,+ 0.0011250155f, 0.0012577884f, 0.0013902494f, 0.0015443219f, 0.0016868083f, 0.0018348265f,+ 0.0019841140f, 0.0021461583f, 0.0023017254f, 0.0024625616f, 0.0026201758f, 0.0027870464f,+ 0.0029469447f, 0.0031125420f, 0.0032739613f, 0.0034418874f, 0.0036008268f, 0.0037603922f,+ 0.0039207432f, 0.0040819753f, 0.0042264269f, 0.0043730719f, 0.0045209852f, 0.0046606460f,+ 0.0047932560f, 0.0049137603f, 0.0050393022f, 0.0051407353f, 0.0052461166f, 0.0053471681f,+ 0.0054196775f, 0.0054876040f, 0.0055475714f, 0.0055938023f, 0.0056220643f, 0.0056455196f,+ 0.0056389199f, 0.0056266114f, 0.0055917128f, 0.0055404363f, 0.0054753783f, 0.0053838975f,+ 0.0052715758f, 0.0051382275f, 0.0049839687f, 0.0048109469f, 0.0046039530f, 0.0043801861f,+ 0.0041251642f, 0.0038456408f, 0.0035401246f, 0.0032091885f, 0.0028446757f, 0.0024508540f,+ 0.0020274176f, 0.0015784682f, 0.0010902329f, 0.0005832264f, 0.0000276045f, -0.0005464280f,+ -0.0011568135f, -0.0018039472f, -0.0024826723f, -0.0031933778f, -0.0039401124f, -0.0047222596f,+ -0.0055337211f, -0.0063792293f, -0.0072615816f, -0.0081798233f, -0.0091325329f, -0.0101150215f,+ -0.0111315548f, -0.0121849995f, 0.0132718220f, 0.0143904666f, 0.0155405553f, 0.0167324712f,+ 0.0179433381f, 0.0191872431f, 0.0204531793f, 0.0217467550f, 0.0230680169f, 0.0244160992f,+ 0.0257875847f, 0.0271859429f, 0.0286072173f, 0.0300502657f, 0.0315017608f, 0.0329754081f,+ 0.0344620948f, 0.0359697560f, 0.0374812850f, 0.0390053679f, 0.0405349170f, 0.0420649094f,+ 0.0436097542f, 0.0451488405f, 0.0466843027f, 0.0482165720f, 0.0497385755f, 0.0512556155f,+ 0.0527630746f, 0.0542452768f, 0.0557173648f, 0.0571616450f, 0.0585915683f, 0.0599837480f,+ 0.0613455171f, 0.0626857808f, 0.0639715898f, 0.0652247106f, 0.0664367512f, 0.0676075985f,+ 0.0687043828f, 0.0697630244f, 0.0707628710f, 0.0717002673f, 0.0725682583f, 0.0733620255f,+ 0.0741003642f, 0.0747452558f, 0.0753137336f, 0.0758008358f, 0.0761992479f, 0.0764992170f,+ 0.0767093490f, 0.0768173975f, 0.0768230011f, 0.0767204924f, 0.0765050718f, 0.0761748321f,+ 0.0757305756f, 0.0751576255f, 0.0744664394f, 0.0736406005f, 0.0726774642f, 0.0715826364f,+ 0.0703533073f, 0.0689664013f, 0.0674525021f, 0.0657690668f, 0.0639444805f, 0.0619602779f,+ 0.0598166570f, 0.0575152691f, 0.0550460034f, 0.0524093821f, 0.0495978676f, 0.0466303305f,+ 0.0434768782f, 0.0401458278f, 0.0366418116f, 0.0329583930f, 0.0290824006f, 0.0250307561f,+ 0.0207997072f, 0.0163701258f, 0.0117623832f, 0.0069636862f, 0.0019765601f, -0.0032086896f,+ -0.0085711749f, -0.0141288827f, -0.0198834129f, -0.0258227288f, -0.0319531274f, -0.0382776572f,+ -0.0447806821f, -0.0514804176f, -0.0583705326f, -0.0654409853f, -0.0726943300f, -0.0801372934f,+ -0.0877547536f, -0.0955533352f, -0.1035329531f, -0.1116826931f, -0.1200077984f, -0.1285002850f,+ -0.1371551761f, -0.1459766491f, -0.1549607071f, -0.1640958855f, -0.1733808172f, -0.1828172548f,+ -0.1923966745f, -0.2021250176f, -0.2119735853f, -0.2219652696f, -0.2320690870f, -0.2423016884f,+ -0.2526480309f, -0.2631053299f, -0.2736634040f, -0.2843214189f, -0.2950716717f, -0.3059098575f,+ -0.3168278913f, -0.3278113727f, -0.3388722693f, -0.3499914122f, 0.3611589903f, 0.3723795546f,+ 0.3836350013f, 0.3949211761f, 0.4062317676f, 0.4175696896f, 0.4289119920f, 0.4402553754f,+ 0.4515996535f, 0.4629308085f, 0.4742453214f, 0.4855253091f, 0.4967708254f, 0.5079817500f,+ 0.5191234970f, 0.5302240895f, 0.5412553448f, 0.5522051258f, 0.5630789140f, 0.5738524131f,+ 0.5845403235f, 0.5951123086f, 0.6055783538f, 0.6159109932f, 0.6261242695f, 0.6361980107f,+ 0.6461269695f, 0.6559016302f, 0.6655139880f, 0.6749663190f, 0.6842353293f, 0.6933282376f,+ 0.7022388719f, 0.7109410426f, 0.7194462634f, 0.7277448900f, 0.7358211758f, 0.7436827863f,+ 0.7513137456f, 0.7587080760f, 0.7658674865f, 0.7727780881f, 0.7794287519f, 0.7858353120f,+ 0.7919735841f, 0.7978466413f, 0.8034485751f, 0.8087695004f, 0.8138191270f, 0.8185776004f,+ 0.8230419890f, 0.8272275347f, 0.8311038457f, 0.8346937361f, 0.8379717337f, 0.8409541392f,+ 0.8436238281f, 0.8459818469f, 0.8480315777f, 0.8497805198f, 0.8511971524f, 0.8523047035f,+ 0.8531020949f, 0.8535720573f, 0.8537385600f, 0.8535720573f, 0.8531020949f, 0.8523047035f,+ 0.8511971524f, 0.8497805198f, 0.8480315777f, 0.8459818469f, 0.8436238281f, 0.8409541392f,+ 0.8379717337f, 0.8346937361f, 0.8311038457f, 0.8272275347f, 0.8230419890f, 0.8185776004f,+ 0.8138191270f, 0.8087695004f, 0.8034485751f, 0.7978466413f, 0.7919735841f, 0.7858353120f,+ 0.7794287519f, 0.7727780881f, 0.7658674865f, 0.7587080760f, 0.7513137456f, 0.7436827863f,+ 0.7358211758f, 0.7277448900f, 0.7194462634f, 0.7109410426f, 0.7022388719f, 0.6933282376f,+ 0.6842353293f, 0.6749663190f, 0.6655139880f, 0.6559016302f, 0.6461269695f, 0.6361980107f,+ 0.6261242695f, 0.6159109932f, 0.6055783538f, 0.5951123086f, 0.5845403235f, 0.5738524131f,+ 0.5630789140f, 0.5522051258f, 0.5412553448f, 0.5302240895f, 0.5191234970f, 0.5079817500f,+ 0.4967708254f, 0.4855253091f, 0.4742453214f, 0.4629308085f, 0.4515996535f, 0.4402553754f,+ 0.4289119920f, 0.4175696896f, 0.4062317676f, 0.3949211761f, 0.3836350013f, 0.3723795546f,+ -0.3611589903f, -0.3499914122f, -0.3388722693f, -0.3278113727f, -0.3168278913f, -0.3059098575f,+ -0.2950716717f, -0.2843214189f, -0.2736634040f, -0.2631053299f, -0.2526480309f, -0.2423016884f,+ -0.2320690870f, -0.2219652696f, -0.2119735853f, -0.2021250176f, -0.1923966745f, -0.1828172548f,+ -0.1733808172f, -0.1640958855f, -0.1549607071f, -0.1459766491f, -0.1371551761f, -0.1285002850f,+ -0.1200077984f, -0.1116826931f, -0.1035329531f, -0.0955533352f, -0.0877547536f, -0.0801372934f,+ -0.0726943300f, -0.0654409853f, -0.0583705326f, -0.0514804176f, -0.0447806821f, -0.0382776572f,+ -0.0319531274f, -0.0258227288f, -0.0198834129f, -0.0141288827f, -0.0085711749f, -0.0032086896f,+ 0.0019765601f, 0.0069636862f, 0.0117623832f, 0.0163701258f, 0.0207997072f, 0.0250307561f,+ 0.0290824006f, 0.0329583930f, 0.0366418116f, 0.0401458278f, 0.0434768782f, 0.0466303305f,+ 0.0495978676f, 0.0524093821f, 0.0550460034f, 0.0575152691f, 0.0598166570f, 0.0619602779f,+ 0.0639444805f, 0.0657690668f, 0.0674525021f, 0.0689664013f, 0.0703533073f, 0.0715826364f,+ 0.0726774642f, 0.0736406005f, 0.0744664394f, 0.0751576255f, 0.0757305756f, 0.0761748321f,+ 0.0765050718f, 0.0767204924f, 0.0768230011f, 0.0768173975f, 0.0767093490f, 0.0764992170f,+ 0.0761992479f, 0.0758008358f, 0.0753137336f, 0.0747452558f, 0.0741003642f, 0.0733620255f,+ 0.0725682583f, 0.0717002673f, 0.0707628710f, 0.0697630244f, 0.0687043828f, 0.0676075985f,+ 0.0664367512f, 0.0652247106f, 0.0639715898f, 0.0626857808f, 0.0613455171f, 0.0599837480f,+ 0.0585915683f, 0.0571616450f, 0.0557173648f, 0.0542452768f, 0.0527630746f, 0.0512556155f,+ 0.0497385755f, 0.0482165720f, 0.0466843027f, 0.0451488405f, 0.0436097542f, 0.0420649094f,+ 0.0405349170f, 0.0390053679f, 0.0374812850f, 0.0359697560f, 0.0344620948f, 0.0329754081f,+ 0.0315017608f, 0.0300502657f, 0.0286072173f, 0.0271859429f, 0.0257875847f, 0.0244160992f,+ 0.0230680169f, 0.0217467550f, 0.0204531793f, 0.0191872431f, 0.0179433381f, 0.0167324712f,+ 0.0155405553f, 0.0143904666f, -0.0132718220f, -0.0121849995f, -0.0111315548f, -0.0101150215f,+ -0.0091325329f, -0.0081798233f, -0.0072615816f, -0.0063792293f, -0.0055337211f, -0.0047222596f,+ -0.0039401124f, -0.0031933778f, -0.0024826723f, -0.0018039472f, -0.0011568135f, -0.0005464280f,+ 0.0000276045f, 0.0005832264f, 0.0010902329f, 0.0015784682f, 0.0020274176f, 0.0024508540f,+ 0.0028446757f, 0.0032091885f, 0.0035401246f, 0.0038456408f, 0.0041251642f, 0.0043801861f,+ 0.0046039530f, 0.0048109469f, 0.0049839687f, 0.0051382275f, 0.0052715758f, 0.0053838975f,+ 0.0054753783f, 0.0055404363f, 0.0055917128f, 0.0056266114f, 0.0056389199f, 0.0056455196f,+ 0.0056220643f, 0.0055938023f, 0.0055475714f, 0.0054876040f, 0.0054196775f, 0.0053471681f,+ 0.0052461166f, 0.0051407353f, 0.0050393022f, 0.0049137603f, 0.0047932560f, 0.0046606460f,+ 0.0045209852f, 0.0043730719f, 0.0042264269f, 0.0040819753f, 0.0039207432f, 0.0037603922f,+ 0.0036008268f, 0.0034418874f, 0.0032739613f, 0.0031125420f, 0.0029469447f, 0.0027870464f,+ 0.0026201758f, 0.0024625616f, 0.0023017254f, 0.0021461583f, 0.0019841140f, 0.0018348265f,+ 0.0016868083f, 0.0015443219f, 0.0013902494f, 0.0012577884f, 0.0011250155f, 0.0009885988f,+ 0.0008608443f, 0.0007458025f, 0.0006239376f, 0.0005107388f, 0.0004026540f, 0.0002949531f,+ 0.0002043017f, 0.0001094383f, 0.0000134949f, -0.0000617334f, -0.0001446380f, -0.0002098337f,+ -0.0002896981f, -0.0003501175f, -0.0004095121f, -0.0004606325f, -0.0005145572f, -0.0005564576f,+ -0.0005946118f, -0.0006341594f, -0.0006650415f, -0.0006917937f, -0.0007215391f, -0.0007319357f,+ -0.0007530001f, -0.0007630793f, -0.0007757977f, -0.0007801449f, -0.0007803664f, -0.0007779869f,+ -0.0007834332f, -0.0007724848f, -0.0007681371f, -0.0007490598f, -0.0007440941f, -0.0007255043f,+ -0.0007157736f, -0.0006941614f, -0.0006777690f, -0.0006540333f, -0.0006312493f, -0.0006132747f,+ -0.0005870930f, -0.0005677802f, -0.0005466565f, -0.0005226564f, -0.0005040714f, -0.0004893791f,+ -0.0004875227f, -0.0004947518f, -0.0005617692f, -0.0005525286f,+};+static const float SBR_NOISE[1024] = {+ -0.99948153278296f, -0.59483417516607f, 0.97113454393991f, -0.67528515225647f, 0.14130051758487f, -0.95090983575689f,+ -0.47005496701697f, -0.37340549728647f, 0.80705063769351f, 0.29653668284408f, -0.38981478896926f, 0.89572605717087f,+ -0.01053049862020f, -0.66959058036166f, -0.91266367957293f, -0.11522938140034f, 0.54840422910309f, 0.75221367176302f,+ 0.40009252867955f, -0.98929400334421f, -0.99867974711855f, -0.88147068645358f, -0.95531076805040f, 0.90908757154593f,+ -0.45725933317144f, -0.56716323646760f, -0.72929675029275f, -0.98008272727324f, 0.75622801399036f, 0.20950329995549f,+ 0.07069442601050f, -0.78247898470706f, 0.74496252926055f, -0.91169004445807f, -0.96440182703856f, -0.94739918296622f,+ 0.30424629369539f, -0.49438267012479f, 0.66565033746925f, 0.64652935542491f, 0.91697008020594f, 0.17514097332009f,+ -0.70774918760427f, 0.52548653416543f, -0.70051415345560f, -0.45340028808763f, -0.99496513054797f, -0.90071908066973f,+ 0.98164490790123f, -0.77463155528697f, -0.54671580548181f, -0.02570928536004f, -0.01689629065389f, 0.00287506445732f,+ -0.86110349531986f, 0.42548583726477f, -0.98892980586032f, -0.87881132267556f, 0.51756627678691f, 0.66926784710139f,+ -0.99635026409640f, -0.58107730574765f, -0.99969370862163f, 0.98369989360250f, 0.55266258627194f, 0.59449057465591f,+ 0.34581177741673f, 0.94879421061866f, 0.62664209577999f, -0.74402970906471f, -0.77149701404973f, -0.33883658042801f,+ -0.91592244254432f, 0.03687901376713f, -0.76285492357887f, -0.91371867919124f, 0.79788337195331f, -0.93180971199849f,+ 0.54473080610200f, -0.11919206037186f, -0.85639281671058f, 0.42429854760451f, -0.92882402971423f, 0.27871809078609f,+ -0.11708371046774f, -0.99800843444966f, 0.21356749817493f, -0.90716295627033f, -0.76191692573909f, 0.99768118356265f,+ 0.98111043100884f, -0.95854459734407f, -0.85913269895572f, 0.95766566168880f, -0.93307242253692f, 0.49431757696466f,+ 0.30485754879632f, -0.70540034357529f, 0.85289650925190f, 0.46766131791044f, 0.91328082618125f, -0.99839597361769f,+ -0.05890199924154f, 0.70741827819497f, 0.28398686150148f, 0.34633555702188f, 0.95258164539612f, -0.54893416026939f,+ -0.78566324168507f, -0.75568541079691f, -0.95789495447877f, -0.20423194696966f, 0.82411158711197f, 0.96654618432562f,+ -0.65185446735885f, -0.88734990773289f, -0.93643603134666f, 0.99870790442385f, 0.91427159529618f, -0.98290505544444f,+ -0.70395684036886f, 0.58796798221039f, 0.00563771969365f, 0.61768196727244f, 0.89065051931895f, 0.52783352697585f,+ -0.68683707712762f, 0.80806944710339f, 0.72165342518718f, -0.69259857349564f, -0.62928247730667f, 0.13627037407335f,+ 0.29938434065514f, -0.46051329682246f, -0.91781958879280f, -0.74012716684186f, 0.99298717043688f, 0.40816610075661f,+ 0.82368298622748f, -0.74036047190173f, -0.98512833386833f, -0.99972330709594f, -0.95915368242257f, -0.99237800466040f,+ -0.21411126572790f, -0.93424819052545f, -0.68821476106884f, -0.26892306315457f, 0.91851997982317f, 0.09358228901785f,+ -0.96062769559127f, 0.36099095133739f, 0.51646184922287f, -0.71373332873917f, 0.61130721139669f, 0.46950141175917f,+ 0.47336129371299f, -0.27333178296162f, 0.90998308703519f, 0.96715662938132f, 0.44844799194357f, 0.99211574628306f,+ 0.66614891079092f, 0.96590176169121f, 0.74922239129237f, -0.89879858826087f, -0.99571588506485f, 0.52785521494349f,+ 0.97401082477563f, -0.16855870075190f, 0.72683747733879f, -0.48060774432251f, 0.95432193457128f, 0.68849603408441f,+ -0.72962208425191f, -0.76608443420917f, -0.85359479233537f, 0.88738125901579f, -0.81412430338535f, -0.97480768049637f,+ -0.87930772356786f, 0.74748307690436f, -0.71573331064977f, -0.98570608178923f, 0.83524300028228f, 0.83702537075163f,+ -0.48086065601423f, -0.98848504923531f, 0.97139128574778f, 0.80093621198236f, 0.51992825347895f, 0.80247631400510f,+ -0.00848591195325f, -0.76670128000486f, -0.70294374303036f, 0.55359910445577f, -0.95894428168140f, -0.43265504344783f,+ 0.97079252950321f, 0.09325857238682f, -0.92404293670797f, 0.85507704027855f, -0.69506469500450f, 0.98633412625459f,+ 0.26559203620024f, 0.73314307966524f, 0.28038443336943f, 0.14537913654427f, -0.74138124825523f, 0.99310339807762f,+ -0.01752795995444f, -0.82616635284178f, -0.55126773094930f, -0.98898543862153f, 0.97960898850996f, -0.94021446752851f,+ -0.99196309146936f, 0.67019017358456f, -0.67684928085260f, 0.12631491649378f, 0.09140039465500f, -0.20537731453108f,+ -0.71658965751996f, -0.97788200391224f, 0.81014640078925f, 0.53722648362443f, 0.40616991671205f, -0.26469008598449f,+ -0.67680188682972f, 0.94502052337695f, 0.86849774348749f, -0.18333598647899f, -0.99500381284851f, -0.02634122068550f,+ 0.84329189340667f, 0.10406957462213f, -0.09215968531446f, 0.69540012101253f, 0.99956173327206f, -0.12358542001404f,+ -0.79732779473535f, -0.91582524736159f, 0.96349973642406f, 0.96640458041000f, -0.79942778496547f, 0.64323902822857f,+ -0.11566039853896f, 0.28587846253726f, -0.39922954514662f, 0.94129601616966f, 0.99089197565987f, -0.92062625581587f,+ 0.28631285179909f, -0.91035047143603f, -0.83302725605608f, -0.67330410892084f, 0.95404443402072f, 0.49162765398743f,+ -0.06449863579434f, 0.03250560813135f, -0.99575054486311f, 0.42389784469507f, -0.65501142790847f, 0.82546114655624f,+ -0.81254441908887f, -0.51627234660629f, -0.99646369485481f, 0.84490533520752f, 0.00287840603348f, 0.64768261158166f,+ 0.70176989408455f, -0.20453028573322f, 0.96361882270190f, 0.40706967140989f, -0.68883758192426f, 0.91338958840772f,+ -0.34875585502238f, 0.71472290693300f, 0.91980081243087f, 0.66507455644919f, -0.99009048343881f, 0.85868021604848f,+ 0.68865791458395f, 0.55660316809678f, -0.99484402129368f, -0.20052559254934f, 0.94214511408023f, -0.99696425367461f,+ -0.67414626793544f, 0.49548221180078f, -0.47339353684664f, -0.85904328834047f, 0.14323651387360f, -0.94145598222488f,+ -0.29268293575672f, 0.05759224927952f, 0.43793861458754f, -0.78904969892724f, -0.36345126374441f, 0.64874435357162f,+ -0.08750604656825f, 0.97686944362527f, -0.96495267812511f, -0.53960305946511f, 0.55526940659947f, 0.78891523734774f,+ 0.73538215752630f, 0.96452072373404f, -0.30889773919437f, -0.80664389776860f, 0.03574995626194f, -0.97325616900959f,+ 0.98720684660488f, 0.48409133691962f, -0.81689296271203f, -0.90827703628298f, 0.67866860118215f, 0.81284503870856f,+ -0.15808569732583f, 0.85279555024382f, 0.80723395114371f, -0.24717418514605f, 0.47788757329038f, -0.46333147839295f,+ 0.96367554763201f, 0.38486749303242f, -0.99143875716818f, -0.24945277239809f, 0.83081876925833f, -0.94780851414763f,+ -0.58753191905341f, 0.01290772389163f, 0.95538108220960f, -0.85557052096538f, -0.96490920476211f, -0.64020970923102f,+ -0.97327101028521f, 0.12378128133110f, 0.91400366022124f, 0.57972471346930f, -0.99925837363824f, 0.71084847864067f,+ -0.86875903507313f, -0.20291699203564f, -0.26240034795124f, -0.68264554369108f, -0.24664412953388f, -0.87642273115183f,+ 0.02416275806869f, 0.27192914288905f, 0.82068619590515f, -0.85087787994476f, 0.88547373760759f, -0.89636802901469f,+ -0.18173078152226f, -0.26152145156800f, 0.09355476558534f, 0.54845123045604f, -0.54668414224090f, 0.95980774020221f,+ 0.37050990604091f, -0.59910140383171f, -0.70373594262891f, 0.91227665827081f, -0.34600785879594f, -0.99441426144200f,+ -0.68774481731008f, -0.30238837956299f, -0.26843291251234f, 0.83115668004362f, 0.49072334613242f, -0.45359708737775f,+ 0.38975993093975f, 0.95515358099121f, -0.97757125224150f, 0.05305894580606f, -0.17325552859616f, -0.92770672250494f,+ 0.99948035025744f, 0.58285545563426f, -0.64946246527458f, 0.68645507104960f, -0.12016920576437f, -0.57147322153312f,+ -0.58947456517751f, -0.34847132454388f, -0.41815140454465f, 0.16276422358861f, 0.99885650204884f, 0.11136095490444f,+ -0.56649614128386f, -0.90494866361587f, 0.94138021032330f, 0.35281916733018f, -0.75725076534641f, 0.53650549640587f,+ 0.20541973692630f, -0.94435144369918f, 0.99980371023351f, 0.79835913565599f, 0.29078277605775f, 0.35393777921520f,+ -0.62858772103030f, 0.38765693387102f, 0.43440904467688f, -0.98546330463232f, -0.98298583762390f, 0.21021524625209f,+ 0.19513029146934f, -0.94239832251867f, -0.95476662400101f, 0.98364554179143f, 0.93379635304810f, -0.70881994583682f,+ -0.85235410573336f, -0.08342347966410f, -0.86425093011245f, -0.45795025029466f, 0.38879779059045f, 0.97274429344593f,+ 0.92045124735495f, -0.62433652524220f, 0.89162532251878f, 0.54950955570563f, -0.36834336949252f, 0.96458298020975f,+ 0.93891760988045f, -0.89968353740388f, 0.99267657565094f, -0.03757034316958f, -0.94063471614176f, 0.41332338538963f,+ 0.99740224117019f, -0.16830494996370f, -0.35899413170555f, -0.46633226649613f, 0.05237237274947f, -0.25640361602661f,+ 0.36703583957424f, -0.38653265641875f, 0.91653180367913f, -0.30587628726597f, 0.69000803499316f, 0.90952171386132f,+ -0.38658751133527f, 0.99501571208985f, -0.29250814029851f, 0.37444994344615f, -0.60182204677608f, 0.86779651036123f,+ -0.97418588163217f, 0.96468523666475f, 0.88461574003963f, 0.57508405276414f, 0.05198933055162f, 0.21269661669964f,+ -0.53499621979720f, 0.97241553731237f, -0.49429560226497f, 0.98183865291903f, -0.98935142339139f, -0.40249159006933f,+ -0.98081380091130f, -0.72856895534041f, -0.27338148835532f, 0.99950922447209f, 0.06310802338302f, -0.54539587529618f,+ -0.20461677199539f, -0.14209977628489f, 0.66223843141647f, 0.72528579940326f, -0.84764345483665f, 0.02372316801261f,+ -0.89039863483811f, 0.88866581484602f, 0.95903308477986f, 0.76744927173873f, 0.73504123909879f, -0.03747203173192f,+ -0.31744434966056f, -0.36834111883652f, -0.34110827591623f, 0.40211222807691f, 0.47803883714199f, -0.39423219786288f,+ 0.98299195879514f, 0.01989791390047f, -0.30963073129751f, -0.18076720599336f, 0.99992588229018f, -0.26281872094289f,+ -0.93149731080767f, -0.98313162570490f, 0.99923472302773f, -0.80142993767554f, -0.26024169633417f, -0.75999759855752f,+ -0.35712514743563f, 0.19298963768574f, -0.99899084509530f, 0.74645156992493f, 0.86557171579452f, 0.55593866696299f,+ 0.33408042438752f, 0.86185953874709f, 0.99010736374716f, 0.04602397576623f, -0.66694269691195f, -0.91643611810148f,+ 0.64016792079480f, 0.15649530836856f, 0.99570534804836f, 0.45844586038111f, -0.63431466947340f, 0.21079116459234f,+ -0.07706847005931f, -0.89581437101329f, 0.98590090577724f, 0.88241721133981f, 0.80099335254678f, -0.36851896710853f,+ 0.78368131392666f, 0.45506999802597f, 0.08707806671691f, 0.80938994918745f, -0.86811883080712f, 0.39347308654705f,+ -0.39466529740375f, -0.66809432114456f, 0.97875325649683f, -0.72467840967746f, -0.95038560288864f, 0.89563219587625f,+ 0.17005239424212f, 0.54683053962658f, -0.76910792026848f, -0.96226617549298f, 0.99743281016846f, 0.42697157037567f,+ 0.95437383549973f, 0.97002324109952f, 0.99578905365569f, -0.54106826257356f, 0.28058259829990f, -0.85361420634036f,+ 0.85256524470573f, -0.64567607735589f, -0.50608540105128f, -0.65846015480300f, -0.97210735183243f, -0.23095213067791f,+ 0.95424048234441f, -0.99240147091219f, -0.96926570524023f, 0.73775654896574f, 0.30872163214726f, 0.41514960556126f,+ -0.24523839572639f, 0.63206633394807f, -0.33813265086024f, -0.38661779441897f, -0.05826828420146f, -0.06940774188029f,+ -0.22898461455054f, 0.97054853316316f, -0.18509915019881f, 0.47565762892084f, -0.10488238045009f, -0.87769947402394f,+ -0.71886586182037f, 0.78030982480538f, 0.99793873738654f, 0.90041310491497f, 0.57563307626120f, -0.91034337352097f,+ 0.28909646383717f, 0.96307783970534f, 0.42188998312520f, 0.48148651230437f, 0.93335049681047f, -0.43537023883588f,+ -0.97087374418267f, 0.86636445711364f, 0.36722871286923f, 0.65291654172961f, -0.81093025665696f, 0.08778370229363f,+ -0.26240603062237f, -0.92774095379098f, 0.83996497984604f, 0.55839849139647f, -0.99909615720225f, -0.96024605713970f,+ 0.74649464155061f, 0.12144893606462f, -0.74774595569805f, -0.26898062008959f, 0.95781667469567f, -0.79047927052628f,+ 0.95472308713099f, -0.08588776019550f, 0.48708332746299f, 0.99999041579432f, 0.46332038247497f, 0.10964126185063f,+ -0.76497004940162f, 0.89210929242238f, 0.57397389364339f, 0.35289703373760f, 0.75374316974495f, 0.96705214651335f,+ -0.59174397685714f, -0.89405370422752f, 0.75087906691890f, -0.29612672982396f, -0.98607857336230f, 0.25034911730023f,+ -0.40761056640505f, -0.90045573444695f, 0.66929266740477f, 0.98629493401748f, -0.97463695257310f, -0.00190223301301f,+ 0.90145509409859f, 0.99781390365446f, -0.87259289048043f, 0.99233587353666f, -0.91529461447692f, -0.15698707534206f,+ -0.03305738840705f, -0.37205262859764f, 0.07223051368337f, -0.88805001733626f, 0.99498012188353f, 0.97094358113387f,+ -0.74904939500519f, 0.99985483641521f, 0.04585228574211f, 0.99812337444082f, -0.89054954257993f, -0.31791913188064f,+ -0.83782144651251f, 0.97637632547466f, 0.33454804933804f, -0.86231516800408f, -0.99707579362824f, 0.93237990079441f,+ -0.22827527843994f, 0.18874759397997f, 0.67248046289143f, -0.03646211390569f, -0.05146538187944f, -0.92599700120679f,+ 0.99947295749905f, 0.93625229707912f, 0.66951124390363f, 0.98905825623893f, -0.99602956559179f, -0.44654715757688f,+ 0.82104905483590f, 0.99540741724928f, 0.99186510988782f, 0.72023001312947f, -0.65284592392918f, 0.52186723253637f,+ 0.93885443798188f, -0.74895312615259f, 0.96735248738388f, 0.90891816978629f, -0.22225968841114f, 0.57124029781228f,+ -0.44132783753414f, -0.92688840659280f, -0.85694974219574f, 0.88844532719844f, 0.91783042091762f, -0.46356892383970f,+ 0.72556974415690f, -0.99899555770747f, -0.99711581834508f, 0.58211560180426f, 0.77638976371966f, 0.94321834873819f,+ 0.07717324253925f, 0.58638399856595f, -0.56049829194163f, 0.82522301569036f, 0.98398893639988f, 0.39467440420569f,+ 0.47546946844938f, 0.68613044836811f, 0.65675089314631f, 0.18331637134880f, 0.03273375457980f, -0.74933109564108f,+ -0.38684144784738f, 0.51337349030406f, -0.97346267944545f, -0.96549364384098f, -0.53282156061942f, -0.91423265091354f,+ 0.99817310731176f, 0.61133572482148f, -0.50254500772635f, -0.88829338134294f, 0.01995873238855f, 0.85223515096765f,+ 0.99930381973804f, 0.94578896296649f, 0.82907767600783f, -0.06323442598128f, -0.58660709669728f, 0.96840773806582f,+ -0.17573736667267f, -0.48166920859485f, 0.83434292401346f, -0.13023450646997f, 0.05946491307025f, 0.20511047074866f,+ 0.81505484574602f, -0.94685947861369f, -0.44976380954860f, 0.40894572671545f, -0.89746474625671f, 0.99846578838537f,+ 0.39677256130792f, -0.74854668609359f, -0.07588948563079f, 0.74096214084170f, 0.76343198951445f, 0.41746629422634f,+ -0.74490104699626f, 0.94725911744610f, 0.64880119792759f, 0.41336660830571f, 0.62319537462542f, -0.93098313552599f,+ 0.42215817594807f, -0.07712787385208f, 0.02704554141885f, -0.05417518053666f, 0.80001773566818f, 0.91542195141039f,+ -0.79351832348816f, -0.36208897989136f, 0.63872359151636f, 0.08128252493444f, 0.52890520960295f, 0.60048872455592f,+ 0.74238552914587f, 0.04491915291044f, 0.99096131449250f, -0.19451182854402f, -0.80412329643109f, -0.88513818199457f,+ -0.64612616129736f, 0.72198674804544f, 0.11657770663191f, -0.83662833815041f, -0.95053182488101f, -0.96939905138082f,+ -0.62228872928622f, 0.82767262846661f, 0.03004475787316f, -0.99738896333384f, -0.97987214341034f, 0.36526129686425f,+ -0.99986980746200f, -0.36021610299715f, 0.89110648599879f, -0.97894250343044f, 0.10407960510582f, 0.77357793811619f,+ 0.95964737821728f, -0.35435818285502f, 0.50843233159162f, 0.96107691266205f, 0.17006334670615f, -0.76854025314829f,+ 0.25872675063360f, 0.99893303933816f, -0.01115998681937f, 0.98496019742444f, -0.79598702973261f, 0.97138411318894f,+ -0.99264708948101f, -0.99542822402536f, -0.99829663752818f, 0.01877138824311f, -0.70801016548184f, 0.33680685948117f,+ -0.70467057786826f, 0.93272777501857f, 0.99846021905254f, -0.98725746254433f, -0.63364968534650f, -0.16473594423746f,+ -0.16258217500792f, -0.95939125400802f, -0.43645594360633f, -0.94805030113284f, -0.99848471702976f, 0.96245166923809f,+ -0.16796458968998f, -0.98987511890470f, -0.87979225745213f, -0.71725725041680f, 0.44183099021786f, -0.93568974498761f,+ 0.93310180125532f, -0.99913308068246f, -0.93941931782002f, -0.56409379640356f, -0.88590003188677f, 0.47624600491382f,+ 0.99971463703691f, -0.83889954253462f, -0.75376385639978f, 0.00814643438625f, 0.93887685615875f, -0.11284528204636f,+ 0.85126435782309f, 0.52349251543547f, 0.39701421446381f, 0.81779634174316f, -0.37024464187437f, -0.87071656222959f,+ -0.36024828242896f, 0.34655735648287f, -0.93388812549209f, -0.84476541096429f, -0.65298804552119f, -0.18439575450921f,+ 0.11960319006843f, 0.99899346780168f, 0.94292565553160f, 0.83163906518293f, 0.75081145286948f, -0.35533223142265f,+ 0.56721979748394f, -0.24076836414499f, 0.46857766746029f, -0.30140233457198f, 0.97312313923635f, -0.99548191630031f,+ -0.38299976567017f, 0.98516909715427f, 0.41025800019463f, 0.02116736935734f, 0.09638062008048f, 0.04411984381457f,+ -0.85283249275397f, 0.91475563922421f, 0.88866808958124f, -0.99735267083226f, -0.48202429536989f, -0.96805608884164f,+ 0.27572582416567f, 0.58634753335832f, -0.65889129659168f, 0.58835634138583f, 0.98838086953732f, 0.99994349600236f,+ -0.20651349620689f, 0.54593044066355f, -0.62126416356920f, -0.59893681700392f, 0.20320105410437f, -0.86879180355289f,+ -0.97790548600584f, 0.96290806999242f, 0.11112534735126f, 0.21484763313301f, -0.41368337314182f, 0.28216837680365f,+ 0.24133038992960f, 0.51294362630238f, -0.66393410674885f, -0.08249679629081f, -0.53697829178752f, -0.97649903936228f,+ -0.97224737889348f, 0.22081333579837f, 0.87392477144549f, -0.12796173740361f, 0.19050361015753f, 0.01602615387195f,+ -0.46353441212724f, -0.95249041539006f, -0.07064096339021f, -0.94479803205886f, -0.92444085484466f, -0.10457590187436f,+ -0.83822593578728f, -0.01695043208885f, 0.75214681811150f, -0.99955681042665f, -0.42102998829339f, 0.99720941999394f,+ -0.72094786237696f, -0.35008961934255f, 0.78843311019251f, 0.52851398958271f, 0.97394027897442f, -0.26695944086561f,+ 0.99206463477946f, -0.57010120849429f, 0.76789609461795f, -0.76519356730966f, -0.82002421836409f, -0.73530179553767f,+ 0.81924990025724f, 0.99698425250579f, -0.26719850873357f, 0.68903369776193f, -0.43311260380975f, 0.85321815947490f,+ 0.99194979673836f, 0.91876249766422f, -0.80692001248487f, -0.32627540663214f, 0.43080003649976f, -0.21919095636638f,+ 0.67709491937357f, -0.95478075822906f, 0.56151770568316f, -0.70693811747778f, 0.10831862810749f, -0.08628837174592f,+ 0.91229417540436f, -0.65987351408410f, -0.48972893932274f, 0.56289246362686f, -0.89033658689697f, -0.71656563987082f,+ 0.65269447475094f, 0.65916004833932f, 0.67439478141121f, -0.81684380846796f, -0.47770832416973f, -0.16789556203025f,+ -0.99715979260878f, -0.93565784007648f, -0.90889593602546f, 0.62034397054380f, -0.06618622548177f, -0.23812217221359f,+ 0.99430266919728f, 0.18812555317553f, 0.97686402381843f, -0.28664534366620f, 0.94813650221268f, -0.97506640027128f,+ -0.95434497492853f, -0.79607978501983f, -0.49104783137150f, 0.32895214359663f, 0.99881175120751f, 0.88993983831354f,+ 0.50449166760303f, -0.85995072408434f, 0.47162891065108f, -0.18680204049569f, -0.62081581361840f, 0.75000676218956f,+ -0.43867015250812f, 0.99998069244322f, 0.98630563232075f, -0.53578899600662f, -0.61510362277374f, -0.89515019899997f,+ -0.03841517601843f, -0.69888815681179f, -0.30102157304644f, -0.07667808922205f, 0.41881284182683f, 0.02188098922282f,+ -0.86135454941237f, 0.98947480909359f, 0.67226861393788f, -0.13494389011014f, -0.70737398842068f, -0.76547349325992f,+ 0.94044946687963f, 0.09026201157416f, -0.82386352534327f, 0.08924768823676f, -0.32070666698656f, 0.50143421908753f,+ 0.57593163224487f, -0.98966422921509f, -0.36326018419965f, 0.07440243123228f, 0.99979044674350f, -0.14130287347405f,+ -0.92366023326932f, -0.97979298068180f, -0.44607178518598f, -0.54233252016394f, 0.44226800932956f, 0.71326756742752f,+ 0.03671907158312f, 0.63606389366675f, 0.52175424682195f, -0.85396826735705f, -0.94701139690956f, -0.01826348194255f,+ -0.98759606946049f, 0.82288714303073f, 0.87434794743625f, 0.89399495655433f, -0.93412041758744f, 0.41374052024363f,+ 0.96063943315511f, 0.93116709541280f, 0.97534253457837f, 0.86150930812689f, 0.99642466504163f, 0.70190043427512f,+ -0.94705089665984f, -0.29580042814306f, 0.91599807087376f, -0.98147830385781f,+};+static const uint8_t PS_HUFF_SYM[242] = {+ 28, 32, 29, 31, 27, 33, 26, 34, 25, 35, 24, 36, 37, 40, 19, 41, 22, 38, 9, 51,+ 11, 49, 13, 47, 16, 18, 42, 44, 12, 48, 4, 5, 2, 3, 15, 21, 39, 45, 8, 52,+ 6, 7, 55, 56, 53, 54, 17, 43, 59, 60, 57, 58, 0, 1, 10, 50, 14, 46, 20, 23,+ 30, 31, 26, 34, 27, 33, 35, 24, 36, 39, 41, 9, 10, 48, 49, 17, 23, 37, 43, 11,+ 12, 4, 56, 2, 3, 59, 60, 57, 58, 0, 1, 5, 55, 6, 54, 13, 15, 20, 40, 22,+ 38, 45, 47, 7, 53, 18, 42, 16, 44, 8, 52, 14, 46, 50, 51, 19, 21, 25, 28, 32,+ 29, 30, 14, 15, 13, 16, 12, 17, 11, 10, 18, 19, 9, 20, 8, 7, 21, 22, 6, 23,+ 24, 5, 25, 4, 3, 0, 1, 2, 26, 27, 28, 14, 13, 15, 12, 16, 11, 17, 10, 18,+ 9, 19, 8, 20, 21, 7, 22, 6, 23, 0, 1, 2, 3, 4, 5, 24, 25, 26, 27, 28,+ 7, 8, 6, 9, 5, 10, 4, 11, 12, 3, 13, 2, 14, 1, 0, 7, 8, 6, 9, 5,+ 10, 4, 11, 3, 12, 2, 13, 1, 0, 14, 1, 4, 5, 3, 6, 2, 7, 0, 5, 4,+ 3, 2, 6, 1, 7, 0, 7, 1, 3, 6, 2, 5, 4, 0, 5, 2, 6, 4, 3, 1,+ 7, 0,+};+static const uint32_t PS_HUFF_CODE[242] = {+ 0x0, 0x1, 0x1, 0x2, 0xc, 0xd, 0x1c, 0x1d,+ 0x3c, 0x3d, 0x7c, 0x7d, 0xfc, 0x3f4, 0x7ea, 0x7eb,+ 0x1fb, 0x1fc, 0xfe80, 0xfe81, 0xfe82, 0xfe83, 0x7f42, 0x7f43,+ 0x1fd1, 0xfe9, 0xfea, 0x1fd6, 0xfeb8, 0xfeb9, 0x1fd74, 0x1fd75,+ 0x1fd76, 0x1fd77, 0x3faf, 0x3fb, 0x3fc, 0x3fd0, 0x1fe88, 0x1fe89,+ 0x1fe8a, 0x1fe8b, 0x1fe8c, 0x1fe8d, 0x1fe8e, 0x1fe8f, 0x1fe9, 0x1fea,+ 0x1feb0, 0x1feb1, 0x1feb2, 0x1feb3, 0x1feb4, 0x1feb5, 0x1feb6, 0x1feb7,+ 0x7fae, 0x7faf, 0x7fb, 0x1ff, 0x1, 0x0, 0x20, 0x21,+ 0x11, 0x12, 0x4c, 0x9a, 0x9b, 0x270, 0x4e2, 0x2718,+ 0x2719, 0x271a, 0x271b, 0x9c7, 0x139, 0x13a, 0x9d8, 0x2764,+ 0x2765, 0x4ecc, 0x4ecd, 0x4ece, 0x4ecf, 0x4ed0, 0x4ed1, 0x4ed2,+ 0x4ed3, 0x4ed4, 0x4ed5, 0x4ed6, 0x4ed7, 0x4ed8, 0x4ed9, 0x276d,+ 0x13b7, 0x4ee, 0x4ef, 0x278, 0x279, 0x13d0, 0x27a2, 0x4f46,+ 0x4f47, 0x9e9, 0x9ea, 0x13d6, 0x13d7, 0x4f60, 0x4f61, 0x27b1,+ 0x27b2, 0x4f66, 0x4f67, 0x9ed, 0x4f7, 0x9f, 0xa, 0xb,+ 0x3, 0x1, 0x0, 0x4, 0x5, 0xc, 0xd, 0x1c,+ 0x1d, 0x3c, 0x3d, 0x3e, 0x7e, 0xfe, 0x1fe, 0x3fe,+ 0x7fe, 0x1ffc, 0x1ffd, 0x3ffc, 0x3ffd, 0x7ffc, 0x7ffd, 0xfffc,+ 0x1fffa, 0x1fffb, 0x1fffc, 0x1fffd, 0x1fffe, 0x3fffe, 0x3ffff, 0x0,+ 0x2, 0x6, 0xe, 0x1e, 0x3e, 0x7e, 0xfe, 0x1fe,+ 0x3fe, 0x7fe, 0xffe, 0x1ffe, 0x3ffe, 0x7ffe, 0x1fffc, 0x1fffd,+ 0x7fff8, 0x7fff9, 0x7fffa, 0x7fffb, 0xffff8, 0xffff9, 0xffffa, 0xffffb,+ 0xffffc, 0xffffd, 0xffffe, 0xfffff, 0x0, 0x2, 0x6, 0xe,+ 0x1e, 0x3e, 0x7e, 0xfe, 0x1fe, 0x3fe, 0x7fe, 0xffe,+ 0x1ffe, 0x3ffe, 0x3fff, 0x0, 0x2, 0x6, 0xe, 0x1e,+ 0x3e, 0x7e, 0xfe, 0x1fe, 0x3fe, 0x7fe, 0xffe, 0x1ffe,+ 0x3ffe, 0x3fff, 0x0, 0x2, 0x3, 0x4, 0x5, 0x6,+ 0x7, 0x1, 0x0, 0x2, 0x3, 0x2, 0x3, 0x2,+ 0x3, 0x1, 0x0, 0x1, 0x4, 0x5, 0x6, 0xe,+ 0xf, 0x1, 0x0, 0x1, 0x2, 0x6, 0x7, 0x2,+ 0x3, 0x1,+};+static const uint8_t PS_HUFF_LEN[242] = {+ 4, 4, 3, 3, 5, 5, 6, 6, 7, 7, 8, 8, 9, 11, 12, 12, 10, 10, 17, 17,+ 17, 17, 16, 16, 14, 13, 13, 14, 17, 17, 18, 18, 18, 18, 15, 11, 11, 15, 18, 18,+ 18, 18, 18, 18, 18, 18, 14, 14, 18, 18, 18, 18, 18, 18, 18, 18, 16, 16, 12, 10,+ 1, 2, 7, 7, 6, 6, 8, 9, 9, 11, 12, 15, 15, 15, 15, 13, 10, 10, 13, 15,+ 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 14, 12, 12, 11,+ 11, 14, 15, 16, 16, 13, 13, 14, 14, 16, 16, 15, 15, 16, 16, 13, 12, 9, 5, 5,+ 3, 1, 1, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 8, 9, 10, 11, 13, 13, 14,+ 14, 15, 15, 16, 17, 17, 17, 17, 17, 18, 18, 1, 2, 3, 4, 5, 6, 7, 8, 9,+ 10, 11, 12, 13, 14, 15, 17, 17, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20,+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 1, 2, 3, 4, 5,+ 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 3, 4, 4, 4, 4, 4, 4, 1, 4, 5,+ 5, 4, 4, 3, 3, 1, 3, 3, 4, 4, 4, 5, 5, 1, 4, 4, 4, 5, 5, 3,+ 3, 1,+};+static const uint16_t PS_HUFF_AT[11] = {+ 0, 61, 122, 151, 180, 195, 210, 218, 226, 234, 242,+};+static const int8_t PS_HUFF_OFFSET[10] = {+ -30, -30, -14, -14, -7, -7, 0, 0, 0, 0,+};+static const int8_t PS_K_TO_I_20[71] = {+ 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 15, 15,+ 15, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,+ 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,+};+static const int8_t PS_K_TO_I_34[91] = {+ 0, 1, 2, 3, 4, 5, 6, 6, 7, 2, 1, 0, 10, 10, 4, 5, 6, 7, 8, 9,+ 10, 11, 12, 9, 14, 11, 12, 13, 14, 15, 16, 13, 16, 17, 18, 19, 20, 21, 22, 22,+ 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30,+ 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,+ 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,+};+static const float PS_G0_Q8[7] = {+ 0.00746082949812f, 0.02270420949825f, 0.04546865930473f, 0.07266113929591f,+ 0.09885108575264f, 0.11793710567217f, 0.12500000000000f,+};+static const float PS_G0_Q12[7] = {+ 0.04081179924692f, 0.03812810994926f, 0.05144908135699f, 0.06399831151592f,+ 0.07428313801106f, 0.08100347892914f, 0.08333333333333f,+};+static const float PS_G1_Q8[7] = {+ 0.01565675600122f, 0.03752716391991f, 0.05417891378782f, 0.08417044116767f,+ 0.10307344158036f, 0.12222452249753f, 0.12500000000000f,+};+static const float PS_G2_Q4[7] = {+ -0.05908211155639f, -0.04871498374946f, 0.00000000000000f, 0.07778723915851f,+ 0.16486303567403f, 0.23279856662996f, 0.25000000000000f,+};++#endifdiff --git a/src/common/audio_alac.c b/src/common/audio_alac.cnew file mode 100644index 0000000..36aa0ac--- /dev/null+++ b/src/common/audio_alac.c@@ -0,0 +1,370 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_alac.c - the Apple Lossless Audio Codec, as Apple's open source+ * decoder defines it: the ALACSpecificConfig, frames of single, pair and+ * LFE elements, each channel's adaptive Rice-coded residual with its runs+ * of zeros and its sign-LMS predictor, the stereo decorrelation, the+ * low bits sent apart for 20, 24 and 32-bit sound, and uncompressed+ * frames. Exact: the integers are those the encoder started from.+ */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "audio_aac.h"+#include "audio_ogg.h"++struct alac {+ uint32_t frame_length;+ int sample_size, pb, mb, kb, channels;+ uint32_t rate;+ int32_t *err[2], *out[2], *extra[2];+};++static int log2u(unsigned v)+{+ return v ? 31 - __builtin_clz(v) : 0;+}++/* ones before a zero, at most 9 (the zero taken only below 9) */+static unsigned unary_0_9(struct abr *b)+{+ unsigned x = 0;+ while (x < 9 && abr_bit(b))+ x++;+ return x;+}++static unsigned decode_scalar(struct abr *b, int k, int bps)+{+ unsigned x = unary_0_9(b);+ if (x > 8) {+ x = abr_bits(b, bps);+ } else if (k != 1) {+ unsigned extra = abr_peek(b) >> (32 - k);+ x = (x << k) - x;+ if (extra > 1) {+ x += extra - 1;+ b->pos += (size_t)k;+ } else {+ b->pos += (size_t)(k - 1);+ }+ }+ return x;+}++static int rice_decompress(const struct alac *c, struct abr *b, int32_t *out,+ int n, int bps, int mult)+{+ unsigned history = (unsigned)c->mb;+ int sign_modifier = 0;+ for (int i = 0; i < n; i++) {+ if (abr_left(b) == 0)+ return -1;+ int k = log2u((history >> 9) + 3);+ if (k > c->kb)+ k = c->kb;+ unsigned x = decode_scalar(b, k, bps);+ x += (unsigned)sign_modifier;+ sign_modifier = 0;+ out[i] = (int32_t)((x >> 1) ^ -(x & 1));+ if (x > 0xffff)+ history = 0xffff;+ else+ history += x * (unsigned)mult - ((history * (unsigned)mult) >> 9);+ /* runs of zeros */+ if (history < 128 && i + 1 < n) {+ k = 7 - log2u(history) + (int)((history + 16) >> 6);+ if (k > c->kb)+ k = c->kb;+ int block = (int)decode_scalar(b, k, 16);+ if (block > 0) {+ if (block >= n - i)+ block = n - i - 1;+ memset(out + i + 1, 0, sizeof(int32_t) * (size_t)block);+ i += block;+ }+ if (block <= 0xffff)+ sign_modifier = 1;+ history = 0;+ }+ }+ return 0;+}++static int32_t sign_extend(uint32_t v, int bits)+{+ unsigned shift = 32u - (unsigned)bits;+ return (int32_t)(v << shift) >> shift;+}++static int sign_of(int v)+{+ return (v > 0) - (v < 0);+}++static void lpc_prediction(const int32_t *error, int32_t *outs, int n, int bps,+ int16_t *coefs, int order, int quant)+{+ uint32_t *out = (uint32_t *)outs;+ uint32_t *pred = out;+ out[0] = (uint32_t)error[0];+ if (n <= 1)+ return;+ if (!order) {+ memcpy(out + 1, error + 1, sizeof(int32_t) * (size_t)(n - 1));+ return;+ }+ if (order == 31) {+ for (int i = 1; i < n; i++)+ out[i] =+ (uint32_t)sign_extend(out[i - 1] + (uint32_t)error[i], bps);+ return;+ }+ int i;+ for (i = 1; i <= order && i < n; i++)+ out[i] = (uint32_t)sign_extend(out[i - 1] + (uint32_t)error[i], bps);+ for (; i < n; i++) {+ /* in 32 bits that wrap, as the reference decoder's arithmetic */+ uint32_t acc = 0;+ unsigned error_val = (unsigned)error[i];+ int d = (int)*pred++;+ for (int j = 0; j < order; j++)+ acc += (pred[j] - (unsigned)d) * (unsigned)coefs[j];+ int val =+ (int)(((int64_t)(int32_t)acc + (1LL << (quant - 1))) >> quant);+ out[i] =+ (uint32_t)sign_extend((uint32_t)val + (unsigned)d + error_val, bps);+ int error_sign = sign_of((int)error_val);+ if (error_sign) {+ for (int j = 0;+ j < order && (int)(error_val * (unsigned)error_sign) > 0;+ j++) {+ val = (int)((unsigned)d - pred[j]);+ int sign = sign_of(val) * error_sign;+ coefs[j] = (int16_t)(coefs[j] - sign);+ val = (int)((unsigned)val * (unsigned)sign);+ error_val -= (unsigned)(val >> quant) * (unsigned)(j + 1);+ }+ }+ }+}++/* an element of ch channels into out[0..ch-1] at its first sample: the+ samples it holds, or -1 */+static int decode_element(struct alac *c, struct abr *b, int ch, int *nb)+{+ abr_bits(b, 4);+ abr_bits(b, 12);+ int has_size = abr_bit(b);+ int extra_bits = (int)abr_bits(b, 2) << 3;+ int bps = c->sample_size - extra_bits + ch - 1;+ if (bps > 32 || bps < 1)+ return -1;+ int compressed = !abr_bit(b);+ uint32_t n = has_size ? abr_bits(b, 32) : c->frame_length;+ if (!n || n > c->frame_length)+ return -1;+ if (*nb && (int)n != *nb)+ return -1;+ *nb = (int)n;+ int shift = 0, weight = 0;+ if (compressed) {+ int16_t coefs[2][32];+ int order[2], ptype[2], quant[2], mult[2];+ if (!c->kb)+ return -1;+ shift = (int)abr_bits(b, 8);+ weight = (int)abr_bits(b, 8);+ if (ch == 2 && weight && shift > 31)+ return -1;+ for (int k = 0; k < ch; k++) {+ ptype[k] = (int)abr_bits(b, 4);+ quant[k] = (int)abr_bits(b, 4);+ mult[k] = (int)abr_bits(b, 3);+ order[k] = (int)abr_bits(b, 5);+ if ((uint32_t)order[k] >= c->frame_length || !quant[k])+ return -1;+ for (int i = order[k] - 1; i >= 0; i--)+ coefs[k][i] = (int16_t)abr_bits(b, 16);+ }+ if (extra_bits) {+ if (abr_left(b) < (size_t)n * (size_t)ch * (size_t)extra_bits)+ return -1;+ for (uint32_t i = 0; i < n; i++)+ for (int k = 0; k < ch; k++)+ c->extra[k][i] = (int32_t)abr_bits(b, extra_bits);+ }+ for (int k = 0; k < ch; k++) {+ if (rice_decompress(c, b, c->err[k], (int)n, bps,+ mult[k] * c->pb / 4) != 0)+ return -1;+ if (ptype[k] == 15)+ lpc_prediction(c->err[k], c->err[k], (int)n, bps, NULL, 31, 0);+ lpc_prediction(c->err[k], c->out[k], (int)n, bps, coefs[k],+ order[k], quant[k]);+ }+ } else {+ if (abr_left(b) < (size_t)n * (size_t)ch * (size_t)c->sample_size)+ return -1;+ for (uint32_t i = 0; i < n; i++)+ for (int k = 0; k < ch; k++)+ c->out[k][i] =+ sign_extend(abr_bits(b, c->sample_size), c->sample_size);+ extra_bits = 0;+ }+ if (ch == 2 && weight) {+ for (uint32_t i = 0; i < n; i++) {+ int32_t a = c->out[0][i], bb = c->out[1][i];+ a = (int32_t)((uint32_t)a -+ (uint32_t)((int)((int64_t)bb * weight) >> shift));+ bb = (int32_t)((uint32_t)bb + (uint32_t)a);+ c->out[0][i] = bb;+ c->out[1][i] = a;+ }+ }+ if (extra_bits)+ for (int k = 0; k < ch; k++)+ for (uint32_t i = 0; i < n; i++)+ c->out[k][i] = (int32_t)((uint32_t)c->out[k][i] << extra_bits |+ (uint32_t)c->extra[k][i]);+ return 0;+}++int janas_alac_stream(const uint8_t *cookie, size_t cn, const uint8_t *data,+ const size_t *off, const size_t *len, size_t count,+ const struct audio_edit *edit, struct janas_audio *a,+ char *why, size_t wn)+{+ /* the cookie, past the atoms' heads it may carry */+ if (cn >= 12 && !memcmp(cookie + 4, "frma", 4)) {+ cookie += 12;+ cn -= 12;+ }+ if (cn >= 12 && !memcmp(cookie + 4, "alac", 4)) {+ cookie += 12;+ cn -= 12;+ } else if (cn >= 28 && !(cookie[0] | cookie[1] | cookie[2] | cookie[3])) {+ cookie += 4; /* the alac box's version and flags */+ cn -= 4;+ }+ if (cn < 24) {+ snprintf(why, wn, "an ALAC configuration cut short");+ return -1;+ }+ struct alac c;+ memset(&c, 0, sizeof c);+ c.frame_length = (uint32_t)cookie[0] << 24 | (uint32_t)cookie[1] << 16 |+ (uint32_t)cookie[2] << 8 | cookie[3];+ c.sample_size = cookie[5];+ c.pb = cookie[6];+ c.mb = cookie[7];+ c.kb = cookie[8];+ c.channels = cookie[9];+ c.rate = (uint32_t)cookie[20] << 24 | (uint32_t)cookie[21] << 16 |+ (uint32_t)cookie[22] << 8 | cookie[23];+ if (!c.frame_length || c.frame_length > 65536 || !c.channels ||+ c.channels > 8 || !c.rate || c.rate > 384000 ||+ (c.sample_size != 16 && c.sample_size != 20 && c.sample_size != 24 &&+ c.sample_size != 32)) {+ snprintf(why, wn, "an ALAC configuration out of range");+ return -1;+ }+ float *pcm = NULL;+ size_t got = 0, cap = 0;+ int rc = -1;+ for (int k = 0; k < 2; k++) {+ c.err[k] = malloc(sizeof(int32_t) * c.frame_length);+ c.out[k] = malloc(sizeof(int32_t) * c.frame_length);+ c.extra[k] = malloc(sizeof(int32_t) * c.frame_length);+ if (!c.err[k] || !c.out[k] || !c.extra[k])+ goto oom;+ }+ double *mix = calloc(c.frame_length, sizeof(double));+ if (!mix)+ goto oom;+ double scale = 1.0 / (double)(1u << (c.sample_size - 1)) / c.channels;+ int bad = 0;+ for (size_t pk = 0; pk < count; pk++) {+ struct abr b = {.p = data + off[pk], .n = len[pk]};+ int nb = 0, ch = 0, ok = 1;+ memset(mix, 0, sizeof(double) * c.frame_length);+ while (abr_left(&b) >= 3) {+ int el = (int)abr_bits(&b, 3);+ if (el == 7)+ break;+ if (el > 1 && el != 3) {+ ok = 0;+ break;+ }+ int n = el == 1 ? 2 : 1;+ if (ch + n > c.channels || decode_element(&c, &b, n, &nb) != 0) {+ ok = 0;+ break;+ }+ for (int k = 0; k < n; k++)+ for (int i = 0; i < nb; i++)+ mix[i] += c.out[k][i];+ ch += n;+ }+ if (!ok || !nb) {+ /* a packet that cannot be read is silence of a frame */+ if (++bad > 8 && !got) {+ snprintf(why, wn, "an ALAC stream that is none");+ goto end;+ }+ nb = (int)c.frame_length;+ memset(mix, 0, sizeof(double) * c.frame_length);+ }+ if ((size_t)nb > JANAS_AUDIO_MAX_SAMPLES - got) {+ snprintf(why, wn, "a sound longer than this reads");+ goto end;+ }+ if (got + (size_t)nb > cap) {+ size_t nc = cap ? cap * 2 : 65536;+ while (nc < got + (size_t)nb)+ nc *= 2;+ float *t = realloc(pcm, sizeof(float) * nc);+ if (!t)+ goto oom2;+ pcm = t;+ cap = nc;+ }+ for (int i = 0; i < nb; i++)+ pcm[got + (size_t)i] = (float)(mix[i] * scale);+ got += (size_t)nb;+ }+ size_t skip = 0, total = got;+ audio_edit_apply(edit, c.rate, &skip, &total);+ if (!total) {+ snprintf(why, wn, "an ALAC stream without sound");+ goto end;+ }+ memmove(pcm, pcm + skip, sizeof(float) * total);+ a->pcm = pcm;+ a->n = total;+ a->rate = c.rate;+ pcm = NULL;+ rc = 0;+ goto end;+oom2:+ snprintf(why, wn, "out of memory");+end:+ free(mix);+ free(pcm);+ for (int k = 0; k < 2; k++) {+ free(c.err[k]);+ free(c.out[k]);+ free(c.extra[k]);+ }+ return rc;+oom:+ for (int k = 0; k < 2; k++) {+ free(c.err[k]);+ free(c.out[k]);+ free(c.extra[k]);+ }+ snprintf(why, wn, "out of memory");+ return -1;+}diff --git a/src/common/audio_fft.c b/src/common/audio_fft.cnew file mode 100644index 0000000..8dab8ed--- /dev/null+++ b/src/common/audio_fft.c@@ -0,0 +1,222 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_fft.c - a mixed-radix complex FFT, decimation in time, recursive+ * over the factors of its length (4 first, then 2, 3, 5 and whatever is+ * left), butterflies of 2 and 4 written out and any other radix as a small+ * DFT; and the inverse MDCT through a DCT-IV of n / 2 points made of one+ * FFT of n / 4 (audio_fft.h).+ *+ * The inverse MDCT: with M = n / 2, the DCT-IV u[m] = sum_k x[k]+ * cos(pi / M (m + 1/2)(k + 1/2)) splits into even and odd-reversed inputs,+ * z[p] = (x[2p] + i x[M - 1 - 2p]) e^(-i pi p / M), whose FFT Z, turned by+ * e^(-i pi (q + 1/4) / M), gives u[2q] = Re Z[q] and u[M - 1 - 2q] =+ * -Im Z[q]; the MDCT's output is u shifted by n / 4 with its symmetries.+ */+#include "audio_fft.h"++#include <math.h>+#include <stdlib.h>+#include <string.h>++struct afft {+ int n, nf;+ int fac[64]; /* (radix, the length below it) pairs */+ float *tw; /* e^(-2 pi i k / n), k < n */+};++struct afft *afft_new(int n)+{+ if (n < 1)+ return NULL;+ struct afft *f = calloc(1, sizeof *f);+ if (!f || !(f->tw = malloc(sizeof(float) * 2 * (size_t)n))) {+ free(f);+ return NULL;+ }+ f->n = n;+ for (int k = 0; k < n; k++) {+ double a = -2 * M_PI * k / n;+ f->tw[2 * k] = (float)cos(a);+ f->tw[2 * k + 1] = (float)sin(a);+ }+ int m = n, p = 4;+ while (m > 1) {+ while (m % p) {+ p = p == 4 ? 2 : p == 2 ? 3 : p + 2;+ if (p * p > m)+ p = m;+ }+ m /= p;+ f->fac[2 * f->nf] = p;+ f->fac[2 * f->nf + 1] = m;+ f->nf++;+ }+ return f;+}++void afft_free(struct afft *f)+{+ if (f) {+ free(f->tw);+ free(f);+ }+}++static inline void cmul(float *r, const float *a, const float *b)+{+ float re = a[0] * b[0] - a[1] * b[1], im = a[0] * b[1] + a[1] * b[0];+ r[0] = re;+ r[1] = im;+}++static void work(const struct afft *f, float *out, const float *in, int fstride,+ const int *fac)+{+ int p = fac[0], m = fac[1];+ if (m == 1) {+ for (int q = 0; q < p; q++) {+ out[2 * q] = in[2 * q * fstride];+ out[2 * q + 1] = in[2 * q * fstride + 1];+ }+ } else {+ for (int q = 0; q < p; q++)+ work(f, out + 2 * q * m, in + 2 * q * fstride, fstride * p,+ fac + 2);+ }+ const float *tw = f->tw;+ if (p == 2) {+ for (int k = 0; k < m; k++) {+ float *a = out + 2 * k, *b = out + 2 * (k + m), t[2];+ cmul(t, b, tw + 2 * k * fstride);+ b[0] = a[0] - t[0];+ b[1] = a[1] - t[1];+ a[0] += t[0];+ a[1] += t[1];+ }+ } else if (p == 4) {+ for (int k = 0; k < m; k++) {+ float *a0 = out + 2 * k, *a1 = a0 + 2 * m, *a2 = a1 + 2 * m,+ *a3 = a2 + 2 * m, b1[2], b2[2], b3[2];+ cmul(b1, a1, tw + 2 * k * fstride);+ cmul(b2, a2, tw + 4 * k * fstride);+ cmul(b3, a3, tw + 6 * k * fstride);+ float s0r = a0[0] + b2[0], s0i = a0[1] + b2[1];+ float s1r = a0[0] - b2[0], s1i = a0[1] - b2[1];+ float s2r = b1[0] + b3[0], s2i = b1[1] + b3[1];+ float s3r = b1[0] - b3[0], s3i = b1[1] - b3[1];+ a0[0] = s0r + s2r;+ a0[1] = s0i + s2i;+ a2[0] = s0r - s2r;+ a2[1] = s0i - s2i;+ /* times -i for the forward transform */+ a1[0] = s1r + s3i;+ a1[1] = s1i - s3r;+ a3[0] = s1r - s3i;+ a3[1] = s1i + s3r;+ }+ } else {+ float sc[2 * 64];+ for (int u = 0; u < m; u++) {+ for (int q = 0; q < p && q < 64; q++) {+ sc[2 * q] = out[2 * (u + q * m)];+ sc[2 * q + 1] = out[2 * (u + q * m) + 1];+ }+ for (int q1 = 0; q1 < p; q1++) {+ int k = u + q1 * m, t = 0;+ float acc[2] = {sc[0], sc[1]}, x[2];+ for (int q = 1; q < p; q++) {+ t += fstride * k;+ t %= f->n;+ cmul(x, sc + 2 * q, tw + 2 * t);+ acc[0] += x[0];+ acc[1] += x[1];+ }+ out[2 * k] = acc[0];+ out[2 * k + 1] = acc[1];+ }+ }+ }+}++void afft_run(const struct afft *f, const float *in, float *out)+{+ work(f, out, in, 1, f->fac);+}++struct aimdct {+ int n;+ struct afft *f;+ float *pre, *post, *z, *zz, *u;+};++struct aimdct *aimdct_new(int n)+{+ if (n < 8 || n % 4)+ return NULL;+ struct aimdct *m = calloc(1, sizeof *m);+ int M = n / 2, h = n / 4;+ if (!m)+ return NULL;+ m->n = n;+ m->f = afft_new(h);+ m->pre = malloc(sizeof(float) * 2 * (size_t)h);+ m->post = malloc(sizeof(float) * 2 * (size_t)h);+ m->z = malloc(sizeof(float) * 2 * (size_t)h);+ m->zz = malloc(sizeof(float) * 2 * (size_t)h);+ m->u = malloc(sizeof(float) * (size_t)M);+ if (!m->f || !m->pre || !m->post || !m->z || !m->zz || !m->u) {+ aimdct_free(m);+ return NULL;+ }+ for (int p = 0; p < h; p++) {+ double a = -M_PI * p / M, b = -M_PI * (p + 0.25) / M;+ m->pre[2 * p] = (float)cos(a);+ m->pre[2 * p + 1] = (float)sin(a);+ m->post[2 * p] = (float)cos(b);+ m->post[2 * p + 1] = (float)sin(b);+ }+ return m;+}++void aimdct_free(struct aimdct *m)+{+ if (!m)+ return;+ afft_free(m->f);+ free(m->pre);+ free(m->post);+ free(m->z);+ free(m->zz);+ free(m->u);+ free(m);+}++void aimdct_dct4(struct aimdct *m, const float *x, int stride, float *u)+{+ int n = m->n, M = n / 2, h = n / 4;+ for (int p = 0; p < h; p++) {+ float v[2] = {x[2 * p * stride], x[(M - 1 - 2 * p) * stride]};+ cmul(m->z + 2 * p, v, m->pre + 2 * p);+ }+ afft_run(m->f, m->z, m->zz);+ for (int q = 0; q < h; q++) {+ float t[2];+ cmul(t, m->zz + 2 * q, m->post + 2 * q);+ u[2 * q] = t[0];+ u[M - 1 - 2 * q] = -t[1];+ }+}++void aimdct_run(struct aimdct *m, const float *x, float *y)+{+ int n = m->n, h = n / 4;+ float *u = m->u;+ aimdct_dct4(m, x, 1, u);+ for (int i = 0; i < h; i++)+ y[i] = u[i + h];+ for (int i = h; i < 3 * h; i++)+ y[i] = -u[3 * h - 1 - i];+ for (int i = 3 * h; i < n; i++)+ y[i] = -u[i - 3 * h];+}diff --git a/src/common/audio_fft.h b/src/common/audio_fft.hnew file mode 100644index 0000000..d496f0a--- /dev/null+++ b/src/common/audio_fft.h@@ -0,0 +1,32 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_fft.h - the transforms the sound decoders share (audio_fft.c): a+ * complex FFT of any length made of the factors 2, 3, 4 and 5 (and others,+ * more slowly), and the inverse MDCT on it.+ */+#ifndef JANAS_COMMON_AUDIO_FFT_H+#define JANAS_COMMON_AUDIO_FFT_H++#include <stddef.h>++struct afft;++/* a forward FFT of n points (e^-i), or NULL when out of memory */+struct afft *afft_new(int n);+void afft_free(struct afft *f);+/* in and out: n complex numbers each, (re, im) pairs one after another */+void afft_run(const struct afft *f, const float *in, float *out);++/* The inverse MDCT of n / 2 coefficients into n samples, unscaled:+ y[i] = sum_k x[k] cos(2 pi / n (i + 1/2 + n / 4)(k + 1/2)); n a multiple+ of 4. */+struct aimdct;+struct aimdct *aimdct_new(int n);+void aimdct_free(struct aimdct *m);+void aimdct_run(struct aimdct *m, const float *x, float *y);+/* the DCT-IV the inverse MDCT is made of: u[m] = sum_k x[k]+ cos(pi / M (m + 1/2)(k + 1/2)), M = n / 2, x read every stride */+void aimdct_dct4(struct aimdct *m, const float *x, int stride, float *u);++#endifdiff --git a/src/common/audio_flac.c b/src/common/audio_flac.cnew file mode 100644index 0000000..718de6f--- /dev/null+++ b/src/common/audio_flac.c@@ -0,0 +1,371 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_flac.c - a FLAC file (audio.h), as RFC 9639 lays it out: the+ * "fLaC" marker, the metadata blocks (STREAMINFO read: rate, channels,+ * bits), then frames - a header checked by its CRC-8, a subframe a channel+ * (constant, verbatim, fixed predictors of order 0 to 4, linear predictors+ * of order 1 to 32, with their Rice-coded residuals and the bits a channel+ * wastes), the stereo decorrelations (left/side, side/right, mid/side), a+ * CRC-16 over the whole frame. A frame that does not check is skipped and+ * the next sync code sought, so a damaged file gives what it holds intact.+ * The samples are kept as 64-bit integers until the channels are averaged.+ */+#include "audio.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++/* ---- bits, most significant first ---- */++struct br {+ const uint8_t *p;+ size_t n; /* bytes */+ size_t pos; /* bits */+ int err; /* read past the end */+};++/* k bits (0 to 57) as an unsigned number */+static uint64_t bits(struct br *b, int k)+{+ if (!k)+ return 0;+ if (b->err || b->pos + (size_t)k > b->n * 8) {+ b->err = 1;+ b->pos = b->n * 8;+ return 0;+ }+ size_t at = b->pos >> 3;+ uint64_t w = 0;+ for (int i = 0; i < 8; i++)+ w = w << 8 | (at + (size_t)i < b->n ? b->p[at + (size_t)i] : 0);+ uint64_t v = (w << (b->pos & 7)) >> (64 - k);+ b->pos += (size_t)k;+ return v;+}++static int64_t sbits(struct br *b, int k)+{+ if (k == 0)+ return 0;+ uint64_t v = bits(b, k);+ if (k < 64 && v >> (k - 1))+ return (int64_t)(v - ((uint64_t)1 << k));+ return (int64_t)v;+}++/* zeros before a one */+static uint32_t unary(struct br *b)+{+ uint32_t q = 0;+ for (;;) {+ if (b->err || b->pos >= b->n * 8) {+ b->err = 1;+ return 0;+ }+ uint8_t byte = (uint8_t)(b->p[b->pos >> 3] << (b->pos & 7));+ if (byte) {+ int lead = __builtin_clz((unsigned)byte) - 24;+ q += (uint32_t)lead;+ b->pos += (size_t)lead + 1;+ return q;+ }+ uint32_t left = 8 - (uint32_t)(b->pos & 7);+ q += left;+ b->pos += left;+ if (q > (1u << 24)) { /* no residual is that long */+ b->err = 1;+ return 0;+ }+ }+}++/* ---- checks ---- */++static uint8_t crc8(const uint8_t *p, size_t n)+{+ uint8_t c = 0;+ for (size_t i = 0; i < n; i++) {+ c ^= p[i];+ for (int k = 0; k < 8; k++)+ c = (uint8_t)(c & 0x80 ? c << 1 ^ 0x07 : c << 1);+ }+ return c;+}++static uint16_t crc16(const uint8_t *p, size_t n)+{+ uint16_t c = 0;+ for (size_t i = 0; i < n; i++) {+ c ^= (uint16_t)(p[i] << 8);+ for (int k = 0; k < 8; k++)+ c = (uint16_t)(c & 0x8000 ? c << 1 ^ 0x8005 : c << 1);+ }+ return c;+}++/* ---- a subframe ---- */++static int residual(struct br *b, int64_t *s, uint32_t n, uint32_t order)+{+ uint32_t method = (uint32_t)bits(b, 2);+ if (method > 1)+ return -1;+ int pbits = method ? 5 : 4;+ uint32_t esc = method ? 31 : 15;+ uint32_t porder = (uint32_t)bits(b, 4), parts = 1u << porder;+ if ((n >> porder) << porder != n || (n >> porder) < order)+ return -1;+ uint32_t i = order;+ for (uint32_t p = 0; p < parts && !b->err; p++) {+ uint32_t end = (p + 1) * (n >> porder);+ uint32_t k = (uint32_t)bits(b, pbits);+ if (k == esc) {+ int raw = (int)bits(b, 5);+ for (; i < end; i++)+ s[i] = sbits(b, raw);+ continue;+ }+ for (; i < end && !b->err; i++) {+ uint64_t v = (uint64_t)unary(b) << k | bits(b, (int)k);+ s[i] = (int64_t)(v >> 1) ^ -(int64_t)(v & 1);+ }+ }+ return b->err ? -1 : 0;+}++static int subframe(struct br *b, int64_t *s, uint32_t n, int bps)+{+ if (bits(b, 1))+ return -1;+ uint32_t type = (uint32_t)bits(b, 6);+ int wasted = 0;+ if (bits(b, 1)) {+ uint32_t w = unary(b) + 1;+ if ((int)w >= bps)+ return -1;+ wasted = (int)w;+ bps -= wasted;+ }+ if (b->err)+ return -1;+ if (type == 0) {+ int64_t v = sbits(b, bps);+ for (uint32_t i = 0; i < n; i++)+ s[i] = v;+ } else if (type == 1) {+ for (uint32_t i = 0; i < n; i++)+ s[i] = sbits(b, bps);+ } else if (type >= 8 && type <= 12) {+ uint32_t order = type & 7;+ if (order > n)+ return -1;+ for (uint32_t i = 0; i < order; i++)+ s[i] = sbits(b, bps);+ if (residual(b, s, n, order))+ return -1;+ /* in 64 bits that wrap, as below */+ uint64_t *u = (uint64_t *)s;+ for (uint32_t i = order; i < n; i++)+ switch (order) {+ case 1:+ u[i] += u[i - 1];+ break;+ case 2:+ u[i] += 2 * u[i - 1] - u[i - 2];+ break;+ case 3:+ u[i] += 3 * u[i - 1] - 3 * u[i - 2] + u[i - 3];+ break;+ case 4:+ u[i] += 4 * u[i - 1] - 6 * u[i - 2] + 4 * u[i - 3] - u[i - 4];+ break;+ }+ } else if (type >= 32) {+ uint32_t order = (type & 31) + 1;+ if (order > n)+ return -1;+ for (uint32_t i = 0; i < order; i++)+ s[i] = sbits(b, bps);+ int prec = (int)bits(b, 4) + 1;+ int shift = (int)sbits(b, 5);+ if (prec == 16 || shift < 0)+ return -1;+ int64_t c[32];+ for (uint32_t i = 0; i < order; i++)+ c[i] = sbits(b, prec);+ if (residual(b, s, n, order))+ return -1;+ /* in 64 bits that wrap: a valid stream never needs more, a+ broken one must not overflow */+ for (uint32_t i = order; i < n; i++) {+ uint64_t sum = 0;+ for (uint32_t k = 0; k < order; k++)+ sum += (uint64_t)c[k] * (uint64_t)s[i - 1 - k];+ s[i] =+ (int64_t)((uint64_t)s[i] + (uint64_t)((int64_t)sum >> shift));+ }+ } else {+ return -1;+ }+ if (wasted)+ for (uint32_t i = 0; i < n; i++)+ s[i] = (int64_t)((uint64_t)s[i] << wasted);+ return b->err ? -1 : 0;+}++/* ---- frames ---- */++struct stream {+ uint32_t rate, ch, bps;+ float *out;+ size_t n, cap;+ int64_t *s[8];+};++/* the frame at d[at]: its length, or 0 when it is not one */+static size_t frame(struct stream *st, const uint8_t *d, size_t n, size_t at)+{+ struct br b = {.p = d + at, .n = n - at};+ if (bits(&b, 15) != 0x7FFC) /* sync and the reserved bit */+ return 0;+ bits(&b, 1); /* fixed or variable block size: the same to us */+ uint32_t bs = (uint32_t)bits(&b, 4), rc = (uint32_t)bits(&b, 4);+ uint32_t ca = (uint32_t)bits(&b, 4), sz = (uint32_t)bits(&b, 3);+ if (bits(&b, 1) || bs == 0 || rc == 15 || ca > 10 || sz == 3)+ return 0;+ /* the frame's number, coded as UTF-8 */+ uint32_t c0 = (uint32_t)bits(&b, 8), more = 0;+ while (more < 7 && (c0 << more & 0x80))+ more++;+ if (more == 1 || more > 7)+ return 0;+ for (uint32_t i = 1; i < more; i++)+ if ((bits(&b, 8) & 0xC0) != 0x80)+ return 0;+ uint32_t block = bs == 1 ? 192+ : bs <= 5 ? 576u << (bs - 2)+ : bs == 6 ? (uint32_t)bits(&b, 8) + 1+ : bs == 7 ? (uint32_t)bits(&b, 16) + 1+ : 256u << (bs - 8);+ if (rc == 12)+ bits(&b, 8);+ else if (rc == 13 || rc == 14)+ bits(&b, 16);+ size_t hdr = b.pos / 8;+ if (b.err || bits(&b, 8) != crc8(d + at, hdr))+ return 0;+ static const uint32_t SZ[8] = {0, 8, 12, 0, 16, 20, 24, 32};+ uint32_t bps = sz ? SZ[sz] : st->bps;+ uint32_t ch = ca < 8 ? ca + 1 : 2;+ if (ch != st->ch || bps != st->bps || block > 65535)+ return 0;+ for (uint32_t c = 0; c < ch; c++) {+ int side =+ (ca == 8 && c == 1) || (ca == 9 && c == 0) || (ca == 10 && c == 1);+ if (subframe(&b, st->s[c], block, (int)bps + side))+ return 0;+ }+ b.pos = (b.pos + 7) & ~(size_t)7;+ size_t len = b.pos / 8;+ if (bits(&b, 16) != crc16(d + at, len) || b.err)+ return 0;+ int64_t *L = st->s[0], *R = st->s[1];+ for (uint32_t i = 0; ch == 2 && ca >= 8 && i < block; i++) {+ if (ca == 8) {+ R[i] = L[i] - R[i];+ } else if (ca == 9) {+ L[i] += R[i];+ } else {+ int64_t mid = (int64_t)((uint64_t)L[i] << 1) | (R[i] & 1);+ L[i] = (mid + R[i]) >> 1;+ R[i] = (mid - R[i]) >> 1;+ }+ }+ if (st->n + block > JANAS_AUDIO_MAX_SAMPLES)+ return 0;+ if (st->n + block > st->cap) {+ size_t cap = st->cap ? st->cap * 2 : 1 << 16;+ while (cap < st->n + block)+ cap *= 2;+ float *o = realloc(st->out, sizeof(float) * cap);+ if (!o)+ return 0;+ st->out = o;+ st->cap = cap;+ }+ double scale = 1.0 / ((double)((uint64_t)1 << (bps - 1)) * ch);+ for (uint32_t i = 0; i < block; i++) {+ double v = 0;+ for (uint32_t c = 0; c < ch; c++)+ v += (double)st->s[c][i];+ st->out[st->n + i] = (float)(v * scale);+ }+ st->n += block;+ return len + 2;+}++int janas_audio_flac(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn)+{+ if (n < 4 || memcmp(d, "fLaC", 4))+ return 1;+ struct stream st = {0};+ size_t at = 4;+ int last = 0, info = 0;+ while (!last) {+ if (at + 4 > n) {+ snprintf(why, wn, "a FLAC file cut short in its metadata");+ return -1;+ }+ last = d[at] >> 7;+ uint32_t type = d[at] & 0x7F;+ size_t len =+ (size_t)d[at + 1] << 16 | (size_t)d[at + 2] << 8 | d[at + 3];+ at += 4;+ if (len > n - at) {+ snprintf(why, wn, "a FLAC file cut short in its metadata");+ return -1;+ }+ if (type == 0 && len >= 34) {+ struct br b = {.p = d + at, .n = len};+ b.pos += 80; /* block and frame sizes */+ st.rate = (uint32_t)bits(&b, 20);+ st.ch = (uint32_t)bits(&b, 3) + 1;+ st.bps = (uint32_t)bits(&b, 5) + 1;+ info = 1;+ }+ at += len;+ }+ if (!info || !st.rate || st.bps < 4) {+ snprintf(why, wn, "a FLAC file without its stream's description");+ return -1;+ }+ for (uint32_t c = 0; c < st.ch; c++)+ if (!(st.s[c] = malloc(sizeof(int64_t) * 65536))) {+ for (uint32_t k = 0; k < c; k++)+ free(st.s[k]);+ snprintf(why, wn, "out of memory");+ return -1;+ }+ while (at + 2 <= n) {+ if (d[at] != 0xFF || (d[at + 1] & 0xFE) != 0xF8) {+ at++;+ continue;+ }+ size_t len = frame(&st, d, n, at);+ at += len ? len : 1;+ }+ for (uint32_t c = 0; c < st.ch; c++)+ free(st.s[c]);+ if (!st.n) {+ free(st.out);+ snprintf(why, wn, "a FLAC file without a frame this reads");+ return -1;+ }+ a->pcm = st.out;+ a->n = st.n;+ a->rate = st.rate;+ return 0;+}diff --git a/src/common/audio_mkv.c b/src/common/audio_mkv.cnew file mode 100644index 0000000..a27fc96--- /dev/null+++ b/src/common/audio_mkv.c@@ -0,0 +1,667 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_mkv.c - Matroska and WebM: the EBML elements, sizes unknown too+ * (what browsers' MediaRecorder writes while it records), the first track+ * of sound with its codec and private data, the blocks of its clusters+ * (simple or in groups, without lacing or with Xiph, EBML or fixed-size+ * lacing), Opus's codec delay and the discard padding of its last block.+ * The codecs: Opus, Vorbis, FLAC, AAC, MPEG audio, ALAC and PCM.+ */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "audio.h"+#include "audio_aac.h"+#include "audio_ogg.h"++#define ID_EBML 0x1A45DFA3+#define ID_SEGMENT 0x18538067+#define ID_INFO 0x1549A966+#define ID_TIMESCALE 0x2AD7B1+#define ID_TRACKS 0x1654AE6B+#define ID_TRACKENTRY 0xAE+#define ID_TRACKNUMBER 0xD7+#define ID_TRACKTYPE 0x83+#define ID_CODECID 0x86+#define ID_CODECPRIVATE 0x63A2+#define ID_CODECDELAY 0x56AA+#define ID_AUDIO 0xE1+#define ID_RATE 0xB5+#define ID_CHANNELS 0x9F+#define ID_BITDEPTH 0x6264+#define ID_CLUSTER 0x1F43B675+#define ID_SIMPLEBLOCK 0xA3+#define ID_BLOCKGROUP 0xA0+#define ID_BLOCK 0xA1+#define ID_DISCARDPADDING 0x75A2+#define ID_CONTENTENCODINGS 0x6D80++#define UNKNOWN ((uint64_t)-1)++struct ebml {+ const uint8_t *p, *end;+};++/* a variable-length integer: its value (the marker removed when strip),+ its length in *len; -1 when cut short */+static int vint(const uint8_t *p, const uint8_t *end, int strip, uint64_t *v,+ int *len)+{+ if (p >= end || !*p)+ return -1;+ int l = 1;+ while (!(*p & (0x80 >> (l - 1))))+ l++;+ if (l > 8 || end - p < l)+ return -1;+ uint64_t x = strip ? *p & (0xFF >> l) : *p;+ int all_ones = strip && x == (uint64_t)(0xFF >> l);+ for (int i = 1; i < l; i++) {+ x = x << 8 | p[i];+ if (p[i] != 0xFF)+ all_ones = 0;+ }+ *v = all_ones ? UNKNOWN : x;+ *len = l;+ return 0;+}++/* the next element's id and payload: 0, or -1 */+static int element(struct ebml *e, uint32_t *id, const uint8_t **body,+ uint64_t *size)+{+ uint64_t v;+ int l1, l2;+ if (vint(e->p, e->end, 0, &v, &l1) != 0 || l1 > 4)+ return -1;+ *id = (uint32_t)v;+ if (vint(e->p + l1, e->end, 1, size, &l2) != 0)+ return -1;+ *body = e->p + l1 + l2;+ if (*size != UNKNOWN && *size > (uint64_t)(e->end - *body))+ *size = (uint64_t)(e->end - *body); /* a file cut short */+ return 0;+}++static uint64_t uint_of(const uint8_t *p, uint64_t n)+{+ uint64_t v = 0;+ for (uint64_t i = 0; i < n && i < 8; i++)+ v = v << 8 | p[i];+ return v;+}++static double float_of(const uint8_t *p, uint64_t n)+{+ if (n == 4) {+ uint32_t u = (uint32_t)uint_of(p, 4);+ float f;+ memcpy(&f, &u, 4);+ return f;+ }+ if (n == 8) {+ uint64_t u = uint_of(p, 8);+ double d;+ memcpy(&d, &u, 8);+ return d;+ }+ return 0;+}++struct mtrack {+ uint64_t number;+ char codec[64];+ const uint8_t *priv;+ size_t priv_len;+ uint64_t codec_delay; /* ns */+ double rate;+ int channels, bits;+ int encoded; /* content encodings: compression or encryption */+};++struct mframes {+ size_t *off, *len;+ size_t count, cap;+ int64_t discard; /* the last block's discard padding, ns */+};++static int add_frame(struct mframes *f, size_t off, size_t len)+{+ if (f->count == f->cap) {+ size_t c = f->cap ? f->cap * 2 : 1024;+ size_t *o = realloc(f->off, sizeof(size_t) * c);+ if (o)+ f->off = o;+ size_t *l = realloc(f->len, sizeof(size_t) * c);+ if (l)+ f->len = l;+ if (!o || !l)+ return -1;+ f->cap = c;+ }+ f->off[f->count] = off;+ f->len[f->count] = len;+ f->count++;+ return 0;+}++static void read_track(const uint8_t *p, const uint8_t *end, struct mtrack *t)+{+ struct ebml e = {p, end};+ memset(t, 0, sizeof *t);+ int type = 0;+ while (e.p < e.end) {+ uint32_t id;+ const uint8_t *b;+ uint64_t n;+ if (element(&e, &id, &b, &n) != 0 || n == UNKNOWN)+ break;+ switch (id) {+ case ID_TRACKNUMBER:+ t->number = uint_of(b, n);+ break;+ case ID_TRACKTYPE:+ type = (int)uint_of(b, n);+ break;+ case ID_CODECID: {+ size_t l =+ n < sizeof t->codec - 1 ? (size_t)n : sizeof t->codec - 1;+ memcpy(t->codec, b, l);+ t->codec[l] = 0;+ break;+ }+ case ID_CODECPRIVATE:+ t->priv = b;+ t->priv_len = (size_t)n;+ break;+ case ID_CODECDELAY:+ t->codec_delay = uint_of(b, n);+ break;+ case ID_CONTENTENCODINGS:+ t->encoded = 1;+ break;+ case ID_AUDIO: {+ struct ebml a = {b, b + n};+ while (a.p < a.end) {+ uint32_t aid;+ const uint8_t *ab;+ uint64_t an;+ if (element(&a, &aid, &ab, &an) != 0 || an == UNKNOWN)+ break;+ if (aid == ID_RATE)+ t->rate = float_of(ab, an);+ else if (aid == ID_CHANNELS)+ t->channels = (int)uint_of(ab, an);+ else if (aid == ID_BITDEPTH)+ t->bits = (int)uint_of(ab, an);+ a.p = ab + an;+ }+ break;+ }+ }+ e.p = b + n;+ }+ if (type != 2)+ t->number = 0;+}++/* a block's frames of the track: 0, or -1 */+static int read_block(const uint8_t *file, const uint8_t *p, uint64_t n,+ uint64_t track, struct mframes *f, int *ours)+{+ const uint8_t *end = p + n;+ uint64_t num;+ int l;+ *ours = 0;+ if (vint(p, end, 1, &num, &l) != 0 || num != track)+ return 0;+ *ours = 1;+ p += l;+ if (end - p < 3)+ return -1;+ int flags = p[2];+ p += 3;+ int lacing = (flags >> 1) & 3;+ if (!lacing)+ return add_frame(f, (size_t)(p - file), (size_t)(end - p));+ if (p >= end)+ return -1;+ int count = *p++ + 1;+ size_t sizes[256], total = 0;+ if (lacing == 1) { /* Xiph */+ for (int i = 0; i < count - 1; i++) {+ size_t s = 0;+ int c;+ do {+ if (p >= end)+ return -1;+ c = *p++;+ s += (size_t)c;+ } while (c == 255);+ sizes[i] = s;+ total += s;+ }+ } else if (lacing == 3) { /* EBML */+ uint64_t s;+ if (vint(p, end, 1, &s, &l) != 0)+ return -1;+ p += l;+ sizes[0] = (size_t)s;+ total = (size_t)s;+ for (int i = 1; i < count - 1; i++) {+ uint64_t raw;+ if (vint(p, end, 1, &raw, &l) != 0)+ return -1;+ p += l;+ int64_t diff = (int64_t)raw - (((int64_t)1 << (7 * l - 1)) - 1);+ int64_t next = (int64_t)sizes[i - 1] + diff;+ if (next < 0)+ return -1;+ sizes[i] = (size_t)next;+ total += (size_t)next;+ }+ } else { /* fixed */+ size_t left = (size_t)(end - p);+ if (left % (size_t)count)+ return -1;+ for (int i = 0; i < count - 1; i++) {+ sizes[i] = left / (size_t)count;+ total += sizes[i];+ }+ }+ if (total > (size_t)(end - p))+ return -1;+ sizes[count - 1] = (size_t)(end - p) - total;+ for (int i = 0; i < count; i++) {+ if (add_frame(f, (size_t)(p - file), sizes[i]) != 0)+ return -1;+ p += sizes[i];+ }+ return 0;+}++/* a cluster's blocks; an unknown size ends at the next element that is+ not a cluster's: its end */+static const uint8_t *read_cluster(const uint8_t *file, const uint8_t *p,+ const uint8_t *end, uint64_t track,+ struct mframes *f, int *err)+{+ struct ebml e = {p, end};+ while (e.p < e.end) {+ uint32_t id;+ const uint8_t *b;+ uint64_t n;+ if (element(&e, &id, &b, &n) != 0)+ return e.end;+ if (id == ID_CLUSTER || id == ID_TRACKS || id == 0x1C53BB6B ||+ id == 0x1254C367 || id == 0x114D9B74 || id == ID_INFO ||+ id == 0x1941A469 || id == 0x1043A770)+ return e.p; /* the next top-level element */+ if (n == UNKNOWN)+ return e.end;+ int ours;+ if (id == ID_SIMPLEBLOCK) {+ if (read_block(file, b, n, track, f, &ours) != 0)+ *err = 1;+ } else if (id == ID_BLOCKGROUP) {+ struct ebml g = {b, b + n};+ int64_t discard = 0;+ ours = 0;+ while (g.p < g.end) {+ uint32_t gid;+ const uint8_t *gb;+ uint64_t gn;+ if (element(&g, &gid, &gb, &gn) != 0 || gn == UNKNOWN)+ break;+ if (gid == ID_BLOCK) {+ if (read_block(file, gb, gn, track, f, &ours) != 0)+ *err = 1;+ } else if (gid == ID_DISCARDPADDING && gn <= 8 && gn) {+ uint64_t u = uint_of(gb, gn);+ if (gn < 8 && u >> (8 * gn - 1))+ u |= ~(uint64_t)0 << (8 * gn);+ discard = (int64_t)u;+ }+ g.p = gb + gn;+ }+ if (ours)+ f->discard = discard;+ }+ e.p = b + n;+ }+ return e.end;+}++/* Vorbis's private data: the three headers, Xiph-laced */+static int vorbis_headers(const uint8_t *p, size_t n, struct ogg_packets *o)+{+ if (n < 1 || p[0] != 2)+ return -1;+ size_t at = 1, s[2];+ for (int i = 0; i < 2; i++) {+ size_t v = 0;+ int c;+ do {+ if (at >= n)+ return -1;+ c = p[at++];+ v += (size_t)c;+ } while (c == 255);+ s[i] = v;+ }+ if (s[0] + s[1] > n - at)+ return -1;+ if (ogg_add_packet(o, p + at, s[0], -1) != 0 ||+ ogg_add_packet(o, p + at + s[0], s[1], -1) != 0 ||+ ogg_add_packet(o, p + at + s[0] + s[1], n - at - s[0] - s[1], -1) != 0)+ return -1;+ return 0;+}++static int concat_decode(const uint8_t *head, size_t hn, const uint8_t *d,+ const struct mframes *f,+ int (*dec)(const uint8_t *, size_t,+ struct janas_audio *, char *, size_t),+ struct janas_audio *a, char *why, size_t wn)+{+ size_t total = hn;+ for (size_t i = 0; i < f->count; i++)+ total += f->len[i];+ uint8_t *buf = malloc(total ? total : 1);+ if (!buf) {+ snprintf(why, wn, "out of memory");+ return -1;+ }+ if (hn)+ memcpy(buf, head, hn);+ size_t at = hn;+ for (size_t i = 0; i < f->count; i++) {+ memcpy(buf + at, d + f->off[i], f->len[i]);+ at += f->len[i];+ }+ int rc = dec(buf, total, a, why, wn);+ free(buf);+ if (rc > 0) {+ snprintf(why, wn, "a Matroska track that is not what it says");+ rc = -1;+ }+ return rc;+}++/* the AudioSpecificConfig of an old A_AAC/MPEGn/PROFILE[/SBR] codec id */+static size_t aac_config_of(const struct mtrack *t, uint8_t *asc)+{+ int aot = strstr(t->codec, "/MAIN") ? 1+ : strstr(t->codec, "/LTP") ? 4+ : strstr(t->codec, "/SSR") ? 3+ : 2;+ int sbr = strstr(t->codec, "/SBR") != NULL;+ int rate = (int)t->rate, core = sbr ? rate / 2 : rate;+ int idx = aac_rate_index(core), ch = t->channels ? t->channels : 2;+ asc[0] = (uint8_t)(aot << 3 | idx >> 1);+ asc[1] = (uint8_t)((idx & 1) << 7 | (ch & 15) << 3);+ if (!sbr)+ return 2;+ int eidx = aac_rate_index(rate);+ asc[2] = 0x56; /* sync extension 0x2b7, SBR */+ asc[3] = 0xE5;+ asc[4] = (uint8_t)(0x80 | eidx << 3);+ return 5;+}++static int pcm_decode(const struct mtrack *t, const uint8_t *d,+ const struct mframes *f, struct janas_audio *a, char *why,+ size_t wn)+{+ int big = strstr(t->codec, "BIG") != NULL;+ int fl = strstr(t->codec, "FLOAT") != NULL;+ int bits = t->bits ? t->bits : 16, ch = t->channels ? t->channels : 1;+ int bps = bits / 8;+ if (bps < 1 || bps > 8 || bits % 8 || ch > 64 || !t->rate ||+ (fl && bps != 4 && bps != 8)) {+ snprintf(why, wn, "a Matroska track of PCM out of range");+ return -1;+ }+ size_t frames = 0;+ for (size_t i = 0; i < f->count; i++)+ frames += f->len[i] / ((size_t)bps * (size_t)ch);+ if (frames > JANAS_AUDIO_MAX_SAMPLES) {+ snprintf(why, wn, "a sound longer than this reads");+ return -1;+ }+ float *pcm = malloc(sizeof(float) * (frames ? frames : 1));+ if (!pcm) {+ snprintf(why, wn, "out of memory");+ return -1;+ }+ size_t at = 0;+ for (size_t i = 0; i < f->count; i++) {+ const uint8_t *p = d + f->off[i];+ size_t nf = f->len[i] / ((size_t)bps * (size_t)ch);+ for (size_t k = 0; k < nf; k++) {+ double s = 0;+ for (int c = 0; c < ch; c++, p += bps) {+ uint64_t u = 0;+ for (int j = 0; j < bps; j++)+ u |= (uint64_t)p[big ? j : bps - 1 - j]+ << (8 * (bps - 1 - j));+ if (fl && bps == 4) {+ uint32_t w = (uint32_t)u;+ float x;+ memcpy(&x, &w, 4);+ s += x;+ } else if (fl) {+ double x;+ memcpy(&x, &u, 8);+ s += x;+ } else if (bps == 1) {+ s += ((double)u - 128) / 128;+ } else {+ int64_t v = (int64_t)(u << (64 - bits)) >> (64 - bits);+ s += (double)v / (double)((int64_t)1 << (bits - 1));+ }+ }+ pcm[at++] = (float)(s / ch);+ }+ }+ a->pcm = pcm;+ a->n = at;+ a->rate = (uint32_t)t->rate;+ return 0;+}++int janas_audio_mkv(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn)+{+ if (n < 4 || memcmp(d, "\x1a\x45\xdf\xa3", 4))+ return 1;+ struct ebml top = {d, d + n};+ const uint8_t *seg = NULL, *seg_end = NULL;+ while (top.p < top.end) {+ uint32_t id;+ const uint8_t *b;+ uint64_t s;+ if (element(&top, &id, &b, &s) != 0)+ break;+ if (id == ID_SEGMENT) {+ seg = b;+ seg_end = s == UNKNOWN ? d + n : b + s;+ break;+ }+ if (s == UNKNOWN)+ break;+ top.p = b + s;+ }+ if (!seg) {+ snprintf(why, wn, "a Matroska file without a segment");+ return -1;+ }+ struct mtrack t;+ memset(&t, 0, sizeof t);+ struct mframes f;+ memset(&f, 0, sizeof f);+ int found = 0, err = 0, rc = -1;+ struct ebml e = {seg, seg_end};+ while (e.p < e.end) {+ uint32_t id;+ const uint8_t *b;+ uint64_t s;+ if (element(&e, &id, &b, &s) != 0)+ break;+ if (id == ID_TRACKS && !found && s != UNKNOWN) {+ struct ebml tr = {b, b + s};+ while (tr.p < tr.end && !found) {+ uint32_t tid;+ const uint8_t *tb;+ uint64_t tn;+ if (element(&tr, &tid, &tb, &tn) != 0 || tn == UNKNOWN)+ break;+ if (tid == ID_TRACKENTRY) {+ read_track(tb, tb + tn, &t);+ found = t.number != 0;+ }+ tr.p = tb + tn;+ }+ e.p = b + s;+ } else if (id == ID_CLUSTER) {+ if (!found) {+ if (s == UNKNOWN)+ break;+ e.p = b + s;+ continue;+ }+ const uint8_t *cend = s == UNKNOWN ? e.end : b + s;+ const uint8_t *next = read_cluster(d, b, cend, t.number, &f, &err);+ if (f.count > JANAS_AUDIO_MAX_SAMPLES / 64) {+ snprintf(why, wn, "a Matroska file of too many blocks");+ goto end;+ }+ e.p = s == UNKNOWN ? next : b + s;+ } else {+ if (s == UNKNOWN)+ break;+ e.p = b + s;+ }+ }+ if (!found) {+ snprintf(why, wn, "a Matroska file without a track of sound");+ goto end;+ }+ if (t.encoded) {+ snprintf(why, wn,+ "a Matroska track whose blocks are compressed or "+ "encrypted, not read");+ goto end;+ }+ if (!f.count) {+ snprintf(why, wn, "a Matroska track of sound without blocks");+ goto end;+ }+ const char *c = t.codec;+ if (!strcmp(c, "A_OPUS")) {+ struct ogg_packets o;+ memset(&o, 0, sizeof o);+ o.codec = 2;+ o.pre_skip = t.codec_delay+ ? (int64_t)((t.codec_delay * 48000 + 500000000ull) /+ 1000000000ull)+ : -1;+ if (!t.priv || ogg_add_packet(&o, t.priv, t.priv_len, -1) != 0) {+ ogg_packets_free(&o);+ snprintf(why, wn, "a Matroska track of Opus without its header");+ goto end;+ }+ int64_t total = 0;+ for (size_t i = 0; i < f.count; i++)+ total += janas_opus_packet_samples(d + f.off[i], f.len[i]);+ int64_t last = -1;+ if (f.discard > 0)+ last = total - (f.discard * 48000 + 500000000) / 1000000000;+ for (size_t i = 0; i < f.count; i++)+ if (ogg_add_packet(&o, d + f.off[i], f.len[i],+ i + 1 == f.count ? last : -1) != 0) {+ ogg_packets_free(&o);+ snprintf(why, wn, "out of memory");+ goto end;+ }+ rc = janas_opus_stream(&o, a, why, wn);+ ogg_packets_free(&o);+ } else if (!strcmp(c, "A_VORBIS")) {+ struct ogg_packets o;+ memset(&o, 0, sizeof o);+ o.codec = 1;+ o.pre_skip = -1;+ if (!t.priv || vorbis_headers(t.priv, t.priv_len, &o) != 0) {+ ogg_packets_free(&o);+ snprintf(why, wn, "a Matroska track of Vorbis without its headers");+ goto end;+ }+ for (size_t i = 0; i < f.count; i++)+ if (ogg_add_packet(&o, d + f.off[i], f.len[i], -1) != 0) {+ ogg_packets_free(&o);+ snprintf(why, wn, "out of memory");+ goto end;+ }+ rc = janas_vorbis_stream(&o, a, why, wn);+ ogg_packets_free(&o);+ } else if (!strcmp(c, "A_FLAC")) {+ if (!t.priv || t.priv_len < 4 + 4 + 34 || memcmp(t.priv, "fLaC", 4)) {+ snprintf(why, wn, "a Matroska track of FLAC without its header");+ goto end;+ }+ rc = concat_decode(t.priv, t.priv_len, d, &f, janas_audio_flac, a, why,+ wn);+ } else if (!strncmp(c, "A_AAC", 5)) {+ struct aac_cfg cfg;+ uint8_t asc[8];+ const uint8_t *cp = t.priv;+ size_t cn = t.priv_len;+ if (!cp || !cn) {+ cn = aac_config_of(&t, asc);+ cp = asc;+ }+ if (aac_config(cp, cn, &cfg, why, wn) != 0)+ goto end;+ rc = aac_stream(&cfg, d, f.off, f.len, f.count, NULL, a, why, wn);+ } else if (!strncmp(c, "A_MPEG/L", 8)) {+ rc = concat_decode(NULL, 0, d, &f, janas_audio_mpeg, a, why, wn);+ } else if (!strcmp(c, "A_ALAC")) {+ if (!t.priv) {+ snprintf(why, wn,+ "a Matroska track of ALAC without its configuration");+ goto end;+ }+ rc = janas_alac_stream(t.priv, t.priv_len, d, f.off, f.len, f.count,+ NULL, a, why, wn);+ } else if (!strncmp(c, "A_PCM/", 6)) {+ rc = pcm_decode(&t, d, &f, a, why, wn);+ } else {+ snprintf(why, wn, "a Matroska track of a codec not read (%s)",+ c[0] ? c : "none named");+ }+ /* the codec delay and the last block's discard padding, for the+ codecs that do not take them themselves (Opus does) */+ if (rc == 0 && strcmp(c, "A_OPUS") && (t.codec_delay || f.discard > 0)) {+ uint64_t skip =+ (t.codec_delay * a->rate + 500000000ull) / 1000000000ull;+ uint64_t cut =+ f.discard > 0+ ? ((uint64_t)f.discard * a->rate + 500000000ull) / 1000000000ull+ : 0;+ if (skip + cut >= a->n) {+ janas_audio_free(a);+ snprintf(why, wn, "a Matroska track without sound");+ rc = -1;+ } else {+ memmove(a->pcm, a->pcm + skip, sizeof(float) * (a->n - skip - cut));+ a->n -= (size_t)(skip + cut);+ }+ }+end:+ (void)err;+ free(f.off);+ free(f.len);+ return rc;+}diff --git a/src/common/audio_mp2.c b/src/common/audio_mp2.cnew file mode 100644index 0000000..9dba923--- /dev/null+++ b/src/common/audio_mp2.c@@ -0,0 +1,175 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_mp2.c - MPEG audio Layers I and II (audio_mpa.h), as ISO/IEC+ * 11172-3 and 13818-3 describe them: the bit allocation (Layer II by the+ * table the bit rate, the rate and the channels choose, the subbands above+ * the joint stereo bound shared), the scale factors (Layer II's three+ * with their selection information), the samples (Layer II's of 3, 5 and 9+ * levels grouped three to a code), requantized to (2m - (N - 1)) / N times+ * 2^(1 - i / 3), and the polyphase filter bank.+ */+#include <math.h>+#include <string.h>++#include "audio_mpa.h"+#include "audio_mpa_tab.h"++static float level(uint32_t m, uint32_t steps, int sf)+{+ return (float)((2.0 * m - (steps - 1)) / steps * exp2(1 - sf / 3.0));+}++static int layer1(struct mpa_br *b, const struct mpa_hdr *h,+ struct mpa_synth *s, float (*pcm)[1152])+{+ int nch = h->ch, bound = 32;+ if (h->mode == MPA_MODE_JOINT)+ bound = (h->mode_ext + 1) * 4;+ int alloc[2][32] = {{0}}, sf[2][32] = {{0}};+ for (int sb = 0; sb < 32; sb++)+ for (int ch = 0; ch < nch; ch++) {+ if (sb >= bound && ch == 1) {+ alloc[1][sb] = alloc[0][sb];+ continue;+ }+ alloc[ch][sb] = (int)mpa_bits(b, 4);+ if (alloc[ch][sb] == 15)+ return -1;+ }+ for (int sb = 0; sb < 32; sb++)+ for (int ch = 0; ch < nch; ch++)+ if (alloc[ch][sb])+ sf[ch][sb] = (int)mpa_bits(b, 6);+ for (int t = 0; t < 12; t++) {+ float sbv[2][32];+ for (int sb = 0; sb < 32; sb++) {+ uint32_t m = 0;+ for (int ch = 0; ch < nch; ch++) {+ int a = alloc[ch][sb];+ if (!a) {+ sbv[ch][sb] = 0;+ continue;+ }+ if (sb < bound || ch == 0)+ m = mpa_bits(b, a + 1);+ sbv[ch][sb] = level(m, (1u << (a + 1)) - 1, sf[ch][sb]);+ }+ }+ for (int ch = 0; ch < nch; ch++)+ mpa_synth(s, ch, sbv[ch], pcm[ch] + 32 * t);+ }+ return 384;+}++static int l2_table(const struct mpa_hdr *h)+{+ int chr = h->bitrate / h->ch;+ if (h->lsf)+ return 4;+ if ((h->rate == 48000 && chr >= 56) || (chr >= 56 && chr <= 80))+ return 0;+ if (h->rate != 48000 && chr >= 96)+ return 1;+ if (h->rate != 32000 && chr <= 48)+ return 2;+ return 3;+}++static int layer2(struct mpa_br *b, const struct mpa_hdr *h,+ struct mpa_synth *s, float (*pcm)[1152])+{+ int table = l2_table(h), nch = h->ch;+ int sblimit = MPA_SBLIMIT[table];+ const uint8_t *tab = table < 2 ? MPA_ALLOC_1+ : table < 4 ? MPA_ALLOC_3+ : MPA_ALLOC_4;+ int bound = sblimit;+ if (h->mode == MPA_MODE_JOINT && (h->mode_ext + 1) * 4 < sblimit)+ bound = (h->mode_ext + 1) * 4;+ int qidx[2][32] = {{0}}, scfsi[2][32] = {{0}}, sf[2][32][3];+ const uint8_t *t = tab;+ for (int sb = 0; sb < sblimit; sb++) {+ int nb = t[0];+ for (int ch = 0; ch < nch; ch++) {+ if (sb >= bound && ch == 1) {+ qidx[1][sb] = qidx[0][sb];+ continue;+ }+ int a = (int)mpa_bits(b, nb);+ qidx[ch][sb] = a ? t[a] + 1 : 0; /* 0: no samples */+ }+ t += 1 << nb; /* the row: its width, then the classes */+ }+ for (int sb = 0; sb < sblimit; sb++)+ for (int ch = 0; ch < nch; ch++)+ if (qidx[ch][sb])+ scfsi[ch][sb] = (int)mpa_bits(b, 2);+ for (int sb = 0; sb < sblimit; sb++)+ for (int ch = 0; ch < nch; ch++) {+ int *f = sf[ch][sb];+ if (!qidx[ch][sb])+ continue;+ switch (scfsi[ch][sb]) {+ case 0:+ f[0] = (int)mpa_bits(b, 6);+ f[1] = (int)mpa_bits(b, 6);+ f[2] = (int)mpa_bits(b, 6);+ break;+ case 1:+ f[0] = f[1] = (int)mpa_bits(b, 6);+ f[2] = (int)mpa_bits(b, 6);+ break;+ case 2:+ f[0] = f[1] = f[2] = (int)mpa_bits(b, 6);+ break;+ default:+ f[0] = (int)mpa_bits(b, 6);+ f[1] = f[2] = (int)mpa_bits(b, 6);+ break;+ }+ }+ for (int gr = 0; gr < 12; gr++) {+ float sbv[2][3][32];+ memset(sbv, 0, sizeof sbv);+ for (int sb = 0; sb < sblimit; sb++) {+ uint32_t m[3] = {0, 0, 0};+ for (int ch = 0; ch < nch; ch++) {+ int q = qidx[ch][sb];+ if (!q)+ continue;+ uint32_t steps = MPA_QSTEPS[q - 1];+ int bits = MPA_QBITS[q - 1];+ if (sb < bound || ch == 0) {+ if (bits < 0) { /* three to a code */+ uint32_t c = mpa_bits(b, -bits);+ m[0] = c % steps;+ c /= steps;+ m[1] = c % steps;+ m[2] = c / steps;+ } else {+ for (int k = 0; k < 3; k++)+ m[k] = mpa_bits(b, bits);+ }+ }+ for (int k = 0; k < 3; k++)+ sbv[ch][k][sb] = level(m[k] < steps ? m[k] : steps - 1,+ steps, sf[ch][sb][gr / 4]);+ }+ }+ for (int k = 0; k < 3; k++)+ for (int ch = 0; ch < nch; ch++)+ mpa_synth(s, ch, sbv[ch][k], pcm[ch] + 32 * (3 * gr + k));+ }+ return 1152;+}++int mpa_layer12(const uint8_t *f, size_t n, const struct mpa_hdr *h,+ struct mpa_synth *s, float (*pcm)[1152])+{+ size_t at = 4 + (h->crc ? 2 : 0);+ if (n <= at)+ return -1;+ struct mpa_br b = {.p = f + at, .n = n - at};+ return h->layer == 1 ? layer1(&b, h, s, pcm) : layer2(&b, h, s, pcm);+}diff --git a/src/common/audio_mp3.c b/src/common/audio_mp3.cnew file mode 100644index 0000000..6139f2e--- /dev/null+++ b/src/common/audio_mp3.c@@ -0,0 +1,838 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_mp3.c - MPEG audio files (audio.h): MPEG-1, 2 and 2.5, Layer III+ * here, Layers I and II in audio_mp2.c, as ISO/IEC 11172-3 and 13818-3+ * describe them, the tables of the standards in audio_mpa_tab.h.+ *+ * Layer III: the side information, the bit reservoir (a frame's granules+ * may begin in the frames before it), the scale factors (MPEG-1's, with the+ * bands they share between granules; MPEG-2's, with the intensity stereo+ * variant), the Huffman-coded big values and quadruples, requantization+ * (|x|^4/3 by the global gain, the scale factors, the pre-emphasis and the+ * subblock gains), mid/side and intensity stereo, the short blocks+ * reordered, the aliasing butterflies, the IMDCT of 36 or three of 12 with+ * their windows and the overlap, the frequency inversion, and the+ * polyphase filter bank. The file: an ID3v2 tag skipped (audio.c), frames+ * found by their sync and checked by the next one, free format, the Xing,+ * Info or VBRI frame skipped and LAME's encoder delay and padding taken off+ * as FFmpeg does (529 samples of the decoder's own delay with them). The+ * channels are averaged.+ */+#include "audio.h"++#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "audio_mpa.h"+#include "audio_mpa_tab.h"++/* ---- the header and the synthesis ---- */++int mpa_header(const uint8_t *p, struct mpa_hdr *h)+{+ static const int FREQ[3] = {44100, 48000, 32000};+ if (p[0] != 0xFF || (p[1] & 0xE0) != 0xE0)+ return -1;+ int ver = (p[1] >> 3) & 3, layer = (p[1] >> 1) & 3;+ int br = p[2] >> 4, sr = (p[2] >> 2) & 3;+ /* the emphasis "reserved" (10) is let through: the standard's own+ test streams have it */+ if (ver == 1 || layer == 0 || br == 15 || sr == 3)+ return -1;+ int v25 = ver == 0;+ h->lsf = ver != 3;+ h->layer = 4 - layer;+ h->crc = !(p[1] & 1);+ h->rate_index = sr + 3 * (h->lsf + v25);+ h->rate = FREQ[sr] >> (h->lsf + v25);+ h->bitrate = MPA_BITRATE[h->lsf * 45 + (h->layer - 1) * 15 + br];+ h->pad = (p[2] >> 1) & 1;+ h->mode = p[3] >> 6;+ h->mode_ext = (p[3] >> 4) & 3;+ h->ch = h->mode == MPA_MODE_MONO ? 1 : 2;+ if (!h->bitrate)+ h->size = 0;+ else if (h->layer == 1)+ h->size = ((size_t)12000 * h->bitrate / h->rate + h->pad) * 4;+ else if (h->layer == 2 || !h->lsf)+ h->size = (size_t)144000 * h->bitrate / h->rate + h->pad;+ else+ h->size = (size_t)72000 * h->bitrate / h->rate + h->pad;+ return 0;+}++void mpa_synth_init(struct mpa_synth *s)+{+ memset(s->v, 0, sizeof s->v);+ s->off[0] = s->off[1] = 0;+ for (int i = 0; i < 64; i++)+ for (int k = 0; k < 32; k++)+ s->n[i][k] = (float)cos((16 + i) * (2 * k + 1) * M_PI / 64);+ for (int i = 0; i <= 256; i++)+ s->d[i] = (float)(MPA_WINDOW[i] / 65536.0);+ /* D is odd about 256 but at the multiples of 64 */+ for (int i = 1; i < 256; i++)+ s->d[512 - i] = i % 64 ? -s->d[i] : s->d[i];+}++void mpa_synth(struct mpa_synth *s, int ch, const float *sb, float *pcm)+{+ int off = s->off[ch] = (s->off[ch] - 64) & 1023;+ float *v = s->v[ch];+ for (int i = 0; i < 64; i++) {+ float x = 0;+ for (int k = 0; k < 32; k++)+ x += s->n[i][k] * sb[k];+ v[(off + i) & 1023] = x;+ }+ for (int j = 0; j < 32; j++) {+ float sum = 0;+ for (int i = 0; i < 8; i++)+ sum += s->d[64 * i + j] * v[(off + 128 * i + j) & 1023] ++ s->d[64 * i + 32 + j] * v[(off + 128 * i + 96 + j) & 1023];+ pcm[j] = sum;+ }+}++/* ---- Layer III ---- */++struct gran {+ int part23, big, gain, sfc, btype, mixed, tsel[3], sbg[3];+ int region[3]; /* the big values' regions end at these lines */+ int preflag, sfscale, c1sel, scfsi;+ int long_end, short_start; /* the bands long and short */+ int sf[40];+};++struct l3 {+ struct mpa_synth syn;+ float pow43[8207];+ uint32_t hleft[1378]; /* the codes left-aligned in 32 bits */+ uint32_t qleft[2][16]; /* the count1 tables', in their order */+ uint8_t qlen[2][16], qval[2][16];+ float cos36[36][18], cos12[12][6];+ float win[4][36]; /* the long windows of block types 0..3 (2: short) */+ float overlap[2][32][18];+ uint8_t res[8192]; /* the main data of the frames before */+ size_t res_n;+ struct gran g[2][2];+ float xr[2][576];+ float sb[2][36][32]; /* the subband samples of a frame */+};++static void l3_init(struct l3 *d)+{+ memset(d, 0, sizeof *d);+ mpa_synth_init(&d->syn);+ for (int i = 0; i < 8207; i++)+ d->pow43[i] = (float)pow(i, 4.0 / 3.0);+ for (int i = 0; i < 1378; i++)+ d->hleft[i] = MPA_HUFF_CODE[i] << (32 - MPA_HUFF_LEN[i]);+ for (int t = 0; t < 2; t++) {+ int order[16];+ for (int v = 0; v < 16; v++)+ order[v] = v;+ /* sorted by their left-aligned codes */+ for (int i = 0; i < 16; i++)+ for (int j = i + 1; j < 16; j++) {+ uint32_t a = (uint32_t)MPA_QUAD_CODE[t * 16 + order[i]]+ << (32 - MPA_QUAD_LEN[t * 16 + order[i]]);+ uint32_t b = (uint32_t)MPA_QUAD_CODE[t * 16 + order[j]]+ << (32 - MPA_QUAD_LEN[t * 16 + order[j]]);+ if (b < a) {+ int x = order[i];+ order[i] = order[j];+ order[j] = x;+ }+ }+ for (int i = 0; i < 16; i++) {+ int v = order[i];+ d->qlen[t][i] = MPA_QUAD_LEN[t * 16 + v];+ d->qleft[t][i] = (uint32_t)MPA_QUAD_CODE[t * 16 + v]+ << (32 - d->qlen[t][i]);+ d->qval[t][i] = (uint8_t)v;+ }+ }+ for (int i = 0; i < 36; i++)+ for (int k = 0; k < 18; k++)+ d->cos36[i][k] =+ (float)cos(M_PI / 72 * (2 * i + 1 + 18) * (2 * k + 1));+ for (int i = 0; i < 12; i++)+ for (int k = 0; k < 6; k++)+ d->cos12[i][k] =+ (float)cos(M_PI / 24 * (2 * i + 1 + 6) * (2 * k + 1));+ for (int i = 0; i < 36; i++) {+ double l = sin(M_PI / 36 * (i + 0.5));+ d->win[0][i] = (float)l;+ d->win[1][i] = (float)(i < 18 ? l+ : i < 24 ? 1+ : i < 30 ? sin(M_PI / 12 * (i - 18 + 0.5))+ : 0);+ d->win[3][i] = (float)(i < 6 ? 0+ : i < 12 ? sin(M_PI / 12 * (i - 6 + 0.5))+ : i < 18 ? 1+ : l);+ d->win[2][i] = i < 12 ? (float)sin(M_PI / 12 * (i + 0.5)) : 0;+ }+}++static uint32_t peek32(const struct mpa_br *b)+{+ struct mpa_br c = *b;+ return mpa_bits(&c, 32);+}++/* a pair of big values of table l (1..15 of MPA_HUFF_START) */+static int huff_pair(const struct l3 *d, struct mpa_br *b, int l, int *x,+ int *y)+{+ uint32_t v = peek32(b);+ int lo = MPA_HUFF_START[l - 1], hi = MPA_HUFF_START[l] - 1;+ while (lo < hi) { /* the last code at or below v */+ int mid = (lo + hi + 1) / 2;+ if (d->hleft[mid] <= v)+ lo = mid;+ else+ hi = mid - 1;+ }+ b->pos += MPA_HUFF_LEN[lo];+ *x = MPA_HUFF_SYM[lo] >> 4;+ *y = MPA_HUFF_SYM[lo] & 15;+ return 0;+}++static int huff_quad(const struct l3 *d, struct mpa_br *b, int t)+{+ uint32_t v = peek32(b);+ int i = 15;+ while (i > 0 && d->qleft[t][i] > v)+ i--;+ b->pos += d->qlen[t][i];+ return d->qval[t][i];+}++static const uint8_t *band_long(const struct mpa_hdr *h)+{+ return MPA_BAND_LONG + 22 * h->rate_index;+}++static const uint8_t *band_short(const struct mpa_hdr *h)+{+ return MPA_BAND_SHORT + 13 * h->rate_index;+}++/* the first line of long band i */+static int long_start(const struct mpa_hdr *h, int i)+{+ int s = 0;+ for (int k = 0; k < i && k < 22; k++)+ s += band_long(h)[k];+ return s;+}++static int side_info(struct mpa_br *b, const struct mpa_hdr *h,+ struct gran (*g)[2], int *mdb)+{+ int ng = h->lsf ? 1 : 2;+ if (h->lsf) {+ *mdb = (int)mpa_bits(b, 8);+ mpa_bits(b, h->ch);+ } else {+ *mdb = (int)mpa_bits(b, 9);+ mpa_bits(b, h->ch == 2 ? 3 : 5);+ for (int ch = 0; ch < h->ch; ch++) {+ g[0][ch].scfsi = 0;+ g[1][ch].scfsi = (int)mpa_bits(b, 4);+ }+ }+ for (int gr = 0; gr < ng; gr++)+ for (int ch = 0; ch < h->ch; ch++) {+ struct gran *q = &g[gr][ch];+ q->part23 = (int)mpa_bits(b, 12);+ q->big = (int)mpa_bits(b, 9);+ if (q->big > 288)+ return -1;+ q->gain = (int)mpa_bits(b, 8);+ /* mid/side alone: its 1 / sqrt(2) in the gain */+ if ((h->mode == MPA_MODE_JOINT) && (h->mode_ext & 3) == 2)+ q->gain -= 2;+ q->sfc = (int)mpa_bits(b, h->lsf ? 9 : 4);+ int r0, r1;+ if (mpa_bits(b, 1)) {+ q->btype = (int)mpa_bits(b, 2);+ if (!q->btype)+ return -1;+ q->mixed = (int)mpa_bits(b, 1);+ q->tsel[0] = (int)mpa_bits(b, 5);+ q->tsel[1] = (int)mpa_bits(b, 5);+ q->tsel[2] = 0;+ for (int i = 0; i < 3; i++)+ q->sbg[i] = (int)mpa_bits(b, 3);+ if (q->btype == 2)+ r0 = h->rate_index != 8 ? 36 : 72;+ else+ r0 = h->rate_index <= 2 ? 36+ : h->rate_index != 8 ? 54+ : 108;+ r1 = 576;+ } else {+ q->btype = q->mixed = 0;+ for (int i = 0; i < 3; i++)+ q->tsel[i] = (int)mpa_bits(b, 5);+ int a1 = (int)mpa_bits(b, 4), a2 = (int)mpa_bits(b, 3);+ r0 = long_start(h, a1 + 1);+ r1 = long_start(h, a1 + a2 + 2 < 22 ? a1 + a2 + 2 : 22);+ }+ int bv = 2 * q->big;+ q->region[0] = r0 < bv ? r0 : bv;+ q->region[1] = r1 < bv ? r1 : bv;+ q->region[2] = bv;+ if (q->btype == 2) {+ q->long_end = q->mixed ? (h->rate_index <= 2 ? 8 : 6) : 0;+ q->short_start = q->mixed ? 3 : 0;+ } else {+ q->long_end = 22;+ q->short_start = 13;+ }+ q->preflag = h->lsf ? 0 : (int)mpa_bits(b, 1);+ q->sfscale = (int)mpa_bits(b, 1);+ q->c1sel = (int)mpa_bits(b, 1);+ }+ return 0;+}++/* MPEG-2's scale factor lengths: v split by the radices n1, n2, n3 (0: no+ such length) */+static void lsf_expand(int *sl, int v, int n1, int n2, int n3)+{+ if (n3) {+ sl[3] = v % n3;+ v /= n3;+ }+ if (n2) {+ sl[2] = v % n2;+ v /= n2;+ }+ sl[1] = v % n1;+ sl[0] = v / n1;+}++static void scale_factors(struct mpa_br *b, const struct mpa_hdr *h,+ struct gran *q, const struct gran *first, int ch)+{+ int j = 0;+ memset(q->sf, 0, sizeof q->sf);+ if (!h->lsf) {+ int s1 = MPA_SLEN[q->sfc], s2 = MPA_SLEN[16 + q->sfc];+ if (q->btype == 2) {+ int n = q->mixed ? 17 : 18;+ for (int i = 0; i < n; i++)+ q->sf[j++] = (int)mpa_bits(b, s1);+ for (int i = 0; i < 18; i++)+ q->sf[j++] = (int)mpa_bits(b, s2);+ } else {+ for (int k = 0; k < 4; k++) {+ int n = k == 0 ? 6 : 5;+ if (q->scfsi & (8 >> k)) {+ for (int i = 0; i < n; i++, j++)+ q->sf[j] = first->sf[j];+ } else {+ for (int i = 0; i < n; i++)+ q->sf[j++] = (int)mpa_bits(b, k < 2 ? s1 : s2);+ }+ }+ }+ return;+ }+ int t1 = q->btype == 2 ? (q->mixed ? 2 : 1) : 0, t2, sl[4] = {0};+ int sf = q->sfc;+ if ((h->mode_ext & 1) && ch == 1) {+ sf >>= 1;+ if (sf < 180) {+ lsf_expand(sl, sf, 6, 6, 0);+ t2 = 3;+ } else if (sf < 244) {+ lsf_expand(sl, sf - 180, 4, 4, 0);+ t2 = 4;+ } else {+ lsf_expand(sl, sf - 244, 3, 0, 0);+ t2 = 5;+ }+ } else if (sf < 400) {+ lsf_expand(sl, sf, 5, 4, 4);+ t2 = 0;+ } else if (sf < 500) {+ lsf_expand(sl, sf - 400, 5, 4, 0);+ t2 = 1;+ } else {+ lsf_expand(sl, sf - 500, 3, 0, 0);+ t2 = 2;+ q->preflag = 1;+ }+ for (int k = 0; k < 4; k++) {+ int n = MPA_LSF_NSF[t2 * 12 + t1 * 4 + k];+ for (int i = 0; i < n && j < 40; i++)+ q->sf[j++] = (int)mpa_bits(b, sl[k]);+ }+}++/* the granule's lines, requantized: the scale of each line in quarters of+ an octave below the global gain */+static void exponents(const struct mpa_hdr *h, const struct gran *q, int *e)+{+ int gain = q->gain - 210, shift = q->sfscale + 1, at = 0;+ const uint8_t *bl = band_long(h), *bs = band_short(h);+ for (int i = 0; i < q->long_end; i++) {+ int v = gain -+ ((q->sf[i] + (q->preflag ? MPA_PRETAB[22 + i] : 0)) << shift);+ for (int j = 0; j < bl[i] && at < 576; j++)+ e[at++] = v;+ }+ int k = q->long_end;+ for (int i = q->short_start; i < 13; i++)+ for (int w = 0; w < 3; w++) {+ int v = gain - (q->sbg[w] << 3) - (q->sf[k++] << shift);+ for (int j = 0; j < bs[i] && at < 576; j++)+ e[at++] = v;+ }+ while (at < 576)+ e[at++] = gain;+}++static float requant(const struct l3 *d, int v, int e)+{+ float x = d->pow43[v < 0 ? -v : v] * exp2f(0.25f * (float)e);+ return v < 0 ? -x : x;+}++static void huffman(const struct l3 *d, struct mpa_br *b, const struct gran *q,+ const int *e, size_t end, float *xr)+{+ int at = 0;+ for (int r = 0; r < 3; r++) {+ int tab = MPA_HUFF_DATA[2 * q->tsel[r]];+ int lin = MPA_HUFF_DATA[2 * q->tsel[r] + 1];+ for (; at < q->region[r]; at += 2) {+ if (b->pos >= end || !tab) {+ xr[at] = xr[at + 1] = 0;+ continue;+ }+ int x, y;+ huff_pair(d, b, tab, &x, &y);+ if (x == 15 && lin)+ x += (int)mpa_bits(b, lin);+ if (x && mpa_bits(b, 1))+ x = -x;+ if (y == 15 && lin)+ y += (int)mpa_bits(b, lin);+ if (y && mpa_bits(b, 1))+ y = -y;+ xr[at] = x ? requant(d, x, e[at]) : 0;+ xr[at + 1] = y ? requant(d, y, e[at + 1]) : 0;+ }+ }+ size_t last = 0;+ while (at <= 572) {+ if (b->pos >= end) {+ if (b->pos > end && last) { /* the last quadruple ran over */+ at -= 4;+ b->pos = last;+ }+ break;+ }+ last = b->pos;+ int v = huff_quad(d, b, q->c1sel);+ for (int k = 0; k < 4; k++) {+ int s = (v >> (3 - k)) & 1;+ if (s && mpa_bits(b, 1))+ s = -1;+ xr[at + k] = s ? requant(d, s, e[at + k]) : 0;+ }+ at += 4;+ }+ while (at < 576)+ xr[at++] = 0;+ b->pos = end;+}++/* mid/side and intensity stereo, on the lines as they were coded */+static void stereo(const struct mpa_hdr *h, const struct gran *g1, float *t0,+ float *t1)+{+ static const float IS_SQRT2 = 0.70710678118654752f;+ if (!(h->mode_ext & 1)) {+ if (h->mode_ext & 2)+ for (int i = 0; i < 576; i++) {+ float a = t0[i], b = t1[i];+ t0[i] = a + b;+ t1[i] = a - b;+ }+ return;+ }+ /* intensity: the bands above the last line of the right channel that+ is not zero, from the top down */+ int sf_max = h->lsf ? 16 : 7;+ float is0[16], is1[16];+ for (int i = 0; i < 16; i++) {+ if (!h->lsf) {+ double f = tan(i * M_PI / 12);+ is0[i] = i < 7 ? (float)(f / (1 + f)) : 0;+ is1[i] = i < 7 ? (float)(1 / (1 + f)) : 0;+ if (i == 6) { /* tan(pi / 2) */+ is0[i] = 1;+ is1[i] = 0;+ }+ } else {+ int j = g1->sfc & 1;+ float f = (float)exp2(-(j + 1) * ((i + 1) >> 1) / 4.0);+ is0[i] = i & 1 ? f : 1;+ is1[i] = i & 1 ? 1 : f;+ }+ }+ const uint8_t *bl = band_long(h), *bs = band_short(h);+ int found_short[3] = {0, 0, 0}, at = 576;+ int k = g1->long_end + (12 - g1->short_start) * 3;+ for (int i = 12; i >= g1->short_start; i--) {+ if (i != 11)+ k -= 3;+ int len = bs[i];+ for (int l = 2; l >= 0; l--) {+ at -= len;+ int ms = 1;+ if (!found_short[l]) {+ for (int j = 0; j < len; j++)+ if (t1[at + j] != 0) {+ found_short[l] = 1;+ break;+ }+ int sf = g1->sf[k + l];+ if (!found_short[l] && sf < sf_max) {+ for (int j = 0; j < len; j++) {+ float x = t0[at + j];+ t0[at + j] = x * is0[sf];+ t1[at + j] = x * is1[sf];+ }+ ms = 0;+ }+ }+ if (ms && (h->mode_ext & 2))+ for (int j = 0; j < len; j++) {+ float a = t0[at + j], b = t1[at + j];+ t0[at + j] = (a + b) * IS_SQRT2;+ t1[at + j] = (a - b) * IS_SQRT2;+ }+ }+ }+ int found = found_short[0] | found_short[1] | found_short[2];+ for (int i = g1->long_end - 1; i >= 0; i--) {+ int len = bl[i], ms = 1;+ at -= len;+ if (!found) {+ for (int j = 0; j < len; j++)+ if (t1[at + j] != 0) {+ found = 1;+ break;+ }+ int sf = g1->sf[i == 21 ? 20 : i];+ if (!found && sf < sf_max) {+ for (int j = 0; j < len; j++) {+ float x = t0[at + j];+ t0[at + j] = x * is0[sf];+ t1[at + j] = x * is1[sf];+ }+ ms = 0;+ }+ }+ if (ms && (h->mode_ext & 2))+ for (int j = 0; j < len; j++) {+ float a = t0[at + j], b = t1[at + j];+ t0[at + j] = (a + b) * IS_SQRT2;+ t1[at + j] = (a - b) * IS_SQRT2;+ }+ }+}++/* short bands from coded order (window after window) to interleaved */+static void reorder(const struct mpa_hdr *h, const struct gran *q, float *xr)+{+ if (q->btype != 2)+ return;+ float tmp[576];+ int at = q->mixed ? (h->rate_index != 8 ? 36 : 72) : 0;+ const uint8_t *bs = band_short(h);+ for (int i = q->short_start; i < 13 && at < 576; i++) {+ int len = bs[i];+ if (at + 3 * len > 576)+ break;+ for (int j = 0; j < len; j++)+ for (int w = 0; w < 3; w++)+ tmp[3 * j + w] = xr[at + w * len + j];+ memcpy(xr + at, tmp, sizeof(float) * 3 * (size_t)len);+ at += 3 * len;+ }+}++static void antialias(const struct gran *q, float *xr)+{+ static const double C[8] = {-0.6, -0.535, -0.33, -0.185,+ -0.095, -0.041, -0.0142, -0.0037};+ int n = q->btype == 2 ? (q->mixed ? 1 : 0) : 31;+ for (int sb = 1; sb <= n; sb++)+ for (int i = 0; i < 8; i++) {+ double cs = 1 / sqrt(1 + C[i] * C[i]), ca = C[i] * cs;+ float a = xr[18 * sb - 1 - i], b = xr[18 * sb + i];+ xr[18 * sb - 1 - i] = (float)(a * cs - b * ca);+ xr[18 * sb + i] = (float)(b * cs + a * ca);+ }+}++/* the IMDCT of each subband, its overlap, the frequency inversion */+static void imdct(struct l3 *d, const struct gran *q, int ch, const float *xr,+ float (*sb)[32])+{+ for (int s = 0; s < 32; s++) {+ float z[36];+ const float *x = xr + 18 * s;+ int lng = q->btype != 2 || (q->mixed && s < 2);+ if (lng) {+ /* with the mixed flag the two lowest subbands take the normal+ window, whatever the block type */+ const float *w = d->win[q->mixed && s < 2 ? 0 : q->btype];+ for (int i = 0; i < 36; i++) {+ float v = 0;+ for (int k = 0; k < 18; k++)+ v += x[k] * d->cos36[i][k];+ z[i] = v * w[i];+ }+ } else {+ memset(z, 0, sizeof z);+ for (int w = 0; w < 3; w++)+ for (int i = 0; i < 12; i++) {+ float v = 0;+ for (int k = 0; k < 6; k++)+ v += x[3 * k + w] * d->cos12[i][k];+ z[6 + 6 * w + i] += v * d->win[2][i];+ }+ }+ float *o = d->overlap[ch][s];+ for (int i = 0; i < 18; i++) {+ float v = z[i] + o[i];+ o[i] = z[18 + i];+ sb[i][s] = (s & 1) && (i & 1) ? -v : v;+ }+ }+}++/* a Layer III frame (its bytes from the header on) into pcm[ch][]: the+ samples a channel */+static int l3_frame(struct l3 *d, const uint8_t *f, size_t n,+ const struct mpa_hdr *h, float (*pcm)[1152])+{+ size_t side = h->lsf ? (h->ch == 1 ? 9 : 17) : (h->ch == 1 ? 17 : 32);+ size_t at = 4 + (h->crc ? 2 : 0);+ int ng = h->lsf ? 1 : 2, mdb;+ if (n < at + side)+ return -1;+ struct mpa_br b = {.p = f + at, .n = side};+ struct gran g[2][2];+ memset(g, 0, sizeof g);+ if (side_info(&b, h, g, &mdb))+ return -1;+ /* the main data: the reservoir, then this frame's */+ size_t md = n - at - side, old = d->res_n, skip = 0;+ if (md > sizeof d->res) {+ /* more main data than the reservoir holds: its last part only */+ skip = md - sizeof d->res;+ md = sizeof d->res;+ old = 0;+ } else if (old + md > sizeof d->res) {+ size_t drop = old + md - sizeof d->res;+ memmove(d->res, d->res + drop, old - drop);+ old -= drop;+ }+ memcpy(d->res + old, f + at + side + skip, md);+ d->res_n = old + md;+ struct mpa_br m = {.p = d->res, .n = d->res_n};+ long pos = ((long)old - mdb) * 8;+ int e[576];+ for (int gr = 0; gr < ng; gr++) {+ for (int ch = 0; ch < h->ch; ch++) {+ struct gran *q = &g[gr][ch];+ if (pos < 0) { /* its data was in frames not here */+ memset(d->xr[ch], 0, sizeof d->xr[ch]);+ pos += q->part23;+ continue;+ }+ m.pos = (size_t)pos;+ size_t end = (size_t)pos + (size_t)q->part23;+ scale_factors(&m, h, q, &g[0][ch], ch);+ exponents(h, q, e);+ huffman(d, &m, q, e, end, d->xr[ch]);+ pos = (long)end;+ }+ if (h->mode == MPA_MODE_JOINT && h->ch == 2)+ stereo(h, &g[gr][1], d->xr[0], d->xr[1]);+ for (int ch = 0; ch < h->ch; ch++) {+ reorder(h, &g[gr][ch], d->xr[ch]);+ antialias(&g[gr][ch], d->xr[ch]);+ imdct(d, &g[gr][ch], ch, d->xr[ch], d->sb[ch] + 18 * gr);+ }+ }+ /* keep what the next frames may reach back to */+ if (d->res_n > 4096) {+ memmove(d->res, d->res + d->res_n - 4096, 4096);+ d->res_n = 4096;+ }+ for (int ch = 0; ch < h->ch; ch++)+ for (int t = 0; t < 18 * ng; t++)+ mpa_synth(&d->syn, ch, d->sb[ch][t], pcm[ch] + 32 * t);+ return 576 * ng;+}++/* ---- the file ---- */++/* the frame at d[at], its size found by the next sync when it is free+ format: 0, or -1 */+static int frame_at(const uint8_t *d, size_t n, size_t at, struct mpa_hdr *h)+{+ if (at + 4 > n || mpa_header(d + at, h))+ return -1;+ if (h->size)+ return 0;+ for (size_t k = at + 4; k + 4 <= n; k++) {+ struct mpa_hdr x;+ if (!mpa_header(d + k, &x) && x.layer == h->layer &&+ x.rate == h->rate && x.lsf == h->lsf && !x.bitrate) {+ h->size = k - at;+ return 0;+ }+ }+ h->size = n - at;+ return 0;+}++/* a frame there with another after it, or the end of the bytes */+static int frame_ok(const uint8_t *d, size_t n, size_t at, struct mpa_hdr *h)+{+ if (frame_at(d, n, at, h))+ return 0;+ struct mpa_hdr x;+ size_t next = at + h->size;+ if (next >= n || next + 4 > n)+ return h->size > 4;+ return !frame_at(d, n, next, &x) && x.layer == h->layer &&+ x.rate == h->rate;+}++/* Xing, Info or VBRI in the first frame: 1 when it is that frame, with+ LAME's encoder delay and padding when it says them */+static int vbr_frame(const uint8_t *f, const struct mpa_hdr *h, int *delay,+ int *padding)+{+ size_t side = h->lsf ? (h->ch == 1 ? 9 : 17) : (h->ch == 1 ? 17 : 32);+ size_t x = 4 + side;+ if (h->layer != 3 || h->size < x + 8)+ return 0;+ if (h->size >= 36 + 4 && !memcmp(f + 36, "VBRI", 4))+ return 1;+ if (memcmp(f + x, "Xing", 4) && memcmp(f + x, "Info", 4))+ return 0;+ uint32_t flags = (uint32_t)f[x + 4] << 24 | (uint32_t)f[x + 5] << 16 |+ (uint32_t)f[x + 6] << 8 | f[x + 7];+ size_t t = x + 8 + (flags & 1 ? 4 : 0) + (flags & 2 ? 4 : 0) ++ (flags & 4 ? 100 : 0) + (flags & 8 ? 4 : 0);+ if (t + 24 <= h->size &&+ (!memcmp(f + t, "LAME", 4) || !memcmp(f + t, "Lavf", 4) ||+ !memcmp(f + t, "Lavc", 4))) {+ uint32_t v =+ (uint32_t)f[t + 21] << 16 | (uint32_t)f[t + 22] << 8 | f[t + 23];+ *delay = (int)(v >> 12);+ *padding = (int)(v & 0xFFF);+ }+ return 1;+}++int janas_audio_mpeg(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn)+{+ struct mpa_hdr h;+ size_t at = 0;+ /* the first frame, a frame after it, within the first 64 KiB */+ while (at + 4 <= n && at < 65536 && !frame_ok(d, n, at, &h))+ at++;+ if (at + 4 > n || at >= 65536)+ return 1;+ struct l3 *l3 = malloc(sizeof *l3);+ float(*pcm)[1152] = malloc(sizeof(float) * 2 * 1152);+ if (!l3 || !pcm) {+ free(l3);+ free(pcm);+ snprintf(why, wn, "out of memory");+ return -1;+ }+ l3_init(l3);+ int rate = h.rate, layer = h.layer, delay = -1, padding = 0;+ if (vbr_frame(d + at, &h, &delay, &padding))+ at += h.size;+ float *out = NULL;+ size_t got = 0, cap = 0;+ int rc = 0;+ while (at + 4 <= n) {+ if (frame_at(d, n, at, &h) || h.rate != rate || h.layer != layer) {+ at++; /* lost: the next sync */+ continue;+ }+ /* the last frame cut short: what of it there is */+ size_t len = at + h.size <= n ? h.size : n - at;+ int k = h.layer == 3 ? l3_frame(l3, d + at, len, &h, pcm)+ : mpa_layer12(d + at, len, &h, &l3->syn, pcm);+ at += len;+ if (k <= 0)+ continue;+ if (got + (size_t)k > JANAS_AUDIO_MAX_SAMPLES) {+ snprintf(why, wn, "an MPEG audio file longer than this reads");+ rc = -1;+ break;+ }+ if (got + (size_t)k > cap) {+ size_t c = cap ? cap * 2 : 1 << 18;+ float *o = realloc(out, sizeof(float) * c);+ if (!o) {+ snprintf(why, wn, "out of memory");+ rc = -1;+ break;+ }+ out = o;+ cap = c;+ }+ for (int i = 0; i < k; i++)+ out[got + (size_t)i] =+ h.ch == 2 ? 0.5f * (pcm[0][i] + pcm[1][i]) : pcm[0][i];+ got += (size_t)k;+ }+ free(l3);+ free(pcm);+ if (!rc && !got) {+ snprintf(why, wn, "an MPEG audio file without a frame this reads");+ rc = -1;+ }+ if (rc) {+ free(out);+ return -1;+ }+ /* gapless: the encoder's delay and padding, and the decoder's 529 */+ size_t skip = delay >= 0 ? (size_t)delay + 529 : 0;+ size_t trim = padding > 529 ? (size_t)padding - 529 : 0;+ if (skip + trim >= got) {+ skip = 0;+ trim = 0;+ }+ memmove(out, out + skip, sizeof(float) * (got - skip - trim));+ a->pcm = out;+ a->n = got - skip - trim;+ a->rate = (uint32_t)rate;+ return 0;+}diff --git a/src/common/audio_mp4.c b/src/common/audio_mp4.cnew file mode 100644index 0000000..f67d73e--- /dev/null+++ b/src/common/audio_mp4.c@@ -0,0 +1,810 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_mp4.c - the ISO base media file format (MP4, M4A, 3GP, MOV): the+ * first track of sound, its sample description (AAC and MP3 by their+ * esds, ALAC, Opus, FLAC, PCM), its samples found through the sample+ * tables (stsz or stz2, stco or co64, stsc) or the movie fragments (moof,+ * traf, tfhd, trun), the edit list's start and length that trim the+ * encoder's delay and padding. The codecs decode the samples where they+ * lie in the file.+ */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "audio.h"+#include "audio_aac.h"+#include "audio_ogg.h"++static uint32_t be32(const uint8_t *p)+{+ return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 |+ p[3];+}++static uint64_t be64(const uint8_t *p)+{+ return (uint64_t)be32(p) << 32 | be32(p + 4);+}++static uint16_t be16(const uint8_t *p)+{+ return (uint16_t)(p[0] << 8 | p[1]);+}++/* a box at p of the n bytes left: its type, payload and length; 0, or -1 */+struct box {+ uint32_t type;+ const uint8_t *body;+ size_t size, len; /* the whole box, its payload */+};++static int box_at(const uint8_t *p, size_t n, struct box *b)+{+ if (n < 8)+ return -1;+ uint64_t size = be32(p);+ size_t head = 8;+ if (size == 1) {+ if (n < 16)+ return -1;+ size = be64(p + 8);+ head = 16;+ } else if (size == 0) {+ size = n;+ }+ if (size < head || size > n)+ return -1;+ b->type = be32(p + 4);+ b->body = p + head;+ b->size = (size_t)size;+ b->len = (size_t)size - head;+ return 0;+}++#define TAG(s) \+ ((uint32_t)(s)[0] << 24 | (uint32_t)(s)[1] << 16 | (uint32_t)(s)[2] << 8 | \+ (uint32_t)(s)[3])++/* the first child of type t in a container's payload */+static int child(const uint8_t *p, size_t n, uint32_t t, struct box *out)+{+ while (n >= 8) {+ struct box b;+ if (box_at(p, n, &b) != 0)+ return -1;+ if (b.type == t) {+ *out = b;+ return 0;+ }+ p += b.size;+ n -= b.size;+ }+ return -1;+}++struct track {+ uint32_t codec; /* the sample entry's type */+ int channels, bits, rate;+ uint32_t timescale;+ const uint8_t *config; /* esds' DecoderSpecificInfo, ALAC's cookie,+ dOps, dfLa */+ size_t config_len;+ int object_type; /* esds' objectTypeIndication */+ uint32_t lpcm_flags; /* QuickTime's version 2: 1 float, 2 big-endian,+ 4 signed; little-endian when an enda says */+ int little;+ /* the samples */+ size_t *off, *len;+ size_t count, cap;+ uint64_t total_duration;+ /* the edit list */+ int64_t media_time; /* -1: none */+ uint64_t edit_length; /* in the movie's timescale, 0: none */+ uint32_t movie_timescale;+ uint32_t track_id;+ /* fragments' defaults (trex) */+ uint32_t trex_duration, trex_size;+};++static int add_sample(struct track *t, size_t off, size_t len)+{+ if (t->count == t->cap) {+ size_t c = t->cap ? t->cap * 2 : 1024;+ size_t *o = realloc(t->off, sizeof(size_t) * c);+ if (o)+ t->off = o;+ size_t *l = realloc(t->len, sizeof(size_t) * c);+ if (l)+ t->len = l;+ if (!o || !l)+ return -1;+ t->cap = c;+ }+ t->off[t->count] = off;+ t->len[t->count] = len;+ t->count++;+ return 0;+}++/* an MPEG-4 descriptor's tag and length at p: its payload, or NULL */+static const uint8_t *descriptor(const uint8_t *p, const uint8_t *end, int *tag,+ size_t *len)+{+ if (p >= end)+ return NULL;+ *tag = *p++;+ size_t l = 0;+ for (int i = 0; i < 4 && p < end; i++) {+ uint8_t c = *p++;+ l = l << 7 | (c & 0x7F);+ if (!(c & 0x80))+ break;+ }+ if (l > (size_t)(end - p))+ return NULL;+ *len = l;+ return p;+}++static void read_esds(struct track *t, const uint8_t *p, size_t n)+{+ if (n < 4)+ return;+ const uint8_t *end = p + n;+ p += 4; /* version, flags */+ int tag;+ size_t len;+ const uint8_t *d = descriptor(p, end, &tag, &len);+ if (!d || tag != 3 || len < 3)+ return;+ const uint8_t *de = d + len;+ int flags = d[2];+ d += 3;+ if (flags & 0x80)+ d += 2;+ if (flags & 0x40) {+ if (d >= de)+ return;+ d += 1 + *d;+ }+ if (flags & 0x20)+ d += 2;+ if (d >= de)+ return;+ const uint8_t *dc = descriptor(d, de, &tag, &len);+ if (!dc || tag != 4 || len < 13)+ return;+ t->object_type = dc[0];+ const uint8_t *dce = dc + len;+ const uint8_t *ds = descriptor(dc + 13, dce, &tag, &len);+ if (ds && tag == 5) {+ t->config = ds;+ t->config_len = len;+ }+}++static void read_stsd(struct track *t, const uint8_t *p, size_t n)+{+ if (n < 8 || be32(p + 4) == 0)+ return;+ struct box e;+ if (box_at(p + 8, n - 8, &e) != 0 || e.len < 28)+ return;+ t->codec = e.type;+ const uint8_t *b = e.body;+ int version = be16(b + 8);+ t->channels = be16(b + 16);+ t->bits = be16(b + 18);+ t->rate = (int)(be32(b + 24) >> 16);+ size_t at = 28;+ if (version == 1)+ at += 16;+ else if (version == 2) {+ at += 36;+ if (e.len >= 64) {+ /* QuickTime's version 2: a double rate and 32-bit fields */+ union {+ uint64_t u;+ double d;+ } r = {.u = be64(b + 32)};+ t->rate = (int)r.d;+ t->channels = (int)be32(b + 40);+ t->bits = (int)be32(b + 48);+ t->lpcm_flags = be32(b + 52);+ }+ }+ if (at > e.len)+ return;+ const uint8_t *q = b + at;+ size_t left = e.len - at;+ struct box c;+ if (child(q, left, TAG("esds"), &c) == 0)+ read_esds(t, c.body, c.len);+ else if (child(q, left, TAG("wave"), &c) == 0) {+ struct box en;+ if (child(c.body, c.len, TAG("enda"), &en) == 0 && en.len >= 2)+ t->little = en.body[1] & 1;+ /* QuickTime's AAC: the esds inside a wave box */+ struct box c2;+ if (child(c.body, c.len, TAG("esds"), &c2) == 0)+ read_esds(t, c2.body, c2.len);+ else if (child(c.body, c.len, TAG("alac"), &c2) == 0) {+ t->config = c2.body;+ t->config_len = c2.len;+ }+ }+ if (e.type == TAG("alac") && child(q, left, TAG("alac"), &c) == 0) {+ t->config = c.body;+ t->config_len = c.len;+ } else if (e.type == TAG("Opus") && child(q, left, TAG("dOps"), &c) == 0) {+ t->config = c.body;+ t->config_len = c.len;+ } else if (e.type == TAG("fLaC") && child(q, left, TAG("dfLa"), &c) == 0) {+ t->config = c.body;+ t->config_len = c.len;+ }+}++/* the sample tables: 0, or -1 */+static int read_stbl(struct track *t, const uint8_t *p, size_t n,+ const uint8_t *file, size_t fn)+{+ struct box stsz, stsc, stco, stts;+ int co64 = 0, z2 = 0;+ if (child(p, n, TAG("stsd"), &stsz) == 0)+ read_stsd(t, stsz.body, stsz.len);+ if (child(p, n, TAG("stsz"), &stsz) != 0) {+ if (child(p, n, TAG("stz2"), &stsz) != 0)+ return 0; /* fragmented: the samples come with the fragments */+ z2 = 1;+ }+ if (child(p, n, TAG("stco"), &stco) != 0) {+ if (child(p, n, TAG("co64"), &stco) != 0)+ return -1;+ co64 = 1;+ }+ if (child(p, n, TAG("stsc"), &stsc) != 0 || stsz.len < 12 || stco.len < 8 ||+ stsc.len < 8)+ return -1;+ if (child(p, n, TAG("stts"), &stts) == 0 && stts.len >= 8) {+ uint32_t e = be32(stts.body + 4);+ for (uint32_t i = 0; i < e && 8 + 8 * (size_t)i + 8 <= stts.len; i++)+ t->total_duration += (uint64_t)be32(stts.body + 8 + 8 * i) *+ be32(stts.body + 12 + 8 * i);+ }+ uint32_t fixed = 0, field = 0, nsamples;+ if (z2) {+ field = stsz.body[7];+ nsamples = be32(stsz.body + 8);+ if (field != 4 && field != 8 && field != 16)+ return -1;+ if ((size_t)nsamples * field / 8 > stsz.len - 12)+ return -1;+ } else {+ fixed = be32(stsz.body + 4);+ nsamples = be32(stsz.body + 8);+ if (!fixed && (size_t)nsamples * 4 > stsz.len - 12)+ return -1;+ }+ uint32_t nchunks = be32(stco.body + 4);+ if ((size_t)nchunks * (co64 ? 8 : 4) > stco.len - 8)+ return -1;+ uint32_t nstsc = be32(stsc.body + 4);+ if ((size_t)nstsc * 12 > stsc.len - 8)+ return -1;+ uint32_t s = 0;+ for (uint32_t i = 0; i < nstsc && s < nsamples; i++) {+ const uint8_t *e = stsc.body + 8 + 12 * (size_t)i;+ uint32_t first = be32(e), per = be32(e + 4);+ uint32_t last = i + 1 < nstsc ? be32(e + 12) : nchunks + 1;+ if (first == 0 || last < first)+ return -1;+ for (uint32_t ch = first; ch < last && ch <= nchunks && s < nsamples;+ ch++) {+ uint64_t off = co64 ? be64(stco.body + 8 + 8 * (size_t)(ch - 1))+ : be32(stco.body + 8 + 4 * (size_t)(ch - 1));+ for (uint32_t k = 0; k < per && s < nsamples; k++, s++) {+ uint32_t len;+ if (z2) {+ const uint8_t *z = stsz.body + 12;+ len = field == 16 ? be16(z + 2 * (size_t)s)+ : field == 8+ ? z[s]+ : (uint32_t)(z[s / 2] >> (s & 1 ? 0 : 4)) & 15;+ } else {+ len = fixed ? fixed : be32(stsz.body + 12 + 4 * (size_t)s);+ }+ if (off > fn || len > fn - off)+ return t->count ? 0 : -1; /* a file cut short */+ if (add_sample(t, (size_t)off, len) != 0)+ return -1;+ off += len;+ }+ }+ }+ (void)file;+ return 0;+}++static int read_trak(struct track *t, const uint8_t *p, size_t n,+ const uint8_t *file, size_t fn)+{+ struct box mdia, hdlr, minf, stbl, mdhd, tkhd, edts, elst;+ if (child(p, n, TAG("mdia"), &mdia) != 0 ||+ child(mdia.body, mdia.len, TAG("hdlr"), &hdlr) != 0 || hdlr.len < 12 ||+ be32(hdlr.body + 8) != TAG("soun"))+ return 1;+ if (child(p, n, TAG("tkhd"), &tkhd) == 0 && tkhd.len >= 16)+ t->track_id = be32(tkhd.body + (tkhd.body[0] == 1 ? 20 : 12));+ if (child(mdia.body, mdia.len, TAG("mdhd"), &mdhd) == 0 && mdhd.len >= 24)+ t->timescale = be32(mdhd.body + (mdhd.body[0] == 1 ? 20 : 12));+ t->media_time = -1;+ if (child(p, n, TAG("edts"), &edts) == 0 &&+ child(edts.body, edts.len, TAG("elst"), &elst) == 0 && elst.len >= 8) {+ int v = elst.body[0];+ uint32_t cnt = be32(elst.body + 4);+ size_t es = v == 1 ? 20 : 12;+ /* an empty edit first (a delay) is passed over: the sound's own+ edit is the one with a media time */+ for (uint32_t i = 0; i < cnt && 8 + es * (i + 1) <= elst.len; i++) {+ const uint8_t *e = elst.body + 8 + es * i;+ uint64_t dur = v == 1 ? be64(e) : be32(e);+ int64_t mt = v == 1 ? (int64_t)be64(e + 8) : (int32_t)be32(e + 4);+ if (mt < 0)+ continue;+ t->media_time = mt;+ t->edit_length = dur;+ break;+ }+ }+ if (child(mdia.body, mdia.len, TAG("minf"), &minf) != 0 ||+ child(minf.body, minf.len, TAG("stbl"), &stbl) != 0)+ return -1;+ return read_stbl(t, stbl.body, stbl.len, file, fn);+}++/* a movie fragment's runs of the track */+static int read_moof(struct track *t, const uint8_t *moof, size_t n,+ const uint8_t *file, size_t fn)+{+ size_t moof_at = (size_t)(moof - file);+ const uint8_t *p = moof + 8;+ size_t left = n - 8;+ if (n >= 16 && be32(moof) == 1)+ p = moof + 16, left = n - 16;+ while (left >= 8) {+ struct box traf;+ if (box_at(p, left, &traf) != 0)+ return -1;+ p += traf.size;+ left -= traf.size;+ if (traf.type != TAG("traf"))+ continue;+ struct box tfhd;+ if (child(traf.body, traf.len, TAG("tfhd"), &tfhd) != 0 || tfhd.len < 8)+ continue;+ uint32_t tf = be32(tfhd.body) & 0xFFFFFF;+ if (be32(tfhd.body + 4) != t->track_id && t->track_id)+ continue;+ size_t at = 8;+ uint64_t base = moof_at;+ uint32_t def_size = t->trex_size;+ if (tf & 1) {+ if (at + 8 > tfhd.len)+ return -1;+ base = be64(tfhd.body + at);+ at += 8;+ }+ if (tf & 2)+ at += 4;+ if (tf & 8)+ at += 4;+ if (tf & 0x10) {+ if (at + 4 > tfhd.len)+ return -1;+ def_size = be32(tfhd.body + at);+ at += 4;+ }+ const uint8_t *q = traf.body;+ size_t ql = traf.len;+ uint64_t next = base;+ while (ql >= 8) {+ struct box tr;+ if (box_at(q, ql, &tr) != 0)+ return -1;+ q += tr.size;+ ql -= tr.size;+ if (tr.type != TAG("trun") || tr.len < 8)+ continue;+ uint32_t fl = be32(tr.body) & 0xFFFFFF;+ uint32_t cnt = be32(tr.body + 4);+ size_t r = 8;+ uint64_t off = next;+ if (fl & 1) {+ if (r + 4 > tr.len)+ return -1;+ off = base + (uint64_t)(int64_t)(int32_t)be32(tr.body + r);+ r += 4;+ }+ if (fl & 4)+ r += 4;+ size_t per = (fl & 0x100 ? 4 : 0) + (fl & 0x200 ? 4 : 0) ++ (fl & 0x400 ? 4 : 0) + (fl & 0x800 ? 4 : 0);+ if (r > tr.len || (per && cnt > (tr.len - r) / per))+ return -1;+ for (uint32_t i = 0; i < cnt; i++) {+ const uint8_t *e = tr.body + r + per * i;+ uint32_t size = def_size;+ if (fl & 0x100) {+ t->total_duration += be32(e);+ e += 4;+ } else {+ t->total_duration += t->trex_duration;+ }+ if (fl & 0x200)+ size = be32(e);+ if (off > fn || size > fn - off)+ return 0;+ if (add_sample(t, (size_t)off, size) != 0)+ return -1;+ off += size;+ }+ next = off;+ }+ }+ return 0;+}++/* ---- the codecs ---- */++/* MP3 in MP4: the frames one after another, as an MP3 file */+static int mp4_mpeg(const struct track *t, const uint8_t *d,+ struct janas_audio *a, char *why, size_t wn)+{+ /* the frames carry no gapless tag here: the edit list trims */+ size_t total = 0;+ for (size_t i = 0; i < t->count; i++)+ total += t->len[i];+ uint8_t *buf = malloc(total ? total : 1);+ if (!buf) {+ snprintf(why, wn, "out of memory");+ return -1;+ }+ size_t at = 0;+ for (size_t i = 0; i < t->count; i++) {+ memcpy(buf + at, d + t->off[i], t->len[i]);+ at += t->len[i];+ }+ int rc = janas_audio_mpeg(buf, total, a, why, wn);+ free(buf);+ if (rc > 0) {+ snprintf(why, wn, "an MP4 track of MPEG audio without frames");+ rc = -1;+ }+ if (rc == 0) {+ struct audio_edit ed = {t->media_time, t->edit_length,+ t->timescale ? t->timescale : a->rate,+ t->movie_timescale};+ size_t skip = 0, keep = a->n;+ audio_edit_apply(&ed, a->rate, &skip, &keep);+ if (!keep) {+ janas_audio_free(a);+ snprintf(why, wn, "an MP4 track of MPEG audio without sound");+ return -1;+ }+ memmove(a->pcm, a->pcm + skip, sizeof(float) * keep);+ a->n = keep;+ }+ return rc;+}++/* FLAC in MP4: the dfLa's metadata blocks and the frames, as a FLAC file */+static int mp4_flac(const struct track *t, const uint8_t *d,+ struct janas_audio *a, char *why, size_t wn)+{+ if (!t->config || t->config_len < 4 + 4 + 34) {+ snprintf(why, wn, "an MP4 track of FLAC without its STREAMINFO");+ return -1;+ }+ size_t total = 4 + t->config_len - 4;+ for (size_t i = 0; i < t->count; i++)+ total += t->len[i];+ uint8_t *buf = malloc(total);+ if (!buf) {+ snprintf(why, wn, "out of memory");+ return -1;+ }+ memcpy(buf, "fLaC", 4);+ memcpy(buf + 4, t->config + 4, t->config_len - 4);+ size_t at = t->config_len;+ for (size_t i = 0; i < t->count; i++) {+ memcpy(buf + at, d + t->off[i], t->len[i]);+ at += t->len[i];+ }+ int rc = janas_audio_flac(buf, total, a, why, wn);+ free(buf);+ if (rc > 0) {+ snprintf(why, wn, "an MP4 track of FLAC that is none");+ rc = -1;+ }+ return rc;+}++/* Opus in MP4: an OpusHead made from the dOps, the edit list's length as+ the last granule position */+static int mp4_opus(const struct track *t, const uint8_t *d,+ struct janas_audio *a, char *why, size_t wn)+{+ const uint8_t *c = t->config;+ if (!c || t->config_len < 11 || c[0] != 0) {+ snprintf(why, wn, "an MP4 track of Opus without its dOps");+ return -1;+ }+ uint8_t head[19 + 2 + 255];+ int channels = c[1], family = c[10];+ size_t hn = 19;+ memcpy(head, "OpusHead", 8);+ head[8] = 1;+ head[9] = (uint8_t)channels;+ head[10] = c[3]; /* the pre-skip, little-endian in Ogg */+ head[11] = c[2];+ head[12] = c[7];+ head[13] = c[6];+ head[14] = c[5];+ head[15] = c[4];+ head[16] = c[9];+ head[17] = c[8];+ head[18] = (uint8_t)family;+ if (family) {+ if (t->config_len < 13 + (size_t)channels) {+ snprintf(why, wn,+ "an MP4 track of Opus with its mapping cut short");+ return -1;+ }+ memcpy(head + 19, c + 11, 2 + (size_t)channels);+ hn += 2 + (size_t)channels;+ }+ struct ogg_packets o;+ memset(&o, 0, sizeof o);+ o.codec = 2;+ o.pre_skip = -1;+ int rc = -1;+ if (ogg_add_packet(&o, head, hn, -1) != 0)+ goto oom;+ int64_t last = -1;+ if (t->media_time >= 0 && t->edit_length && t->movie_timescale &&+ t->timescale)+ last = t->media_time * 48000 / (int64_t)t->timescale ++ (int64_t)(t->edit_length * 48000 / t->movie_timescale);+ for (size_t i = 0; i < t->count; i++)+ if (ogg_add_packet(&o, d + t->off[i], t->len[i],+ i + 1 == t->count ? last : -1) != 0)+ goto oom;+ rc = janas_opus_stream(&o, a, why, wn);+ ogg_packets_free(&o);+ return rc;+oom:+ ogg_packets_free(&o);+ snprintf(why, wn, "out of memory");+ return -1;+}++/* uncompressed sound in a sample entry of its own */+static int mp4_pcm(const struct track *t, const uint8_t *d,+ struct janas_audio *a, char *why, size_t wn)+{+ int big = !t->little, fl = 0, bits = t->bits, uns = 0;+ uint32_t c = t->codec;+ if (c == TAG("lpcm")) {+ fl = t->lpcm_flags & 1;+ big = (t->lpcm_flags & 2) != 0;+ uns = !fl && !(t->lpcm_flags & 4) && bits == 8;+ } else if (c == TAG("sowt"))+ big = 0, bits = 16;+ else if (c == TAG("twos"))+ bits = t->bits == 8 ? 8 : 16;+ else if (c == TAG("in24"))+ bits = 24;+ else if (c == TAG("in32"))+ bits = 32;+ else if (c == TAG("fl32"))+ bits = 32, fl = 1;+ else if (c == TAG("fl64"))+ bits = 64, fl = 1;+ else if (c == TAG("raw "))+ bits = 8;+ int ch = t->channels, bps = bits / 8;+ if (ch <= 0 || ch > 64 || !t->rate || !bps || bps > 8 ||+ (fl && bps != 4 && bps != 8)) {+ snprintf(why, wn, "an MP4 track of uncompressed sound out of range");+ return -1;+ }+ size_t frames = 0;+ for (size_t i = 0; i < t->count; i++)+ frames += t->len[i] / ((size_t)bps * (size_t)ch);+ if (frames > JANAS_AUDIO_MAX_SAMPLES) {+ snprintf(why, wn, "a sound longer than this reads");+ return -1;+ }+ float *pcm = malloc(sizeof(float) * (frames ? frames : 1));+ if (!pcm) {+ snprintf(why, wn, "out of memory");+ return -1;+ }+ size_t at = 0;+ for (size_t i = 0; i < t->count; i++) {+ const uint8_t *p = d + t->off[i];+ size_t nf = t->len[i] / ((size_t)bps * (size_t)ch);+ for (size_t f = 0; f < nf; f++) {+ double s = 0;+ for (int k = 0; k < ch; k++, p += bps) {+ uint64_t u = 0;+ for (int j = 0; j < bps; j++)+ u |= (uint64_t)p[big ? j : bps - 1 - j]+ << (8 * (bps - 1 - j));+ double v;+ if (fl && bits == 32) {+ uint32_t w = (uint32_t)u;+ float x;+ memcpy(&x, &w, 4);+ v = x;+ } else if (fl) {+ double x;+ memcpy(&x, &u, 8);+ v = x;+ } else if (c == TAG("raw ") || uns) {+ v = ((double)u - 128) / 128;+ } else {+ int64_t sv = (int64_t)(u << (64 - bits)) >> (64 - bits);+ v = (double)sv / (double)((int64_t)1 << (bits - 1));+ }+ s += v;+ }+ pcm[at++] = (float)(s / ch);+ }+ }+ a->pcm = pcm;+ a->n = at;+ a->rate = (uint32_t)t->rate;+ return 0;+}++int janas_audio_mp4(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn)+{+ struct box ftyp;+ if (box_at(d, n, &ftyp) != 0 ||+ (ftyp.type != TAG("ftyp") && ftyp.type != TAG("moov") &&+ ftyp.type != TAG("mdat") && ftyp.type != TAG("free") &&+ ftyp.type != TAG("wide") && ftyp.type != TAG("skip")))+ return 1;+ struct track t;+ memset(&t, 0, sizeof t);+ t.media_time = -1;+ int rc = -1, found = 0;+ /* the movie, then the fragments */+ const uint8_t *p = d;+ size_t left = n;+ while (left >= 8) {+ struct box b;+ if (box_at(p, left, &b) != 0)+ break; /* a box cut short at the end */+ if (b.type == TAG("moov") && !found) {+ struct box mvhd, mvex, trex;+ if (child(b.body, b.len, TAG("mvhd"), &mvhd) == 0 && mvhd.len >= 20)+ t.movie_timescale =+ be32(mvhd.body + (mvhd.body[0] == 1 ? 20 : 12));+ const uint8_t *q = b.body;+ size_t ql = b.len;+ while (ql >= 8 && !found) {+ struct box tk;+ if (box_at(q, ql, &tk) != 0)+ break;+ if (tk.type == TAG("trak")) {+ int r = read_trak(&t, tk.body, tk.len, d, n);+ if (r < 0) {+ snprintf(why, wn,+ "an MP4 file whose sample tables are broken");+ goto end;+ }+ if (r == 0)+ found = 1;+ }+ q += tk.size;+ ql -= tk.size;+ }+ if (found && child(b.body, b.len, TAG("mvex"), &mvex) == 0) {+ const uint8_t *m = mvex.body;+ size_t ml = mvex.len;+ while (ml >= 8 && child(m, ml, TAG("trex"), &trex) == 0) {+ if (trex.len >= 24 && be32(trex.body + 4) == t.track_id) {+ t.trex_duration = be32(trex.body + 12);+ t.trex_size = be32(trex.body + 16);+ break;+ }+ size_t skip = (size_t)(trex.body + trex.len - m);+ m += skip;+ ml -= skip;+ }+ }+ } else if (b.type == TAG("moof") && found) {+ if (read_moof(&t, p, b.size, d, n) != 0) {+ snprintf(why, wn, "an MP4 file whose fragments are broken");+ goto end;+ }+ }+ p += b.size;+ left -= b.size;+ }+ if (!found) {+ snprintf(why, wn, "an MP4 file without a track of sound");+ goto end;+ }+ if (!t.count) {+ snprintf(why, wn, "an MP4 track of sound without samples");+ goto end;+ }+ uint32_t c = t.codec;+ char fourcc[5] = {(char)(c >> 24), (char)(c >> 16), (char)(c >> 8), (char)c,+ 0};+ for (int i = 0; i < 4; i++)+ if (fourcc[i] < 32 || fourcc[i] > 126)+ fourcc[i] = '?';+ if (c == TAG("mp4a") || c == TAG("enca")) {+ int ot = t.object_type;+ if (ot == 0x69 || ot == 0x6B) {+ rc = mp4_mpeg(&t, d, a, why, wn);+ } else if (ot == 0x40 || ot == 0x66 || ot == 0x67 || ot == 0x68 ||+ (!ot && t.config)) {+ struct aac_cfg cfg;+ if (!t.config) {+ snprintf(why, wn,+ "an MP4 track of AAC without its configuration");+ goto end;+ }+ if (aac_config(t.config, t.config_len, &cfg, why, wn) != 0)+ goto end;+ if (ot >= 0x66 && ot <= 0x68)+ cfg.aot = ot == 0x66 ? AOT_MAIN : ot == 0x67 ? AOT_LC : cfg.aot;+ struct audio_edit ed = {t.media_time, t.edit_length,+ t.timescale ? t.timescale+ : (uint32_t)cfg.rate,+ t.movie_timescale};+ rc = aac_stream(&cfg, d, t.off, t.len, t.count, &ed, a, why, wn);+ } else if (ot == 0xA5 || ot == 0xA6) {+ snprintf(why, wn, "an MP4 track of AC-3 or E-AC-3, not read");+ } else {+ snprintf(why, wn,+ "an MP4 track of a codec not read (object type 0x%02x)",+ ot);+ }+ } else if (c == TAG(".mp3")) {+ rc = mp4_mpeg(&t, d, a, why, wn);+ } else if (c == TAG("alac")) {+ if (!t.config) {+ snprintf(why, wn, "an MP4 track of ALAC without its configuration");+ goto end;+ }+ struct audio_edit ed = {t.media_time, t.edit_length,+ t.timescale ? t.timescale : (uint32_t)t.rate,+ t.movie_timescale};+ rc = janas_alac_stream(t.config, t.config_len, d, t.off, t.len, t.count,+ &ed, a, why, wn);+ } else if (c == TAG("Opus")) {+ rc = mp4_opus(&t, d, a, why, wn);+ } else if (c == TAG("fLaC")) {+ rc = mp4_flac(&t, d, a, why, wn);+ } else if (c == TAG("sowt") || c == TAG("twos") || c == TAG("in24") ||+ c == TAG("in32") || c == TAG("fl32") || c == TAG("fl64") ||+ c == TAG("raw ") || c == TAG("lpcm")) {+ rc = mp4_pcm(&t, d, a, why, wn);+ } else {+ snprintf(why, wn, "an MP4 track of a codec not read (%s)", fourcc);+ }+end:+ free(t.off);+ free(t.len);+ return rc;+}diff --git a/src/common/audio_mpa.h b/src/common/audio_mpa.hnew file mode 100644index 0000000..382c2e3--- /dev/null+++ b/src/common/audio_mpa.h@@ -0,0 +1,70 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_mpa.h - what the MPEG audio decoders share (audio_mp3.c for Layer+ * III and the file, audio_mp2.c for Layers I and II): a frame's header, a+ * reader of bits, the polyphase synthesis filter bank of ISO/IEC 11172-3.+ * Inside src/common.+ */+#ifndef JANAS_COMMON_AUDIO_MPA_H+#define JANAS_COMMON_AUDIO_MPA_H++#include <stddef.h>+#include <stdint.h>++#define MPA_MODE_STEREO 0+#define MPA_MODE_JOINT 1+#define MPA_MODE_DUAL 2+#define MPA_MODE_MONO 3++struct mpa_hdr {+ int lsf; /* MPEG-2 or 2.5: half rates, one granule */+ int layer; /* 1, 2, 3 */+ int crc; /* a CRC follows the header */+ int bitrate; /* kbit/s; 0: free format */+ int rate; /* samples a second */+ int rate_index; /* 0 .. 8: 44.1, 48, 32, 22.05, 24, 16, 11.025, 12, 8 */+ int pad, mode, mode_ext, ch;+ size_t size; /* the frame's bytes (free format: 0 until found) */+};++/* the header at p (4 bytes): 0, or -1 when it is not one */+int mpa_header(const uint8_t *p, struct mpa_hdr *h);++struct mpa_br {+ const uint8_t *p;+ size_t n; /* bytes */+ size_t pos; /* bits */+};++/* k bits, 0 to 32; past the end the bits read as zeros */+static inline uint32_t mpa_bits(struct mpa_br *b, int k)+{+ if (!k)+ return 0;+ size_t at = b->pos >> 3;+ uint64_t w = 0;+ for (int i = 0; i < 8; i++)+ w = w << 8 | (at + (size_t)i < b->n ? b->p[at + (size_t)i] : 0);+ b->pos += (size_t)k;+ return (uint32_t)((w << ((b->pos - (size_t)k) & 7)) >> (64 - k));+}++/* the synthesis filter bank of each channel */+struct mpa_synth {+ float v[2][1024];+ int off[2];+ float n[64][32]; /* the matrixing: cos((16 + i)(2k + 1) pi / 64) */+ float d[512]; /* the window D */+};++void mpa_synth_init(struct mpa_synth *s);+/* 32 subband samples of channel ch into 32 PCM samples */+void mpa_synth(struct mpa_synth *s, int ch, const float *sb, float *pcm);++/* audio_mp2.c: a Layer I or II frame (its bytes from the header on) into+ pcm[ch][384 or 1152]: how many samples a channel, or -1 */+int mpa_layer12(const uint8_t *f, size_t n, const struct mpa_hdr *h,+ struct mpa_synth *s, float (*pcm)[1152]);++#endifdiff --git a/src/common/audio_mpa_tab.h b/src/common/audio_mpa_tab.hnew file mode 100644index 0000000..9c4abf4--- /dev/null+++ b/src/common/audio_mpa_tab.h@@ -0,0 +1,508 @@+/* SPDX-License-Identifier: GPL-3.0-or-later */+/* audio_mpa_tab.h - the data tables of MPEG audio (ISO/IEC 11172-3 Annex B,+ 13818-3), numbers of the standards, generated outside the repository+ from FFmpeg's transcription and checked by decoding against FFmpeg. */+/* Generated: no clang-format. */+#ifndef JANAS_COMMON_AUDIO_MPA_TAB_H+#define JANAS_COMMON_AUDIO_MPA_TAB_H+#include <stdint.h>+/* the big-value tables 1..15 of FFmpeg's numbering (see MPA_HUFF_DATA):+ each entry a code, its length, x << 4 | y */+static const uint16_t MPA_HUFF_START[16] = {+ 0, 4, 13, 22, 38, 54, 90, 126, 162, 226, 290, 354, 610, 866, 1122, 1378,+};+static const uint32_t MPA_HUFF_CODE[1378] = {+ 0, 1, 1, 1, 0, 1, 1, 2,+ 3, 1, 2, 3, 1, 0, 1, 1,+ 2, 3, 1, 1, 2, 3, 0, 1,+ 1, 1, 4, 5, 6, 7, 4, 5,+ 6, 7, 1, 2, 3, 1, 0, 1,+ 1, 2, 3, 2, 3, 4, 5, 3,+ 4, 5, 3, 2, 6, 7, 0, 1,+ 2, 3, 2, 3, 4, 5, 3, 4,+ 10, 11, 6, 14, 15, 8, 9, 5,+ 6, 7, 16, 17, 18, 19, 10, 11,+ 12, 13, 7, 4, 10, 11, 3, 2,+ 3, 1, 0, 1, 1, 1, 4, 5,+ 3, 4, 5, 3, 4, 10, 11, 12,+ 13, 7, 8, 9, 5, 12, 13, 14,+ 15, 16, 17, 18, 19, 5, 6, 7,+ 2, 3, 1, 4, 5, 3, 0, 1,+ 1, 2, 6, 7, 4, 5, 6, 7,+ 4, 5, 6, 14, 15, 8, 9, 10,+ 11, 6, 7, 8, 9, 5, 6, 14,+ 15, 8, 9, 5, 6, 7, 4, 5,+ 6, 7, 0, 1, 2, 3, 4, 5,+ 3, 4, 5, 6, 7, 8, 9, 20,+ 21, 11, 6, 7, 16, 17, 9, 10,+ 22, 23, 12, 26, 27, 7, 8, 18,+ 19, 40, 41, 21, 22, 46, 47, 12,+ 13, 14, 30, 31, 32, 33, 34, 35,+ 18, 19, 20, 21, 22, 23, 12, 13,+ 14, 15, 8, 9, 10, 11, 3, 2,+ 3, 1, 0, 1, 2, 3, 4, 5,+ 6, 14, 15, 8, 9, 5, 6, 7,+ 16, 17, 18, 19, 5, 6, 14, 15,+ 4, 10, 11, 12, 13, 14, 30, 31,+ 32, 33, 17, 9, 20, 21, 11, 12,+ 26, 27, 28, 58, 59, 30, 31, 32,+ 33, 34, 35, 18, 19, 10, 11, 24,+ 25, 13, 7, 4, 10, 11, 3, 4,+ 5, 3, 0, 1, 1, 2, 3, 4,+ 5, 6, 7, 4, 5, 12, 13, 7,+ 8, 9, 10, 11, 12, 26, 27, 14,+ 15, 16, 17, 18, 38, 39, 10, 11,+ 12, 26, 27, 28, 29, 30, 31, 16,+ 17, 18, 19, 40, 41, 21, 22, 23,+ 12, 13, 14, 15, 32, 33, 17, 9,+ 10, 11, 6, 7, 16, 17, 9, 5,+ 6, 7, 0, 1, 1, 1, 1, 2,+ 3, 4, 5, 6, 7, 8, 9, 10,+ 22, 23, 6, 7, 16, 17, 9, 10,+ 11, 12, 13, 14, 15, 16, 17, 18,+ 38, 39, 20, 42, 43, 11, 12, 26,+ 27, 28, 29, 15, 16, 17, 18, 19,+ 20, 21, 22, 23, 24, 25, 52, 53,+ 27, 14, 30, 31, 16, 17, 36, 37,+ 19, 40, 41, 21, 44, 45, 23, 48,+ 49, 25, 26, 54, 55, 14, 15, 16,+ 34, 35, 36, 37, 38, 39, 20, 42,+ 43, 22, 23, 48, 49, 50, 51, 52,+ 53, 54, 55, 56, 57, 58, 59, 30,+ 31, 64, 65, 33, 34, 35, 36, 74,+ 75, 76, 77, 39, 80, 81, 41, 21,+ 44, 45, 46, 47, 48, 49, 50, 51,+ 26, 27, 28, 29, 60, 61, 31, 32,+ 66, 67, 34, 70, 71, 72, 73, 37,+ 38, 78, 79, 40, 82, 83, 42, 86,+ 87, 44, 90, 91, 23, 24, 25, 52,+ 53, 54, 55, 56, 57, 58, 59, 30,+ 31, 32, 33, 68, 69, 35, 72, 73,+ 37, 76, 77, 78, 79, 40, 41, 84,+ 85, 43, 44, 90, 91, 92, 93, 94,+ 95, 24, 25, 52, 53, 54, 55, 56,+ 114, 115, 29, 30, 31, 64, 65, 33,+ 34, 70, 71, 72, 73, 74, 75, 76,+ 77, 78, 79, 20, 42, 43, 44, 45,+ 46, 47, 96, 97, 49, 50, 51, 26,+ 27, 56, 57, 58, 59, 60, 61, 31,+ 16, 34, 35, 36, 37, 19, 20, 21,+ 22, 23, 12, 13, 14, 15, 4, 5,+ 3, 1, 0, 1, 2, 3, 2, 6,+ 7, 8, 9, 10, 11, 6, 14, 15,+ 8, 9, 10, 11, 12, 13, 14, 15,+ 16, 17, 18, 19, 20, 21, 22, 23,+ 24, 25, 26, 27, 28, 29, 30, 62,+ 63, 16, 17, 36, 37, 38, 39, 20,+ 21, 22, 23, 24, 25, 52, 53, 27,+ 28, 29, 30, 31, 32, 33, 34, 70,+ 71, 36, 37, 38, 39, 40, 41, 42,+ 43, 44, 45, 46, 47, 48, 49, 50,+ 51, 52, 53, 54, 55, 56, 57, 58,+ 59, 60, 122, 123, 62, 63, 64, 65,+ 66, 67, 34, 70, 71, 72, 73, 74,+ 75, 38, 39, 80, 81, 41, 42, 43,+ 44, 90, 91, 46, 47, 48, 49, 50,+ 51, 52, 106, 107, 54, 55, 56, 57,+ 58, 118, 119, 30, 62, 63, 64, 65,+ 66, 67, 68, 69, 70, 71, 72, 73,+ 37, 76, 77, 78, 79, 40, 82, 83,+ 42, 86, 87, 88, 89, 90, 91, 92,+ 93, 47, 48, 49, 50, 51, 52, 53,+ 108, 109, 55, 56, 57, 58, 59, 60,+ 122, 123, 62, 63, 64, 65, 66, 67,+ 34, 70, 71, 72, 73, 74, 75, 76,+ 77, 78, 79, 40, 41, 42, 43, 88,+ 89, 90, 91, 92, 93, 94, 95, 48,+ 49, 50, 51, 52, 53, 108, 109, 55,+ 56, 57, 58, 59, 60, 61, 124, 125,+ 63, 32, 66, 67, 34, 35, 36, 37,+ 76, 77, 39, 40, 41, 42, 43, 22,+ 46, 47, 24, 25, 52, 53, 27, 28,+ 29, 15, 16, 17, 18, 19, 5, 12,+ 13, 7, 0, 1, 2, 3, 4, 5,+ 6, 7, 4, 10, 11, 12, 13, 7,+ 8, 9, 10, 11, 3, 16, 17, 9,+ 10, 11, 12, 26, 1728, 3458, 3459, 865,+ 866, 1734, 1735, 434, 870, 871, 436, 437,+ 876, 877, 439, 440, 882, 883, 884, 885,+ 443, 888, 889, 445, 223, 7, 16, 17,+ 9, 10, 352, 706, 707, 708, 709, 710,+ 711, 712, 713, 714, 715, 358, 359, 720,+ 721, 722, 723, 362, 726, 727, 364, 365,+ 366, 734, 735, 736, 737, 369, 370, 742,+ 743, 372, 746, 747, 187, 376, 377, 378,+ 379, 380, 381, 382, 383, 384, 385, 386,+ 387, 388, 389, 195, 392, 393, 394, 395,+ 396, 397, 199, 400, 401, 201, 202, 406,+ 407, 102, 206, 207, 208, 209, 210, 211,+ 212, 426, 427, 107, 216, 217, 218, 219,+ 110, 222, 223, 224, 225, 226, 227, 228,+ 229, 115, 232, 233, 117, 118, 119, 120,+ 242, 243, 244, 245, 246, 247, 248, 249,+ 125, 252, 253, 127, 256, 257, 129, 260,+ 261, 131, 264, 265, 133, 67, 68, 138,+ 139, 140, 141, 142, 143, 72, 73, 148,+ 149, 75, 76, 154, 155, 156, 157, 158,+ 159, 160, 161, 81, 164, 165, 83, 84,+ 85, 172, 173, 87, 88, 178, 179, 90,+ 91, 184, 185, 93, 47, 48, 98, 99,+ 100, 101, 102, 103, 52, 53, 54, 110,+ 111, 56, 114, 115, 58, 59, 30, 62,+ 63, 64, 65, 66, 67, 68, 69, 35,+ 36, 74, 75, 38, 39, 20, 21, 44,+ 45, 23, 12, 13, 14, 15, 4, 5,+ 3, 1, 0, 1, 2, 3, 4, 5,+ 6, 7, 4, 10, 11, 6, 7, 16,+ 17, 9, 10, 11, 12, 13, 14, 15,+ 16, 17, 18, 19, 20, 42, 43, 88,+ 356, 357, 358, 359, 360, 361, 362, 363,+ 364, 365, 366, 367, 368, 369, 370, 371,+ 372, 373, 374, 375, 376, 377, 378, 379,+ 380, 381, 382, 383, 3, 512, 513, 514,+ 515, 1032, 1033, 517, 259, 520, 521, 261,+ 262, 263, 264, 530, 531, 266, 267, 268,+ 269, 540, 541, 271, 272, 273, 274, 275,+ 276, 277, 278, 279, 280, 281, 282, 283,+ 284, 285, 286, 287, 288, 289, 290, 291,+ 292, 293, 294, 295, 296, 297, 298, 299,+ 300, 301, 302, 303, 304, 305, 306, 307,+ 308, 309, 620, 621, 311, 312, 313, 314,+ 315, 316, 317, 318, 319, 320, 321, 322,+ 323, 648, 649, 325, 652, 653, 327, 164,+ 330, 331, 332, 333, 668, 669, 335, 168,+ 169, 340, 341, 171, 344, 345, 346, 347,+ 174, 175, 352, 353, 177, 178, 179, 180,+ 181, 182, 183, 184, 185, 186, 187, 188,+ 189, 190, 191, 192, 193, 194, 195, 196,+ 197, 198, 199, 200, 201, 202, 203, 204,+ 205, 206, 207, 208, 209, 210, 211, 212,+ 426, 427, 214, 215, 216, 434, 435, 109,+ 220, 221, 111, 112, 113, 114, 115, 116,+ 117, 118, 119, 120, 121, 122, 123, 248,+ 249, 125, 126, 127, 128, 129, 130, 262,+ 263, 66, 134, 135, 68, 69, 70, 71,+ 72, 146, 147, 74, 75, 38, 39, 80,+ 81, 41, 21, 22, 46, 47, 12, 13,+ 14, 15,+};+static const uint8_t MPA_HUFF_LEN[1378] = {+ 3, 3, 2, 1, 6, 6, 5, 5, 5, 3, 3, 3, 1, 6, 6, 5,+ 5, 5, 3, 2, 2, 2, 8, 8, 7, 6, 7, 7, 7, 7, 6, 6,+ 6, 6, 3, 3, 3, 1, 7, 7, 6, 6, 6, 5, 5, 5, 5, 4,+ 4, 4, 3, 2, 3, 3, 10, 10, 10, 10, 9, 9, 9, 9, 8, 8,+ 9, 9, 8, 9, 9, 8, 8, 7, 7, 7, 8, 8, 8, 8, 7, 7,+ 7, 7, 6, 5, 6, 6, 4, 3, 3, 1, 11, 11, 10, 9, 10, 10,+ 9, 9, 9, 8, 8, 9, 9, 9, 9, 8, 8, 8, 7, 8, 8, 8,+ 8, 8, 8, 8, 8, 6, 6, 6, 4, 4, 2, 3, 3, 2, 9, 9,+ 8, 8, 9, 9, 8, 8, 8, 8, 7, 7, 7, 8, 8, 7, 7, 7,+ 7, 6, 6, 6, 6, 5, 5, 6, 6, 5, 5, 4, 4, 4, 3, 3,+ 3, 3, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 11,+ 11, 10, 9, 9, 10, 10, 9, 9, 10, 10, 9, 10, 10, 8, 8, 9,+ 9, 10, 10, 9, 9, 10, 10, 8, 8, 8, 9, 9, 9, 9, 9, 9,+ 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 4, 3,+ 3, 1, 10, 10, 10, 10, 10, 10, 10, 11, 11, 10, 10, 9, 9, 9,+ 10, 10, 10, 10, 8, 8, 9, 9, 7, 8, 8, 8, 8, 8, 9, 9,+ 9, 9, 8, 7, 8, 8, 7, 7, 8, 8, 8, 9, 9, 8, 8, 8,+ 8, 8, 8, 7, 7, 6, 6, 7, 7, 6, 5, 4, 5, 5, 3, 3,+ 3, 2, 10, 10, 9, 9, 9, 9, 9, 9, 9, 8, 8, 9, 9, 8,+ 8, 8, 8, 8, 8, 9, 9, 8, 8, 8, 8, 8, 9, 9, 7, 7,+ 7, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 8, 8, 7, 7, 7,+ 6, 6, 6, 6, 7, 7, 6, 5, 5, 5, 4, 4, 5, 5, 4, 3,+ 3, 3, 19, 19, 18, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,+ 17, 17, 15, 15, 16, 16, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,+ 16, 16, 15, 16, 16, 14, 14, 15, 15, 15, 15, 14, 14, 14, 14, 14,+ 14, 14, 14, 14, 14, 14, 15, 15, 14, 13, 14, 14, 13, 13, 14, 14,+ 13, 14, 14, 13, 14, 14, 13, 14, 14, 13, 13, 14, 14, 12, 12, 12,+ 13, 13, 13, 13, 13, 13, 12, 13, 13, 12, 12, 13, 13, 13, 13, 13,+ 13, 13, 13, 13, 13, 13, 13, 12, 12, 13, 13, 12, 12, 12, 12, 13,+ 13, 13, 13, 12, 13, 13, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12,+ 11, 11, 11, 11, 12, 12, 11, 11, 12, 12, 11, 12, 12, 12, 12, 11,+ 11, 12, 12, 11, 12, 12, 11, 12, 12, 11, 12, 12, 10, 10, 10, 11,+ 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 11, 11, 10, 11, 11,+ 10, 11, 11, 11, 11, 10, 10, 11, 11, 10, 10, 11, 11, 11, 11, 11,+ 11, 9, 9, 10, 10, 10, 10, 10, 11, 11, 9, 9, 9, 10, 10, 9,+ 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 8, 9, 9, 9, 9,+ 9, 9, 10, 10, 9, 9, 9, 8, 8, 9, 9, 9, 9, 9, 9, 8,+ 7, 8, 8, 8, 8, 7, 7, 7, 7, 7, 6, 6, 6, 6, 4, 4,+ 3, 1, 13, 13, 13, 13, 12, 13, 13, 13, 13, 13, 13, 12, 13, 13,+ 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,+ 12, 12, 12, 12, 12, 12, 12, 13, 13, 11, 11, 12, 12, 12, 12, 11,+ 11, 11, 11, 11, 11, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11, 12,+ 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,+ 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 11, 11, 11, 11,+ 11, 11, 10, 11, 11, 11, 11, 11, 11, 10, 10, 11, 11, 10, 10, 10,+ 10, 11, 11, 10, 10, 10, 10, 10, 10, 10, 11, 11, 10, 10, 10, 10,+ 10, 11, 11, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,+ 9, 10, 10, 10, 10, 9, 10, 10, 9, 10, 10, 10, 10, 10, 10, 10,+ 10, 9, 9, 9, 9, 9, 9, 9, 10, 10, 9, 9, 9, 9, 9, 9,+ 10, 10, 9, 9, 9, 9, 9, 9, 8, 9, 9, 9, 9, 9, 9, 9,+ 9, 9, 9, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 8,+ 8, 8, 8, 8, 8, 9, 9, 8, 8, 8, 8, 8, 8, 8, 9, 9,+ 8, 7, 8, 8, 7, 7, 7, 7, 8, 8, 7, 7, 7, 7, 7, 6,+ 7, 7, 6, 6, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 3, 4,+ 4, 3, 11, 11, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 11, 10,+ 10, 10, 10, 10, 8, 10, 10, 9, 9, 9, 9, 10, 16, 17, 17, 15,+ 15, 16, 16, 14, 15, 15, 14, 14, 15, 15, 14, 14, 15, 15, 15, 15,+ 14, 15, 15, 14, 13, 8, 9, 9, 8, 8, 13, 14, 14, 14, 14, 14,+ 14, 14, 14, 14, 14, 13, 13, 14, 14, 14, 14, 13, 14, 14, 13, 13,+ 13, 14, 14, 14, 14, 13, 13, 14, 14, 13, 14, 14, 12, 13, 13, 13,+ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 12, 13, 13, 13, 13,+ 13, 13, 12, 13, 13, 12, 12, 13, 13, 11, 12, 12, 12, 12, 12, 12,+ 12, 13, 13, 11, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12,+ 12, 11, 12, 12, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12,+ 11, 12, 12, 11, 12, 12, 11, 12, 12, 11, 12, 12, 11, 10, 10, 11,+ 11, 11, 11, 11, 11, 10, 10, 11, 11, 10, 10, 11, 11, 11, 11, 11,+ 11, 11, 11, 10, 11, 11, 10, 10, 10, 11, 11, 10, 10, 11, 11, 10,+ 10, 11, 11, 10, 9, 9, 10, 10, 10, 10, 10, 10, 9, 9, 9, 10,+ 10, 9, 10, 10, 9, 9, 8, 9, 9, 9, 9, 9, 9, 9, 9, 8,+ 8, 9, 9, 8, 8, 7, 7, 8, 8, 7, 6, 6, 6, 6, 4, 4,+ 3, 1, 8, 8, 8, 8, 8, 8, 8, 8, 7, 8, 8, 7, 7, 8,+ 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 9,+ 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,+ 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 4, 11, 11, 11,+ 11, 12, 12, 11, 10, 11, 11, 10, 10, 10, 10, 11, 11, 10, 10, 10,+ 10, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 10, 10, 10, 10,+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 10, 11, 11, 10, 9,+ 10, 10, 10, 10, 11, 11, 10, 9, 9, 10, 10, 9, 10, 10, 10, 10,+ 9, 9, 10, 10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,+ 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 9, 9, 9, 10, 10, 8,+ 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9,+ 9, 8, 8, 8, 8, 8, 8, 9, 9, 7, 8, 8, 7, 7, 7, 7,+ 7, 8, 8, 7, 7, 6, 6, 7, 7, 6, 5, 5, 6, 6, 4, 4,+ 4, 4,+};+static const uint8_t MPA_HUFF_SYM[1378] = {+ 17, 1, 16, 0, 34, 2, 18, 33, 32, 17, 1, 16, 0, 34, 2, 18,+ 33, 32, 16, 17, 1, 0, 51, 35, 50, 49, 19, 3, 48, 34, 18, 33,+ 2, 32, 17, 1, 16, 0, 51, 3, 35, 50, 48, 19, 49, 34, 2, 18,+ 33, 32, 1, 17, 16, 0, 85, 69, 84, 83, 53, 68, 37, 82, 21, 81,+ 5, 52, 80, 67, 51, 36, 66, 20, 65, 64, 4, 35, 50, 3, 19, 49,+ 48, 34, 18, 33, 2, 32, 17, 1, 16, 0, 85, 84, 69, 83, 53, 68,+ 37, 82, 5, 21, 81, 52, 67, 80, 51, 36, 66, 20, 65, 4, 64, 35,+ 50, 19, 49, 3, 48, 34, 2, 32, 18, 33, 17, 1, 16, 0, 85, 69,+ 53, 83, 84, 5, 68, 37, 82, 21, 81, 52, 67, 80, 4, 36, 66, 51,+ 64, 20, 65, 35, 50, 19, 49, 3, 48, 34, 2, 18, 33, 32, 17, 1,+ 16, 0, 119, 103, 118, 87, 117, 102, 71, 116, 86, 101, 55, 115, 70, 85,+ 84, 99, 39, 114, 100, 7, 112, 98, 69, 53, 6, 83, 68, 23, 113, 54,+ 38, 37, 82, 21, 81, 52, 67, 22, 97, 96, 5, 80, 36, 66, 51, 4,+ 20, 65, 64, 35, 50, 3, 19, 49, 48, 34, 18, 33, 2, 32, 17, 1,+ 16, 0, 119, 103, 118, 117, 102, 71, 116, 87, 85, 86, 101, 55, 115, 70,+ 69, 84, 53, 83, 39, 114, 100, 7, 113, 23, 112, 54, 99, 96, 68, 37,+ 82, 5, 21, 98, 38, 6, 22, 97, 81, 52, 80, 67, 51, 36, 66, 20,+ 65, 4, 64, 35, 50, 19, 49, 3, 48, 34, 33, 18, 2, 32, 17, 1,+ 16, 0, 119, 103, 118, 87, 117, 102, 71, 116, 101, 86, 55, 115, 85, 39,+ 114, 70, 100, 23, 113, 7, 112, 54, 99, 69, 84, 68, 6, 5, 38, 98,+ 97, 22, 96, 53, 83, 37, 82, 21, 81, 52, 67, 80, 4, 36, 66, 20,+ 51, 65, 35, 50, 64, 3, 48, 19, 49, 34, 18, 33, 2, 32, 0, 17,+ 1, 16, 254, 252, 253, 237, 255, 239, 223, 238, 207, 222, 191, 251, 206, 220,+ 175, 233, 236, 221, 250, 205, 190, 235, 159, 249, 234, 189, 219, 143, 248, 204,+ 174, 158, 142, 127, 126, 247, 218, 173, 188, 203, 246, 111, 232, 95, 157, 217,+ 245, 231, 172, 187, 79, 244, 202, 230, 243, 63, 141, 216, 47, 242, 110, 156,+ 15, 201, 94, 171, 125, 215, 78, 200, 214, 62, 185, 155, 170, 31, 241, 240,+ 186, 229, 228, 140, 109, 227, 226, 46, 14, 30, 225, 224, 93, 213, 124, 199,+ 77, 139, 184, 212, 154, 169, 108, 198, 61, 211, 123, 45, 210, 29, 183, 92,+ 197, 153, 122, 195, 167, 151, 75, 209, 13, 208, 138, 168, 76, 196, 107, 182,+ 60, 44, 194, 91, 181, 137, 28, 193, 152, 12, 192, 180, 106, 166, 121, 59,+ 179, 136, 90, 43, 165, 105, 164, 120, 135, 148, 119, 118, 178, 27, 177, 11,+ 176, 150, 74, 58, 163, 89, 149, 42, 162, 26, 161, 10, 104, 160, 134, 73,+ 147, 57, 88, 133, 103, 41, 146, 87, 117, 56, 131, 102, 71, 116, 86, 101,+ 115, 25, 145, 9, 144, 72, 132, 114, 70, 100, 40, 130, 24, 55, 39, 23,+ 113, 85, 7, 112, 54, 99, 69, 84, 38, 98, 53, 129, 8, 128, 22, 97,+ 6, 96, 83, 68, 37, 82, 5, 21, 81, 52, 67, 80, 36, 66, 51, 20,+ 65, 4, 64, 35, 50, 19, 49, 3, 48, 34, 18, 33, 2, 32, 17, 1,+ 16, 0, 255, 239, 254, 223, 238, 253, 207, 252, 222, 237, 191, 251, 206, 236,+ 221, 175, 250, 190, 235, 205, 220, 159, 249, 234, 189, 219, 143, 248, 204, 158,+ 233, 127, 247, 173, 218, 188, 111, 174, 15, 203, 246, 142, 232, 95, 157, 245,+ 126, 231, 172, 202, 187, 217, 141, 79, 244, 63, 243, 216, 230, 47, 242, 110,+ 240, 31, 241, 156, 201, 94, 171, 186, 229, 125, 215, 78, 228, 140, 200, 62,+ 109, 214, 227, 155, 185, 46, 170, 226, 30, 225, 14, 224, 93, 213, 124, 199,+ 77, 139, 212, 184, 154, 169, 108, 198, 61, 211, 210, 45, 13, 29, 123, 183,+ 209, 92, 208, 197, 138, 168, 76, 196, 107, 182, 153, 12, 60, 195, 122, 167,+ 166, 192, 11, 194, 44, 91, 181, 28, 137, 152, 193, 75, 180, 106, 59, 121,+ 179, 151, 136, 43, 90, 178, 165, 27, 177, 176, 105, 150, 74, 164, 120, 135,+ 58, 163, 89, 149, 42, 162, 26, 161, 10, 160, 104, 134, 73, 148, 57, 147,+ 119, 9, 88, 133, 41, 103, 118, 146, 145, 25, 144, 72, 132, 87, 117, 56,+ 131, 102, 71, 40, 130, 24, 129, 116, 8, 128, 86, 101, 55, 115, 70, 39,+ 114, 100, 23, 85, 113, 7, 112, 54, 99, 69, 84, 38, 98, 22, 6, 96,+ 53, 97, 83, 68, 37, 82, 21, 81, 5, 80, 52, 67, 36, 66, 51, 65,+ 20, 4, 35, 50, 64, 3, 19, 49, 48, 34, 18, 33, 2, 32, 17, 1,+ 16, 0, 239, 254, 223, 253, 207, 252, 191, 251, 175, 250, 159, 249, 248, 143,+ 127, 247, 111, 246, 255, 95, 245, 79, 244, 243, 240, 63, 206, 236, 221, 222,+ 233, 234, 217, 238, 237, 235, 190, 205, 220, 219, 174, 204, 173, 218, 126, 172,+ 202, 201, 125, 94, 189, 242, 47, 15, 31, 241, 158, 188, 203, 142, 232, 157,+ 231, 187, 141, 216, 110, 230, 156, 171, 186, 229, 215, 78, 228, 140, 200, 62,+ 109, 214, 155, 185, 170, 225, 212, 184, 169, 123, 183, 208, 227, 14, 224, 93,+ 213, 124, 199, 77, 139, 154, 108, 198, 61, 92, 197, 13, 138, 168, 153, 76,+ 182, 122, 60, 91, 137, 28, 192, 152, 121, 226, 46, 30, 211, 45, 210, 209,+ 59, 151, 136, 29, 196, 107, 195, 167, 44, 194, 181, 193, 12, 75, 180, 106,+ 166, 179, 90, 165, 43, 178, 27, 177, 11, 176, 105, 150, 74, 164, 120, 135,+ 163, 58, 89, 42, 149, 104, 161, 134, 119, 148, 73, 87, 103, 162, 26, 10,+ 160, 57, 147, 88, 133, 41, 146, 118, 9, 25, 145, 144, 72, 132, 117, 56,+ 131, 102, 40, 130, 71, 116, 24, 129, 128, 8, 86, 55, 115, 101, 70, 39,+ 114, 100, 85, 7, 23, 113, 112, 54, 99, 69, 84, 38, 98, 22, 97, 6,+ 96, 83, 53, 68, 37, 82, 81, 21, 5, 52, 67, 80, 36, 66, 51, 20,+ 65, 4, 64, 35, 50, 19, 49, 3, 48, 34, 18, 33, 2, 32, 17, 1,+ 16, 0, 239, 254, 223, 253, 207, 252, 191, 251, 250, 175, 159, 249, 248, 143,+ 127, 247, 111, 246, 95, 245, 79, 244, 63, 243, 47, 242, 241, 31, 240, 15,+ 238, 222, 237, 206, 236, 221, 190, 235, 205, 220, 174, 234, 189, 219, 204, 158,+ 233, 173, 218, 188, 203, 142, 232, 157, 217, 126, 231, 172, 255, 202, 187, 141,+ 216, 14, 224, 13, 230, 110, 156, 201, 94, 186, 229, 171, 125, 215, 228, 140,+ 200, 78, 46, 62, 109, 214, 227, 155, 185, 170, 226, 30, 225, 93, 213, 124,+ 199, 77, 139, 184, 212, 154, 169, 108, 198, 61, 211, 45, 210, 29, 123, 183,+ 209, 92, 197, 138, 168, 153, 76, 196, 107, 182, 208, 12, 60, 195, 122, 167,+ 44, 194, 91, 181, 28, 137, 152, 193, 75, 192, 11, 59, 176, 10, 26, 180,+ 106, 166, 121, 151, 160, 9, 144, 179, 136, 43, 90, 178, 165, 27, 177, 105,+ 150, 164, 74, 120, 135, 58, 163, 89, 149, 42, 162, 161, 104, 134, 119, 73,+ 148, 57, 147, 88, 133, 41, 103, 118, 146, 25, 145, 72, 132, 87, 117, 56,+ 131, 102, 40, 130, 24, 71, 116, 129, 8, 128, 86, 101, 23, 7, 112, 115,+ 55, 39, 114, 70, 100, 85, 113, 54, 99, 69, 84, 38, 98, 22, 97, 6,+ 96, 53, 83, 68, 37, 82, 21, 5, 80, 81, 52, 67, 36, 66, 51, 20,+ 65, 4, 64, 35, 50, 19, 49, 3, 48, 34, 18, 33, 2, 32, 17, 1,+ 16, 0,+};+/* table_select 0..31: FFmpeg's table number (0: none) and linbits */+static const uint8_t MPA_HUFF_DATA[64] = {+ 0, 0, 1, 0, 2, 0, 3, 0, 0, 0, 4, 0, 5, 0, 6, 0,+ 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, 0, 0, 13, 0,+ 14, 1, 14, 2, 14, 3, 14, 4, 14, 6, 14, 8, 14, 10, 14, 13,+ 15, 4, 15, 5, 15, 6, 15, 7, 15, 8, 15, 9, 15, 11, 15, 13,+};+/* count1 tables A and B: the code and length of each value vwxy */+static const uint8_t MPA_QUAD_CODE[32] = {+ 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1,+ 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,+};+static const uint8_t MPA_QUAD_LEN[32] = {+ 1, 4, 4, 5, 4, 6, 5, 6, 4, 5, 5, 6, 5, 6, 6, 6,+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,+};+/* widths of the scale factor bands, by rate: 44.1, 48, 32, 22.05, 24, 16, 11.025, 12, 8 kHz */+static const uint8_t MPA_BAND_LONG[198] = {+ 4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10, 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158,+ 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10, 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192,+ 4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12, 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26,+ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54,+ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 18, 22, 26, 32, 38, 46, 54, 62, 70, 76, 36,+ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54,+ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54,+ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54,+ 12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2,+};+static const uint8_t MPA_BAND_SHORT[117] = {+ 4, 4, 4, 4, 6, 8, 10, 12, 14, 18, 22, 30, 56,+ 4, 4, 4, 4, 6, 6, 10, 12, 14, 16, 20, 26, 66,+ 4, 4, 4, 4, 6, 8, 12, 16, 20, 26, 34, 42, 12,+ 4, 4, 4, 6, 6, 8, 10, 14, 18, 26, 32, 42, 18,+ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 32, 44, 12,+ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18,+ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18,+ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18,+ 8, 8, 8, 12, 16, 20, 24, 28, 36, 2, 2, 2, 26,+};+static const uint8_t MPA_PRETAB[44] = {+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0,+};+static const uint8_t MPA_SLEN[32] = {+ 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4,+ 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3,+};+static const uint8_t MPA_LSF_NSF[72] = {+ 6, 5, 5, 5, 9, 9, 9, 9, 6, 9, 9, 9,+ 6, 5, 7, 3, 9, 9, 12, 6, 6, 9, 12, 6,+ 11, 10, 0, 0, 18, 18, 0, 0, 15, 18, 0, 0,+ 7, 7, 7, 0, 12, 12, 12, 0, 6, 15, 12, 0,+ 6, 6, 6, 3, 12, 9, 9, 6, 6, 12, 9, 6,+ 8, 8, 5, 0, 15, 12, 9, 0, 6, 18, 9, 0,+};+/* the synthesis window D[0..256] times 65536 */+static const int32_t MPA_WINDOW[257] = {+ 0, -1, -1, -1, -1, -1, -1, -2, -2, -2, -2, -3, -3, -4, -4, -5,+ -5, -6, -7, -7, -8, -9, -10, -11, -13, -14, -16, -17, -19, -21, -24, -26,+ -29, -31, -35, -38, -41, -45, -49, -53, -58, -63, -68, -73, -79, -85, -91, -97,+ -104, -111, -117, -125, -132, -139, -147, -154, -161, -169, -176, -183, -190, -196, -202, -208,+ 213, 218, 222, 225, 227, 228, 228, 227, 224, 221, 215, 208, 200, 189, 177, 163,+ 146, 127, 106, 83, 57, 29, -2, -36, -72, -111, -153, -197, -244, -294, -347, -401,+ -459, -519, -581, -645, -711, -779, -848, -919, -991, -1064, -1137, -1210, -1283, -1356, -1428, -1498,+ -1567, -1634, -1698, -1759, -1817, -1870, -1919, -1962, -2001, -2032, -2057, -2075, -2085, -2087, -2080, -2063,+ 2037, 2000, 1952, 1893, 1822, 1739, 1644, 1535, 1414, 1280, 1131, 970, 794, 605, 402, 185,+ -45, -288, -545, -814, -1095, -1388, -1692, -2006, -2330, -2663, -3004, -3351, -3705, -4063, -4425, -4788,+ -5153, -5517, -5879, -6237, -6589, -6935, -7271, -7597, -7910, -8209, -8491, -8755, -8998, -9219, -9416, -9585,+ -9727, -9838, -9916, -9959, -9966, -9935, -9863, -9750, -9592, -9389, -9139, -8840, -8492, -8092, -7640, -7134,+ 6574, 5959, 5288, 4561, 3776, 2935, 2037, 1082, 70, -998, -2122, -3300, -4533, -5818, -7154, -8540,+ -9975, -11455, -12980, -14548, -16155, -17799, -19478, -21189, -22929, -24694, -26482, -28289, -30112, -31947, -33791, -35640,+ -37489, -39336, -41176, -43006, -44821, -46617, -48390, -50137, -51853, -53534, -55178, -56778, -58333, -59838, -61289, -62684,+ -64019, -65290, -66494, -67629, -68692, -69679, -70590, -71420, -72169, -72835, -73415, -73908, -74313, -74630, -74856, -74992,+ 75038,+};+/* bit rates in kbit/s: [lsf][layer - 1][index] */+static const uint16_t MPA_BITRATE[90] = {+ 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,+ 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,+ 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,+ 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,+ 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,+ 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,+};+/* Layer II: subbands by table, quantization steps and bits (negative: grouped), the allocation tables A..D and the LSF one */+static const uint8_t MPA_SBLIMIT[5] = {+ 27, 30, 8, 12, 30,+};+static const uint16_t MPA_QSTEPS[17] = {+ 3, 5, 7, 9, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767,+ 65535,+};+static const int8_t MPA_QBITS[17] = {+ -5, -7, 3, -10, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,+ 16,+};+static const uint8_t MPA_ALLOC_1[300] = {+ 4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,+ 4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,+ 4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16,+ 3, 0, 1, 2, 3, 4, 5, 16, 3, 0, 1, 2, 3, 4, 5, 16,+ 3, 0, 1, 2, 3, 4, 5, 16, 3, 0, 1, 2, 3, 4, 5, 16,+ 3, 0, 1, 2, 3, 4, 5, 16, 3, 0, 1, 2, 3, 4, 5, 16,+ 3, 0, 1, 2, 3, 4, 5, 16, 3, 0, 1, 2, 3, 4, 5, 16,+ 3, 0, 1, 2, 3, 4, 5, 16, 3, 0, 1, 2, 3, 4, 5, 16,+ 3, 0, 1, 2, 3, 4, 5, 16, 3, 0, 1, 2, 3, 4, 5, 16,+ 2, 0, 1, 16, 2, 0, 1, 16, 2, 0, 1, 16, 2, 0, 1, 16,+ 2, 0, 1, 16, 2, 0, 1, 16, 2, 0, 1, 16,+};+static const uint8_t MPA_ALLOC_3[112] = {+ 4, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,+ 4, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,+ 3, 0, 1, 3, 4, 5, 6, 7, 3, 0, 1, 3, 4, 5, 6, 7,+ 3, 0, 1, 3, 4, 5, 6, 7, 3, 0, 1, 3, 4, 5, 6, 7,+ 3, 0, 1, 3, 4, 5, 6, 7, 3, 0, 1, 3, 4, 5, 6, 7,+ 3, 0, 1, 3, 4, 5, 6, 7, 3, 0, 1, 3, 4, 5, 6, 7,+ 3, 0, 1, 3, 4, 5, 6, 7, 3, 0, 1, 3, 4, 5, 6, 7,+};+static const uint8_t MPA_ALLOC_4[196] = {+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,+ 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,+ 3, 0, 1, 3, 4, 5, 6, 7, 3, 0, 1, 3, 4, 5, 6, 7,+ 3, 0, 1, 3, 4, 5, 6, 7, 3, 0, 1, 3, 4, 5, 6, 7,+ 3, 0, 1, 3, 4, 5, 6, 7, 3, 0, 1, 3, 4, 5, 6, 7,+ 3, 0, 1, 3, 4, 5, 6, 7, 2, 0, 1, 3, 2, 0, 1, 3,+ 2, 0, 1, 3, 2, 0, 1, 3, 2, 0, 1, 3, 2, 0, 1, 3,+ 2, 0, 1, 3, 2, 0, 1, 3, 2, 0, 1, 3, 2, 0, 1, 3,+ 2, 0, 1, 3, 2, 0, 1, 3, 2, 0, 1, 3, 2, 0, 1, 3,+ 2, 0, 1, 3, 2, 0, 1, 3, 2, 0, 1, 3, 2, 0, 1, 3,+ 2, 0, 1, 3,+};+#endifdiff --git a/src/common/audio_ogg.c b/src/common/audio_ogg.cnew file mode 100644index 0000000..0adc69e--- /dev/null+++ b/src/common/audio_ogg.c@@ -0,0 +1,246 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_ogg.c - an Ogg file (RFC 3533; audio.h): its pages, checked by+ * their CRC, joined into the packets of the first logical stream that+ * holds sound, with the granule position of the page each packet ends on;+ * the codec found by its first packet - Vorbis (audio_vorbis.c), Opus+ * (audio_opus.c), FLAC (its packets made a FLAC stream again for+ * audio_flac.c).+ */+#include "audio.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "audio_ogg.h"++static uint32_t crc_table[256];++static void crc_init(void)+{+ for (uint32_t i = 0; i < 256; i++) {+ uint32_t r = i << 24;+ for (int k = 0; k < 8; k++)+ r = r & 0x80000000u ? r << 1 ^ 0x04C11DB7u : r << 1;+ crc_table[i] = r;+ }+}++static uint32_t page_crc(const uint8_t *p, size_t n)+{+ uint32_t c = 0;+ for (size_t i = 0; i < n; i++) {+ uint8_t b = i >= 22 && i < 26 ? 0 : p[i]; /* its own field as 0 */+ c = c << 8 ^ crc_table[(c >> 24) ^ b];+ }+ return c;+}++static uint32_t le32(const uint8_t *p)+{+ return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 |+ (uint32_t)p[3] << 24;+}++void ogg_packets_free(struct ogg_packets *o)+{+ free(o->data);+ free(o->off);+ free(o->len);+ free(o->granule);+ memset(o, 0, sizeof *o);+}++int ogg_add_packet(struct ogg_packets *o, const uint8_t *p, size_t n,+ int64_t granule)+{+ if (o->count == o->cap) {+ size_t c = o->cap ? o->cap * 2 : 256;+ size_t *off = realloc(o->off, sizeof(size_t) * c);+ if (off)+ o->off = off;+ size_t *len = realloc(o->len, sizeof(size_t) * c);+ if (len)+ o->len = len;+ int64_t *g = realloc(o->granule, sizeof(int64_t) * c);+ if (g)+ o->granule = g;+ if (!off || !len || !g)+ return -1;+ o->cap = c;+ }+ if (o->used + n > o->room) {+ size_t r = o->room ? o->room * 2 : 1 << 16;+ while (r < o->used + n)+ r *= 2;+ uint8_t *d = realloc(o->data, r);+ if (!d)+ return -1;+ o->data = d;+ o->room = r;+ }+ memcpy(o->data + o->used, p, n);+ o->off[o->count] = o->used;+ o->len[o->count] = n;+ o->granule[o->count] = granule;+ o->used += n;+ o->count++;+ return 0;+}++/* the codec of a stream's first packet: 1 Vorbis, 2 Opus, 3 FLAC, 0 not+ sound this reads */+static int codec_of(const uint8_t *p, size_t n)+{+ if (n >= 7 && p[0] == 1 && !memcmp(p + 1, "vorbis", 6))+ return 1;+ if (n >= 8 && !memcmp(p, "OpusHead", 8))+ return 2;+ if (n >= 13 && p[0] == 0x7F && !memcmp(p + 1, "FLAC", 4))+ return 3;+ return 0;+}++int ogg_read(const uint8_t *d, size_t n, struct ogg_packets *o, char *why,+ size_t wn)+{+ memset(o, 0, sizeof *o);+ o->pre_skip = -1;+ if (!crc_table[1])+ crc_init();+ uint32_t serial = 0;+ int chosen = 0;+ /* a packet across pages: its parts so far */+ uint8_t *part = NULL;+ size_t part_n = 0, part_cap = 0;+ for (size_t at = 0; at + 27 <= n;) {+ const uint8_t *pg = d + at;+ if (memcmp(pg, "OggS", 4) || pg[4] != 0) {+ at++; /* lost: the next capture pattern */+ continue;+ }+ size_t segs = pg[26], body = 0;+ if (at + 27 + segs > n)+ break;+ for (size_t i = 0; i < segs; i++)+ body += pg[27 + i];+ size_t size = 27 + segs + body;+ if (at + size > n || page_crc(pg, size) != le32(pg + 22)) {+ at++;+ continue;+ }+ uint32_t ser = le32(pg + 14);+ int64_t granule =+ (int64_t)((uint64_t)le32(pg + 6) | (uint64_t)le32(pg + 10) << 32);+ const uint8_t *seg = pg + 27, *data = pg + 27 + segs;+ if (!chosen && (pg[5] & 2)) { /* a stream begins: is it sound? */+ int c = segs && seg[0] < 255 ? codec_of(data, seg[0]) : 0;+ if (c) {+ chosen = c;+ serial = ser;+ }+ }+ if (chosen && ser == serial) {+ if (!(pg[5] & 1))+ part_n = 0; /* not a continuation: nothing pending */+ /* the packets ending on this page; the last ends here only+ when its last segment is shorter than 255 */+ int last_end = -1;+ for (size_t i = 0; i < segs; i++)+ if (seg[i] < 255)+ last_end = (int)i;+ size_t pos = 0;+ for (size_t i = 0; i < segs; i++) {+ size_t l = seg[i];+ if (part_n + l > part_cap) {+ size_t c = part_cap ? part_cap * 2 : 4096;+ while (c < part_n + l)+ c *= 2;+ uint8_t *q = realloc(part, c);+ if (!q) {+ free(part);+ ogg_packets_free(o);+ snprintf(why, wn, "out of memory");+ return -1;+ }+ part = q;+ part_cap = c;+ }+ memcpy(part + part_n, data + pos, l);+ part_n += l;+ pos += l;+ if (l < 255) {+ if (ogg_add_packet(o, part, part_n,+ (int)i == last_end ? granule : -1)) {+ free(part);+ ogg_packets_free(o);+ snprintf(why, wn, "out of memory");+ return -1;+ }+ part_n = 0;+ }+ }+ if (pg[5] & 4) /* the stream's last page */+ break;+ }+ at += size;+ }+ free(part);+ if (!chosen || !o->count) {+ ogg_packets_free(o);+ snprintf(why, wn, "an Ogg file without Vorbis, Opus or FLAC sound");+ return -1;+ }+ o->codec = chosen;+ return 0;+}++/* Ogg FLAC: 0x7F "FLAC", the mapping's version and header count, then+ "fLaC" and STREAMINFO; the other packets metadata blocks then frames */+static int ogg_flac(const struct ogg_packets *o, struct janas_audio *a,+ char *why, size_t wn)+{+ size_t total = 0;+ for (size_t i = 0; i < o->count; i++)+ total += o->len[i];+ uint8_t *s = malloc(total + 4);+ if (!s) {+ snprintf(why, wn, "out of memory");+ return -1;+ }+ size_t n = 0;+ const uint8_t *first = o->data + o->off[0];+ if (o->len[0] < 51) {+ free(s);+ snprintf(why, wn, "an Ogg FLAC stream without its STREAMINFO");+ return -1;+ }+ memcpy(s, first + 9, o->len[0] - 9); /* "fLaC" and STREAMINFO */+ n = o->len[0] - 9;+ for (size_t i = 1; i < o->count; i++) {+ memcpy(s + n, o->data + o->off[i], o->len[i]);+ n += o->len[i];+ }+ /* the metadata blocks keep their own last-block flags; the first is+ marked last when no other follows it in the header packets */+ int rc = janas_audio_flac(s, n, a, why, wn);+ free(s);+ return rc;+}++int janas_audio_ogg(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn)+{+ if (n < 4 || memcmp(d, "OggS", 4))+ return 1;+ struct ogg_packets o;+ if (ogg_read(d, n, &o, why, wn))+ return -1;+ int rc = o.codec == 1 ? janas_vorbis_stream(&o, a, why, wn)+ : o.codec == 2 ? janas_opus_stream(&o, a, why, wn)+ : ogg_flac(&o, a, why, wn);+ ogg_packets_free(&o);+ return rc;+}diff --git a/src/common/audio_ogg.h b/src/common/audio_ogg.hnew file mode 100644index 0000000..3a568c8--- /dev/null+++ b/src/common/audio_ogg.h@@ -0,0 +1,67 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_ogg.h - the packets of a stream, as the Ogg (audio_ogg.c), MP4+ * (audio_mp4.c) and Matroska/WebM (audio_mkv.c) containers give them to+ * the codecs that read them: Vorbis (audio_vorbis.c), Opus (audio_opus.c),+ * ALAC (audio_alac.c); and the trim a container's edit says. Inside+ * src/common.+ */+#ifndef JANAS_COMMON_AUDIO_OGG_H+#define JANAS_COMMON_AUDIO_OGG_H++#include <stddef.h>+#include <stdint.h>++#include "audio.h"++struct ogg_packets {+ int codec; /* 1 Vorbis, 2 Opus, 3 FLAC */+ uint8_t *data;+ size_t used, room;+ size_t *off, *len;+ int64_t *granule; /* of the page a packet ends on, or -1 */+ size_t count, cap;+ /* from a container that says them apart from the packets (Matroska):+ the codec's private data and the samples to drop at the start */+ int64_t pre_skip; /* -1: as the codec's header says */+};++/* a packet added, copied: 0, or -1 when out of memory */+int ogg_add_packet(struct ogg_packets *o, const uint8_t *p, size_t n,+ int64_t granule);++/* an edit list's trim: the samples skipped at the start (media_time, in+ the track's timescale; -1: none) and the length kept (in the movie's+ timescale; 0: all) */+struct audio_edit {+ int64_t media_time;+ uint64_t length;+ uint32_t timescale, movie_timescale;+};++/* got samples at rate trimmed as e says (NULL: not at all): the first+ *skip dropped, *total kept */+void audio_edit_apply(const struct audio_edit *e, uint32_t rate, size_t *skip,+ size_t *total);++/* the packets of the first stream holding sound: 0, or -1 with why */+int ogg_read(const uint8_t *d, size_t n, struct ogg_packets *o, char *why,+ size_t wn);+void ogg_packets_free(struct ogg_packets *o);++/* the codecs: 0, or -1 with why */+int janas_vorbis_stream(const struct ogg_packets *o, struct janas_audio *a,+ char *why, size_t wn);+int janas_opus_stream(const struct ogg_packets *o, struct janas_audio *a,+ char *why, size_t wn);+/* the samples at 48 kHz an Opus packet makes (its TOC and frame count) */+int janas_opus_packet_samples(const uint8_t *p, size_t n);+/* ALAC: its magic cookie (ALACSpecificConfig, with or without the frma+ and alac atoms' heads), the packets at data + off[i] */+int janas_alac_stream(const uint8_t *cookie, size_t cn, const uint8_t *data,+ const size_t *off, const size_t *len, size_t count,+ const struct audio_edit *edit, struct janas_audio *a,+ char *why, size_t wn);++#endifdiff --git a/src/common/audio_opus.c b/src/common/audio_opus.cnew file mode 100644index 0000000..182d09d--- /dev/null+++ b/src/common/audio_opus.c@@ -0,0 +1,612 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_opus.c - Opus (RFC 6716; in Ogg, RFC 7845; in Matroska): the+ * identification header (channels, pre-skip, output gain, the channel+ * mapping and its streams), the packets (the table-of-contents byte, the+ * four ways frames are packed, self-delimited framing between the streams+ * of a multistream packet), and each frame as the reference decoder takes+ * it: SILK, hybrid or CELT; a CELT frame's bands from 17 up in hybrid, its+ * last band by the bandwidth; the redundant 5 ms CELT frames that smooth a+ * change of mode, the concealment that stands in for them when there are+ * none, the fades. Everything at 48 kHz; the channels averaged; the+ * pre-skip and the samples past the last granule position taken off.+ */+#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "audio_ogg.h"+#include "audio_opus.h"+#include "audio_opus_tab.h"++#define MODE_SILK 1000+#define MODE_HYBRID 1001+#define MODE_CELT 1002+#define BW_NB 1101+#define BW_MB 1102+#define BW_WB 1103+#define BW_SWB 1104+#define BW_FB 1105+#define F20 960+#define F10 480+#define F5 240+#define F2_5 120++struct odec {+ int channels; /* the output's */+ struct celt_dec *celt;+ struct silk_dec *silk;+ int mode, prev_mode, prev_redundancy, bandwidth, frame_size;+ int stream_channels;+ uint32_t range_final;+ float *pcm[2], *silk_pcm[2], *redund[2], *trans[2], *tmp[2];+};++static struct odec *odec_new(int channels)+{+ struct odec *o = calloc(1, sizeof *o);+ if (!o)+ return NULL;+ o->channels = channels;+ o->celt = jcelt_new(channels);+ o->silk = jsilk_new();+ for (int c = 0; c < 2; c++) {+ o->silk_pcm[c] = malloc(sizeof(float) * 2880);+ o->redund[c] = malloc(sizeof(float) * F5);+ o->trans[c] = malloc(sizeof(float) * F5);+ o->tmp[c] = malloc(sizeof(float) * 2880);+ if (!o->silk_pcm[c] || !o->redund[c] || !o->trans[c] || !o->tmp[c])+ o->celt = NULL;+ }+ if (!o->celt || !o->silk) {+ free(o);+ return NULL;+ }+ return o;+}++static void odec_free(struct odec *o)+{+ if (!o)+ return;+ jcelt_free(o->celt);+ jsilk_free(o->silk);+ for (int c = 0; c < 2; c++) {+ free(o->silk_pcm[c]);+ free(o->redund[c]);+ free(o->trans[c]);+ free(o->tmp[c]);+ }+ free(o);+}++static void smooth_fade(float **in1, int o1, float **in2, int o2, float **out,+ int oo, int n, int ch)+{+ for (int c = 0; c < ch; c++)+ for (int i = 0; i < n; i++) {+ float w = CELT_WINDOW[i] * CELT_WINDOW[i];+ out[c][oo + i] = w * in2[c][o2 + i] + (1.f - w) * in1[c][o1 + i];+ }+}++/* one frame of a packet (len bytes, NULL for concealment) into pcm[c][0+ ..]: the samples a channel, or -1 */+static int decode_frame(struct odec *st, const uint8_t *data, int len,+ float **pcm, int frame_size)+{+ int ch = st->channels, audiosize, mode, bandwidth;+ struct rc dec;+ if (len <= 1) {+ /* a lost or empty frame: no more concealed than the TOC says */+ data = NULL;+ if (frame_size > st->frame_size)+ frame_size = st->frame_size;+ }+ if (frame_size < F2_5 && !data) {+ for (int c = 0; c < ch; c++)+ memset(pcm[c], 0,+ sizeof(float) * (size_t)(frame_size > 0 ? frame_size : 0));+ return frame_size > 0 ? frame_size : 0;+ }+ if (data) {+ audiosize = st->frame_size;+ mode = st->mode;+ bandwidth = st->bandwidth;+ rc_init(&dec, data, (uint32_t)len);+ } else {+ audiosize = frame_size;+ mode = st->prev_redundancy ? MODE_CELT : st->prev_mode;+ bandwidth = 0;+ if (mode == 0) {+ for (int c = 0; c < ch; c++)+ memset(pcm[c], 0, sizeof(float) * (size_t)audiosize);+ return audiosize;+ }+ if (audiosize > F20) {+ int done = 0;+ while (done < audiosize) {+ float *p[2] = {pcm[0] + done, ch > 1 ? pcm[1] + done : NULL};+ int k = audiosize - done < F20 ? audiosize - done : F20;+ int ret = decode_frame(st, NULL, 0, p, k);+ if (ret < 0)+ return ret;+ done += ret;+ }+ return frame_size;+ } else if (audiosize < F20) {+ if (audiosize > F10)+ audiosize = F10;+ else if (mode != MODE_SILK && audiosize > F5 && audiosize < F10)+ audiosize = F5;+ }+ }+ int transition = 0;+ float **ptrans = NULL;+ if (data && st->prev_mode > 0 &&+ ((mode == MODE_CELT && st->prev_mode != MODE_CELT &&+ !st->prev_redundancy) ||+ (mode != MODE_CELT && st->prev_mode == MODE_CELT))) {+ transition = 1;+ }+ if (transition && mode == MODE_CELT) {+ ptrans = st->trans;+ decode_frame(st, NULL, 0, ptrans, audiosize < F5 ? audiosize : F5);+ }+ if (audiosize > frame_size)+ return -1;+ frame_size = audiosize;+ if (mode != MODE_CELT) {+ if (st->prev_mode == MODE_CELT)+ jsilk_reset(st->silk);+ int khz = mode == MODE_SILK ? (bandwidth == BW_NB ? 8+ : bandwidth == BW_MB ? 12+ : 16)+ : 16;+ int ms = 1000 * audiosize / 48000;+ if (ms < 10)+ ms = 10;+ int got = 0;+ while (got < frame_size) {+ float *p[2] = {st->silk_pcm[0] + got, st->silk_pcm[1] + got};+ int n = jsilk_frame(st->silk, data ? &dec : NULL, ms, khz,+ st->stream_channels, ch, p, got == 0);+ if (n <= 0) {+ if (!data) {+ for (int c = 0; c < ch; c++)+ memset(st->silk_pcm[c] + got, 0,+ sizeof(float) * (size_t)(frame_size - got));+ break;+ }+ return -1;+ }+ got += n;+ }+ }+ int start_band = 0, redundancy = 0, celt_to_silk = 0, redund_bytes = 0;+ if (mode != MODE_CELT && data &&+ rc_tell(&dec) + 17 + 20 * (mode == MODE_HYBRID) <= 8 * len) {+ redundancy = mode == MODE_HYBRID ? rc_bit_logp(&dec, 12) : 1;+ if (redundancy) {+ celt_to_silk = rc_bit_logp(&dec, 1);+ redund_bytes = mode == MODE_HYBRID+ ? (int)rc_uint(&dec, 256) + 2+ : len - ((rc_tell(&dec) + 7) >> 3);+ len -= redund_bytes;+ if (len * 8 < rc_tell(&dec)) {+ len = 0;+ redund_bytes = 0;+ redundancy = 0;+ }+ dec.storage -= (uint32_t)redund_bytes;+ }+ }+ if (mode != MODE_CELT)+ start_band = 17;+ if (redundancy)+ transition = 0;+ if (transition && mode != MODE_CELT) {+ ptrans = st->trans;+ decode_frame(st, NULL, 0, ptrans, audiosize < F5 ? audiosize : F5);+ }+ int end_band = 21;+ if (bandwidth) {+ end_band = bandwidth == BW_NB ? 13+ : bandwidth == BW_MB || bandwidth == BW_WB ? 17+ : bandwidth == BW_SWB ? 19+ : 21;+ }+ uint32_t redundant_rng = 0;+ struct rc rd;+ if (redundancy && celt_to_silk) {+ jcelt_bands(st->celt, 0, end_band);+ rc_init(&rd, data + len, (uint32_t)redund_bytes);+ jcelt_frame(st->celt, &rd, redund_bytes, F5, st->stream_channels,+ st->redund);+ redundant_rng = jcelt_rng(st->celt);+ }+ jcelt_bands(st->celt, start_band, end_band);+ if (mode != MODE_SILK) {+ int n = frame_size < F20 ? frame_size : F20;+ if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy)+ jcelt_reset(st->celt);+ if (data) {+ if (jcelt_frame(st->celt, &dec, len, n, st->stream_channels, pcm))+ return -1;+ } else {+ jcelt_plc(st->celt, n, pcm);+ }+ } else {+ for (int c = 0; c < ch; c++)+ memset(pcm[c], 0, sizeof(float) * (size_t)frame_size);+ if (st->prev_mode == MODE_HYBRID &&+ !(redundancy && celt_to_silk && st->prev_redundancy)) {+ static const uint8_t SILENCE[2] = {0xFF, 0xFF};+ struct rc sr;+ jcelt_bands(st->celt, 0, end_band);+ rc_init(&sr, SILENCE, 2);+ jcelt_frame(st->celt, &sr, 2, F2_5, st->stream_channels, pcm);+ }+ }+ if (mode != MODE_CELT)+ for (int c = 0; c < ch; c++)+ for (int i = 0; i < frame_size; i++)+ pcm[c][i] += st->silk_pcm[c][i];+ if (redundancy && !celt_to_silk) {+ jcelt_reset(st->celt);+ jcelt_bands(st->celt, 0, end_band);+ rc_init(&rd, data + len, (uint32_t)redund_bytes);+ jcelt_frame(st->celt, &rd, redund_bytes, F5, st->stream_channels,+ st->redund);+ redundant_rng = jcelt_rng(st->celt);+ smooth_fade(pcm, frame_size - F2_5, st->redund, F2_5, pcm,+ frame_size - F2_5, F2_5, ch);+ }+ if (redundancy && celt_to_silk &&+ (st->prev_mode != MODE_SILK || st->prev_redundancy)) {+ for (int c = 0; c < ch; c++)+ memcpy(pcm[c], st->redund[c], sizeof(float) * F2_5);+ smooth_fade(st->redund, F2_5, pcm, F2_5, pcm, F2_5, F2_5, ch);+ }+ if (transition) {+ if (audiosize >= F5) {+ for (int c = 0; c < ch; c++)+ memcpy(pcm[c], ptrans[c], sizeof(float) * F2_5);+ smooth_fade(ptrans, F2_5, pcm, F2_5, pcm, F2_5, F2_5, ch);+ } else {+ smooth_fade(ptrans, 0, pcm, 0, pcm, 0, F2_5, ch);+ }+ }+ st->range_final = len <= 1 ? 0 : dec.rng ^ redundant_rng;+ st->prev_mode = mode;+ st->prev_redundancy = redundancy && !celt_to_silk;+ return audiosize;+}++/* ---- packets ---- */++static int frame_samples(int toc)+{+ int cfg = toc >> 3;+ if (cfg < 12)+ return (cfg & 3) == 3 ? 2880 : 480 << (cfg & 3);+ if (cfg < 16)+ return cfg & 1 ? 960 : 480;+ return 120 << (cfg & 3);+}++int janas_opus_packet_samples(const uint8_t *p, size_t n)+{+ if (!n)+ return 0;+ int code = p[0] & 3, count = code == 0 ? 1 : code < 3 ? 2 : -1;+ if (count < 0)+ count = n >= 2 ? p[1] & 0x3F : 0;+ return frame_samples(p[0]) * count;+}++static int toc_mode(int toc)+{+ return toc & 0x80 ? MODE_CELT+ : (toc & 0x60) == 0x60 ? MODE_HYBRID+ : MODE_SILK;+}++static int toc_bandwidth(int toc)+{+ if (toc & 0x80) {+ int b = (toc >> 5) & 3;+ return b == 0 ? BW_NB : BW_WB + b - 1;+ }+ if ((toc & 0x60) == 0x60)+ return toc & 0x10 ? BW_FB : BW_SWB;+ return BW_NB + ((toc >> 5) & 3);+}++/* a size field: 1 or 2 bytes; -1 when cut short */+static int size_field(const uint8_t *p, int n, int *size)+{+ if (n < 1)+ return -1;+ if (p[0] < 252) {+ *size = p[0];+ return 1;+ }+ if (n < 2)+ return -1;+ *size = 4 * p[1] + p[0];+ return 2;+}++/* the frames of a packet: their count, sizes in size[], the offset of the+ first in *off, the packet's bytes used in *used (self-delimited), or -1 */+static int parse_packet(const uint8_t *data, int len, int self_delim, int *size,+ int *off, int *used)+{+ if (len < 1)+ return -1;+ const uint8_t *p = data + 1;+ int left = len - 1, count, fs = frame_samples(data[0]), last = 0;+ int pad = 0, cbr = 0;+ switch (data[0] & 3) {+ case 0:+ count = 1;+ last = left;+ break;+ case 1:+ count = 2;+ cbr = 1;+ if (!self_delim) {+ if (left & 1)+ return -1;+ last = size[0] = left / 2;+ }+ break;+ case 2: {+ count = 2;+ int k = size_field(p, left, &size[0]);+ if (k < 0 || size[0] > left - k)+ return -1;+ p += k;+ left -= k;+ last = left - size[0];+ break;+ }+ default: {+ if (left < 1)+ return -1;+ int ch = *p++;+ left--;+ count = ch & 0x3F;+ if (count <= 0 || fs * count > 5760)+ return -1;+ if (ch & 0x40) {+ int t;+ do {+ if (left <= 0)+ return -1;+ t = *p++;+ left--;+ int tmp = t == 255 ? 254 : t;+ left -= tmp;+ pad += tmp;+ } while (t == 255);+ }+ if (left < 0)+ return -1;+ cbr = !(ch & 0x80);+ if (!cbr) {+ last = left;+ for (int i = 0; i < count - 1; i++) {+ int k = size_field(p, left, &size[i]);+ if (k < 0 || size[i] > left - k)+ return -1;+ p += k;+ left -= k;+ last -= k + size[i];+ }+ if (last < 0)+ return -1;+ } else if (!self_delim) {+ last = left / count;+ if (last * count != left)+ return -1;+ for (int i = 0; i < count - 1; i++)+ size[i] = last;+ }+ }+ }+ if (self_delim) {+ int k = size_field(p, left, &size[count - 1]);+ if (k < 0 || size[count - 1] > left - k)+ return -1;+ p += k;+ left -= k;+ if (cbr) {+ if (size[count - 1] * count > left)+ return -1;+ for (int i = 0; i < count - 1; i++)+ size[i] = size[count - 1];+ } else if (k + size[count - 1] > last) {+ return -1;+ }+ } else {+ if (last > 1275)+ return -1;+ size[count - 1] = last;+ }+ *off = (int)(p - data);+ int total = *off;+ for (int i = 0; i < count; i++)+ total += size[i];+ *used = total + pad;+ return count;+}++/* the frames of one stream's packet into pcm[c] from at: samples, or -1 */+static int decode_packet(struct odec *st, const uint8_t *data, int len,+ int self_delim, float **pcm, int *used)+{+ int size[48], off,+ count = parse_packet(data, len, self_delim, size, &off, used);+ if (count < 0 || count * frame_samples(data[0]) > 5760)+ return -1;+ st->mode = toc_mode(data[0]);+ st->bandwidth = toc_bandwidth(data[0]);+ st->frame_size = frame_samples(data[0]);+ st->stream_channels = data[0] & 4 ? 2 : 1;+ int got = 0;+ const uint8_t *p = data + off;+ for (int i = 0; i < count; i++) {+ float *o[2] = {pcm[0] + got, st->channels > 1 ? pcm[1] + got : NULL};+ int k = decode_frame(st, p, size[i], o, 5760 - got);+ if (k < 0)+ return -1;+ p += size[i];+ got += k;+ }+ return got;+}++/* ---- the stream ---- */++int janas_opus_stream(const struct ogg_packets *o, struct janas_audio *a,+ char *why, size_t wn)+{+ const uint8_t *h = o->data + o->off[0];+ size_t hn = o->len[0];+ if (hn < 19 || memcmp(h, "OpusHead", 8) || (h[8] & 0xF0)) {+ snprintf(why, wn, "an Opus stream without its header");+ return -1;+ }+ int channels = h[9], pre_skip = h[10] | h[11] << 8;+ int16_t gain_q8 = (int16_t)(h[16] | h[17] << 8);+ int family = h[18], streams = 1, coupled = channels > 1;+ uint8_t map[255] = {0, 1};+ if (o->pre_skip >= 0)+ pre_skip = (int)o->pre_skip;+ if (!channels) {+ snprintf(why, wn, "an Opus stream of no channels");+ return -1;+ }+ if (family == 0) {+ if (channels > 2) {+ snprintf(why, wn,+ "an Opus stream of more channels than its "+ "mapping allows");+ return -1;+ }+ } else {+ if (hn < 21 + (size_t)channels) {+ snprintf(why, wn,+ "an Opus stream whose channel mapping is cut "+ "short");+ return -1;+ }+ streams = h[19];+ coupled = h[20];+ memcpy(map, h + 21, (size_t)channels);+ if (!streams || coupled > streams || streams + coupled > 255) {+ snprintf(why, wn,+ "an Opus stream of a channel mapping out of "+ "range");+ return -1;+ }+ }+ struct odec **dec = calloc((size_t)streams, sizeof *dec);+ float *buf = malloc(sizeof(float) * 2 * 5760 * (size_t)streams);+ float *out = NULL;+ int rc = -1;+ size_t got = 0, cap = 0;+ if (!dec || !buf)+ goto oom;+ for (int s = 0; s < streams; s++)+ if (!(dec[s] = odec_new(s < coupled ? 2 : 1)))+ goto oom;+ /* the comment header is the second packet in Ogg; in Matroska none */+ size_t first = o->count > 1 && o->len[1] >= 8 &&+ !memcmp(o->data + o->off[1], "OpusTags", 8)+ ? 2+ : 1;+ float gain = gain_q8 ? (float)pow(10.0, gain_q8 / (20.0 * 256)) : 1.f;+ int64_t last_granule = -1;+ for (size_t pk = first; pk < o->count; pk++) {+ const uint8_t *p = o->data + o->off[pk];+ int left = (int)o->len[pk], n = -1;+ if (o->granule[pk] >= 0)+ last_granule = o->granule[pk];+ if (!left)+ continue;+ for (int s = 0; s < streams; s++) {+ float *pc[2] = {buf + (size_t)s * 2 * 5760,+ buf + (size_t)s * 2 * 5760 + 5760};+ int used = 0;+ int k = decode_packet(dec[s], p, left, s < streams - 1, pc, &used);+ if (k < 0 || (n >= 0 && k != n)) {+ n = -1;+ break;+ }+ n = k;+ p += used;+ left -= used;+ }+ if (n <= 0)+ continue; /* a packet that does not read: skipped */+ if (got + (size_t)n > JANAS_AUDIO_MAX_SAMPLES) {+ snprintf(why, wn, "an Opus stream longer than this reads");+ goto end;+ }+ if (got + (size_t)n > cap) {+ size_t c = cap ? cap * 2 : 1 << 18;+ while (c < got + (size_t)n)+ c *= 2;+ float *q = realloc(out, sizeof(float) * c);+ if (!q)+ goto oom;+ out = q;+ cap = c;+ }+ for (int i = 0; i < n; i++) {+ double sum = 0;+ int used_ch = 0;+ for (int c = 0; c < channels; c++) {+ int m = family == 0 ? c : map[c];+ if (m == 255) { /* a silent channel */+ used_ch++;+ continue;+ }+ int s = m < 2 * coupled ? m / 2 : m - coupled;+ int sc = m < 2 * coupled ? m % 2 : 0;+ sum += buf[(size_t)s * 2 * 5760 + (size_t)sc * 5760 + i];+ used_ch++;+ }+ out[got + (size_t)i] = (float)(sum / used_ch) * gain;+ }+ got += (size_t)n;+ }+ size_t skip = (size_t)pre_skip, total = got;+ if (last_granule >= 0 && (size_t)last_granule < total)+ total = (size_t)last_granule;+ if (skip >= total) {+ snprintf(why, wn, "an Opus stream without sound");+ goto end;+ }+ memmove(out, out + skip, sizeof(float) * (total - skip));+ a->pcm = out;+ a->n = total - skip;+ a->rate = 48000;+ out = NULL;+ rc = 0;+ goto end;+oom:+ snprintf(why, wn, "out of memory");+end:+ free(out);+ free(buf);+ for (int s = 0; dec && s < streams; s++)+ odec_free(dec[s]);+ free(dec);+ return rc;+}diff --git a/src/common/audio_opus.h b/src/common/audio_opus.hnew file mode 100644index 0000000..9a95ebe--- /dev/null+++ b/src/common/audio_opus.h@@ -0,0 +1,213 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_opus.h - what the parts of the Opus decoder share (RFC 6716): the+ * range decoder (section 4.1), and the CELT (audio_opus_celt.c) and SILK+ * (audio_opus_silk.c) layers as audio_opus.c drives them. Inside+ * src/common.+ */+#ifndef JANAS_COMMON_AUDIO_OPUS_H+#define JANAS_COMMON_AUDIO_OPUS_H++#include <stddef.h>+#include <stdint.h>++/* ---- the range decoder ---- */++struct rc {+ const uint8_t *buf;+ uint32_t storage, offs, end_offs;+ uint32_t end_window;+ int nend_bits, nbits_total;+ uint32_t rng, val, ext;+ int rem, error;+};++#define RC_CODE_BOT (1u << 23)+#define RC_CODE_TOP (1u << 31)++static inline int rc_ilog(uint32_t v)+{+ return v ? 32 - __builtin_clz(v) : 0;+}++static inline int rc_byte(struct rc *r)+{+ return r->offs < r->storage ? r->buf[r->offs++] : 0;+}++static inline int rc_byte_end(struct rc *r)+{+ return r->end_offs < r->storage ? r->buf[r->storage - ++r->end_offs] : 0;+}++static inline void rc_normalize(struct rc *r)+{+ while (r->rng <= RC_CODE_BOT) {+ r->nbits_total += 8;+ r->rng <<= 8;+ int sym = r->rem;+ r->rem = rc_byte(r);+ sym = (sym << 8 | r->rem) >> 1;+ r->val = ((r->val << 8) + (255 & ~(uint32_t)sym)) & (RC_CODE_TOP - 1);+ }+}++static inline void rc_init(struct rc *r, const uint8_t *buf, uint32_t n)+{+ r->buf = buf;+ r->storage = n;+ r->offs = r->end_offs = 0;+ r->end_window = 0;+ r->nend_bits = 0;+ r->nbits_total = 33 - 24;+ r->rng = 1u << 7;+ r->rem = rc_byte(r);+ r->val = r->rng - 1 - (uint32_t)(r->rem >> 1);+ r->error = 0;+ rc_normalize(r);+}++static inline unsigned rc_decode(struct rc *r, unsigned ft)+{+ r->ext = r->rng / ft;+ unsigned s = r->val / r->ext;+ return ft - (s + 1 < ft ? s + 1 : ft);+}++static inline unsigned rc_decode_bin(struct rc *r, unsigned bits)+{+ r->ext = r->rng >> bits;+ unsigned s = r->val / r->ext;+ return (1u << bits) - (s + 1 < (1u << bits) ? s + 1 : (1u << bits));+}++static inline void rc_update(struct rc *r, unsigned fl, unsigned fh,+ unsigned ft)+{+ uint32_t s = r->ext * (ft - fh);+ r->val -= s;+ r->rng = fl > 0 ? r->ext * (fh - fl) : r->rng - s;+ rc_normalize(r);+}++/* a one with probability 1 / 2^logp */+static inline int rc_bit_logp(struct rc *r, unsigned logp)+{+ uint32_t s = r->rng >> logp;+ int ret = r->val < s;+ if (!ret)+ r->val -= s;+ r->rng = ret ? s : r->rng - s;+ rc_normalize(r);+ return ret;+}++static inline int rc_icdf(struct rc *r, const uint8_t *icdf, unsigned ftb)+{+ uint32_t s = r->rng, d = r->val, q = s >> ftb, t;+ int ret = -1;+ do {+ t = s;+ s = q * icdf[++ret];+ } while (d < s);+ r->val = d - s;+ r->rng = t - s;+ rc_normalize(r);+ return ret;+}++/* raw bits from the end of the frame */+static inline uint32_t rc_bits(struct rc *r, unsigned bits)+{+ uint32_t window = r->end_window;+ int avail = r->nend_bits;+ if ((unsigned)avail < bits) {+ do {+ window |= (uint32_t)rc_byte_end(r) << avail;+ avail += 8;+ } while (avail <= 32 - 8);+ }+ uint32_t ret = window & ((bits < 32 ? 1u << bits : 0) - 1u);+ window = bits < 32 ? window >> bits : 0;+ avail -= (int)bits;+ r->end_window = window;+ r->nend_bits = avail;+ r->nbits_total += (int)bits;+ return ret;+}++static inline uint32_t rc_uint(struct rc *r, uint32_t ft)+{+ ft--;+ int ftb = rc_ilog(ft);+ if (ftb > 8) {+ ftb -= 8;+ unsigned f = (unsigned)(ft >> ftb) + 1;+ unsigned s = rc_decode(r, f);+ rc_update(r, s, s + 1, f);+ uint32_t t = (uint32_t)s << ftb | rc_bits(r, (unsigned)ftb);+ if (t <= ft)+ return t;+ r->error = 1;+ return ft;+ }+ ft++;+ unsigned s = rc_decode(r, (unsigned)ft);+ rc_update(r, s, s + 1, (unsigned)ft);+ return s;+}++static inline int rc_tell(const struct rc *r)+{+ return r->nbits_total - rc_ilog(r->rng);+}++/* in eighths of a bit */+static inline uint32_t rc_tell_frac(const struct rc *r)+{+ static const unsigned CORR[8] = {35733, 38967, 42495, 46340,+ 50535, 55109, 60097, 65535};+ uint32_t nbits = (uint32_t)r->nbits_total << 3;+ int l = rc_ilog(r->rng);+ uint32_t q = r->rng >> (l - 16);+ unsigned b = (q >> 12) - 8;+ b += q > CORR[b];+ return nbits - (uint32_t)((l << 3) + (int)b);+}++/* ---- the layers ---- */++struct celt_dec;+/* a CELT decoder of channels channels (1, 2): NULL when out of memory */+struct celt_dec *jcelt_new(int channels);+void jcelt_free(struct celt_dec *d);+void jcelt_reset(struct celt_dec *d);+/* the bands decoded: start 0 (17 in hybrid frames), end by bandwidth */+void jcelt_bands(struct celt_dec *d, int start, int end);+/* a frame of n samples (120, 240, 480, 960 at 48 kHz) from r (len bytes+ of the packet's frame) into out[ch][n]; stream_ch the channels coded;+ 0 or -1 */+int jcelt_frame(struct celt_dec *d, struct rc *r, int len, int n, int stream_ch,+ float **out);+/* the decoder's range after the frame (the bitstream check) */+uint32_t jcelt_rng(const struct celt_dec *d);+/* n samples concealed, as when a packet is lost: what a change of mode+ without a redundant frame fades from */+void jcelt_plc(struct celt_dec *d, int n, float **out);++struct silk_dec;+struct silk_dec *jsilk_new(void);+void jsilk_free(struct silk_dec *d);+void jsilk_reset(struct silk_dec *d);+/* the rate of the output: 8, 12, 16, 24 or 48 kHz (48 by default) */+void jsilk_set_rate(struct silk_dec *d, int hz);+/* the SILK layer of a frame: one call a SILK frame of up to 20 ms of a+ packet's frame of ms milliseconds (10, 20, 40, 60) at its internal rate+ (bandwidth: 8, 12, 16 kHz), stereo or mono, into out[ch] at 48 kHz+ (resampled as libopus does); first: the first call of the packet's+ frame; r NULL: a lost frame concealed. The samples a channel, or -1 */+int jsilk_frame(struct silk_dec *d, struct rc *r, int ms, int khz,+ int stream_ch, int out_ch, float **out, int first);++#endifdiff --git a/src/common/audio_opus_celt.c b/src/common/audio_opus_celt.cnew file mode 100644index 0000000..c4851d2--- /dev/null+++ b/src/common/audio_opus_celt.c@@ -0,0 +1,1889 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_opus_celt.c - the CELT layer of Opus (RFC 6716 section 4.3;+ * audio_opus.h), as the reference decoder computes it in floating point:+ * silence, the post-filter's parameters, transients, the coarse energy+ * (Laplace-coded, predicted in time and frequency), the time-frequency+ * resolution changes, the spreading, the dynamic allocation boosts and the+ * allocation trim, the bit allocation (interpolated between the allocation+ * vectors, bands skipped from the top, intensity and dual stereo, fine+ * energy bits), the fine energy, the shapes of the bands (pyramid vector+ * quantization: theta splits, mid/side stereo, folding from lower bands,+ * the pulses' combinatorial index, spreading rotations, Haar and Hadamard+ * reshuffles), the final energy bits, anti-collapse, the bands scaled by+ * their energies, the inverse MDCT with its short overlap (a DCT-IV on+ * audio_fft.c, mirrored for the overlap), the pitch post-filter and the+ * de-emphasis.+ */+#include "audio_opus.h"++#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "audio_fft.h"+#include "audio_opus_tab.h"++#define NB 21 /* bands */+#define OV 120 /* overlap */+#define DBS 2048 /* the decoder's memory a channel */+#define BITRES 3+#define MAX_FINE_BITS 8+#define FINE_OFFSET 21+#define QTHETA_OFFSET 4+#define QTHETA_OFFSET_TWOPHASE 16+#define SPREAD_NONE 0+#define SPREAD_NORMAL 2+#define SPREAD_AGGRESSIVE 3+#define PVQ_N 180+#define PVQ_K 132++struct celt_dec {+ int start, end, cc;+ float mem[2][DBS + OV];+ float old_e[2 * NB], old_loge[2 * NB], old_loge2[2 * NB], bg_loge[2 * NB];+ float preemph[2];+ int pf_period, pf_period_old, pf_tapset, pf_tapset_old;+ float pf_gain, pf_gain_old;+ uint32_t rng;+ struct aimdct *mdct[4]; /* 240, 480, 960, 1920 points */+ uint32_t *pvq; /* U(a, b), a <= b: pvq[a * PVQ_N + b] */+ float x[2 * 960], freq[960], freq2[960], u[960];+ /* concealment: frames concealed since the last decoded one (in 2.5 ms+ units), whether the pitch-based kind is not to be used yet, its last+ pitch, the overlap still to be folded with the next frame, the LPC+ of the concealed signal */+ int loss_duration, skip_plc, last_pitch, prefilter_and_fold;+ float lpc[2][24];+ float norm[2 * 8 * 100]; /* the bands as folded from */+};++/* ---- small helpers ---- */++static inline int imin(int a, int b)+{+ return a < b ? a : b;+}++static inline int imax(int a, int b)+{+ return a > b ? a : b;+}++static int frac_mul16(int a, int b)+{+ return (16384 + (int32_t)(int16_t)a * (int16_t)b) >> 15;+}++static int bitexact_cos(int x)+{+ int32_t tmp = (4096 + (int32_t)x * x) >> 13;+ int x2 = tmp;+ x2 =+ (32767 - x2) ++ frac_mul16(x2, (-7651 + frac_mul16(x2, (8277 + frac_mul16(-626, x2)))));+ return 1 + x2;+}++static int bitexact_log2tan(int isin, int icos)+{+ int lc = rc_ilog((uint32_t)icos), ls = rc_ilog((uint32_t)isin);+ icos <<= 15 - lc;+ isin <<= 15 - ls;+ return (ls - lc) * (1 << 11) ++ frac_mul16(isin, frac_mul16(isin, -2597) + 7932) -+ frac_mul16(icos, frac_mul16(icos, -2597) + 7932);+}++static uint32_t lcg(uint32_t s)+{+ return 1664525 * s + 1013904223;+}++static unsigned isqrt32(uint32_t v)+{+ unsigned r = (unsigned)sqrt((double)v);+ while ((uint64_t)r * r > v)+ r--;+ while ((uint64_t)(r + 1) * (r + 1) <= v)+ r++;+ return r;+}++static uint32_t pvq_u(const struct celt_dec *d, int a, int b)+{+ if (a > b) {+ int t = a;+ a = b;+ b = t;+ }+ return d->pvq[a * PVQ_N + b];+}++/* ---- the decoder ---- */++struct celt_dec *jcelt_new(int channels)+{+ struct celt_dec *d = calloc(1, sizeof *d);+ if (!d)+ return NULL;+ d->cc = channels;+ d->end = NB;+ for (int i = 0; i < 4; i++)+ if (!(d->mdct[i] = aimdct_new(240 << i))) {+ jcelt_free(d);+ return NULL;+ }+ d->pvq = calloc((size_t)PVQ_K * PVQ_N, sizeof *d->pvq);+ if (!d->pvq) {+ jcelt_free(d);+ return NULL;+ }+ /* U(n, k) = U(n - 1, k) + U(n, k - 1) + U(n - 1, k - 1), U(0, 0) = 1 */+ uint64_t row[2][PVQ_N + 2];+ uint64_t *prev = row[0], *cur = row[1];+ for (int k = 0; k < PVQ_N; k++)+ prev[k] = k == 0;+ for (int a = 0; a < PVQ_K; a++) {+ for (int b = a; b < PVQ_N; b++)+ d->pvq[a * PVQ_N + b] =+ prev[b] > 0xFFFFFFFFu ? 0xFFFFFFFFu : (uint32_t)prev[b];+ /* the next row from this one */+ cur[0] = 0;+ for (int k = 1; k < PVQ_N; k++) {+ uint64_t v = prev[k] + cur[k - 1] + prev[k - 1];+ cur[k] = v > (1ull << 40) ? (1ull << 40) : v;+ }+ uint64_t *t = prev;+ prev = cur;+ cur = t;+ }+ jcelt_reset(d);+ return d;+}++void jcelt_free(struct celt_dec *d)+{+ if (!d)+ return;+ for (int i = 0; i < 4; i++)+ aimdct_free(d->mdct[i]);+ free(d->pvq);+ free(d);+}++void jcelt_reset(struct celt_dec *d)+{+ memset(d->mem, 0, sizeof d->mem);+ memset(d->old_e, 0, sizeof d->old_e);+ memset(d->bg_loge, 0, sizeof d->bg_loge);+ for (int i = 0; i < 2 * NB; i++)+ d->old_loge[i] = d->old_loge2[i] = -28.f;+ d->preemph[0] = d->preemph[1] = 0;+ d->pf_period = d->pf_period_old = d->pf_tapset = d->pf_tapset_old = 0;+ d->pf_gain = d->pf_gain_old = 0;+ d->rng = 0;+ d->loss_duration = 0;+ d->skip_plc = 1;+ d->last_pitch = 0;+ d->prefilter_and_fold = 0;+ memset(d->lpc, 0, sizeof d->lpc);+}++void jcelt_bands(struct celt_dec *d, int start, int end)+{+ d->start = start;+ d->end = end;+}++uint32_t jcelt_rng(const struct celt_dec *d)+{+ return d->rng;+}++/* ---- energies ---- */++static int laplace(struct rc *r, unsigned fs, int decay)+{+ int val = 0;+ unsigned fl = 0, fm = rc_decode_bin(r, 15);+ if (fm >= fs) {+ val++;+ fl = fs;+ fs = ((32768 - 2 * 16 - fs) * (unsigned)(16384 - decay) >> 15) + 1;+ while (fs > 1 && fm >= fl + 2 * fs) {+ fs *= 2;+ fl += fs;+ fs = ((fs - 2) * (unsigned)decay >> 15) + 1;+ val++;+ }+ if (fs <= 1) {+ int di = (int)((fm - fl) >> 1);+ val += di;+ fl += 2 * (unsigned)di;+ }+ if (fm < fl + fs)+ val = -val;+ else+ fl += fs;+ }+ rc_update(r, fl, fl + fs < 32768 ? fl + fs : 32768, 32768);+ return val;+}++static void coarse_energy(struct celt_dec *d, struct rc *r, int intra, int C,+ int LM)+{+ static const float PRED[4] = {29440 / 32768.f, 26112 / 32768.f,+ 21248 / 32768.f, 16384 / 32768.f};+ static const float BETA[4] = {30147 / 32768.f, 22282 / 32768.f,+ 12124 / 32768.f, 6554 / 32768.f};+ static const uint8_t SMALL[3] = {2, 1, 0};+ const uint8_t *prob = CELT_EPROB + (LM * 2 + intra) * 42;+ float prev[2] = {0, 0};+ float coef = intra ? 0 : PRED[LM], beta = intra ? 4915 / 32768.f : BETA[LM];+ int budget = (int)r->storage * 8;+ for (int i = d->start; i < d->end; i++)+ for (int c = 0; c < C; c++) {+ int tell = rc_tell(r), qi;+ if (budget - tell >= 15) {+ int pi = 2 * imin(i, 20);+ qi = laplace(r, (unsigned)prob[pi] << 7, prob[pi + 1] << 6);+ } else if (budget - tell >= 2) {+ qi = rc_icdf(r, SMALL, 2);+ qi = (qi >> 1) ^ -(qi & 1);+ } else if (budget - tell >= 1) {+ qi = -rc_bit_logp(r, 1);+ } else {+ qi = -1;+ }+ float q = (float)qi, *e = &d->old_e[i + c * NB];+ *e = *e > -9.f ? *e : -9.f;+ *e = coef * *e + prev[c] + q;+ prev[c] = prev[c] + q - beta * q;+ }+}++static void tf_decode(struct rc *r, int start, int end, int transient, int *tf,+ int LM)+{+ uint32_t budget = r->storage * 8, tell = (uint32_t)rc_tell(r);+ int logp = transient ? 2 : 4, changed = 0, curr = 0;+ int rsv = LM > 0 && tell + (uint32_t)logp + 1 <= budget;+ budget -= (uint32_t)rsv;+ for (int i = start; i < end; i++) {+ if (tell + (uint32_t)logp <= budget) {+ curr ^= rc_bit_logp(r, (unsigned)logp);+ tell = (uint32_t)rc_tell(r);+ changed |= curr;+ }+ tf[i] = curr;+ logp = transient ? 4 : 5;+ }+ int sel = 0;+ const int8_t *t = CELT_TF_SELECT + 8 * LM;+ if (rsv && t[4 * transient + changed] != t[4 * transient + 2 + changed])+ sel = rc_bit_logp(r, 1);+ for (int i = start; i < end; i++)+ tf[i] = t[4 * transient + 2 * sel + tf[i]];+}++/* ---- the allocation ---- */++static const uint8_t LOG2_FRAC[24] = {0, 8, 13, 16, 19, 21, 23, 24,+ 26, 27, 28, 29, 30, 31, 32, 32,+ 33, 34, 34, 35, 36, 36, 37, 37};++static const uint8_t *cache_of(int LM, int band)+{+ return CELT_CACHE_BITS + CELT_CACHE_INDEX[(LM + 1) * NB + band];+}++static int bits2pulses(int band, int LM, int bits)+{+ const uint8_t *cache = cache_of(LM, band);+ int lo = 0, hi = cache[0];+ bits--;+ for (int i = 0; i < 6; i++) {+ int mid = (lo + hi + 1) >> 1;+ if ((int)cache[mid] >= bits)+ hi = mid;+ else+ lo = mid;+ }+ if (bits - (lo == 0 ? -1 : (int)cache[lo]) <= (int)cache[hi] - bits)+ return lo;+ return hi;+}++static int pulses2bits(int band, int LM, int pulses)+{+ return pulses == 0 ? 0 : cache_of(LM, band)[pulses] + 1;+}++static int get_pulses(int i)+{+ return i < 8 ? i : (8 + (i & 7)) << ((i >> 3) - 1);+}++static int band_w(int j)+{+ return CELT_EBANDS[j + 1] - CELT_EBANDS[j];+}++static int interp_bits2pulses(struct rc *r, int start, int end, int skip_start,+ const int *bits1, const int *bits2,+ const int *thresh, const int *cap, int32_t total,+ int32_t *balance_out, int skip_rsv,+ int *intensity, int intensity_rsv,+ int *dual_stereo, int dual_stereo_rsv, int *bits,+ int *ebits, int *fine_priority, int C, int LM)+{+ int alloc_floor = C << BITRES, stereo = C > 1, logM = LM << BITRES;+ int lo = 0, hi = 1 << 6, coded;+ int32_t psum;+ for (int i = 0; i < 6; i++) {+ int mid = (lo + hi) >> 1, done = 0;+ psum = 0;+ for (int j = end; j-- > start;) {+ int tmp = bits1[j] + (mid * bits2[j] >> 6);+ if (tmp >= thresh[j] || done) {+ done = 1;+ psum += imin(tmp, cap[j]);+ } else if (tmp >= alloc_floor) {+ psum += alloc_floor;+ }+ }+ if (psum > total)+ hi = mid;+ else+ lo = mid;+ }+ psum = 0;+ int done = 0;+ for (int j = end; j-- > start;) {+ int tmp = bits1[j] + (lo * bits2[j] >> 6);+ if (tmp < thresh[j] && !done)+ tmp = tmp >= alloc_floor ? alloc_floor : 0;+ else+ done = 1;+ tmp = imin(tmp, cap[j]);+ bits[j] = tmp;+ psum += tmp;+ }+ for (coded = end;; coded--) {+ int j = coded - 1;+ if (j <= skip_start) {+ total += skip_rsv;+ break;+ }+ int32_t left = total - psum;+ int span = CELT_EBANDS[coded] - CELT_EBANDS[start];+ int32_t percoeff = left / span;+ left -= span * percoeff;+ int rem = imax((int)left - (CELT_EBANDS[j] - CELT_EBANDS[start]), 0);+ int bw = CELT_EBANDS[coded] - CELT_EBANDS[j];+ int band_bits = (int)(bits[j] + percoeff * bw + rem);+ if (band_bits >= imax(thresh[j], alloc_floor + (1 << BITRES))) {+ if (rc_bit_logp(r, 1))+ break;+ psum += 1 << BITRES;+ band_bits -= 1 << BITRES;+ }+ psum -= bits[j] + intensity_rsv;+ if (intensity_rsv > 0)+ intensity_rsv = LOG2_FRAC[j - start];+ psum += intensity_rsv;+ if (band_bits >= alloc_floor) {+ psum += alloc_floor;+ bits[j] = alloc_floor;+ } else {+ bits[j] = 0;+ }+ }+ if (intensity_rsv > 0)+ *intensity = start + (int)rc_uint(r, (uint32_t)(coded + 1 - start));+ else+ *intensity = 0;+ if (*intensity <= start) {+ total += dual_stereo_rsv;+ dual_stereo_rsv = 0;+ }+ *dual_stereo = dual_stereo_rsv > 0 ? rc_bit_logp(r, 1) : 0;+ int32_t left = total - psum;+ int span = CELT_EBANDS[coded] - CELT_EBANDS[start];+ int32_t percoeff = left / span;+ left -= span * percoeff;+ for (int j = start; j < coded; j++)+ bits[j] += (int)percoeff * band_w(j);+ for (int j = start; j < coded; j++) {+ int tmp = (int)(left < band_w(j) ? left : band_w(j));+ bits[j] += tmp;+ left -= tmp;+ }+ int32_t balance = 0;+ int j;+ for (j = start; j < coded; j++) {+ int N0 = band_w(j), N = N0 << LM;+ int32_t bit = bits[j] + balance, excess;+ if (N > 1) {+ excess = bit - cap[j] > 0 ? bit - cap[j] : 0;+ bits[j] = (int)(bit - excess);+ int den =+ C * N + (C == 2 && N > 2 && !*dual_stereo && j < *intensity);+ int nclogn = den * (CELT_LOGN[j] + logM);+ int offset = (nclogn >> 1) - den * FINE_OFFSET;+ if (N == 2)+ offset += den << BITRES >> 2;+ if (bits[j] + offset < den * 2 << BITRES)+ offset += nclogn >> 2;+ else if (bits[j] + offset < den * 3 << BITRES)+ offset += nclogn >> 3;+ ebits[j] = imax(0, bits[j] + offset + (den << (BITRES - 1)));+ ebits[j] = (ebits[j] / den) >> BITRES;+ if (C * ebits[j] > (bits[j] >> BITRES))+ ebits[j] = bits[j] >> stereo >> BITRES;+ ebits[j] = imin(ebits[j], MAX_FINE_BITS);+ fine_priority[j] = ebits[j] * (den << BITRES) >= bits[j] + offset;+ bits[j] -= C * ebits[j] << BITRES;+ } else {+ excess = bit - (C << BITRES) > 0 ? bit - (C << BITRES) : 0;+ bits[j] = (int)(bit - excess);+ ebits[j] = 0;+ fine_priority[j] = 1;+ }+ if (excess > 0) {+ int extra_fine = imin((int)(excess >> (stereo + BITRES)),+ MAX_FINE_BITS - ebits[j]);+ ebits[j] += extra_fine;+ int extra_bits = extra_fine * C << BITRES;+ fine_priority[j] = extra_bits >= excess - balance;+ excess -= extra_bits;+ }+ balance = excess;+ }+ *balance_out = balance;+ for (; j < end; j++) {+ ebits[j] = bits[j] >> stereo >> BITRES;+ bits[j] = 0;+ fine_priority[j] = ebits[j] < 1;+ }+ return coded;+}++static int allocation(struct rc *r, int start, int end, const int *offsets,+ const int *cap, int trim, int *intensity, int *dual,+ int32_t total, int32_t *balance, int *pulses, int *ebits,+ int *fine_priority, int C, int LM)+{+ int bits1[NB], bits2[NB], thresh[NB], trim_off[NB];+ total = total > 0 ? total : 0;+ int skip_start = start;+ int skip_rsv = total >= 1 << BITRES ? 1 << BITRES : 0;+ total -= skip_rsv;+ int intensity_rsv = 0, dual_rsv = 0;+ if (C == 2) {+ intensity_rsv = LOG2_FRAC[end - start];+ if (intensity_rsv > total) {+ intensity_rsv = 0;+ } else {+ total -= intensity_rsv;+ dual_rsv = total >= 1 << BITRES ? 1 << BITRES : 0;+ total -= dual_rsv;+ }+ }+ for (int j = start; j < end; j++) {+ thresh[j] = imax(C << BITRES, (3 * band_w(j) << LM << BITRES) >> 4);+ trim_off[j] = C * band_w(j) * (trim - 5 - LM) * (end - j - 1) *+ (1 << (LM + BITRES)) >>+ 6;+ if (band_w(j) << LM == 1)+ trim_off[j] -= C << BITRES;+ }+ int lo = 1, hi = 11 - 1;+ do {+ int done = 0, psum = 0, mid = (lo + hi) >> 1;+ for (int j = end; j-- > start;) {+ int b = C * band_w(j) * CELT_ALLOC[mid * NB + j] << LM >> 2;+ if (b > 0)+ b = imax(0, b + trim_off[j]);+ b += offsets[j];+ if (b >= thresh[j] || done) {+ done = 1;+ psum += imin(b, cap[j]);+ } else if (b >= C << BITRES) {+ psum += C << BITRES;+ }+ }+ if (psum > total)+ hi = mid - 1;+ else+ lo = mid + 1;+ } while (lo <= hi);+ hi = lo--;+ for (int j = start; j < end; j++) {+ int b1 = C * band_w(j) * CELT_ALLOC[lo * NB + j] << LM >> 2;+ int b2 = hi >= 11 ? cap[j]+ : C * band_w(j) * CELT_ALLOC[hi * NB + j] << LM >> 2;+ if (b1 > 0)+ b1 = imax(0, b1 + trim_off[j]);+ if (b2 > 0)+ b2 = imax(0, b2 + trim_off[j]);+ if (lo > 0)+ b1 += offsets[j];+ b2 += offsets[j];+ if (offsets[j] > 0)+ skip_start = j;+ b2 = imax(0, b2 - b1);+ bits1[j] = b1;+ bits2[j] = b2;+ }+ return interp_bits2pulses(r, start, end, skip_start, bits1, bits2, thresh,+ cap, total, balance, skip_rsv, intensity,+ intensity_rsv, dual, dual_rsv, pulses, ebits,+ fine_priority, C, LM);+}++/* ---- the shapes ---- */++static void exp_rotation1(float *X, int len, int stride, float c, float s)+{+ float ms = -s, *x = X;+ for (int i = 0; i < len - stride; i++) {+ float x1 = x[0], x2 = x[stride];+ x[stride] = c * x2 + s * x1;+ *x++ = c * x1 + ms * x2;+ }+ x = &X[len - 2 * stride - 1];+ for (int i = len - 2 * stride - 1; i >= 0; i--) {+ float x1 = x[0], x2 = x[stride];+ x[stride] = c * x2 + s * x1;+ *x-- = c * x1 + ms * x2;+ }+}++static void exp_rotation(float *X, int len, int stride, int K, int spread)+{+ static const int FACTOR[3] = {15, 10, 5};+ if (2 * K >= len || spread == SPREAD_NONE)+ return;+ int factor = FACTOR[spread - 1], stride2 = 0;+ float gain = (float)len / (float)(len + factor * K);+ float theta = .5f * gain * gain;+ float c = cosf(.5f * (float)M_PI * theta);+ float s = cosf(.5f * (float)M_PI * (1.f - theta));+ if (len >= 8 * stride) {+ stride2 = 1;+ while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len)+ stride2++;+ }+ len /= stride;+ for (int i = 0; i < stride; i++) {+ if (stride2)+ exp_rotation1(X + i * len, len, stride2, s, c);+ exp_rotation1(X + i * len, len, 1, c, s);+ }+}++static void renormalise(float *X, int N, float gain)+{+ float E = 1e-15f;+ for (int i = 0; i < N; i++)+ E += X[i] * X[i];+ float g = gain * (1.f / sqrtf(E));+ for (int i = 0; i < N; i++)+ X[i] *= g;+}++/* the vector of pulses with index i among those of n dimensions and k+ pulses (the reference's cwrsi), into y: their squared norm */+static float cwrsi(const struct celt_dec *d, int n, int k, uint32_t i, int *y)+{+ float yy = 0;+ while (n > 2) {+ uint32_t p, q;+ int s, k0, val;+ if (k >= n) {+ p = pvq_u(d, n, k + 1);+ s = -(i >= p);+ i -= p & (uint32_t)s;+ k0 = k;+ q = pvq_u(d, n, n);+ if (q > i) {+ k = n;+ do+ p = pvq_u(d, --k, n);+ while (p > i);+ } else {+ for (p = pvq_u(d, n, k); p > i; p = pvq_u(d, n, k))+ k--;+ }+ i -= p;+ val = (k0 - k + s) ^ s;+ } else {+ p = pvq_u(d, k, n);+ q = pvq_u(d, k + 1, n);+ if (p <= i && i < q) {+ i -= p;+ val = 0;+ } else {+ s = -(i >= q);+ i -= q & (uint32_t)s;+ k0 = k;+ do+ p = pvq_u(d, --k, n);+ while (p > i);+ i -= p;+ val = (k0 - k + s) ^ s;+ }+ }+ *y++ = val;+ yy += (float)val * val;+ n--;+ }+ uint32_t p = 2 * (uint32_t)k + 1;+ int s = -(i >= p);+ i -= p & (uint32_t)s;+ int k0 = k;+ k = (int)((i + 1) >> 1);+ if (k)+ i -= 2 * (uint32_t)k - 1;+ int val = (k0 - k + s) ^ s;+ *y++ = val;+ yy += (float)val * val;+ s = -(int)i;+ val = (k + s) ^ s;+ *y = val;+ yy += (float)val * val;+ return yy;+}++static unsigned alg_unquant(const struct celt_dec *d, float *X, int N, int K,+ int spread, int B, struct rc *r, float gain)+{+ int iy[176];+ uint32_t idx = rc_uint(r, pvq_u(d, N, K) + pvq_u(d, N, K + 1));+ float ryy = cwrsi(d, N, K, idx, iy);+ float g = gain * (1.f / sqrtf(ryy));+ for (int i = 0; i < N; i++)+ X[i] = g * (float)iy[i];+ exp_rotation(X, N, B, K, spread);+ if (B <= 1)+ return 1;+ int N0 = N / B;+ unsigned mask = 0;+ for (int i = 0; i < B; i++) {+ int t = 0;+ for (int j = 0; j < N0; j++)+ t |= iy[i * N0 + j];+ mask |= (unsigned)(t != 0) << i;+ }+ return mask;+}++static void haar1(float *X, int N0, int stride)+{+ N0 >>= 1;+ for (int i = 0; i < stride; i++)+ for (int j = 0; j < N0; j++) {+ float a = .70710678f * X[stride * 2 * j + i];+ float b = .70710678f * X[stride * (2 * j + 1) + i];+ X[stride * 2 * j + i] = a + b;+ X[stride * (2 * j + 1) + i] = a - b;+ }+}++static void deinterleave(float *X, int N0, int stride, int hadamard)+{+ float tmp[960];+ int N = N0 * stride;+ for (int i = 0; i < stride; i++)+ for (int j = 0; j < N0; j++) {+ int to = hadamard ? CELT_ORDERY[stride - 2 + i] : i;+ tmp[to * N0 + j] = X[j * stride + i];+ }+ memcpy(X, tmp, sizeof(float) * (size_t)N);+}++static void interleave(float *X, int N0, int stride, int hadamard)+{+ float tmp[960];+ int N = N0 * stride;+ for (int i = 0; i < stride; i++)+ for (int j = 0; j < N0; j++) {+ int from = hadamard ? CELT_ORDERY[stride - 2 + i] : i;+ tmp[j * stride + i] = X[from * N0 + j];+ }+ memcpy(X, tmp, sizeof(float) * (size_t)N);+}++static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo)+{+ static const int16_t EXP2[8] = {16384, 17866, 19483, 21247,+ 23170, 25267, 27554, 30048};+ int N2 = 2 * N - 1;+ if (stereo && N == 2)+ N2--;+ int qb = (b + N2 * offset) / N2;+ qb = imin(b - pulse_cap - (4 << BITRES), qb);+ qb = imin(8 << BITRES, qb);+ if (qb < (1 << BITRES >> 1))+ return 1;+ int qn = EXP2[qb & 7] >> (14 - (qb >> BITRES));+ return (qn + 1) >> 1 << 1;+}++struct bctx {+ struct celt_dec *d;+ struct rc *r;+ int i, intensity, spread, tf_change, avoid_split_noise;+ int32_t remaining;+ uint32_t seed;+};++struct split {+ int inv, imid, iside, delta, itheta, qalloc;+};++static void compute_theta(struct bctx *c, struct split *s, int N, int *b, int B,+ int B0, int LM, int stereo, int *fill)+{+ struct rc *r = c->r;+ int i = c->i, itheta = 0, inv = 0;+ int pulse_cap = CELT_LOGN[i] + LM * (1 << BITRES);+ int offset = (pulse_cap >> 1) -+ (stereo && N == 2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);+ int qn = compute_qn(N, *b, offset, pulse_cap, stereo);+ if (stereo && i >= c->intensity)+ qn = 1;+ uint32_t tell = rc_tell_frac(r);+ if (qn != 1) {+ if (stereo && N > 2) {+ int p0 = 3, x0 = qn / 2, ft = p0 * (x0 + 1) + x0, x;+ int fs = (int)rc_decode(r, (unsigned)ft);+ if (fs < (x0 + 1) * p0)+ x = fs / p0;+ else+ x = x0 + 1 + (fs - (x0 + 1) * p0);+ rc_update(+ r, (unsigned)(x <= x0 ? p0 * x : (x - 1 - x0) + (x0 + 1) * p0),+ (unsigned)(x <= x0 ? p0 * (x + 1) : (x - x0) + (x0 + 1) * p0),+ (unsigned)ft);+ itheta = x;+ } else if (B0 > 1 || stereo) {+ itheta = (int)rc_uint(r, (uint32_t)qn + 1);+ } else {+ int ft = ((qn >> 1) + 1) * ((qn >> 1) + 1), fs, fl;+ int fm = (int)rc_decode(r, (unsigned)ft);+ if (fm < ((qn >> 1) * ((qn >> 1) + 1) >> 1)) {+ itheta = (int)(isqrt32(8 * (uint32_t)fm + 1) - 1) >> 1;+ fs = itheta + 1;+ fl = itheta * (itheta + 1) >> 1;+ } else {+ itheta = (int)(2 * (qn + 1) -+ (int)isqrt32(8 * (uint32_t)(ft - fm - 1) + 1)) >>+ 1;+ fs = qn + 1 - itheta;+ fl = ft - ((qn + 1 - itheta) * (qn + 2 - itheta) >> 1);+ }+ rc_update(r, (unsigned)fl, (unsigned)(fl + fs), (unsigned)ft);+ }+ itheta = itheta * 16384 / qn;+ } else if (stereo) {+ inv = *b > 2 << BITRES && c->remaining > 2 << BITRES ? rc_bit_logp(r, 2)+ : 0;+ itheta = 0;+ }+ int qalloc = (int)(rc_tell_frac(r) - tell);+ *b -= qalloc;+ int imid, iside, delta;+ if (itheta == 0) {+ imid = 32767;+ iside = 0;+ *fill &= (1 << B) - 1;+ delta = -16384;+ } else if (itheta == 16384) {+ imid = 0;+ iside = 32767;+ *fill &= ((1 << B) - 1) << B;+ delta = 16384;+ } else {+ imid = bitexact_cos(itheta);+ iside = bitexact_cos(16384 - itheta);+ delta = frac_mul16((N - 1) << 7, bitexact_log2tan(iside, imid));+ }+ s->inv = inv;+ s->imid = imid;+ s->iside = iside;+ s->delta = delta;+ s->itheta = itheta;+ s->qalloc = qalloc;+}++static unsigned band_n1(struct bctx *c, float *X, float *Y, float *lowband_out)+{+ float *x = X;+ for (int k = 0; k < 1 + (Y != NULL); k++) {+ int sign = 0;+ if (c->remaining >= 1 << BITRES) {+ sign = (int)rc_bits(c->r, 1);+ c->remaining -= 1 << BITRES;+ }+ x[0] = sign ? -1.f : 1.f;+ x = Y;+ }+ if (lowband_out)+ lowband_out[0] = X[0];+ return 1;+}++static unsigned partition(struct bctx *c, float *X, int N, int b, int B,+ float *lowband, int LM, float gain, int fill)+{+ int i = c->i, B0 = B;+ unsigned cm = 0;+ const uint8_t *cache = cache_of(LM, i);+ if (LM != -1 && b > cache[cache[0]] + 12 && N > 2) {+ struct split s;+ float *Y, *next_lowband2 = NULL;+ N >>= 1;+ Y = X + N;+ LM -= 1;+ if (B == 1)+ fill = (fill & 1) | (fill << 1);+ B = (B + 1) >> 1;+ compute_theta(c, &s, N, &b, B, B0, LM, 0, &fill);+ float mid = (1.f / 32768) * s.imid, side = (1.f / 32768) * s.iside;+ int delta = s.delta;+ if (B0 > 1 && (s.itheta & 0x3fff)) {+ if (s.itheta > 8192)+ delta -= delta >> (4 - LM);+ else+ delta = imin(0, delta + (N << BITRES >> (5 - LM)));+ }+ int mbits = imax(0, imin(b, (b - delta) / 2)), sbits = b - mbits;+ c->remaining -= s.qalloc;+ if (lowband)+ next_lowband2 = lowband + N;+ int32_t rebalance = c->remaining;+ if (mbits >= sbits) {+ cm = partition(c, X, N, mbits, B, lowband, LM, gain * mid, fill);+ rebalance = mbits - (rebalance - c->remaining);+ if (rebalance > 3 << BITRES && s.itheta != 0)+ sbits += (int)rebalance - (3 << BITRES);+ cm |= partition(c, Y, N, sbits, B, next_lowband2, LM, gain * side,+ fill >> B)+ << (B0 >> 1);+ } else {+ cm = partition(c, Y, N, sbits, B, next_lowband2, LM, gain * side,+ fill >> B)+ << (B0 >> 1);+ rebalance = sbits - (rebalance - c->remaining);+ if (rebalance > 3 << BITRES && s.itheta != 16384)+ mbits += (int)rebalance - (3 << BITRES);+ cm |= partition(c, X, N, mbits, B, lowband, LM, gain * mid, fill);+ }+ return cm;+ }+ int q = bits2pulses(i, LM, b), curr = pulses2bits(i, LM, q);+ c->remaining -= curr;+ while (c->remaining < 0 && q > 0) {+ c->remaining += curr;+ q--;+ curr = pulses2bits(i, LM, q);+ c->remaining -= curr;+ }+ if (q != 0)+ return alg_unquant(c->d, X, N, get_pulses(q), c->spread, B, c->r, gain);+ unsigned mask = (unsigned)(1UL << B) - 1;+ fill &= (int)mask;+ if (!fill) {+ memset(X, 0, sizeof(float) * (size_t)N);+ return 0;+ }+ if (!lowband) {+ for (int j = 0; j < N; j++) {+ c->seed = lcg(c->seed);+ X[j] = (float)((int32_t)c->seed >> 20);+ }+ cm = mask;+ } else {+ for (int j = 0; j < N; j++) {+ c->seed = lcg(c->seed);+ X[j] = lowband[j] + (c->seed & 0x8000 ? 1.f / 256 : -1.f / 256);+ }+ cm = (unsigned)fill;+ }+ renormalise(X, N, gain);+ return cm;+}++static unsigned quant_band(struct bctx *c, float *X, int N, int b, int B,+ float *lowband, int LM, float *lowband_out,+ float gain, float *scratch, int fill)+{+ int N0 = N, N_B = N, B0 = B, time_divide = 0, recombine = 0;+ int long_blocks = B0 == 1, tf_change = c->tf_change;+ N_B /= B;+ if (N == 1)+ return band_n1(c, X, NULL, lowband_out);+ if (tf_change > 0)+ recombine = tf_change;+ if (scratch && lowband &&+ (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) {+ memcpy(scratch, lowband, sizeof(float) * (size_t)N);+ lowband = scratch;+ }+ for (int k = 0; k < recombine; k++) {+ static const uint8_t T[16] = {0, 1, 1, 1, 2, 3, 3, 3,+ 2, 3, 3, 3, 2, 3, 3, 3};+ if (lowband)+ haar1(lowband, N >> k, 1 << k);+ fill = T[fill & 0xF] | T[fill >> 4] << 2;+ }+ B >>= recombine;+ N_B <<= recombine;+ while ((N_B & 1) == 0 && tf_change < 0) {+ if (lowband)+ haar1(lowband, N_B, B);+ fill |= fill << B;+ B <<= 1;+ N_B >>= 1;+ time_divide++;+ tf_change++;+ }+ B0 = B;+ int N_B0 = N_B;+ if (B0 > 1 && lowband)+ deinterleave(lowband, N_B >> recombine, B0 << recombine, long_blocks);+ unsigned cm = partition(c, X, N, b, B, lowband, LM, gain, fill);+ if (B0 > 1)+ interleave(X, N_B >> recombine, B0 << recombine, long_blocks);+ N_B = N_B0;+ B = B0;+ for (int k = 0; k < time_divide; k++) {+ B >>= 1;+ N_B <<= 1;+ cm |= cm >> B;+ haar1(X, N_B, B);+ }+ for (int k = 0; k < recombine; k++) {+ static const uint8_t T[16] = {0x00, 0x03, 0x0C, 0x0F, 0x30, 0x33,+ 0x3C, 0x3F, 0xC0, 0xC3, 0xCC, 0xCF,+ 0xF0, 0xF3, 0xFC, 0xFF};+ cm = T[cm];+ haar1(X, N0 >> k, 1 << k);+ }+ B <<= recombine;+ if (lowband_out) {+ float n = sqrtf((float)N0);+ for (int j = 0; j < N0; j++)+ lowband_out[j] = n * X[j];+ }+ return cm & ((1u << B) - 1);+}++static void stereo_merge(float *X, float *Y, float mid, int N)+{+ float xp = 0, side = 0;+ for (int j = 0; j < N; j++) {+ xp += Y[j] * X[j];+ side += Y[j] * Y[j];+ }+ xp *= mid;+ float El = mid * mid + side - 2 * xp, Er = mid * mid + side + 2 * xp;+ if (Er < 6e-4f || El < 6e-4f) {+ memcpy(Y, X, sizeof(float) * (size_t)N);+ return;+ }+ float lgain = 1.f / sqrtf(El), rgain = 1.f / sqrtf(Er);+ for (int j = 0; j < N; j++) {+ float l = mid * X[j], r = Y[j];+ X[j] = lgain * (l - r);+ Y[j] = rgain * (l + r);+ }+}++static unsigned band_stereo(struct bctx *c, float *X, float *Y, int N, int b,+ int B, float *lowband, int LM, float *lowband_out,+ float *scratch, int fill)+{+ if (N == 1)+ return band_n1(c, X, Y, lowband_out);+ int orig_fill = fill;+ struct split s;+ unsigned cm;+ compute_theta(c, &s, N, &b, B, B, LM, 1, &fill);+ float mid = (1.f / 32768) * s.imid, side = (1.f / 32768) * s.iside;+ if (N == 2) {+ int mbits = b, sbits = 0, sign = 0;+ if (s.itheta != 0 && s.itheta != 16384)+ sbits = 1 << BITRES;+ mbits -= sbits;+ int ch = s.itheta > 8192;+ c->remaining -= s.qalloc + sbits;+ float *x2 = ch ? Y : X, *y2 = ch ? X : Y;+ if (sbits)+ sign = (int)rc_bits(c->r, 1);+ sign = 1 - 2 * sign;+ cm = quant_band(c, x2, N, mbits, B, lowband, LM, lowband_out, 1.f,+ scratch, orig_fill);+ y2[0] = (float)-sign * x2[1];+ y2[1] = (float)sign * x2[0];+ X[0] *= mid;+ X[1] *= mid;+ Y[0] *= side;+ Y[1] *= side;+ float t = X[0];+ X[0] = t - Y[0];+ Y[0] = t + Y[0];+ t = X[1];+ X[1] = t - Y[1];+ Y[1] = t + Y[1];+ } else {+ int mbits = imax(0, imin(b, (b - s.delta) / 2)), sbits = b - mbits;+ c->remaining -= s.qalloc;+ int32_t rebalance = c->remaining;+ if (mbits >= sbits) {+ cm = quant_band(c, X, N, mbits, B, lowband, LM, lowband_out, 1.f,+ scratch, fill);+ rebalance = mbits - (rebalance - c->remaining);+ if (rebalance > 3 << BITRES && s.itheta != 0)+ sbits += (int)rebalance - (3 << BITRES);+ cm |= quant_band(c, Y, N, sbits, B, NULL, LM, NULL, side, NULL,+ fill >> B);+ } else {+ cm = quant_band(c, Y, N, sbits, B, NULL, LM, NULL, side, NULL,+ fill >> B);+ rebalance = sbits - (rebalance - c->remaining);+ if (rebalance > 3 << BITRES && s.itheta != 16384)+ mbits += (int)rebalance - (3 << BITRES);+ cm |= quant_band(c, X, N, mbits, B, lowband, LM, lowband_out, 1.f,+ scratch, fill);+ }+ }+ if (N != 2)+ stereo_merge(X, Y, mid, N);+ if (s.inv)+ for (int j = 0; j < N; j++)+ Y[j] = -Y[j];+ return cm;+}++static void all_bands(struct celt_dec *d, struct rc *r, int start, int end,+ float *X_, float *Y_, uint8_t *collapse, int *pulses,+ int short_blocks, int spread, int dual_stereo,+ int intensity, int *tf_res, int32_t total_bits,+ int32_t balance, int LM, int coded_bands)+{+ int M = 1 << LM, B = short_blocks ? M : 1, C = Y_ ? 2 : 1;+ int norm_off = M * CELT_EBANDS[start], update_lowband = 1;+ int lowband_offset = 0;+ float *norm = d->norm, *norm2 = norm + M * CELT_EBANDS[NB - 1] - norm_off;+ float *scratch = X_ + M * CELT_EBANDS[NB - 1];+ struct bctx c = {.d = d,+ .r = r,+ .intensity = intensity,+ .spread = spread,+ .seed = d->rng,+ .avoid_split_noise = B > 1};+ for (int i = start; i < end; i++) {+ int last = i == end - 1, effective_lowband = -1, b;+ float *X = X_ + M * CELT_EBANDS[i],+ *Y = Y_ ? Y_ + M * CELT_EBANDS[i] : NULL;+ int N = M * CELT_EBANDS[i + 1] - M * CELT_EBANDS[i];+ unsigned x_cm, y_cm;+ c.i = i;+ int32_t tell = (int32_t)rc_tell_frac(r);+ if (i != start)+ balance -= tell;+ int32_t remaining = total_bits - tell - 1;+ c.remaining = remaining;+ if (i <= coded_bands - 1) {+ int32_t curr_balance = balance / imin(3, coded_bands - i);+ b = imax(0, imin(16383, (int)imin((int)(remaining + 1),+ pulses[i] + (int)curr_balance)));+ } else {+ b = 0;+ }+ if ((M * CELT_EBANDS[i] - N >= M * CELT_EBANDS[start] ||+ i == start + 1) &&+ (update_lowband || lowband_offset == 0))+ lowband_offset = i;+ if (i == start + 1) { /* the folding of the second band, in hybrid */+ int n1 = M * band_w(start), n2 = M * band_w(start + 1);+ if (n2 > n1) {+ memmove(&norm[n1], &norm[2 * n1 - n2],+ sizeof(float) * (size_t)(n2 - n1));+ if (dual_stereo)+ memmove(&norm2[n1], &norm2[2 * n1 - n2],+ sizeof(float) * (size_t)(n2 - n1));+ }+ }+ c.tf_change = tf_res[i];+ float *sc = scratch;+ if (last)+ sc = NULL;+ if (lowband_offset != 0 &&+ (spread != SPREAD_AGGRESSIVE || B > 1 || c.tf_change < 0)) {+ effective_lowband =+ imax(0, M * CELT_EBANDS[lowband_offset] - norm_off - N);+ int fold_start = lowband_offset;+ while (M * CELT_EBANDS[--fold_start] > effective_lowband + norm_off)+ ;+ int fold_end = lowband_offset - 1;+ while (++fold_end < i &&+ M * CELT_EBANDS[fold_end] < effective_lowband + norm_off + N)+ ;+ x_cm = y_cm = 0;+ int f = fold_start;+ do {+ x_cm |= collapse[f * C + 0];+ y_cm |= collapse[f * C + C - 1];+ } while (++f < fold_end);+ } else {+ x_cm = y_cm = (1u << B) - 1;+ }+ if (dual_stereo && i == intensity) {+ dual_stereo = 0;+ for (int j = 0; j < M * CELT_EBANDS[i] - norm_off; j++)+ norm[j] = .5f * (norm[j] + norm2[j]);+ }+ float *lo1 = effective_lowband != -1 ? norm + effective_lowband : NULL;+ float *out1 = last ? NULL : norm + M * CELT_EBANDS[i] - norm_off;+ if (dual_stereo) {+ float *lo2 =+ effective_lowband != -1 ? norm2 + effective_lowband : NULL;+ float *out2 = last ? NULL : norm2 + M * CELT_EBANDS[i] - norm_off;+ x_cm = quant_band(&c, X, N, b / 2, B, lo1, LM, out1, 1.f, sc,+ (int)x_cm);+ y_cm = quant_band(&c, Y, N, b / 2, B, lo2, LM, out2, 1.f, sc,+ (int)y_cm);+ } else {+ if (Y)+ x_cm = band_stereo(&c, X, Y, N, b, B, lo1, LM, out1, sc,+ (int)(x_cm | y_cm));+ else+ x_cm = quant_band(&c, X, N, b, B, lo1, LM, out1, 1.f, sc,+ (int)(x_cm | y_cm));+ y_cm = x_cm;+ }+ collapse[i * C + 0] = (uint8_t)x_cm;+ collapse[i * C + C - 1] = (uint8_t)y_cm;+ balance += pulses[i] + tell;+ update_lowband = b > (N << BITRES);+ c.avoid_split_noise = 0;+ }+ d->rng = c.seed;+}++static void anti_collapse(struct celt_dec *d, float *X_, const uint8_t *masks,+ int LM, int C, int size, int start, int end,+ const int *pulses)+{+ uint32_t seed = d->rng;+ for (int i = start; i < end; i++) {+ int N0 = band_w(i);+ int depth = (1 + pulses[i]) / N0 >> LM;+ float thresh = .5f * exp2f(-.125f * (float)depth);+ float sqrt_1 = 1.f / sqrtf((float)(N0 << LM));+ for (int c = 0; c < C; c++) {+ float prev1 = d->old_loge[c * NB + i],+ prev2 = d->old_loge2[c * NB + i];+ if (C == 1) {+ prev1 =+ prev1 > d->old_loge[NB + i] ? prev1 : d->old_loge[NB + i];+ prev2 =+ prev2 > d->old_loge2[NB + i] ? prev2 : d->old_loge2[NB + i];+ }+ float ediff =+ d->old_e[c * NB + i] - (prev1 < prev2 ? prev1 : prev2);+ ediff = ediff > 0 ? ediff : 0;+ float rr = 2.f * exp2f(-ediff);+ if (LM == 3)+ rr *= 1.41421356f;+ rr = rr < thresh ? rr : thresh;+ rr *= sqrt_1;+ float *X = X_ + c * size + (CELT_EBANDS[i] << LM);+ int renorm = 0;+ for (int k = 0; k < 1 << LM; k++)+ if (!(masks[i * C + c] & 1 << k)) {+ for (int j = 0; j < N0; j++) {+ seed = lcg(seed);+ X[(j << LM) + k] = seed & 0x8000 ? rr : -rr;+ }+ renorm = 1;+ }+ if (renorm)+ renormalise(X, N0 << LM, 1.f);+ }+ }+}++/* ---- synthesis ---- */++static void denormalise(const float *X, float *freq, const float *loge,+ int start, int end, int M, int N, int silence)+{+ int bound = M * CELT_EBANDS[end];+ if (silence) {+ bound = 0;+ start = end = 0;+ }+ memset(freq, 0, sizeof(float) * (size_t)N);+ for (int i = start; i < end; i++) {+ float lg = loge[i] + CELT_EMEANS[i];+ float g = exp2f(lg < 32.f ? lg : 32.f);+ for (int j = M * CELT_EBANDS[i]; j < M * CELT_EBANDS[i + 1]; j++)+ freq[j] = X[j] * g;+ }+ (void)bound;+}++/* the inverse MDCT of a block of n / 2 coefficients (every stride-th of+ in) into out[OV / 2 ...], then its overlap mirrored with what out[0 ..+ OV) held */+static void imdct_block(struct celt_dec *d, int which, const float *in,+ int stride, float *out)+{+ struct aimdct *m = d->mdct[which];+ int N2 = 120 << which;+ aimdct_dct4(m, in, stride, d->u);+ for (int k = 0; k < N2; k++)+ out[OV / 2 + k] = -d->u[N2 - 1 - k];+ const float *w = CELT_WINDOW;+ for (int i = 0; i < OV / 2; i++) {+ float x1 = out[OV - 1 - i], x2 = out[i];+ out[i] = w[OV - 1 - i] * x2 - w[i] * x1;+ out[OV - 1 - i] = w[i] * x2 + w[OV - 1 - i] * x1;+ }+}++static void synthesis(struct celt_dec *d, float *X, float **out_syn, int start,+ int end, int C, int CC, int transient, int LM,+ int silence)+{+ int M = 1 << LM, N = 120 << LM, B, NB_, which;+ if (transient) {+ B = M;+ NB_ = 120;+ which = 0;+ } else {+ B = 1;+ NB_ = N;+ which = LM;+ }+ float *freq = d->freq;+ if (CC == 2 && C == 1) {+ denormalise(X, freq, d->old_e, start, end, M, N, silence);+ for (int c = 0; c < 2; c++)+ for (int b = 0; b < B; b++)+ imdct_block(d, which, freq + b, B, out_syn[c] + NB_ * b);+ } else if (CC == 1 && C == 2) {+ float *f2 = d->freq2;+ denormalise(X, freq, d->old_e, start, end, M, N, silence);+ denormalise(X + N, f2, d->old_e + NB, start, end, M, N, silence);+ for (int i = 0; i < N; i++)+ freq[i] = .5f * freq[i] + .5f * f2[i];+ for (int b = 0; b < B; b++)+ imdct_block(d, which, freq + b, B, out_syn[0] + NB_ * b);+ } else {+ for (int c = 0; c < CC; c++) {+ denormalise(X + c * N, freq, d->old_e + c * NB, start, end, M, N,+ silence);+ for (int b = 0; b < B; b++)+ imdct_block(d, which, freq + b, B, out_syn[c] + NB_ * b);+ }+ }+}++/* the pitch post-filter (pre-filter with negative gains): y from x, which+ may be the same buffer, its history before it */+static void comb_filter(float *y, const float *x, int T0, int T1, int N,+ float g0, float g1, int tap0, int tap1, int overlap)+{+ static const float G[3][3] = {{0.3066406250f, 0.2170410156f, 0.1296386719f},+ {0.4638671875f, 0.2680664062f, 0.f},+ {0.7998046875f, 0.1000976562f, 0.f}};+ if (g0 == 0 && g1 == 0) {+ if (x != y)+ memmove(y, x, sizeof(float) * (size_t)N);+ return;+ }+ T0 = imax(T0, 15);+ T1 = imax(T1, 15);+ float g00 = g0 * G[tap0][0], g01 = g0 * G[tap0][1], g02 = g0 * G[tap0][2];+ float g10 = g1 * G[tap1][0], g11 = g1 * G[tap1][1], g12 = g1 * G[tap1][2];+ float x1 = x[-T1 + 1], x2 = x[-T1], x3 = x[-T1 - 1], x4 = x[-T1 - 2];+ if (g0 == g1 && T0 == T1 && tap0 == tap1)+ overlap = 0;+ int i;+ for (i = 0; i < overlap; i++) {+ float x0 = x[i - T1 + 2];+ float f = CELT_WINDOW[i] * CELT_WINDOW[i];+ y[i] = x[i] + (1.f - f) * g00 * x[i - T0] ++ (1.f - f) * g01 * (x[i - T0 + 1] + x[i - T0 - 1]) ++ (1.f - f) * g02 * (x[i - T0 + 2] + x[i - T0 - 2]) ++ f * g10 * x2 + f * g11 * (x1 + x3) + f * g12 * (x0 + x4);+ x4 = x3;+ x3 = x2;+ x2 = x1;+ x1 = x0;+ }+ if (g1 == 0) {+ if (x != y)+ memmove(y + overlap, x + overlap,+ sizeof(float) * (size_t)(N - overlap));+ return;+ }+ x4 = x[i - T1 - 2];+ x3 = x[i - T1 - 1];+ x2 = x[i - T1];+ x1 = x[i - T1 + 1];+ for (; i < N; i++) {+ float x0 = x[i - T1 + 2];+ y[i] = x[i] + g10 * x2 + g11 * (x1 + x3) + g12 * (x0 + x4);+ x4 = x3;+ x3 = x2;+ x2 = x1;+ x1 = x0;+ }+}++/* the concealed overlap pre-filtered and folded as an MDCT would leave+ it, so the next decoded frame blends with it */+static void prefilter_and_fold(struct celt_dec *d, int N)+{+ float etmp[OV];+ for (int c = 0; c < d->cc; c++) {+ comb_filter(etmp, d->mem[c] + DBS - N, d->pf_period_old, d->pf_period,+ OV, -d->pf_gain_old, -d->pf_gain, d->pf_tapset_old,+ d->pf_tapset, 0);+ for (int i = 0; i < OV / 2; i++)+ d->mem[c][DBS - N + i] = CELT_WINDOW[i] * etmp[OV - 1 - i] ++ CELT_WINDOW[OV - i - 1] * etmp[i];+ }+}++int jcelt_frame(struct celt_dec *d, struct rc *r, int len, int n, int stream_ch,+ float **out)+{+ static const uint8_t SPREAD_ICDF[4] = {25, 23, 2, 0};+ static const uint8_t TRIM_ICDF[11] = {126, 124, 119, 109, 87, 41,+ 19, 9, 4, 2, 0};+ static const uint8_t TAPSET_ICDF[3] = {2, 1, 0};+ int LM, C = stream_ch, CC = d->cc, start = d->start, end = d->end;+ for (LM = 0; LM <= 3 && 120 << LM != n; LM++)+ ;+ if (LM > 3 || len < 0 || len > 1275)+ return -1;+ int M = 1 << LM, N = n;+ float *out_syn[2];+ for (int c = 0; c < CC; c++)+ out_syn[c] = d->mem[c] + DBS - N;+ int eff_end = end > NB ? NB : end;+ if (C == 1)+ for (int i = 0; i < NB; i++)+ d->old_e[i] =+ d->old_e[i] > d->old_e[NB + i] ? d->old_e[i] : d->old_e[NB + i];+ if (d->loss_duration == 0)+ d->skip_plc = 0;+ int32_t total_bits = len * 8;+ int tell = rc_tell(r), silence;+ if (tell >= total_bits)+ silence = 1;+ else if (tell == 1)+ silence = rc_bit_logp(r, 15);+ else+ silence = 0;+ if (silence) {+ tell = len * 8;+ r->nbits_total += tell - rc_tell(r);+ }+ float pf_gain = 0;+ int pf_pitch = 0, pf_tapset = 0;+ if (start == 0 && tell + 16 <= total_bits) {+ if (rc_bit_logp(r, 1)) {+ int octave = (int)rc_uint(r, 6);+ pf_pitch =+ (16 << octave) + (int)rc_bits(r, 4 + (unsigned)octave) - 1;+ int qg = (int)rc_bits(r, 3);+ if (rc_tell(r) + 2 <= total_bits)+ pf_tapset = rc_icdf(r, TAPSET_ICDF, 2);+ pf_gain = .09375f * (float)(qg + 1);+ }+ tell = rc_tell(r);+ }+ int transient = 0;+ if (LM > 0 && tell + 3 <= total_bits) {+ transient = rc_bit_logp(r, 3);+ tell = rc_tell(r);+ }+ int short_blocks = transient ? M : 0;+ int intra = tell + 3 <= total_bits ? rc_bit_logp(r, 3) : 0;+ if (!intra && d->loss_duration != 0) {+ /* after concealment, a safe start for the energy's prediction */+ int missing = imin(10, d->loss_duration >> LM);+ float safety = LM == 0 ? 1.5f : LM == 1 ? .5f : 0;+ for (int c = 0; c < 2; c++)+ for (int i = start; i < end; i++) {+ float *e = &d->old_e[c * NB + i];+ float e1 = d->old_loge[c * NB + i],+ e2 = d->old_loge2[c * NB + i];+ if (*e < (e1 > e2 ? e1 : e2)) {+ float slope =+ e1 - *e > .5f * (e2 - *e) ? e1 - *e : .5f * (e2 - *e);+ float t = (1 + missing) * slope;+ float v = *e - (t > 0 ? t : 0);+ *e = v > -20.f ? v : -20.f;+ } else {+ float m = *e < e1 ? *e : e1;+ *e = m < e2 ? m : e2;+ }+ *e -= safety;+ }+ }+ coarse_energy(d, r, intra, C, LM);+ int tf_res[NB];+ tf_decode(r, start, end, transient, tf_res, LM);+ tell = rc_tell(r);+ int spread = SPREAD_NORMAL;+ if (tell + 4 <= total_bits)+ spread = rc_icdf(r, SPREAD_ICDF, 5);+ int cap[NB], offsets[NB];+ for (int i = 0; i < NB; i++) {+ int w = band_w(i) << LM;+ cap[i] = (CELT_CACHE_CAPS[NB * (2 * LM + C - 1) + i] + 64) * C * w >> 2;+ }+ int dynalloc_logp = 6;+ total_bits <<= BITRES;+ uint32_t tf = rc_tell_frac(r);+ for (int i = start; i < end; i++) {+ int width = C * band_w(i) << LM;+ int quanta = imin(width << BITRES, imax(6 << BITRES, width));+ int loop_logp = dynalloc_logp, boost = 0;+ while ((int32_t)tf + (loop_logp << BITRES) < total_bits &&+ boost < cap[i]) {+ int flag = rc_bit_logp(r, (unsigned)loop_logp);+ tf = rc_tell_frac(r);+ if (!flag)+ break;+ boost += quanta;+ total_bits -= quanta;+ loop_logp = 1;+ }+ offsets[i] = boost;+ if (boost > 0)+ dynalloc_logp = imax(2, dynalloc_logp - 1);+ }+ for (int i = 0; i < start; i++)+ offsets[i] = 0;+ int trim = (int32_t)tf + (6 << BITRES) <= total_bits+ ? rc_icdf(r, TRIM_ICDF, 7)+ : 5;+ int32_t bits =+ (((int32_t)len * 8) << BITRES) - (int32_t)rc_tell_frac(r) - 1;+ int anti_rsv =+ transient && LM >= 2 && bits >= ((LM + 2) << BITRES) ? 1 << BITRES : 0;+ bits -= anti_rsv;+ int pulses[NB], fine_quant[NB], fine_prio[NB], intensity = 0, dual = 0;+ int32_t balance = 0;+ int coded =+ allocation(r, start, end, offsets, cap, trim, &intensity, &dual, bits,+ &balance, pulses, fine_quant, fine_prio, C, LM);+ for (int i = start; i < end; i++) {+ if (fine_quant[i] <= 0)+ continue;+ for (int c = 0; c < C; c++) {+ int q2 = (int)rc_bits(r, (unsigned)fine_quant[i]);+ d->old_e[i + c * NB] += ((float)q2 + .5f) *+ (float)(1 << (14 - fine_quant[i])) *+ (1.f / 16384) -+ .5f;+ }+ }+ for (int c = 0; c < CC; c++)+ memmove(d->mem[c], d->mem[c] + N,+ sizeof(float) * (size_t)(DBS - N + OV));+ uint8_t collapse[2 * NB];+ float *X = d->x;+ memset(X, 0, sizeof(float) * (size_t)(C * N));+ all_bands(d, r, start, end, X, C == 2 ? X + N : NULL, collapse, pulses,+ short_blocks, spread, dual, intensity, tf_res,+ len * (8 << BITRES) - anti_rsv, balance, LM, coded);+ int anti_on = anti_rsv > 0 ? (int)rc_bits(r, 1) : 0;+ int bits_left = len * 8 - rc_tell(r);+ for (int prio = 0; prio < 2; prio++)+ for (int i = start; i < end && bits_left >= C; i++) {+ if (fine_quant[i] >= MAX_FINE_BITS || fine_prio[i] != prio)+ continue;+ for (int c = 0; c < C; c++) {+ int q2 = (int)rc_bits(r, 1);+ d->old_e[i + c * NB] += ((float)q2 - .5f) *+ (float)(1 << (14 - fine_quant[i] - 1)) *+ (1.f / 16384);+ bits_left--;+ }+ }+ if (anti_on)+ anti_collapse(d, X, collapse, LM, C, N, start, end, pulses);+ if (silence)+ for (int i = 0; i < C * NB; i++)+ d->old_e[i] = -28.f;+ if (d->prefilter_and_fold)+ prefilter_and_fold(d, N);+ synthesis(d, X, out_syn, start, eff_end, C, CC, transient, LM, silence);+ for (int c = 0; c < CC; c++) {+ d->pf_period = imax(d->pf_period, 15);+ d->pf_period_old = imax(d->pf_period_old, 15);+ comb_filter(out_syn[c], out_syn[c], d->pf_period_old, d->pf_period, 120,+ d->pf_gain_old, d->pf_gain, d->pf_tapset_old, d->pf_tapset,+ OV);+ if (LM != 0)+ comb_filter(out_syn[c] + 120, out_syn[c] + 120, d->pf_period,+ pf_pitch, N - 120, d->pf_gain, pf_gain, d->pf_tapset,+ pf_tapset, OV);+ }+ d->pf_period_old = d->pf_period;+ d->pf_gain_old = d->pf_gain;+ d->pf_tapset_old = d->pf_tapset;+ d->pf_period = pf_pitch;+ d->pf_gain = pf_gain;+ d->pf_tapset = pf_tapset;+ if (LM != 0) {+ d->pf_period_old = d->pf_period;+ d->pf_gain_old = d->pf_gain;+ d->pf_tapset_old = d->pf_tapset;+ }+ if (C == 1)+ memcpy(&d->old_e[NB], d->old_e, sizeof(float) * NB);+ if (!transient) {+ memcpy(d->old_loge2, d->old_loge, sizeof d->old_loge);+ memcpy(d->old_loge, d->old_e, sizeof d->old_e);+ } else {+ for (int i = 0; i < 2 * NB; i++)+ d->old_loge[i] =+ d->old_loge[i] < d->old_e[i] ? d->old_loge[i] : d->old_e[i];+ }+ float max_bg = (float)imin(160, d->loss_duration + M) * 0.001f;+ for (int i = 0; i < 2 * NB; i++)+ d->bg_loge[i] = d->bg_loge[i] + max_bg < d->old_e[i]+ ? d->bg_loge[i] + max_bg+ : d->old_e[i];+ for (int c = 0; c < 2; c++) {+ for (int i = 0; i < start; i++) {+ d->old_e[c * NB + i] = 0;+ d->old_loge[c * NB + i] = d->old_loge2[c * NB + i] = -28.f;+ }+ for (int i = end; i < NB; i++) {+ d->old_e[c * NB + i] = 0;+ d->old_loge[c * NB + i] = d->old_loge2[c * NB + i] = -28.f;+ }+ }+ d->rng = r->rng;+ d->loss_duration = 0;+ d->prefilter_and_fold = 0;+ for (int c = 0; c < CC; c++) {+ float m = d->preemph[c];+ for (int j = 0; j < N; j++) {+ float tmp = out_syn[c][j] + 1e-30f + m;+ m = 0.85000610f * tmp;+ out[c][j] = tmp * (1.f / 32768);+ }+ d->preemph[c] = m;+ }+ return rc_tell(r) > 8 * len ? -1 : 0;+}++/* ---- concealment ---- */++static void autocorr(const float *x, float *ac, const float *window,+ int overlap, int lag, int n)+{+ float xx[1024];+ const float *xp = x;+ if (overlap) {+ memcpy(xx, x, sizeof(float) * (size_t)n);+ for (int i = 0; i < overlap; i++) {+ xx[i] = x[i] * window[i];+ xx[n - i - 1] = x[n - i - 1] * window[i];+ }+ xp = xx;+ }+ for (int k = 0; k <= lag; k++) {+ float s = 0;+ for (int i = k; i < n; i++)+ s += xp[i] * xp[i - k];+ ac[k] = s;+ }+}++static void celt_lpc(float *lpc, const float *ac, int p)+{+ float err = ac[0];+ memset(lpc, 0, sizeof(float) * (size_t)p);+ if (ac[0] <= 1e-10f)+ return;+ for (int i = 0; i < p; i++) {+ float rr = 0;+ for (int j = 0; j < i; j++)+ rr += lpc[j] * ac[i - j];+ rr += ac[i + 1];+ float r = -rr / err;+ lpc[i] = r;+ for (int j = 0; j < (i + 1) >> 1; j++) {+ float t1 = lpc[j], t2 = lpc[i - 1 - j];+ lpc[j] = t1 + r * t2;+ lpc[i - 1 - j] = t2 + r * t1;+ }+ err = err - r * r * err;+ if (err <= .001f * ac[0])+ break;+ }+}++static void find_best_pitch(const float *xcorr, const float *y, int len,+ int max_pitch, int *best)+{+ float Syy = 1, bnum[2] = {-1, -1}, bden[2] = {0, 0};+ best[0] = 0;+ best[1] = 1;+ for (int j = 0; j < len; j++)+ Syy += y[j] * y[j];+ for (int i = 0; i < max_pitch; i++) {+ if (xcorr[i] > 0) {+ float x16 = xcorr[i] * 1e-12f, num = x16 * x16;+ if (num * bden[1] > bnum[1] * Syy) {+ if (num * bden[0] > bnum[0] * Syy) {+ bnum[1] = bnum[0];+ bden[1] = bden[0];+ best[1] = best[0];+ bnum[0] = num;+ bden[0] = Syy;+ best[0] = i;+ } else {+ bnum[1] = num;+ bden[1] = Syy;+ best[1] = i;+ }+ }+ }+ Syy += y[i + len] * y[i + len] - y[i] * y[i];+ Syy = Syy > 1 ? Syy : 1;+ }+}++static int plc_pitch(struct celt_dec *d)+{+ float lp[DBS >> 1];+ float ac[5], lpc[4], lpc2[5], tmp = 1.f;+ int len = DBS, C = d->cc;+ for (int i = 1; i < len >> 1; i++) {+ lp[i] = .25f * d->mem[0][2 * i - 1] + .25f * d->mem[0][2 * i + 1] ++ .5f * d->mem[0][2 * i];+ if (C == 2)+ lp[i] += .25f * d->mem[1][2 * i - 1] + .25f * d->mem[1][2 * i + 1] ++ .5f * d->mem[1][2 * i];+ }+ lp[0] = .25f * d->mem[0][1] + .5f * d->mem[0][0];+ if (C == 2)+ lp[0] += .25f * d->mem[1][1] + .5f * d->mem[1][0];+ autocorr(lp, ac, NULL, 0, 4, len >> 1);+ ac[0] *= 1.0001f;+ for (int i = 1; i <= 4; i++)+ ac[i] -= ac[i] * (.008f * i) * (.008f * i);+ celt_lpc(lpc, ac, 4);+ for (int i = 0; i < 4; i++) {+ tmp *= .9f;+ lpc[i] *= tmp;+ }+ lpc2[0] = lpc[0] + .8f;+ lpc2[1] = lpc[1] + .8f * lpc[0];+ lpc2[2] = lpc[2] + .8f * lpc[1];+ lpc2[3] = lpc[3] + .8f * lpc[2];+ lpc2[4] = .8f * lpc[3];+ float m[5] = {0, 0, 0, 0, 0};+ for (int i = 0; i < len >> 1; i++) {+ float s = lp[i] + lpc2[0] * m[0] + lpc2[1] * m[1] + lpc2[2] * m[2] ++ lpc2[3] * m[3] + lpc2[4] * m[4];+ m[4] = m[3];+ m[3] = m[2];+ m[2] = m[1];+ m[1] = m[0];+ m[0] = lp[i];+ lp[i] = s;+ }+ /* the search: x the last part, y all of it */+ const float *x = lp + (720 >> 1), *y = lp;+ int slen = DBS - 720, max_pitch = 720 - 100, lag = slen + max_pitch;+ float x4[(DBS - 720) >> 2], y4[(DBS + 620) >> 2], xcorr[(720 - 100) >> 1];+ int best[2] = {0, 0};+ for (int j = 0; j < slen >> 2; j++)+ x4[j] = x[2 * j];+ for (int j = 0; j < lag >> 2; j++)+ y4[j] = y[2 * j];+ for (int i = 0; i < max_pitch >> 2; i++) {+ float s = 0;+ for (int j = 0; j < slen >> 2; j++)+ s += x4[j] * y4[i + j];+ xcorr[i] = s;+ }+ find_best_pitch(xcorr, y4, slen >> 2, max_pitch >> 2, best);+ for (int i = 0; i < max_pitch >> 1; i++) {+ xcorr[i] = 0;+ if (abs(i - 2 * best[0]) > 2 && abs(i - 2 * best[1]) > 2)+ continue;+ float s = 0;+ for (int j = 0; j < slen >> 1; j++)+ s += x[j] * y[i + j];+ xcorr[i] = s > -1 ? s : -1;+ }+ find_best_pitch(xcorr, y, slen >> 1, max_pitch >> 1, best);+ int offset = 0;+ if (best[0] > 0 && best[0] < (max_pitch >> 1) - 1) {+ float a = xcorr[best[0] - 1], b = xcorr[best[0]],+ c = xcorr[best[0] + 1];+ if ((c - a) > .7f * (b - a))+ offset = 1;+ else if ((a - c) > .7f * (b - c))+ offset = -1;+ }+ return 720 - (2 * best[0] - offset);+}++void jcelt_plc(struct celt_dec *d, int n, float **out)+{+ int LM, N = n, C = d->cc, start = d->start;+ for (LM = 0; LM < 3 && 120 << LM != n; LM++)+ ;+ if (120 << LM != n) { /* not a CELT frame's size: silence */+ for (int c = 0; c < C; c++)+ if (out[c] && n > 0)+ memset(out[c], 0, sizeof(float) * (size_t)n);+ return;+ }+ float *out_syn[2];+ for (int c = 0; c < C; c++)+ out_syn[c] = d->mem[c] + DBS - N;+ int noise = d->loss_duration >= 40 || start != 0 || d->skip_plc;+ if (noise) {+ int end = d->end, eff_end = imax(start, imin(end, NB));+ for (int c = 0; c < C; c++)+ memmove(d->mem[c], d->mem[c] + N,+ sizeof(float) * (size_t)(DBS - N + OV));+ if (d->prefilter_and_fold)+ prefilter_and_fold(d, N);+ float decay = d->loss_duration == 0 ? 1.5f : .5f;+ for (int c = 0; c < C; c++)+ for (int i = start; i < end; i++) {+ float *e = &d->old_e[c * NB + i], v = *e - decay;+ *e = d->bg_loge[c * NB + i] > v ? d->bg_loge[c * NB + i] : v;+ }+ uint32_t seed = d->rng;+ float *X = d->x;+ for (int c = 0; c < C; c++)+ for (int i = start; i < eff_end; i++) {+ int off = N * c + (CELT_EBANDS[i] << LM);+ int blen = band_w(i) << LM;+ for (int j = 0; j < blen; j++) {+ seed = lcg(seed);+ X[off + j] = (float)((int32_t)seed >> 20);+ }+ renormalise(X + off, blen, 1.f);+ }+ d->rng = seed;+ synthesis(d, X, out_syn, start, eff_end, C, C, 0, LM, 0);+ d->prefilter_and_fold = 0;+ d->skip_plc = 1;+ } else {+ float fade = 1.f;+ int pitch;+ if (d->loss_duration == 0) {+ d->last_pitch = pitch = plc_pitch(d);+ } else {+ pitch = d->last_pitch;+ fade = .8f;+ }+ int exc_len = imin(2 * pitch, 1024);+ float exc_buf[1024 + 24], fir_tmp[1024], *exc = exc_buf + 24;+ for (int c = 0; c < C; c++) {+ float *buf = d->mem[c], S1 = 0;+ for (int i = 0; i < 1024 + 24; i++)+ exc[i - 24] = buf[DBS - 1024 - 24 + i];+ if (d->loss_duration == 0) {+ float ac[25];+ autocorr(exc, ac, CELT_WINDOW, OV, 24, 1024);+ ac[0] *= 1.0001f;+ for (int i = 1; i <= 24; i++)+ ac[i] -= ac[i] * (0.008f * 0.008f) * i * i;+ celt_lpc(d->lpc[c], ac, 24);+ }+ for (int i = 0; i < exc_len; i++) {+ float s = exc[1024 - exc_len + i];+ for (int k = 0; k < 24; k++)+ s += d->lpc[c][k] * exc[1024 - exc_len + i - k - 1];+ fir_tmp[i] = s;+ }+ memcpy(exc + 1024 - exc_len, fir_tmp,+ sizeof(float) * (size_t)exc_len);+ float E1 = 1, E2 = 1;+ int dl = exc_len >> 1;+ for (int i = 0; i < dl; i++) {+ float e = exc[1024 - dl + i];+ E1 += e * e;+ e = exc[1024 - 2 * dl + i];+ E2 += e * e;+ }+ E1 = E1 < E2 ? E1 : E2;+ float decay = sqrtf((E1 * .5f) / E2);+ memmove(buf, buf + N, sizeof(float) * (size_t)(DBS - N));+ int ex_off = 1024 - pitch, ex_len = N + OV;+ float att = fade * decay;+ for (int i = 0, j = 0; i < ex_len; i++, j++) {+ if (j >= pitch) {+ j -= pitch;+ att *= decay;+ }+ buf[DBS - N + i] = att * exc[ex_off + j];+ float t = buf[DBS - 1024 - N + ex_off + j];+ S1 += t * t;+ }+ float mem[24];+ for (int i = 0; i < 24; i++)+ mem[i] = buf[DBS - N - 1 - i];+ for (int i = 0; i < ex_len; i++) {+ float s = buf[DBS - N + i];+ for (int k = 0; k < 24; k++)+ s -= d->lpc[c][k] * mem[k];+ memmove(mem + 1, mem, 23 * sizeof(float));+ mem[0] = s;+ buf[DBS - N + i] = s;+ }+ float S2 = 0;+ for (int i = 0; i < ex_len; i++)+ S2 += buf[DBS - N + i] * buf[DBS - N + i];+ if (!(S1 > 0.2f * S2)) {+ for (int i = 0; i < ex_len; i++)+ buf[DBS - N + i] = 0;+ } else if (S1 < S2) {+ float ratio = sqrtf((S1 * .5f + 1) / (S2 + 1));+ for (int i = 0; i < OV; i++)+ buf[DBS - N + i] *= 1.f - CELT_WINDOW[i] * (1.f - ratio);+ for (int i = OV; i < ex_len; i++)+ buf[DBS - N + i] *= ratio;+ }+ }+ d->prefilter_and_fold = 1;+ }+ d->loss_duration = imin(10000, d->loss_duration + (1 << LM));+ for (int c = 0; c < C; c++) {+ float m = d->preemph[c];+ for (int j = 0; j < N; j++) {+ float tmp = out_syn[c][j] + 1e-30f + m;+ m = 0.85000610f * tmp;+ out[c][j] = tmp * (1.f / 32768);+ }+ d->preemph[c] = m;+ }+}diff --git a/src/common/audio_opus_silk.c b/src/common/audio_opus_silk.cnew file mode 100644index 0000000..575f0ba--- /dev/null+++ b/src/common/audio_opus_silk.c@@ -0,0 +1,1682 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_opus_silk.c - the SILK layer of Opus (RFC 6716 section 4.2;+ * audio_opus.h), in the fixed-point arithmetic the reference decoder+ * defines it by, to the bit: the voice activity and LBRR flags, the+ * stereo prediction weights, each frame's indices (signal type, gains,+ * the NLSF codebook path, interpolation, pitch lag and contour, the LTP+ * filters and their scaling, the seed), the excitation (pulse counts per+ * block, the shell-coded split of the pulses, their least significant bits+ * and signs), the parameters (gains dequantized, NLSFs from the two-stage+ * codebook, stabilized, to LPC coefficients checked for stability, pitch+ * lags), the synthesis (excitation with its offsets and random signs, the+ * long-term and the short-term predictors), the mid/side to left/right+ * mixing with its interpolated predictors, and the resampler to 48 kHz+ * (the 2x all-pass upsampler and the fractional FIR) that libopus uses;+ * the concealment of a lost frame and the comfort noise, which a change+ * of mode without redundancy fades from.+ */+#include "audio_opus.h"++#include <stdlib.h>+#include <string.h>++#include "audio_opus_tab.h"++#define MAX_ORDER 16+#define MAX_FRAME 320+#define MAX_SUBFR 80+#define LTP_ORDER 5+#define TYPE_NO_VOICE 0+#define TYPE_UNVOICED 1+#define TYPE_VOICED 2+#define CODE_INDEPENDENTLY 0+#define CODE_INDEPENDENTLY_NO_LTP_SCALING 1+#define CODE_CONDITIONALLY 2+#define N_LEVELS_QGAIN 64+#define MIN_DELTA_GAIN 4 /* -MIN_DELTA_GAIN_QUANT */+#define MAX_DELTA_GAIN 36+#define OFFSET_Q7 (2 * 128 / 6 + 16 * 128)+#define INV_SCALE_Q16 ((65536 * ((86 * 128) / 6)) / (N_LEVELS_QGAIN - 1))++/* ---- fixed-point arithmetic, as SigProc_FIX.h defines it ---- */++/* a left shift of a value that may be negative, as two's complement+ does it */+#define LSH(v, n) ((int32_t)((uint32_t)(int32_t)(v) << (n)))++static inline int32_t smulwb(int32_t a, int32_t b)+{+ return (int32_t)(((int64_t)a * (int16_t)b) >> 16);+}++static inline int32_t smlawb(int32_t a, int32_t b, int32_t c)+{+ return (int32_t)((int64_t)a + (((int64_t)b * (int16_t)c) >> 16));+}++static inline int32_t smulbb(int32_t a, int32_t b)+{+ return (int32_t)(int16_t)a * (int16_t)b;+}++static inline int32_t smlabb(int32_t a, int32_t b, int32_t c)+{+ return (int32_t)((uint32_t)a ++ (uint32_t)((int32_t)(int16_t)b * (int16_t)c));+}++static inline int32_t smulww(int32_t a, int32_t b)+{+ return (int32_t)(((int64_t)a * b) >> 16);+}++static inline int32_t smlaww(int32_t a, int32_t b, int32_t c)+{+ return (int32_t)((int64_t)a + (((int64_t)b * c) >> 16));+}++static inline int32_t smmul(int32_t a, int32_t b)+{+ return (int32_t)(((int64_t)a * b) >> 32);+}++static inline int32_t smultt(int32_t a, int32_t b)+{+ return (a >> 16) * (b >> 16);+}++static inline int32_t rshift_round(int32_t a, int s)+{+ return s == 1 ? (a >> 1) + (a & 1) : ((a >> (s - 1)) + 1) >> 1;+}++static inline int64_t rshift_round64(int64_t a, int s)+{+ return s == 1 ? (a >> 1) + (a & 1) : ((a >> (s - 1)) + 1) >> 1;+}++static inline int32_t sat16(int32_t a)+{+ return a > 32767 ? 32767 : a < -32768 ? -32768 : a;+}++static inline int32_t sat32(int64_t a)+{+ return a > INT32_MAX ? INT32_MAX : a < INT32_MIN ? INT32_MIN : (int32_t)a;+}++static inline int32_t limit(int32_t a, int32_t l1, int32_t l2)+{+ if (l1 > l2)+ return a > l1 ? l1 : a < l2 ? l2 : a;+ return a > l2 ? l2 : a < l1 ? l1 : a;+}++static inline int32_t lshift_sat32(int32_t a, int s)+{+ return (int32_t)((uint32_t)limit(a, INT32_MIN >> s, INT32_MAX >> s) << s);+}++static inline int32_t add_sat32(int32_t a, int32_t b)+{+ return sat32((int64_t)a + b);+}++static inline int32_t sub_sat32(int32_t a, int32_t b)+{+ return sat32((int64_t)a - b);+}++static inline int32_t clz32(int32_t v)+{+ return v ? __builtin_clz((uint32_t)v) : 32;+}++static inline int32_t ror32(int32_t a, int rot)+{+ uint32_t x = (uint32_t)a, r = (uint32_t)rot, m = (uint32_t)-rot;+ if (rot == 0)+ return a;+ if (rot < 0)+ return (int32_t)((x << m) | (x >> (32 - m)));+ return (int32_t)((x << (32 - r)) | (x >> r));+}++static inline int32_t silk_rand(int32_t seed)+{+ return (int32_t)(907633515u + (uint32_t)seed * 196314165u);+}++static void clz_frac(int32_t in, int32_t *lz, int32_t *frac_q7)+{+ int32_t l = clz32(in);+ *lz = l;+ *frac_q7 = ror32(in, 24 - l) & 0x7f;+}++static int32_t sqrt_approx(int32_t x)+{+ int32_t lz, frac;+ if (x <= 0)+ return 0;+ clz_frac(x, &lz, &frac);+ int32_t y = lz & 1 ? 32768 : 46214;+ y >>= lz >> 1;+ return smlawb(y, y, smulbb(213, frac));+}++static int32_t div32_varq(int32_t a, int32_t b, int qres)+{+ int ah = clz32(a > 0 ? a : -a) - 1;+ int32_t an = (int32_t)((uint32_t)a << ah);+ int bh = clz32(b > 0 ? b : -b) - 1;+ int32_t bn = (int32_t)((uint32_t)b << bh);+ int32_t binv = (INT32_MAX >> 2) / (int16_t)(bn >> 16);+ int32_t result = smulwb(an, binv);+ an = (int32_t)((uint32_t)an - ((uint32_t)smmul(bn, result) << 3));+ result = smlawb(result, an, binv);+ int lshift = 29 + ah - bh - qres;+ if (lshift < 0)+ return lshift_sat32(result, -lshift);+ return lshift < 32 ? result >> lshift : 0;+}++static int32_t inverse32_varq(int32_t b, int qres)+{+ int bh = clz32(b > 0 ? b : -b) - 1;+ int32_t bn = (int32_t)((uint32_t)b << bh);+ int32_t binv = (INT32_MAX >> 2) / (int16_t)(bn >> 16);+ int32_t result = (int32_t)((uint32_t)binv << 16);+ int32_t err = (int32_t)((uint32_t)((1 << 29) - smulwb(bn, binv)) << 3);+ result = smlaww(result, err, binv);+ int lshift = 61 - bh - qres;+ if (lshift <= 0)+ return lshift_sat32(result, -lshift);+ return lshift < 32 ? result >> lshift : 0;+}++static int32_t log2lin(int32_t in)+{+ if (in < 0)+ return 0;+ if (in >= 3967)+ return INT32_MAX;+ int32_t out = 1 << (in >> 7), frac = in & 0x7F;+ if (in < 2048)+ return out ++ ((out * smlawb(frac, smulbb(frac, 128 - frac), -174)) >> 7);+ return out + (out >> 7) * smlawb(frac, smulbb(frac, 128 - frac), -174);+}++static void sum_sqr_shift(int32_t *energy, int *shift, const int16_t *x,+ int len)+{+ int shft = 31 - clz32(len), i;+ int32_t nrg = len;+ for (i = 0; i < len - 1; i += 2) {+ uint32_t t = (uint32_t)smulbb(x[i], x[i]);+ t = (uint32_t)smlabb((int32_t)t, x[i + 1], x[i + 1]);+ nrg = (int32_t)((uint32_t)nrg + (t >> shft));+ }+ if (i < len)+ nrg = (int32_t)((uint32_t)nrg + ((uint32_t)smulbb(x[i], x[i]) >> shft));+ shft = shft + 3 - clz32(nrg);+ if (shft < 0)+ shft = 0;+ nrg = 0;+ for (i = 0; i < len - 1; i += 2) {+ uint32_t t = (uint32_t)smulbb(x[i], x[i]);+ t = (uint32_t)smlabb((int32_t)t, x[i + 1], x[i + 1]);+ nrg = (int32_t)((uint32_t)nrg + (t >> shft));+ }+ if (i < len)+ nrg = (int32_t)((uint32_t)nrg + ((uint32_t)smulbb(x[i], x[i]) >> shft));+ *shift = shft;+ *energy = nrg;+}++/* ---- the state ---- */++struct nlsf_cb {+ int vectors, order;+ int32_t step_q16;+ const uint8_t *cb1_q8;+ const int16_t *wght_q9;+ const uint8_t *cb1_icdf, *pred_q8, *ec_sel, *ec_icdf;+ const int16_t *delta_min;+};++static const struct nlsf_cb CB_NB_MB = {32,+ 10,+ 11796,+ SILK_NLSF_CB1_NB_MB_Q8,+ SILK_NLSF_CB1_WGHT_Q9,+ SILK_NLSF_CB1_ICDF_NB_MB,+ SILK_NLSF_PRED_NB_MB_Q8,+ SILK_NLSF_CB2_SELECT_NB_MB,+ SILK_NLSF_CB2_ICDF_NB_MB,+ SILK_NLSF_DELTA_MIN_NB_MB_Q15};+static const struct nlsf_cb CB_WB = {32,+ 16,+ 9830,+ SILK_NLSF_CB1_WB_Q8,+ SILK_NLSF_CB1_WB_WGHT_Q9,+ SILK_NLSF_CB1_ICDF_WB,+ SILK_NLSF_PRED_WB_Q8,+ SILK_NLSF_CB2_SELECT_WB,+ SILK_NLSF_CB2_ICDF_WB,+ SILK_NLSF_DELTA_MIN_WB_Q15};++struct resampler {+ int32_t sIIR[6];+ int16_t sFIR[8];+ int16_t delay[48];+ int fs_in, fs_out, batch, delay_n, copy, up2;+ int32_t inv_ratio_q16;+};++struct indices {+ int8_t gains[4], nlsf[MAX_ORDER + 1], ltp[4];+ int16_t lag;+ int8_t contour, signal_type, quant_offset, nlsf_interp, per, ltp_scale,+ seed;+};++struct cng {+ int32_t exc_buf[MAX_FRAME];+ int16_t smth_nlsf[MAX_ORDER];+ int32_t synth[MAX_ORDER], smth_gain, seed;+ int fs_khz;+};++struct plc {+ int32_t pitch_l_q8;+ int16_t ltp_coef[LTP_ORDER], prev_lpc[MAX_ORDER];+ int32_t last_frame_lost, seed, prev_gain[2], conc_energy;+ int16_t rand_scale, prev_ltp_scale;+ int conc_energy_shift, fs_khz, nb_subfr, subfr_length;+};++struct ctrl {+ int pitch_l[4];+ int32_t gains[4];+ int16_t pred[2][MAX_ORDER], ltp[LTP_ORDER * 4];+ int ltp_scale;+};++struct chan {+ int32_t prev_gain, exc[MAX_FRAME], slpc[MAX_ORDER];+ int16_t out[MAX_FRAME + 2 * MAX_SUBFR];+ int lag_prev;+ int8_t last_gain;+ int fs_khz, nb_subfr, frame_length, subfr_length, ltp_mem, order;+ int16_t prev_nlsf[MAX_ORDER];+ int first_after_reset;+ const uint8_t *lag_low_icdf, *contour_icdf;+ int frames_decoded, frames_per_packet, ec_prev_type;+ int16_t ec_prev_lag;+ int vad[3], lbrr, lbrr_flags[3];+ struct resampler rs;+ const struct nlsf_cb *cb;+ struct indices ind;+ struct cng cng;+ int loss_cnt, prev_type;+ struct plc plc;+};++struct silk_dec {+ struct chan ch[2];+ int16_t pred_prev[2], smid[2], sside[2];+ int api_ch, int_ch, prev_mid_only;+ int last_khz, last_ch; /* what the concealment goes on with */+ int api_hz; /* the output's rate: 48000 unless set */+};++/* ---- the resampler (8, 12, 16 kHz up to 48 kHz) ---- */++static void resampler_init(struct resampler *s, int in_hz, int out_hz)+{+ static const int8_t DELAY[3][5] = {+ {4, 0, 2, 0, 0}, {0, 9, 4, 7, 4}, {0, 3, 12, 7, 7}};+ int rin = in_hz == 8000 ? 0 : in_hz == 12000 ? 1 : 2;+ int rout = out_hz == 8000 ? 0+ : out_hz == 12000 ? 1+ : out_hz == 16000 ? 2+ : out_hz == 24000 ? 3+ : 4;+ memset(s, 0, sizeof *s);+ s->delay_n = DELAY[rin][rout];+ s->fs_in = in_hz / 1000;+ s->fs_out = out_hz / 1000;+ s->batch = s->fs_in * 10;+ s->copy = out_hz == in_hz;+ s->up2 = out_hz == 2 * in_hz;+ int up2x = !s->copy && !s->up2; /* the rest: 2x then the FIR */+ s->inv_ratio_q16 = (int32_t)((uint32_t)(((uint32_t)in_hz << (14 + up2x)) /+ (uint32_t)out_hz)+ << 2);+ while (smulww(s->inv_ratio_q16, out_hz) < (in_hz << up2x))+ s->inv_ratio_q16++;+}++static void up2_hq(int32_t *S, int16_t *out, const int16_t *in, int len)+{+ static const int16_t H0[3] = {1746, 14986, 39083 - 65536};+ static const int16_t H1[3] = {6854, 25769, 55542 - 65536};+ for (int k = 0; k < len; k++) {+ int32_t in32 = LSH(in[k], 10), Y, X, o1, o2;+ Y = in32 - S[0];+ X = smulwb(Y, H0[0]);+ o1 = S[0] + X;+ S[0] = in32 + X;+ Y = o1 - S[1];+ X = smulwb(Y, H0[1]);+ o2 = S[1] + X;+ S[1] = o1 + X;+ Y = o2 - S[2];+ X = smlawb(Y, Y, H0[2]);+ o1 = S[2] + X;+ S[2] = o2 + X;+ out[2 * k] = (int16_t)sat16(rshift_round(o1, 10));+ Y = in32 - S[3];+ X = smulwb(Y, H1[0]);+ o1 = S[3] + X;+ S[3] = in32 + X;+ Y = o1 - S[4];+ X = smulwb(Y, H1[1]);+ o2 = S[4] + X;+ S[4] = o1 + X;+ Y = o2 - S[5];+ X = smlawb(Y, Y, H1[2]);+ o1 = S[5] + X;+ S[5] = o2 + X;+ out[2 * k + 1] = (int16_t)sat16(rshift_round(o1, 10));+ }+}++static int16_t *fir_interpol(int16_t *out, const int16_t *buf, int32_t max_q16,+ int32_t inc_q16)+{+ for (int32_t ix = 0; ix < max_q16; ix += inc_q16) {+ int32_t t = smulwb(ix & 0xFFFF, 12);+ const int16_t *b = &buf[ix >> 16];+ const int16_t *c0 = SILK_FRAC_FIR_12 + 4 * t;+ const int16_t *c1 = SILK_FRAC_FIR_12 + 4 * (11 - t);+ int32_t r = smulbb(b[0], c0[0]);+ r = smlabb(r, b[1], c0[1]);+ r = smlabb(r, b[2], c0[2]);+ r = smlabb(r, b[3], c0[3]);+ r = smlabb(r, b[4], c1[3]);+ r = smlabb(r, b[5], c1[2]);+ r = smlabb(r, b[6], c1[1]);+ r = smlabb(r, b[7], c1[0]);+ *out++ = (int16_t)sat16(rshift_round(r, 15));+ }+ return out;+}++static void iir_fir(struct resampler *s, int16_t *out, const int16_t *in,+ int len)+{+ int16_t buf[2 * 160 + 8];+ int n = 0;+ memcpy(buf, s->sFIR, sizeof s->sFIR);+ for (;;) {+ n = len < s->batch ? len : s->batch;+ up2_hq(s->sIIR, &buf[8], in, n);+ out = fir_interpol(out, buf, (int32_t)n << 17, s->inv_ratio_q16);+ in += n;+ len -= n;+ if (len > 0)+ memcpy(buf, &buf[n << 1], 8 * sizeof(int16_t));+ else+ break;+ }+ memcpy(s->sFIR, &buf[n << 1], 8 * sizeof(int16_t));+}++static void resample(struct resampler *s, int16_t *out, const int16_t *in,+ int len)+{+ int n = s->fs_in - s->delay_n;+ memcpy(&s->delay[s->delay_n], in, sizeof(int16_t) * (size_t)n);+ if (s->copy) {+ memcpy(out, s->delay, sizeof(int16_t) * (size_t)s->fs_in);+ memcpy(&out[s->fs_out], &in[n],+ sizeof(int16_t) * (size_t)(len - s->fs_in));+ } else if (s->up2) {+ up2_hq(s->sIIR, out, s->delay, s->fs_in);+ up2_hq(s->sIIR, &out[s->fs_out], &in[n], len - s->fs_in);+ } else {+ iir_fir(s, out, s->delay, s->fs_in);+ iir_fir(s, &out[s->fs_out], &in[n], len - s->fs_in);+ }+ memcpy(s->delay, &in[len - s->delay_n],+ sizeof(int16_t) * (size_t)s->delay_n);+}++/* ---- channel state ---- */++static void cng_reset(struct chan *c)+{+ int32_t step = 32767 / (c->order + 1), acc = 0;+ for (int i = 0; i < c->order; i++) {+ acc += step;+ c->cng.smth_nlsf[i] = (int16_t)acc;+ }+ c->cng.smth_gain = 0;+ c->cng.seed = 3176576;+}++static void plc_reset(struct chan *c)+{+ c->plc.pitch_l_q8 = (int32_t)((uint32_t)c->frame_length << 7);+ c->plc.prev_gain[0] = c->plc.prev_gain[1] = 65536;+ c->plc.subfr_length = 20;+ c->plc.nb_subfr = 2;+}++static void chan_reset(struct chan *c)+{+ memset(c, 0, sizeof *c);+ c->first_after_reset = 1;+ c->prev_gain = 65536;+ cng_reset(c);+ plc_reset(c);+}++static void set_fs(struct chan *c, int fs_khz, int api_hz)+{+ c->subfr_length = 5 * fs_khz;+ int frame_length = c->nb_subfr * c->subfr_length;+ if (c->fs_khz != fs_khz || c->rs.fs_out * 1000 != api_hz)+ resampler_init(&c->rs, fs_khz * 1000, api_hz);+ if (c->fs_khz != fs_khz || frame_length != c->frame_length) {+ if (fs_khz == 8)+ c->contour_icdf = c->nb_subfr == 4+ ? SILK_PITCH_CONTOUR_NB_ICDF+ : SILK_PITCH_CONTOUR_10_MS_NB_ICDF;+ else+ c->contour_icdf = c->nb_subfr == 4 ? SILK_PITCH_CONTOUR_ICDF+ : SILK_PITCH_CONTOUR_10_MS_ICDF;+ if (c->fs_khz != fs_khz) {+ c->ltp_mem = 20 * fs_khz;+ c->order = fs_khz == 16 ? 16 : 10;+ c->cb = fs_khz == 16 ? &CB_WB : &CB_NB_MB;+ c->lag_low_icdf = fs_khz == 16 ? SILK_UNIFORM8_ICDF+ : fs_khz == 12 ? SILK_UNIFORM6_ICDF+ : SILK_UNIFORM4_ICDF;+ c->first_after_reset = 1;+ c->lag_prev = 100;+ c->last_gain = 10;+ c->prev_type = TYPE_NO_VOICE;+ memset(c->out, 0, sizeof c->out);+ memset(c->slpc, 0, sizeof c->slpc);+ }+ c->fs_khz = fs_khz;+ c->frame_length = frame_length;+ }+}++/* ---- indices ---- */++static void nlsf_unpack(int16_t *ec_ix, uint8_t *pred, const struct nlsf_cb *cb,+ int cb1)+{+ const uint8_t *sel = &cb->ec_sel[cb1 * cb->order / 2];+ for (int i = 0; i < cb->order; i += 2) {+ uint8_t e = *sel++;+ ec_ix[i] = (int16_t)(((e >> 1) & 7) * 9);+ pred[i] = cb->pred_q8[i + (e & 1) * (cb->order - 1)];+ ec_ix[i + 1] = (int16_t)(((e >> 5) & 7) * 9);+ pred[i + 1] = cb->pred_q8[i + ((e >> 4) & 1) * (cb->order - 1) + 1];+ }+}++static void decode_indices(struct chan *c, struct rc *r, int frame, int lbrr,+ int cond)+{+ struct indices *ix = &c->ind;+ int k;+ if (lbrr || c->vad[frame])+ k = rc_icdf(r, SILK_TYPE_OFFSET_VAD_ICDF, 8) + 2;+ else+ k = rc_icdf(r, SILK_TYPE_OFFSET_NO_VAD_ICDF, 8);+ ix->signal_type = (int8_t)(k >> 1);+ ix->quant_offset = (int8_t)(k & 1);+ if (cond == CODE_CONDITIONALLY) {+ ix->gains[0] = (int8_t)rc_icdf(r, SILK_DELTA_GAIN_ICDF, 8);+ } else {+ ix->gains[0] =+ (int8_t)(rc_icdf(r, SILK_GAIN_ICDF + 8 * ix->signal_type, 8) << 3);+ ix->gains[0] += (int8_t)rc_icdf(r, SILK_UNIFORM8_ICDF, 8);+ }+ for (int i = 1; i < c->nb_subfr; i++)+ ix->gains[i] = (int8_t)rc_icdf(r, SILK_DELTA_GAIN_ICDF, 8);+ ix->nlsf[0] = (int8_t)rc_icdf(+ r, &c->cb->cb1_icdf[(ix->signal_type >> 1) * c->cb->vectors], 8);+ int16_t ec_ix[MAX_ORDER];+ uint8_t pred[MAX_ORDER];+ nlsf_unpack(ec_ix, pred, c->cb, ix->nlsf[0]);+ for (int i = 0; i < c->cb->order; i++) {+ k = rc_icdf(r, &c->cb->ec_icdf[ec_ix[i]], 8);+ if (k == 0)+ k -= rc_icdf(r, SILK_NLSF_EXT_ICDF, 8);+ else if (k == 8)+ k += rc_icdf(r, SILK_NLSF_EXT_ICDF, 8);+ ix->nlsf[i + 1] = (int8_t)(k - 4);+ }+ ix->nlsf_interp =+ c->nb_subfr == 4+ ? (int8_t)rc_icdf(r, SILK_NLSF_INTERPOLATION_FACTOR_ICDF, 8)+ : 4;+ if (ix->signal_type == TYPE_VOICED) {+ int absolute = 1;+ if (cond == CODE_CONDITIONALLY && c->ec_prev_type == TYPE_VOICED) {+ int delta = rc_icdf(r, SILK_PITCH_DELTA_ICDF, 8);+ if (delta > 0) {+ ix->lag = (int16_t)(c->ec_prev_lag + delta - 9);+ absolute = 0;+ }+ }+ if (absolute) {+ ix->lag = (int16_t)(rc_icdf(r, SILK_PITCH_LAG_ICDF, 8) *+ (c->fs_khz >> 1));+ ix->lag += (int16_t)rc_icdf(r, c->lag_low_icdf, 8);+ }+ c->ec_prev_lag = ix->lag;+ ix->contour = (int8_t)rc_icdf(r, c->contour_icdf, 8);+ ix->per = (int8_t)rc_icdf(r, SILK_LTP_PER_INDEX_ICDF, 8);+ const uint8_t *gi = ix->per == 0 ? SILK_LTP_GAIN_ICDF_0+ : ix->per == 1 ? SILK_LTP_GAIN_ICDF_1+ : SILK_LTP_GAIN_ICDF_2;+ for (k = 0; k < c->nb_subfr; k++)+ ix->ltp[k] = (int8_t)rc_icdf(r, gi, 8);+ ix->ltp_scale = cond == CODE_INDEPENDENTLY+ ? (int8_t)rc_icdf(r, SILK_LTPSCALE_ICDF, 8)+ : 0;+ }+ c->ec_prev_type = ix->signal_type;+ ix->seed = (int8_t)rc_icdf(r, SILK_UNIFORM4_ICDF, 8);+}++/* ---- the excitation ---- */++static void decode_split(int16_t *a, int16_t *b, struct rc *r, int p,+ const uint8_t *table)+{+ if (p > 0) {+ *a = (int16_t)rc_icdf(r, &table[SILK_SHELL_CODE_TABLE_OFFSETS[p]], 8);+ *b = (int16_t)(p - *a);+ } else {+ *a = *b = 0;+ }+}++static void shell_decoder(int16_t *p0, struct rc *r, int p4)+{+ int16_t p3[2], p2[4], p1[8];+ const uint8_t *T0 = SILK_SHELL_CODE_TABLE0, *T1 = SILK_SHELL_CODE_TABLE1;+ const uint8_t *T2 = SILK_SHELL_CODE_TABLE2, *T3 = SILK_SHELL_CODE_TABLE3;+ decode_split(&p3[0], &p3[1], r, p4, T3);+ decode_split(&p2[0], &p2[1], r, p3[0], T2);+ decode_split(&p1[0], &p1[1], r, p2[0], T1);+ decode_split(&p0[0], &p0[1], r, p1[0], T0);+ decode_split(&p0[2], &p0[3], r, p1[1], T0);+ decode_split(&p1[2], &p1[3], r, p2[1], T1);+ decode_split(&p0[4], &p0[5], r, p1[2], T0);+ decode_split(&p0[6], &p0[7], r, p1[3], T0);+ decode_split(&p2[2], &p2[3], r, p3[1], T2);+ decode_split(&p1[4], &p1[5], r, p2[2], T1);+ decode_split(&p0[8], &p0[9], r, p1[4], T0);+ decode_split(&p0[10], &p0[11], r, p1[5], T0);+ decode_split(&p1[6], &p1[7], r, p2[3], T1);+ decode_split(&p0[12], &p0[13], r, p1[6], T0);+ decode_split(&p0[14], &p0[15], r, p1[7], T0);+}++static void decode_pulses(struct rc *r, int16_t *pulses, int type, int offset,+ int frame_length)+{+ int sum[MAX_FRAME / 16 + 1], nls[MAX_FRAME / 16 + 1];+ int rate = rc_icdf(r, SILK_RATE_LEVELS_ICDF + 9 * (type >> 1), 8);+ int iter = frame_length >> 4;+ if (iter * 16 < frame_length)+ iter++;+ const uint8_t *cdf = SILK_PULSES_PER_BLOCK_ICDF + 18 * rate;+ for (int i = 0; i < iter; i++) {+ nls[i] = 0;+ sum[i] = rc_icdf(r, cdf, 8);+ while (sum[i] == 17) {+ nls[i]++;+ sum[i] = rc_icdf(+ r, SILK_PULSES_PER_BLOCK_ICDF + 18 * 9 + (nls[i] == 10), 8);+ }+ }+ for (int i = 0; i < iter; i++) {+ if (sum[i] > 0)+ shell_decoder(&pulses[16 * i], r, sum[i]);+ else+ memset(&pulses[16 * i], 0, 16 * sizeof(int16_t));+ }+ for (int i = 0; i < iter; i++)+ if (nls[i] > 0) {+ int16_t *p = &pulses[16 * i];+ for (int k = 0; k < 16; k++) {+ int a = p[k];+ for (int j = 0; j < nls[i]; j++) {+ a <<= 1;+ a += rc_icdf(r, SILK_LSB_ICDF, 8);+ }+ p[k] = (int16_t)a;+ }+ sum[i] |= nls[i] << 5;+ }+ /* the signs */+ uint8_t icdf[2] = {0, 0};+ const uint8_t *sp = &SILK_SIGN_ICDF[7 * (offset + (type << 1))];+ int len = (frame_length + 8) >> 4;+ int16_t *q = pulses;+ for (int i = 0; i < len; i++) {+ int p = sum[i];+ if (p > 0) {+ icdf[0] = sp[(p & 0x1F) < 6 ? (p & 0x1F) : 6];+ for (int j = 0; j < 16; j++)+ if (q[j] > 0)+ q[j] = (int16_t)(q[j] * ((rc_icdf(r, icdf, 8) << 1) - 1));+ }+ q += 16;+ }+}++/* ---- parameters ---- */++static void gains_dequant(int32_t *gain, const int8_t *ind, int8_t *prev,+ int conditional, int nb)+{+ for (int k = 0; k < nb; k++) {+ if (k == 0 && !conditional) {+ *prev = (int8_t)(ind[k] > *prev - 16 ? ind[k] : *prev - 16);+ } else {+ int t = ind[k] - MIN_DELTA_GAIN;+ int thr = 2 * MAX_DELTA_GAIN - N_LEVELS_QGAIN + *prev;+ if (t > thr)+ *prev = (int8_t)(*prev + (t << 1) - thr);+ else+ *prev = (int8_t)(*prev + t);+ }+ *prev = (int8_t)limit(*prev, 0, N_LEVELS_QGAIN - 1);+ int32_t v = smulwb(INV_SCALE_Q16, *prev) + OFFSET_Q7;+ gain[k] = log2lin(v < 3967 ? v : 3967);+ }+}++static void nlsf_stabilize(int16_t *nlsf, const int16_t *dmin, int L)+{+ int loops, I = 0;+ for (loops = 0; loops < 20; loops++) {+ int32_t min_diff = nlsf[0] - dmin[0];+ I = 0;+ for (int i = 1; i <= L - 1; i++) {+ int32_t d = nlsf[i] - (nlsf[i - 1] + dmin[i]);+ if (d < min_diff) {+ min_diff = d;+ I = i;+ }+ }+ int32_t d = (1 << 15) - (nlsf[L - 1] + dmin[L]);+ if (d < min_diff) {+ min_diff = d;+ I = L;+ }+ if (min_diff >= 0)+ return;+ if (I == 0) {+ nlsf[0] = dmin[0];+ } else if (I == L) {+ nlsf[L - 1] = (int16_t)((1 << 15) - dmin[L]);+ } else {+ int32_t min_c = 0, max_c = 1 << 15;+ for (int k = 0; k < I; k++)+ min_c += dmin[k];+ min_c += dmin[I] >> 1;+ for (int k = L; k > I; k--)+ max_c -= dmin[k];+ max_c -= dmin[I] >> 1;+ int16_t center = (int16_t)limit(+ rshift_round((int32_t)nlsf[I - 1] + nlsf[I], 1), min_c, max_c);+ nlsf[I - 1] = (int16_t)(center - (dmin[I] >> 1));+ nlsf[I] = (int16_t)(nlsf[I - 1] + dmin[I]);+ }+ }+ for (int i = 1; i < L; i++) { /* sort, increasing */+ int16_t v = nlsf[i];+ int j;+ for (j = i - 1; j >= 0 && v < nlsf[j]; j--)+ nlsf[j + 1] = nlsf[j];+ nlsf[j + 1] = v;+ }+ nlsf[0] = (int16_t)(nlsf[0] > dmin[0] ? nlsf[0] : dmin[0]);+ for (int i = 1; i < L; i++) {+ int32_t lo = sat16((int32_t)nlsf[i - 1] + dmin[i]);+ nlsf[i] = (int16_t)(nlsf[i] > lo ? nlsf[i] : lo);+ }+ int32_t hi = (1 << 15) - dmin[L];+ nlsf[L - 1] = (int16_t)(nlsf[L - 1] < hi ? nlsf[L - 1] : hi);+ for (int i = L - 2; i >= 0; i--) {+ int32_t v = nlsf[i + 1] - dmin[i + 1];+ nlsf[i] = (int16_t)(nlsf[i] < v ? nlsf[i] : v);+ }+}++static void nlsf_decode(int16_t *nlsf, const int8_t *ind,+ const struct nlsf_cb *cb)+{+ uint8_t pred[MAX_ORDER];+ int16_t ec_ix[MAX_ORDER], res[MAX_ORDER];+ nlsf_unpack(ec_ix, pred, cb, ind[0]);+ int32_t out = 0;+ for (int i = cb->order - 1; i >= 0; i--) {+ int32_t p = smulbb(out, (int16_t)pred[i]) >> 8;+ out = LSH(ind[i + 1], 10);+ if (out > 0)+ out = (int16_t)(out - 102);+ else if (out < 0)+ out = (int16_t)(out + 102);+ out = smlawb(p, out, cb->step_q16);+ res[i] = (int16_t)out;+ }+ const uint8_t *e = &cb->cb1_q8[ind[0] * cb->order];+ const int16_t *w = &cb->wght_q9[ind[0] * cb->order];+ for (int i = 0; i < cb->order; i++) {+ int32_t t = (int32_t)(LSH(res[i], 14) / w[i]) + LSH((int16_t)e[i], 7);+ nlsf[i] = (int16_t)limit(t, 0, 32767);+ }+ nlsf_stabilize(nlsf, cb->delta_min, cb->order);+}++static void bwexpander32(int32_t *ar, int d, int32_t chirp)+{+ int32_t m1 = chirp - 65536;+ for (int i = 0; i < d - 1; i++) {+ ar[i] = smulww(chirp, ar[i]);+ chirp += rshift_round(chirp * m1, 16);+ }+ ar[d - 1] = smulww(chirp, ar[d - 1]);+}++static void bwexpander16(int16_t *ar, int d, int32_t chirp)+{+ int32_t m1 = chirp - 65536;+ for (int i = 0; i < d - 1; i++) {+ ar[i] = (int16_t)rshift_round(chirp * ar[i], 16);+ chirp += rshift_round(chirp * m1, 16);+ }+ ar[d - 1] = (int16_t)rshift_round(chirp * ar[d - 1], 16);+}++static int32_t inv_pred_gain_qa(int32_t *A, int order)+{+ const int32_t LIMIT = 16773022; /* 0.99975 in Q24 */+ const int32_t MIN_INV_GAIN = 107374;+ int32_t inv_gain = 1 << 30;+ int k;+ for (k = order - 1; k > 0; k--) {+ if (A[k] > LIMIT || A[k] < -LIMIT)+ return 0;+ int32_t rc = -(int32_t)((uint32_t)A[k] << 7);+ int32_t rc1 = (1 << 30) - smmul(rc, rc);+ inv_gain = (int32_t)((uint32_t)smmul(inv_gain, rc1) << 2);+ if (inv_gain < MIN_INV_GAIN)+ return 0;+ int mult2q = 32 - clz32(rc1 > 0 ? rc1 : -rc1);+ int32_t rc2 = inverse32_varq(rc1, mult2q + 30);+ for (int n = 0; n < (k + 1) >> 1; n++) {+ int32_t t1 = A[n], t2 = A[k - n - 1];+ int64_t v = rshift_round64(+ (int64_t)sub_sat32(+ t1, (int32_t)rshift_round64((int64_t)t2 * rc, 31)) *+ rc2,+ mult2q);+ if (v > INT32_MAX || v < INT32_MIN)+ return 0;+ A[n] = (int32_t)v;+ v = rshift_round64(+ (int64_t)sub_sat32(+ t2, (int32_t)rshift_round64((int64_t)t1 * rc, 31)) *+ rc2,+ mult2q);+ if (v > INT32_MAX || v < INT32_MIN)+ return 0;+ A[k - n - 1] = (int32_t)v;+ }+ }+ if (A[k] > LIMIT || A[k] < -LIMIT)+ return 0;+ int32_t rc = -(int32_t)((uint32_t)A[0] << 7);+ int32_t rc1 = (1 << 30) - smmul(rc, rc);+ inv_gain = (int32_t)((uint32_t)smmul(inv_gain, rc1) << 2);+ return inv_gain < MIN_INV_GAIN ? 0 : inv_gain;+}++static int32_t inv_pred_gain(const int16_t *a, int order)+{+ int32_t A[MAX_ORDER], dc = 0;+ for (int k = 0; k < order; k++) {+ dc += a[k];+ A[k] = LSH(a[k], 12);+ }+ if (dc >= 4096)+ return 0;+ return inv_pred_gain_qa(A, order);+}++static void lpc_fit(int16_t *out, int32_t *in, int qout, int qin, int d)+{+ int i, idx = 0;+ for (i = 0; i < 10; i++) {+ int32_t maxabs = 0;+ for (int k = 0; k < d; k++) {+ int32_t a = in[k] > 0 ? in[k] : -in[k];+ if (a > maxabs) {+ maxabs = a;+ idx = k;+ }+ }+ maxabs = rshift_round(maxabs, qin - qout);+ if (maxabs > 32767) {+ maxabs = maxabs < 163838 ? maxabs : 163838;+ int32_t chirp =+ 65470 - (int32_t)(((uint32_t)(maxabs - 32767) << 14) /+ (uint32_t)((maxabs * (idx + 1)) >> 2));+ bwexpander32(in, d, chirp);+ } else {+ break;+ }+ }+ if (i == 10) {+ for (int k = 0; k < d; k++) {+ out[k] = (int16_t)sat16(rshift_round(in[k], qin - qout));+ in[k] = (int32_t)((uint32_t)(int32_t)out[k] << (qin - qout));+ }+ } else {+ for (int k = 0; k < d; k++)+ out[k] = (int16_t)rshift_round(in[k], qin - qout);+ }+}++static void find_poly(int32_t *out, const int32_t *c, int dd)+{+ out[0] = 1 << 16;+ out[1] = -c[0];+ for (int k = 1; k < dd; k++) {+ int32_t f = c[2 * k];+ out[k + 1] = (int32_t)((uint32_t)out[k - 1] << 1) -+ (int32_t)rshift_round64((int64_t)f * out[k], 16);+ for (int n = k; n > 1; n--)+ out[n] += out[n - 2] -+ (int32_t)rshift_round64((int64_t)f * out[n - 1], 16);+ out[1] -= f;+ }+}++static void nlsf2a(int16_t *a, const int16_t *nlsf, int d)+{+ static const uint8_t ORD16[16] = {0, 15, 8, 7, 4, 11, 12, 3,+ 2, 13, 10, 5, 6, 9, 14, 1};+ static const uint8_t ORD10[10] = {0, 9, 6, 3, 4, 5, 8, 1, 2, 7};+ const uint8_t *ord = d == 16 ? ORD16 : ORD10;+ int32_t cosv[MAX_ORDER] = {0}, P[MAX_ORDER / 2 + 1], Q[MAX_ORDER / 2 + 1];+ int32_t a32[MAX_ORDER];+ for (int k = 0; k < d; k++) {+ int32_t fi = nlsf[k] >> 8, ff = nlsf[k] - (fi << 8);+ int32_t cv = SILK_LSF_COS_Q12[fi],+ delta = SILK_LSF_COS_Q12[fi + 1] - cv;+ cosv[ord[k]] = rshift_round(LSH(cv, 8) + delta * ff, 20 - 16);+ }+ int dd = d >> 1;+ find_poly(P, &cosv[0], dd);+ find_poly(Q, &cosv[1], dd);+ for (int k = 0; k < dd; k++) {+ int32_t pt = P[k + 1] + P[k], qt = Q[k + 1] - Q[k];+ a32[k] = -qt - pt;+ a32[d - k - 1] = qt - pt;+ }+ lpc_fit(a, a32, 12, 17, d);+ for (int i = 0; inv_pred_gain(a, d) == 0 && i < 16; i++) {+ bwexpander32(a32, d, 65536 - (2 << i));+ for (int k = 0; k < d; k++)+ a[k] = (int16_t)rshift_round(a32[k], 17 - 12);+ }+}++static void decode_pitch(int lag_index, int contour, int *lags, int fs_khz,+ int nb)+{+ const int8_t *cb;+ int size;+ if (fs_khz == 8) {+ cb = nb == 4 ? SILK_CB_LAGS_STAGE2 : SILK_CB_LAGS_STAGE2_10_MS;+ size = nb == 4 ? 11 : 3;+ } else {+ cb = nb == 4 ? SILK_CB_LAGS_STAGE3 : SILK_CB_LAGS_STAGE3_10_MS;+ size = nb == 4 ? 34 : 12;+ }+ int min_lag = 2 * fs_khz, max_lag = 18 * fs_khz, lag = min_lag + lag_index;+ for (int k = 0; k < nb; k++)+ lags[k] = limit(lag + cb[k * size + contour], min_lag, max_lag);+}++static void decode_parameters(struct chan *c, struct ctrl *ct, int cond)+{+ int16_t nlsf[MAX_ORDER], nlsf0[MAX_ORDER];+ gains_dequant(ct->gains, c->ind.gains, &c->last_gain,+ cond == CODE_CONDITIONALLY, c->nb_subfr);+ nlsf_decode(nlsf, c->ind.nlsf, c->cb);+ nlsf2a(ct->pred[1], nlsf, c->order);+ if (c->first_after_reset == 1)+ c->ind.nlsf_interp = 4;+ if (c->ind.nlsf_interp < 4) {+ for (int i = 0; i < c->order; i++)+ nlsf0[i] =+ (int16_t)(c->prev_nlsf[i] ++ ((c->ind.nlsf_interp * (nlsf[i] - c->prev_nlsf[i])) >>+ 2));+ nlsf2a(ct->pred[0], nlsf0, c->order);+ } else {+ memcpy(ct->pred[0], ct->pred[1], sizeof(int16_t) * (size_t)c->order);+ }+ memcpy(c->prev_nlsf, nlsf, sizeof(int16_t) * (size_t)c->order);+ if (c->loss_cnt) {+ bwexpander16(ct->pred[0], c->order, 63570);+ bwexpander16(ct->pred[1], c->order, 63570);+ }+ if (c->ind.signal_type == TYPE_VOICED) {+ decode_pitch(c->ind.lag, c->ind.contour, ct->pitch_l, c->fs_khz,+ c->nb_subfr);+ const int8_t *cbk = c->ind.per == 0 ? SILK_LTP_GAIN_VQ_0+ : c->ind.per == 1 ? SILK_LTP_GAIN_VQ_1+ : SILK_LTP_GAIN_VQ_2;+ for (int k = 0; k < c->nb_subfr; k++)+ for (int i = 0; i < LTP_ORDER; i++)+ ct->ltp[k * LTP_ORDER + i] =+ (int16_t)(cbk[c->ind.ltp[k] * LTP_ORDER + i] * 128);+ static const int16_t SCALES[3] = {15565, 12288, 8192};+ ct->ltp_scale = SCALES[c->ind.ltp_scale];+ } else {+ memset(ct->pitch_l, 0, sizeof ct->pitch_l);+ memset(ct->ltp, 0, sizeof ct->ltp);+ c->ind.per = 0;+ ct->ltp_scale = 0;+ }+}++/* ---- synthesis ---- */++static void lpc_analysis(int16_t *out, const int16_t *in, const int16_t *B,+ int len, int d)+{+ for (int ix = d; ix < len; ix++) {+ const int16_t *p = &in[ix - 1];+ int32_t o = smulbb(p[0], B[0]);+ for (int j = 1; j < d; j++)+ o = smlabb(o, p[-j], B[j]);+ o = (int32_t)(((uint32_t)p[1] << 12) - (uint32_t)o);+ out[ix] = (int16_t)sat16(rshift_round(o, 12));+ }+ memset(out, 0, sizeof(int16_t) * (size_t)d);+}++static void decode_core(struct chan *c, struct ctrl *ct, int16_t *xq,+ const int16_t *pulses)+{+ static const int16_t OFFSETS[2][2] = {{100, 240}, {32, 100}};+ int16_t sLTP[MAX_FRAME + 2 * MAX_SUBFR], A_tmp[MAX_ORDER];+ int32_t sLTP_Q15[2 * MAX_FRAME + 2 * MAX_SUBFR], res[MAX_SUBFR];+ int32_t sLPC[MAX_SUBFR + MAX_ORDER];+ int lag = 0;+ int32_t offset = OFFSETS[c->ind.signal_type >> 1][c->ind.quant_offset];+ int interp_flag = c->ind.nlsf_interp < 4;+ int32_t seed = c->ind.seed;+ for (int i = 0; i < c->frame_length; i++) {+ seed = silk_rand(seed);+ c->exc[i] = LSH(pulses[i], 14);+ if (c->exc[i] > 0)+ c->exc[i] -= 80 << 4;+ else if (c->exc[i] < 0)+ c->exc[i] += 80 << 4;+ c->exc[i] += offset << 4;+ if (seed < 0)+ c->exc[i] = -c->exc[i];+ seed = (int32_t)((uint32_t)seed + (uint32_t)pulses[i]);+ }+ memcpy(sLPC, c->slpc, sizeof c->slpc);+ int32_t *pexc = c->exc;+ int16_t *pxq = xq;+ int buf_idx = c->ltp_mem;+ for (int k = 0; k < c->nb_subfr; k++) {+ int32_t *pres = res;+ int16_t *A = ct->pred[k >> 1];+ memcpy(A_tmp, A, sizeof(int16_t) * (size_t)c->order);+ int16_t *B = &ct->ltp[k * LTP_ORDER];+ int type = c->ind.signal_type;+ int32_t gain_q10 = ct->gains[k] >> 6;+ int32_t inv_gain = inverse32_varq(ct->gains[k], 47), gain_adj;+ if (ct->gains[k] != c->prev_gain) {+ gain_adj = div32_varq(c->prev_gain, ct->gains[k], 16);+ for (int i = 0; i < MAX_ORDER; i++)+ sLPC[i] = smulww(gain_adj, sLPC[i]);+ } else {+ gain_adj = 1 << 16;+ }+ c->prev_gain = ct->gains[k];+ if (c->loss_cnt && c->prev_type == TYPE_VOICED &&+ c->ind.signal_type != TYPE_VOICED && k < 2) {+ memset(B, 0, LTP_ORDER * sizeof(int16_t));+ B[LTP_ORDER / 2] = 4096;+ type = TYPE_VOICED;+ ct->pitch_l[k] = c->lag_prev;+ }+ if (type == TYPE_VOICED) {+ lag = ct->pitch_l[k];+ if (k == 0 || (k == 2 && interp_flag)) {+ int start = c->ltp_mem - lag - c->order - LTP_ORDER / 2;+ if (k == 2)+ memcpy(&c->out[c->ltp_mem], xq,+ 2 * (size_t)c->subfr_length * sizeof(int16_t));+ lpc_analysis(&sLTP[start], &c->out[start + k * c->subfr_length],+ A, c->ltp_mem - start, c->order);+ if (k == 0)+ inv_gain =+ (int32_t)((uint32_t)smulwb(inv_gain, ct->ltp_scale)+ << 2);+ for (int i = 0; i < lag + LTP_ORDER / 2; i++)+ sLTP_Q15[buf_idx - i - 1] =+ smulwb(inv_gain, sLTP[c->ltp_mem - i - 1]);+ } else if (gain_adj != 1 << 16) {+ for (int i = 0; i < lag + LTP_ORDER / 2; i++)+ sLTP_Q15[buf_idx - i - 1] =+ smulww(gain_adj, sLTP_Q15[buf_idx - i - 1]);+ }+ }+ if (type == TYPE_VOICED) {+ int32_t *pl = &sLTP_Q15[buf_idx - lag + LTP_ORDER / 2];+ for (int i = 0; i < c->subfr_length; i++) {+ int32_t p = 2;+ p = smlawb(p, pl[0], B[0]);+ p = smlawb(p, pl[-1], B[1]);+ p = smlawb(p, pl[-2], B[2]);+ p = smlawb(p, pl[-3], B[3]);+ p = smlawb(p, pl[-4], B[4]);+ pl++;+ pres[i] = (int32_t)((uint32_t)pexc[i] + ((uint32_t)p << 1));+ sLTP_Q15[buf_idx] = (int32_t)((uint32_t)pres[i] << 1);+ buf_idx++;+ }+ } else {+ pres = pexc;+ }+ for (int i = 0; i < c->subfr_length; i++) {+ int32_t p = c->order >> 1;+ for (int j = 0; j < c->order; j++)+ p = smlawb(p, sLPC[MAX_ORDER + i - j - 1], A_tmp[j]);+ sLPC[MAX_ORDER + i] = add_sat32(pres[i], lshift_sat32(p, 4));+ pxq[i] = (int16_t)sat16(+ rshift_round(smulww(sLPC[MAX_ORDER + i], gain_q10), 8));+ }+ memcpy(sLPC, &sLPC[c->subfr_length], MAX_ORDER * sizeof(int32_t));+ pexc += c->subfr_length;+ pxq += c->subfr_length;+ }+ memcpy(c->slpc, sLPC, sizeof c->slpc);+}++/* ---- concealment ---- */++static void plc_update(struct chan *c, const struct ctrl *ct)+{+ struct plc *p = &c->plc;+ int32_t gain = 0;+ c->prev_type = c->ind.signal_type;+ if (c->ind.signal_type == TYPE_VOICED) {+ for (int j = 0; j * c->subfr_length < ct->pitch_l[c->nb_subfr - 1];+ j++) {+ if (j == c->nb_subfr)+ break;+ int32_t t = 0;+ for (int i = 0; i < LTP_ORDER; i++)+ t += ct->ltp[(c->nb_subfr - 1 - j) * LTP_ORDER + i];+ if (t > gain) {+ gain = t;+ memcpy(p->ltp_coef, &ct->ltp[(c->nb_subfr - 1 - j) * LTP_ORDER],+ sizeof p->ltp_coef);+ p->pitch_l_q8 =+ (int32_t)((uint32_t)ct->pitch_l[c->nb_subfr - 1 - j] << 8);+ }+ }+ memset(p->ltp_coef, 0, sizeof p->ltp_coef);+ p->ltp_coef[LTP_ORDER / 2] = (int16_t)gain;+ if (gain < 11469) {+ int32_t scale = (11469 << 10) / (gain > 1 ? gain : 1);+ for (int i = 0; i < LTP_ORDER; i++)+ p->ltp_coef[i] = (int16_t)(smulbb(p->ltp_coef[i], scale) >> 10);+ } else if (gain > 15565) {+ int32_t scale = (15565 << 14) / (gain > 1 ? gain : 1);+ for (int i = 0; i < LTP_ORDER; i++)+ p->ltp_coef[i] = (int16_t)(smulbb(p->ltp_coef[i], scale) >> 14);+ }+ } else {+ p->pitch_l_q8 = (int32_t)((uint32_t)smulbb(c->fs_khz, 18) << 8);+ memset(p->ltp_coef, 0, sizeof p->ltp_coef);+ }+ memcpy(p->prev_lpc, ct->pred[1], sizeof(int16_t) * (size_t)c->order);+ p->prev_ltp_scale = (int16_t)ct->ltp_scale;+ memcpy(p->prev_gain, &ct->gains[c->nb_subfr - 2], 2 * sizeof(int32_t));+ p->subfr_length = c->subfr_length;+ p->nb_subfr = c->nb_subfr;+}++static void plc_conceal(struct chan *c, struct ctrl *ct, int16_t *frame)+{+ static const int16_t HARM_ATT[2] = {32440, 31130};+ static const int16_t RAND_ATT_V[2] = {31130, 26214};+ static const int16_t RAND_ATT_UV[2] = {32440, 29491};+ struct plc *p = &c->plc;+ int32_t sLTP_Q14[2 * MAX_FRAME + 2 * MAX_SUBFR];+ int16_t sLTP[MAX_FRAME + 2 * MAX_SUBFR], A[MAX_ORDER], ebuf[2 * MAX_SUBFR];+ int32_t prev_q10[2] = {p->prev_gain[0] >> 6, p->prev_gain[1] >> 6};+ if (c->first_after_reset)+ memset(p->prev_lpc, 0, sizeof p->prev_lpc);+ for (int k = 0; k < 2; k++)+ for (int i = 0; i < c->subfr_length; i++)+ ebuf[k * c->subfr_length + i] = (int16_t)sat16(+ smulww(c->exc[i + (k + c->nb_subfr - 2) * c->subfr_length],+ prev_q10[k]) >>+ 8);+ int32_t e1, e2;+ int s1, s2;+ sum_sqr_shift(&e1, &s1, ebuf, c->subfr_length);+ sum_sqr_shift(&e2, &s2, &ebuf[c->subfr_length], c->subfr_length);+ int32_t *rand_ptr;+ if ((e1 >> s2) < (e2 >> s1)) {+ int at = (p->nb_subfr - 1) * p->subfr_length - 128;+ rand_ptr = &c->exc[at > 0 ? at : 0];+ } else {+ int at = p->nb_subfr * p->subfr_length - 128;+ rand_ptr = &c->exc[at > 0 ? at : 0];+ }+ int16_t *B = p->ltp_coef;+ int16_t rand_scale = p->rand_scale;+ int la = c->loss_cnt < 1 ? c->loss_cnt : 1;+ int32_t harm_gain = HARM_ATT[la];+ int32_t rand_gain =+ c->prev_type == TYPE_VOICED ? RAND_ATT_V[la] : RAND_ATT_UV[la];+ bwexpander16(p->prev_lpc, c->order, 64880);+ memcpy(A, p->prev_lpc, sizeof(int16_t) * (size_t)c->order);+ if (c->loss_cnt == 0) {+ rand_scale = 1 << 14;+ if (c->prev_type == TYPE_VOICED) {+ for (int i = 0; i < LTP_ORDER; i++)+ rand_scale = (int16_t)(rand_scale - B[i]);+ rand_scale = rand_scale > 3277 ? rand_scale : 3277;+ rand_scale = (int16_t)(smulbb(rand_scale, p->prev_ltp_scale) >> 14);+ } else {+ int32_t ig = inv_pred_gain(p->prev_lpc, c->order);+ int32_t ds = (1 << 30) >> 3;+ ds = ds < ig ? ds : ig;+ int32_t lo = (1 << 30) >> 8;+ ds = ds > lo ? ds : lo;+ ds = (int32_t)((uint32_t)ds << 3);+ rand_gain = smulwb(ds, rand_gain) >> 14;+ }+ }+ int32_t seed = p->seed;+ int lag = rshift_round(p->pitch_l_q8, 8), buf_idx = c->ltp_mem;+ int idx = c->ltp_mem - lag - c->order - LTP_ORDER / 2;+ lpc_analysis(&sLTP[idx], &c->out[idx], A, c->ltp_mem - idx, c->order);+ int32_t inv_gain = inverse32_varq(p->prev_gain[1], 46);+ if (inv_gain > INT32_MAX >> 1)+ inv_gain = INT32_MAX >> 1;+ for (int i = idx + c->order; i < c->ltp_mem; i++)+ sLTP_Q14[i] = smulwb(inv_gain, sLTP[i]);+ for (int k = 0; k < c->nb_subfr; k++) {+ int32_t *pl = &sLTP_Q14[buf_idx - lag + LTP_ORDER / 2];+ for (int i = 0; i < c->subfr_length; i++) {+ int32_t pr = 2;+ pr = smlawb(pr, pl[0], B[0]);+ pr = smlawb(pr, pl[-1], B[1]);+ pr = smlawb(pr, pl[-2], B[2]);+ pr = smlawb(pr, pl[-3], B[3]);+ pr = smlawb(pr, pl[-4], B[4]);+ pl++;+ seed = silk_rand(seed);+ int ri = (seed >> 25) & 127;+ sLTP_Q14[buf_idx] =+ (int32_t)((uint32_t)smlawb(pr, rand_ptr[ri], rand_scale) << 2);+ buf_idx++;+ }+ for (int j = 0; j < LTP_ORDER; j++)+ B[j] = (int16_t)(smulbb(harm_gain, B[j]) >> 15);+ rand_scale = (int16_t)(smulbb(rand_scale, rand_gain) >> 15);+ p->pitch_l_q8 = smlawb(p->pitch_l_q8, p->pitch_l_q8, 655);+ int32_t mx = (int32_t)((uint32_t)smulbb(18, c->fs_khz) << 8);+ p->pitch_l_q8 = p->pitch_l_q8 < mx ? p->pitch_l_q8 : mx;+ lag = rshift_round(p->pitch_l_q8, 8);+ }+ int32_t *sl = &sLTP_Q14[c->ltp_mem - MAX_ORDER];+ memcpy(sl, c->slpc, sizeof c->slpc);+ for (int i = 0; i < c->frame_length; i++) {+ int32_t pr = c->order >> 1;+ for (int j = 0; j < c->order; j++)+ pr = smlawb(pr, sl[MAX_ORDER + i - j - 1], A[j]);+ sl[MAX_ORDER + i] = add_sat32(sl[MAX_ORDER + i], lshift_sat32(pr, 4));+ frame[i] = (int16_t)sat16(+ rshift_round(smulww(sl[MAX_ORDER + i], prev_q10[1]), 8));+ }+ memcpy(c->slpc, &sl[c->frame_length], sizeof c->slpc);+ p->seed = seed;+ p->rand_scale = rand_scale;+ for (int i = 0; i < 4; i++)+ ct->pitch_l[i] = lag;+}++static void plc_glue(struct chan *c, int16_t *frame, int length)+{+ struct plc *p = &c->plc;+ if (c->loss_cnt) {+ sum_sqr_shift(&p->conc_energy, &p->conc_energy_shift, frame, length);+ p->last_frame_lost = 1;+ return;+ }+ if (p->last_frame_lost) {+ int32_t energy;+ int shift;+ sum_sqr_shift(&energy, &shift, frame, length);+ if (shift > p->conc_energy_shift)+ p->conc_energy >>= shift - p->conc_energy_shift;+ else if (shift < p->conc_energy_shift)+ energy >>= p->conc_energy_shift - shift;+ if (energy > p->conc_energy) {+ int32_t lz = clz32(p->conc_energy) - 1;+ p->conc_energy = (int32_t)((uint32_t)p->conc_energy << lz);+ energy >>= 24 - lz > 0 ? 24 - lz : 0;+ int32_t frac = p->conc_energy / (energy > 1 ? energy : 1);+ int32_t gain = (int32_t)((uint32_t)sqrt_approx(frac) << 4);+ int32_t slope = ((1 << 16) - gain) / length;+ slope = (int32_t)((uint32_t)slope << 2);+ for (int i = 0; i < length; i++) {+ frame[i] = (int16_t)smulwb(gain, frame[i]);+ gain += slope;+ if (gain > 1 << 16)+ break;+ }+ }+ }+ p->last_frame_lost = 0;+}++static void cng(struct chan *c, const struct ctrl *ct, int16_t *frame,+ int length)+{+ struct cng *g = &c->cng;+ if (c->fs_khz != g->fs_khz) {+ cng_reset(c);+ g->fs_khz = c->fs_khz;+ }+ if (c->loss_cnt == 0 && c->prev_type == TYPE_NO_VOICE) {+ for (int i = 0; i < c->order; i++)+ g->smth_nlsf[i] =+ (int16_t)(g->smth_nlsf[i] ++ smulwb((int32_t)c->prev_nlsf[i] - g->smth_nlsf[i],+ 16348));+ int32_t max_gain = 0;+ int subfr = 0;+ for (int i = 0; i < c->nb_subfr; i++)+ if (ct->gains[i] > max_gain) {+ max_gain = ct->gains[i];+ subfr = i;+ }+ memmove(&g->exc_buf[c->subfr_length], g->exc_buf,+ (size_t)((c->nb_subfr - 1) * c->subfr_length) *+ sizeof(int32_t));+ memcpy(g->exc_buf, &c->exc[subfr * c->subfr_length],+ (size_t)c->subfr_length * sizeof(int32_t));+ for (int i = 0; i < c->nb_subfr; i++) {+ g->smth_gain += smulwb(ct->gains[i] - g->smth_gain, 4634);+ if (smulww(g->smth_gain, 46396) > ct->gains[i])+ g->smth_gain = ct->gains[i];+ }+ }+ if (c->loss_cnt) {+ int32_t sig[MAX_FRAME + MAX_ORDER];+ int16_t A[MAX_ORDER];+ int32_t gain = smulww(c->plc.rand_scale, c->plc.prev_gain[1]);+ if (gain >= 1 << 21 || g->smth_gain > 1 << 23) {+ gain = smultt(gain, gain);+ gain = smultt(g->smth_gain, g->smth_gain) -+ (int32_t)((uint32_t)gain << 5);+ gain = (int32_t)((uint32_t)sqrt_approx(gain) << 16);+ } else {+ gain = smulww(gain, gain);+ gain = smulww(g->smth_gain, g->smth_gain) -+ (int32_t)((uint32_t)gain << 5);+ gain = (int32_t)((uint32_t)sqrt_approx(gain) << 8);+ }+ int32_t gain_q10 = gain >> 6;+ int mask = 255;+ while (mask > length)+ mask >>= 1;+ int32_t seed = g->seed;+ for (int i = 0; i < length; i++) {+ seed = silk_rand(seed);+ sig[MAX_ORDER + i] = g->exc_buf[(seed >> 24) & mask];+ }+ g->seed = seed;+ nlsf2a(A, g->smth_nlsf, c->order);+ memcpy(sig, g->synth, sizeof g->synth);+ for (int i = 0; i < length; i++) {+ int32_t pr = c->order >> 1;+ for (int j = 0; j < c->order; j++)+ pr = smlawb(pr, sig[MAX_ORDER + i - j - 1], A[j]);+ sig[MAX_ORDER + i] =+ add_sat32(sig[MAX_ORDER + i], lshift_sat32(pr, 4));+ frame[i] = (int16_t)sat16(+ frame[i] ++ sat16(rshift_round(smulww(sig[MAX_ORDER + i], gain_q10), 8)));+ }+ memcpy(g->synth, &sig[length], sizeof g->synth);+ } else {+ memset(g->synth, 0, (size_t)c->order * sizeof(int32_t));+ }+}++/* ---- frames ---- */++static void decode_frame(struct chan *c, struct rc *r, int16_t *out, int lost,+ int cond)+{+ struct ctrl ct = {.ltp_scale = 0};+ int L = c->frame_length;+ if (!lost) {+ int16_t pulses[MAX_FRAME + 16];+ decode_indices(c, r, c->frames_decoded, 0, cond);+ decode_pulses(r, pulses, c->ind.signal_type, c->ind.quant_offset, L);+ decode_parameters(c, &ct, cond);+ decode_core(c, &ct, out, pulses);+ int mv = c->ltp_mem - L;+ memmove(c->out, &c->out[L], (size_t)mv * sizeof(int16_t));+ memcpy(&c->out[mv], out, (size_t)L * sizeof(int16_t));+ if (c->fs_khz != c->plc.fs_khz) {+ plc_reset(c);+ c->plc.fs_khz = c->fs_khz;+ }+ plc_update(c, &ct);+ c->loss_cnt = 0;+ c->prev_type = c->ind.signal_type;+ c->first_after_reset = 0;+ } else {+ if (c->fs_khz != c->plc.fs_khz) {+ plc_reset(c);+ c->plc.fs_khz = c->fs_khz;+ }+ plc_conceal(c, &ct, out);+ c->loss_cnt++;+ int mv = c->ltp_mem - L;+ memmove(c->out, &c->out[L], (size_t)mv * sizeof(int16_t));+ memcpy(&c->out[mv], out, (size_t)L * sizeof(int16_t));+ }+ cng(c, &ct, out, L);+ plc_glue(c, out, L);+ c->lag_prev = ct.pitch_l[c->nb_subfr - 1];+}++struct silk_dec *jsilk_new(void)+{+ struct silk_dec *d = calloc(1, sizeof *d);+ if (d) {+ d->api_hz = 48000;+ jsilk_reset(d);+ }+ return d;+}++void jsilk_set_rate(struct silk_dec *d, int hz)+{+ d->api_hz = hz;+}++void jsilk_free(struct silk_dec *d)+{+ free(d);+}++void jsilk_reset(struct silk_dec *d)+{+ for (int n = 0; n < 2; n++)+ chan_reset(&d->ch[n]);+ memset(d->pred_prev, 0, sizeof d->pred_prev);+ memset(d->smid, 0, sizeof d->smid);+ memset(d->sside, 0, sizeof d->sside);+ d->prev_mid_only = 0;+}++static void stereo_pred(struct rc *r, int32_t *pred)+{+ int ix[2][3];+ int n = rc_icdf(r, SILK_STEREO_PRED_JOINT_ICDF, 8);+ ix[0][2] = n / 5;+ ix[1][2] = n - 5 * ix[0][2];+ for (n = 0; n < 2; n++) {+ ix[n][0] = rc_icdf(r, SILK_UNIFORM3_ICDF, 8);+ ix[n][1] = rc_icdf(r, SILK_UNIFORM5_ICDF, 8);+ }+ for (n = 0; n < 2; n++) {+ ix[n][0] += 3 * ix[n][2];+ int32_t low = SILK_STEREO_PRED_QUANT_Q13[ix[n][0]];+ int32_t step =+ smulwb(SILK_STEREO_PRED_QUANT_Q13[ix[n][0] + 1] - low, 6554);+ pred[n] = smlabb(low, step, 2 * ix[n][1] + 1);+ }+ pred[0] -= pred[1];+}++static void ms_to_lr(struct silk_dec *d, int16_t *x1, int16_t *x2,+ const int32_t *pred, int fs_khz, int len)+{+ memcpy(x1, d->smid, 2 * sizeof(int16_t));+ memcpy(x2, d->sside, 2 * sizeof(int16_t));+ memcpy(d->smid, &x1[len], 2 * sizeof(int16_t));+ memcpy(d->sside, &x2[len], 2 * sizeof(int16_t));+ int32_t p0 = d->pred_prev[0], p1 = d->pred_prev[1];+ int32_t denom = (1 << 16) / (8 * fs_khz);+ int32_t d0 = rshift_round(smulbb(pred[0] - d->pred_prev[0], denom), 16);+ int32_t d1 = rshift_round(smulbb(pred[1] - d->pred_prev[1], denom), 16);+ int n;+ for (n = 0; n < 8 * fs_khz; n++) {+ p0 += d0;+ p1 += d1;+ int32_t sum =+ (int32_t)((uint32_t)(x1[n] + (int32_t)x1[n + 2] + LSH(x1[n + 1], 1))+ << 9);+ sum = smlawb(LSH(x2[n + 1], 8), sum, p0);+ sum = smlawb(sum, LSH(x1[n + 1], 11), p1);+ x2[n + 1] = (int16_t)sat16(rshift_round(sum, 8));+ }+ p0 = pred[0];+ p1 = pred[1];+ for (; n < len; n++) {+ int32_t sum =+ (int32_t)((uint32_t)(x1[n] + (int32_t)x1[n + 2] + LSH(x1[n + 1], 1))+ << 9);+ sum = smlawb(LSH(x2[n + 1], 8), sum, p0);+ sum = smlawb(sum, LSH(x1[n + 1], 11), p1);+ x2[n + 1] = (int16_t)sat16(rshift_round(sum, 8));+ }+ d->pred_prev[0] = (int16_t)pred[0];+ d->pred_prev[1] = (int16_t)pred[1];+ for (n = 0; n < len; n++) {+ int32_t s = x1[n + 1] + (int32_t)x2[n + 1];+ int32_t df = x1[n + 1] - (int32_t)x2[n + 1];+ x1[n + 1] = (int16_t)sat16(s);+ x2[n + 1] = (int16_t)sat16(df);+ }+}++int jsilk_frame(struct silk_dec *d, struct rc *r, int ms, int khz,+ int stream_ch, int out_ch, float **out, int first)+{+ int lost = r == NULL, mid_only = 0;+ int32_t pred[2] = {0, 0};+ struct chan *ch = d->ch;+ if (lost) {+ khz = d->last_khz;+ stream_ch = d->last_ch;+ } else {+ d->last_khz = khz;+ d->last_ch = stream_ch;+ }+ if (!khz || !stream_ch)+ return -1;+ if (first)+ for (int n = 0; n < stream_ch; n++)+ ch[n].frames_decoded = 0;+ if (stream_ch > d->int_ch)+ chan_reset(&ch[1]);+ int stereo_to_mono =+ stream_ch == 1 && d->int_ch == 2 && khz * 1000 == 1000 * ch[0].fs_khz;+ if (ch[0].frames_decoded == 0)+ for (int n = 0; n < stream_ch; n++) {+ if (ms == 10) {+ ch[n].frames_per_packet = 1;+ ch[n].nb_subfr = 2;+ } else if (ms == 20) {+ ch[n].frames_per_packet = 1;+ ch[n].nb_subfr = 4;+ } else if (ms == 40 || ms == 60) {+ ch[n].frames_per_packet = ms / 20;+ ch[n].nb_subfr = 4;+ } else {+ return -1;+ }+ set_fs(&ch[n], khz, d->api_hz);+ }+ if (out_ch == 2 && stream_ch == 2 && (d->api_ch == 1 || d->int_ch == 1)) {+ memset(d->pred_prev, 0, sizeof d->pred_prev);+ memset(d->sside, 0, sizeof d->sside);+ ch[1].rs = ch[0].rs;+ }+ d->api_ch = out_ch;+ d->int_ch = stream_ch;+ if (!lost && ch[0].frames_decoded == 0) {+ for (int n = 0; n < stream_ch; n++) {+ for (int i = 0; i < ch[n].frames_per_packet; i++)+ ch[n].vad[i] = rc_bit_logp(r, 1);+ ch[n].lbrr = rc_bit_logp(r, 1);+ }+ for (int n = 0; n < stream_ch; n++) {+ memset(ch[n].lbrr_flags, 0, sizeof ch[n].lbrr_flags);+ if (ch[n].lbrr) {+ if (ch[n].frames_per_packet == 1) {+ ch[n].lbrr_flags[0] = 1;+ } else {+ int sym = rc_icdf(r,+ ch[n].frames_per_packet == 2+ ? SILK_LBRR_FLAGS_2_ICDF+ : SILK_LBRR_FLAGS_3_ICDF,+ 8) ++ 1;+ for (int i = 0; i < ch[n].frames_per_packet; i++)+ ch[n].lbrr_flags[i] = (sym >> i) & 1;+ }+ }+ }+ /* the redundant (LBRR) frames: read past */+ for (int i = 0; i < ch[0].frames_per_packet; i++)+ for (int n = 0; n < stream_ch; n++)+ if (ch[n].lbrr_flags[i]) {+ int16_t pulses[MAX_FRAME + 16];+ if (stream_ch == 2 && n == 0) {+ stereo_pred(r, pred);+ if (ch[1].lbrr_flags[i] == 0)+ mid_only =+ rc_icdf(r, SILK_STEREO_ONLY_CODE_MID_ICDF, 8);+ }+ int cond = i > 0 && ch[n].lbrr_flags[i - 1]+ ? CODE_CONDITIONALLY+ : CODE_INDEPENDENTLY;+ decode_indices(&ch[n], r, i, 1, cond);+ decode_pulses(r, pulses, ch[n].ind.signal_type,+ ch[n].ind.quant_offset, ch[n].frame_length);+ }+ }+ if (stream_ch == 2) {+ if (!lost) {+ stereo_pred(r, pred);+ if (ch[1].vad[ch[0].frames_decoded] == 0)+ mid_only = rc_icdf(r, SILK_STEREO_ONLY_CODE_MID_ICDF, 8);+ else+ mid_only = 0;+ } else {+ pred[0] = d->pred_prev[0];+ pred[1] = d->pred_prev[1];+ }+ }+ if (stream_ch == 2 && mid_only == 0 && d->prev_mid_only == 1) {+ memset(ch[1].out, 0, sizeof ch[1].out);+ memset(ch[1].slpc, 0, sizeof ch[1].slpc);+ ch[1].lag_prev = 100;+ ch[1].last_gain = 10;+ ch[1].prev_type = TYPE_NO_VOICE;+ ch[1].first_after_reset = 1;+ }+ int16_t buf[2][MAX_FRAME + 2];+ int has_side = lost ? !d->prev_mid_only : !mid_only;+ int n_dec = ch[0].frame_length;+ for (int n = 0; n < stream_ch; n++) {+ if (n == 0 || has_side) {+ int fi = ch[0].frames_decoded - n, cond;+ if (fi <= 0)+ cond = CODE_INDEPENDENTLY;+ else if (n > 0 && d->prev_mid_only)+ cond = CODE_INDEPENDENTLY_NO_LTP_SCALING;+ else+ cond = CODE_CONDITIONALLY;+ decode_frame(&ch[n], r, &buf[n][2], lost, cond);+ n_dec = ch[n].frame_length;+ } else {+ memset(&buf[n][2], 0, (size_t)n_dec * sizeof(int16_t));+ }+ ch[n].frames_decoded++;+ }+ if (out_ch == 2 && stream_ch == 2) {+ ms_to_lr(d, buf[0], buf[1], pred, ch[0].fs_khz, n_dec);+ } else {+ memcpy(buf[0], d->smid, 2 * sizeof(int16_t));+ memcpy(d->smid, &buf[0][n_dec], 2 * sizeof(int16_t));+ }+ int n_out = n_dec * (d->api_hz / 1000) / ch[0].fs_khz;+ int16_t res[2880];+ int nch = out_ch < stream_ch ? out_ch : stream_ch;+ for (int n = 0; n < nch; n++) {+ resample(&ch[n].rs, res, &buf[n][1], n_dec);+ for (int i = 0; i < n_out; i++)+ out[n][i] = (1.f / 32768) * res[i];+ }+ if (out_ch == 2 && stream_ch == 1) {+ if (stereo_to_mono) {+ resample(&ch[1].rs, res, &buf[0][1], n_dec);+ for (int i = 0; i < n_out; i++)+ out[1][i] = (1.f / 32768) * res[i];+ } else {+ memcpy(out[1], out[0], sizeof(float) * (size_t)n_out);+ }+ }+ if (lost)+ for (int i = 0; i < d->int_ch; i++)+ ch[i].last_gain = 10;+ else+ d->prev_mid_only = mid_only;+ return n_out;+}diff --git a/src/common/audio_opus_tab.h b/src/common/audio_opus_tab.hnew file mode 100644index 0000000..d45a181--- /dev/null+++ b/src/common/audio_opus_tab.h@@ -0,0 +1,597 @@+/* SPDX-License-Identifier: GPL-3.0-or-later */+/* audio_opus_tab.h - the data tables of Opus (RFC 6716, whose reference+ decoder is the specification), generated outside the repository from+ libopus 1.5.2's sources and checked by decoding against it. */+/* Generated: no clang-format. */+#ifndef JANAS_COMMON_AUDIO_OPUS_TAB_H+#define JANAS_COMMON_AUDIO_OPUS_TAB_H+#include <stdint.h>+/* ---- CELT ---- */+static const int16_t CELT_EBANDS[22] = {+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28,+ 34, 40, 48, 60, 78, 100,+};+static const uint8_t CELT_ALLOC[231] = {+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,+ 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0,+ 110, 100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0,+ 118, 110, 103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0,+ 126, 119, 112, 104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0,+ 134, 127, 120, 114, 103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1,+ 144, 137, 130, 124, 113, 107, 101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1,+ 152, 145, 138, 132, 123, 117, 111, 105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1,+ 162, 155, 148, 142, 133, 127, 121, 115, 108, 102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1,+ 172, 165, 158, 152, 143, 137, 131, 125, 118, 112, 106, 100, 94, 87, 81, 75, 69, 63, 56, 45, 20,+ 200, 200, 200, 200, 200, 200, 200, 200, 198, 193, 188, 183, 178, 173, 168, 163, 158, 153, 148, 129, 104,+};+static const float CELT_WINDOW[120] = {+ 6.7286966e-05, 0.00060551348, 0.001681597, 0.0032947962, 0.0054439943, 0.0081276923, 0.011344001, 0.015090633,+ 0.019364886, 0.024163635, 0.029483315, 0.035319905, 0.041668911, 0.048525347, 0.055883718, 0.063737999,+ 0.072081616, 0.080907428, 0.090207705, 0.099974111, 0.11019769, 0.12086883, 0.13197729, 0.14351214,+ 0.15546177, 0.16781389, 0.1805555, 0.1936729, 0.20715171, 0.22097682, 0.23513243, 0.24960208,+ 0.2643686, 0.27941419, 0.2947204, 0.31026818, 0.32603788, 0.34200931, 0.35816177, 0.37447407,+ 0.39092462, 0.40749142, 0.42415215, 0.44088423, 0.45766484, 0.47447104, 0.49127978, 0.50806798,+ 0.52481261, 0.54149077, 0.55807973, 0.57455701, 0.59090049, 0.60708841, 0.62309951, 0.63891306,+ 0.65450896, 0.66986776, 0.68497077, 0.6998001, 0.71433873, 0.72857055, 0.74248043, 0.75605424,+ 0.76927895, 0.78214257, 0.7946343, 0.80674445, 0.81846456, 0.82978733, 0.84070669, 0.85121779,+ 0.86131698, 0.87100183, 0.88027111, 0.88912479, 0.89756398, 0.90559094, 0.91320904, 0.9204227,+ 0.92723738, 0.93365955, 0.93969656, 0.94535671, 0.95064907, 0.95558353, 0.96017067, 0.96442171,+ 0.96834849, 0.97196334, 0.97527906, 0.97830883, 0.98106616, 0.9835648, 0.98581869, 0.98784191,+ 0.98964856, 0.99125274, 0.99266849, 0.99390969, 0.99499004, 0.99592297, 0.99672162, 0.99739874,+ 0.99796667, 0.99843728, 0.99882195, 0.99913147, 0.99937606, 0.99956527, 0.99970802, 0.99981248,+ 0.99988613, 0.99993565, 0.99996697, 0.99998518, 0.99999457, 0.99999859, 0.99999982, 1.0,+};+static const int16_t CELT_LOGN[21] = {+ 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 16, 16, 16, 21,+ 21, 24, 29, 34, 36,+};+static const int16_t CELT_CACHE_INDEX[105] = {+ -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 41, 41, 41, 82, 82, 123, 164, 200, 222,+ 0, 0, 0, 0, 0, 0, 0, 0, 41, 41, 41, 41, 123, 123, 123, 164, 164, 240, 266, 283, 295,+ 41, 41, 41, 41, 41, 41, 41, 41, 123, 123, 123, 123, 240, 240, 240, 266, 266, 305, 318, 328, 336,+ 123, 123, 123, 123, 123, 123, 123, 123, 240, 240, 240, 240, 305, 305, 305, 318, 318, 343, 351, 358, 364,+ 240, 240, 240, 240, 240, 240, 240, 240, 305, 305, 305, 305, 343, 343, 343, 351, 351, 370, 376, 382, 387,+};+static const uint8_t CELT_CACHE_BITS[392] = {+ 40, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28, 31, 34, 36,+ 38, 39, 41, 42, 43, 44, 45, 46, 47, 47, 49, 50, 51, 52, 53, 54,+ 55, 55, 57, 58, 59, 60, 61, 62, 63, 63, 65, 66, 67, 68, 69, 70,+ 71, 71, 40, 20, 33, 41, 48, 53, 57, 61, 64, 66, 69, 71, 73, 75,+ 76, 78, 80, 82, 85, 87, 89, 91, 92, 94, 96, 98, 101, 103, 105, 107,+ 108, 110, 112, 114, 117, 119, 121, 123, 124, 126, 128, 40, 23, 39, 51, 60,+ 67, 73, 79, 83, 87, 91, 94, 97, 100, 102, 105, 107, 111, 115, 118, 121,+ 124, 126, 129, 131, 135, 139, 142, 145, 148, 150, 153, 155, 159, 163, 166, 169,+ 172, 174, 177, 179, 35, 28, 49, 65, 78, 89, 99, 107, 114, 120, 126, 132,+ 136, 141, 145, 149, 153, 159, 165, 171, 176, 180, 185, 189, 192, 199, 205, 211,+ 216, 220, 225, 229, 232, 239, 245, 251, 21, 33, 58, 79, 97, 112, 125, 137,+ 148, 157, 166, 174, 182, 189, 195, 201, 207, 217, 227, 235, 243, 251, 17, 35,+ 63, 86, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250,+ 25, 31, 55, 75, 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180, 185,+ 190, 200, 208, 215, 222, 229, 235, 240, 245, 255, 16, 36, 65, 89, 110, 128,+ 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250, 11, 41, 74, 103, 128,+ 151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138, 163, 186, 207, 227,+ 246, 12, 39, 71, 99, 123, 144, 164, 182, 198, 214, 228, 241, 253, 9, 44,+ 81, 113, 142, 168, 192, 214, 235, 255, 7, 49, 90, 127, 160, 191, 220, 247,+ 6, 51, 95, 134, 170, 203, 234, 7, 47, 87, 123, 155, 184, 212, 237, 6,+ 52, 97, 137, 174, 208, 240, 5, 57, 106, 151, 192, 231, 5, 59, 111, 158,+ 202, 243, 5, 55, 103, 147, 187, 224, 5, 60, 113, 161, 206, 248, 4, 65,+ 122, 175, 224, 4, 67, 127, 182, 234,+};+static const uint8_t CELT_CACHE_CAPS[168] = {+ 224, 224, 224, 224, 224, 224, 224, 224, 160, 160, 160, 160, 185, 185, 185, 178, 178, 168, 134, 61, 37,+ 224, 224, 224, 224, 224, 224, 224, 224, 240, 240, 240, 240, 207, 207, 207, 198, 198, 183, 144, 66, 40,+ 160, 160, 160, 160, 160, 160, 160, 160, 185, 185, 185, 185, 193, 193, 193, 183, 183, 172, 138, 64, 38,+ 240, 240, 240, 240, 240, 240, 240, 240, 207, 207, 207, 207, 204, 204, 204, 193, 193, 180, 143, 66, 40,+ 185, 185, 185, 185, 185, 185, 185, 185, 193, 193, 193, 193, 193, 193, 193, 183, 183, 172, 138, 65, 39,+ 207, 207, 207, 207, 207, 207, 207, 207, 204, 204, 204, 204, 201, 201, 201, 188, 188, 176, 141, 66, 40,+ 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 194, 194, 194, 184, 184, 173, 139, 65, 39,+ 204, 204, 204, 204, 204, 204, 204, 204, 201, 201, 201, 201, 198, 198, 198, 187, 187, 175, 140, 66, 40,+};+static const float CELT_EMEANS[25] = {+ 6.4375, 6.25, 5.75, 5.3125, 5.0625,+ 4.8125, 4.5, 4.375, 4.875, 4.6875,+ 4.5625, 4.4375, 4.875, 4.625, 4.3125,+ 4.5, 4.375, 4.625, 4.75, 4.4375,+ 3.75, 3.75, 3.75, 3.75, 3.75,+};+static const uint8_t CELT_EPROB[336] = {+ 72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128,+ 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40,+ 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11,+ 24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132,+ 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66,+ 91, 67, 100, 59, 108, 50, 120, 40, 122, 37, 97, 43, 78, 50,+ 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74,+ 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18,+ 146, 19, 162, 12, 165, 10, 178, 7, 189, 6, 190, 8, 177, 9,+ 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91,+ 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60,+ 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, 45,+ 61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38,+ 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16,+ 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10,+ 21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73,+ 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55,+ 112, 56, 129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, 42,+ 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36,+ 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25,+ 154, 26, 166, 21, 173, 16, 184, 13, 184, 10, 150, 13, 139, 15,+ 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72,+ 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52,+ 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40,+};+static const int8_t CELT_TF_SELECT[32] = {+ 0, -1, 0, -1, 0, -1, 0, -1,+ 0, -1, 0, -2, 1, 0, 1, -1,+ 0, -2, 0, -3, 2, 0, 1, -1,+ 0, -2, 0, -3, 3, 0, 1, -1,+};+static const int16_t CELT_ORDERY[30] = {+ 1, 0, 3, 0, 2, 1, 7, 0, 4, 3, 6, 1, 5, 2, 15, 0,+ 8, 7, 12, 3, 11, 4, 14, 1, 9, 6, 13, 2, 10, 5,+};+/* ---- SILK ---- */+static const uint8_t SILK_TYPE_OFFSET_VAD_ICDF[4] = {+ 232, 158, 10, 0,+};+static const uint8_t SILK_TYPE_OFFSET_NO_VAD_ICDF[2] = {+ 230, 0,+};+static const uint8_t SILK_UNIFORM3_ICDF[3] = {+ 171, 85, 0,+};+static const uint8_t SILK_UNIFORM4_ICDF[4] = {+ 192, 128, 64, 0,+};+static const uint8_t SILK_UNIFORM5_ICDF[5] = {+ 205, 154, 102, 51, 0,+};+static const uint8_t SILK_UNIFORM6_ICDF[6] = {+ 213, 171, 128, 85, 43, 0,+};+static const uint8_t SILK_UNIFORM8_ICDF[8] = {+ 224, 192, 160, 128, 96, 64, 32, 0,+};+static const uint8_t SILK_NLSF_EXT_ICDF[7] = {+ 100, 40, 16, 7, 3, 1, 0,+};+static const uint8_t SILK_NLSF_INTERPOLATION_FACTOR_ICDF[5] = {+ 243, 221, 192, 181, 0,+};+static const uint8_t SILK_LTPSCALE_ICDF[3] = {+ 128, 64, 0,+};+static const uint8_t SILK_LSB_ICDF[2] = {+ 120, 0,+};+static const uint8_t SILK_LBRR_FLAGS_2_ICDF[3] = {+ 203, 150, 0,+};+static const uint8_t SILK_LBRR_FLAGS_3_ICDF[7] = {+ 215, 195, 166, 125, 110, 82, 0,+};+static const uint8_t SILK_STEREO_PRED_JOINT_ICDF[25] = {+ 249, 247, 246, 245, 244, 234, 210, 202, 201, 200, 197, 174, 82, 59, 56, 55,+ 54, 46, 22, 12, 11, 10, 9, 7, 0,+};+static const uint8_t SILK_STEREO_ONLY_CODE_MID_ICDF[2] = {+ 64, 0,+};+static const int16_t SILK_STEREO_PRED_QUANT_Q13[16] = {+ -13732, -10050, -8266, -7526, -6500, -5000, -2950, -820, 820, 2950, 5000, 6500, 7526, 8266, 10050, 13732,+};+static const uint8_t SILK_GAIN_ICDF[24] = {+ 224, 112, 44, 15, 3, 2, 1, 0, 254, 237, 192, 132, 70, 23, 4, 0,+ 255, 252, 226, 155, 61, 11, 2, 0,+};+static const uint8_t SILK_DELTA_GAIN_ICDF[41] = {+ 250, 245, 234, 203, 71, 50, 42, 38, 35, 33, 31, 29, 28, 27, 26, 25,+ 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9,+ 8, 7, 6, 5, 4, 3, 2, 1, 0,+};+static const uint8_t SILK_PITCH_LAG_ICDF[32] = {+ 253, 250, 244, 233, 212, 182, 150, 131, 120, 110, 98, 85, 72, 60, 49, 40,+ 32, 25, 19, 15, 13, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,+};+static const uint8_t SILK_PITCH_DELTA_ICDF[21] = {+ 210, 208, 206, 203, 199, 193, 183, 168, 142, 104, 74, 52, 37, 27, 20, 14,+ 10, 6, 4, 2, 0,+};+static const uint8_t SILK_PITCH_CONTOUR_ICDF[34] = {+ 223, 201, 183, 167, 152, 138, 124, 111, 98, 88, 79, 70, 62, 56, 50, 44,+ 39, 35, 31, 27, 24, 21, 18, 16, 14, 12, 10, 8, 6, 4, 3, 2,+ 1, 0,+};+static const uint8_t SILK_PITCH_CONTOUR_NB_ICDF[11] = {+ 188, 176, 155, 138, 119, 97, 67, 43, 26, 10, 0,+};+static const uint8_t SILK_PITCH_CONTOUR_10_MS_ICDF[12] = {+ 165, 119, 80, 61, 47, 35, 27, 20, 14, 9, 4, 0,+};+static const uint8_t SILK_PITCH_CONTOUR_10_MS_NB_ICDF[3] = {+ 113, 63, 0,+};+static const uint8_t SILK_LTP_PER_INDEX_ICDF[3] = {+ 179, 99, 0,+};+static const uint8_t SILK_LTP_GAIN_ICDF_0[8] = {+ 71, 56, 43, 30, 21, 12, 6, 0,+};+static const uint8_t SILK_LTP_GAIN_ICDF_1[16] = {+ 199, 165, 144, 124, 109, 96, 84, 71, 61, 51, 42, 32, 23, 15, 8, 0,+};+static const uint8_t SILK_LTP_GAIN_ICDF_2[32] = {+ 241, 225, 211, 199, 187, 175, 164, 153, 142, 132, 123, 114, 105, 96, 88, 80,+ 72, 64, 57, 50, 44, 38, 33, 29, 24, 20, 16, 12, 9, 5, 2, 0,+};+static const int8_t SILK_LTP_GAIN_VQ_0[40] = {+ 4, 6, 24, 7, 5,+ 0, 0, 2, 0, 0,+ 12, 28, 41, 13, -4,+ -9, 15, 42, 25, 14,+ 1, -2, 62, 41, -9,+ -10, 37, 65, -4, 3,+ -6, 4, 66, 7, -8,+ 16, 14, 38, -3, 33,+};+static const int8_t SILK_LTP_GAIN_VQ_1[80] = {+ 13, 22, 39, 23, 12,+ -1, 36, 64, 27, -6,+ -7, 10, 55, 43, 17,+ 1, 1, 8, 1, 1,+ 6, -11, 74, 53, -9,+ -12, 55, 76, -12, 8,+ -3, 3, 93, 27, -4,+ 26, 39, 59, 3, -8,+ 2, 0, 77, 11, 9,+ -8, 22, 44, -6, 7,+ 40, 9, 26, 3, 9,+ -7, 20, 101, -7, 4,+ 3, -8, 42, 26, 0,+ -15, 33, 68, 2, 23,+ -2, 55, 46, -2, 15,+ 3, -1, 21, 16, 41,+};+static const int8_t SILK_LTP_GAIN_VQ_2[160] = {+ -6, 27, 61, 39, 5,+ -11, 42, 88, 4, 1,+ -2, 60, 65, 6, -4,+ -1, -5, 73, 56, 1,+ -9, 19, 94, 29, -9,+ 0, 12, 99, 6, 4,+ 8, -19, 102, 46, -13,+ 3, 2, 13, 3, 2,+ 9, -21, 84, 72, -18,+ -11, 46, 104, -22, 8,+ 18, 38, 48, 23, 0,+ -16, 70, 83, -21, 11,+ 5, -11, 117, 22, -8,+ -6, 23, 117, -12, 3,+ 3, -8, 95, 28, 4,+ -10, 15, 77, 60, -15,+ -1, 4, 124, 2, -4,+ 3, 38, 84, 24, -25,+ 2, 13, 42, 13, 31,+ 21, -4, 56, 46, -1,+ -1, 35, 79, -13, 19,+ -7, 65, 88, -9, -14,+ 20, 4, 81, 49, -29,+ 20, 0, 75, 3, -17,+ 5, -9, 44, 92, -8,+ 1, -3, 22, 69, 31,+ -6, 95, 41, -12, 5,+ 39, 67, 16, -4, 1,+ 0, -6, 120, 55, -36,+ -13, 44, 122, 4, -24,+ 81, 5, 11, 3, 7,+ 2, 0, 9, 10, 88,+};+static const uint8_t SILK_RATE_LEVELS_ICDF[18] = {+ 241, 190, 178, 132, 87, 74, 41, 14, 0, 223, 193, 157, 140, 106, 57, 39,+ 18, 0,+};+static const uint8_t SILK_PULSES_PER_BLOCK_ICDF[180] = {+ 125, 51, 26, 18, 15, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2,+ 1, 0, 198, 105, 45, 22, 15, 12, 11, 10, 9, 8, 7, 6, 5, 4,+ 3, 2, 1, 0, 213, 162, 116, 83, 59, 43, 32, 24, 18, 15, 12, 9,+ 7, 6, 5, 3, 2, 0, 239, 187, 116, 59, 28, 16, 11, 10, 9, 8,+ 7, 6, 5, 4, 3, 2, 1, 0, 250, 229, 188, 135, 86, 51, 30, 19,+ 13, 10, 8, 6, 5, 4, 3, 2, 1, 0, 249, 235, 213, 185, 156, 128,+ 103, 83, 66, 53, 42, 33, 26, 21, 17, 13, 10, 0, 254, 249, 235, 206,+ 164, 118, 77, 46, 27, 16, 10, 7, 5, 4, 3, 2, 1, 0, 255, 253,+ 249, 239, 220, 191, 156, 119, 85, 57, 37, 23, 15, 10, 6, 4, 2, 0,+ 255, 253, 251, 246, 237, 223, 203, 179, 152, 124, 98, 75, 55, 40, 29, 21,+ 15, 0, 255, 254, 253, 247, 220, 162, 106, 67, 42, 28, 18, 12, 9, 6,+ 4, 3, 2, 0,+};+static const uint8_t SILK_SHELL_CODE_TABLE0[152] = {+ 128, 0, 214, 42, 0, 235, 128, 21, 0, 244, 184, 72, 11, 0, 248, 214,+ 128, 42, 7, 0, 248, 225, 170, 80, 25, 5, 0, 251, 236, 198, 126, 54,+ 18, 3, 0, 250, 238, 211, 159, 82, 35, 15, 5, 0, 250, 231, 203, 168,+ 128, 88, 53, 25, 6, 0, 252, 238, 216, 185, 148, 108, 71, 40, 18, 4,+ 0, 253, 243, 225, 199, 166, 128, 90, 57, 31, 13, 3, 0, 254, 246, 233,+ 212, 183, 147, 109, 73, 44, 23, 10, 2, 0, 255, 250, 240, 223, 198, 166,+ 128, 90, 58, 33, 16, 6, 1, 0, 255, 251, 244, 231, 210, 181, 146, 110,+ 75, 46, 25, 12, 5, 1, 0, 255, 253, 248, 238, 221, 196, 164, 128, 92,+ 60, 35, 18, 8, 3, 1, 0, 255, 253, 249, 242, 229, 208, 180, 146, 110,+ 76, 48, 27, 14, 7, 3, 1, 0,+};+static const uint8_t SILK_SHELL_CODE_TABLE1[152] = {+ 129, 0, 207, 50, 0, 236, 129, 20, 0, 245, 185, 72, 10, 0, 249, 213,+ 129, 42, 6, 0, 250, 226, 169, 87, 27, 4, 0, 251, 233, 194, 130, 62,+ 20, 4, 0, 250, 236, 207, 160, 99, 47, 17, 3, 0, 255, 240, 217, 182,+ 131, 81, 41, 11, 1, 0, 255, 254, 233, 201, 159, 107, 61, 20, 2, 1,+ 0, 255, 249, 233, 206, 170, 128, 86, 50, 23, 7, 1, 0, 255, 250, 238,+ 217, 186, 148, 108, 70, 39, 18, 6, 1, 0, 255, 252, 243, 226, 200, 166,+ 128, 90, 56, 30, 13, 4, 1, 0, 255, 252, 245, 231, 209, 180, 146, 110,+ 76, 47, 25, 11, 4, 1, 0, 255, 253, 248, 237, 219, 194, 163, 128, 93,+ 62, 37, 19, 8, 3, 1, 0, 255, 254, 250, 241, 226, 205, 177, 145, 111,+ 79, 51, 30, 15, 6, 2, 1, 0,+};+static const uint8_t SILK_SHELL_CODE_TABLE2[152] = {+ 129, 0, 203, 54, 0, 234, 129, 23, 0, 245, 184, 73, 10, 0, 250, 215,+ 129, 41, 5, 0, 252, 232, 173, 86, 24, 3, 0, 253, 240, 200, 129, 56,+ 15, 2, 0, 253, 244, 217, 164, 94, 38, 10, 1, 0, 253, 245, 226, 189,+ 132, 71, 27, 7, 1, 0, 253, 246, 231, 203, 159, 105, 56, 23, 6, 1,+ 0, 255, 248, 235, 213, 179, 133, 85, 47, 19, 5, 1, 0, 255, 254, 243,+ 221, 194, 159, 117, 70, 37, 12, 2, 1, 0, 255, 254, 248, 234, 208, 171,+ 128, 85, 48, 22, 8, 2, 1, 0, 255, 254, 250, 240, 220, 189, 149, 107,+ 67, 36, 16, 6, 2, 1, 0, 255, 254, 251, 243, 227, 201, 166, 128, 90,+ 55, 29, 13, 5, 2, 1, 0, 255, 254, 252, 246, 234, 213, 183, 147, 109,+ 73, 43, 22, 10, 4, 2, 1, 0,+};+static const uint8_t SILK_SHELL_CODE_TABLE3[152] = {+ 130, 0, 200, 58, 0, 231, 130, 26, 0, 244, 184, 76, 12, 0, 249, 214,+ 130, 43, 6, 0, 252, 232, 173, 87, 24, 3, 0, 253, 241, 203, 131, 56,+ 14, 2, 0, 254, 246, 221, 167, 94, 35, 8, 1, 0, 254, 249, 232, 193,+ 130, 65, 23, 5, 1, 0, 255, 251, 239, 211, 162, 99, 45, 15, 4, 1,+ 0, 255, 251, 243, 223, 186, 131, 74, 33, 11, 3, 1, 0, 255, 252, 245,+ 230, 202, 158, 105, 57, 24, 8, 2, 1, 0, 255, 253, 247, 235, 214, 179,+ 132, 84, 44, 19, 7, 2, 1, 0, 255, 254, 250, 240, 223, 196, 159, 112,+ 69, 36, 15, 6, 2, 1, 0, 255, 254, 253, 245, 231, 209, 176, 136, 93,+ 55, 27, 11, 3, 2, 1, 0, 255, 254, 253, 252, 239, 221, 194, 158, 117,+ 76, 42, 18, 4, 3, 2, 1, 0,+};+static const uint8_t SILK_SHELL_CODE_TABLE_OFFSETS[17] = {+ 0, 0, 2, 5, 9, 14, 20, 27, 35, 44, 54, 65, 77, 90, 104, 119,+ 135,+};+static const uint8_t SILK_SIGN_ICDF[42] = {+ 254, 49, 67, 77, 82, 93, 99, 198, 11, 18, 24, 31, 36, 45, 255, 46,+ 66, 78, 87, 94, 104, 208, 14, 21, 32, 42, 51, 66, 255, 94, 104, 109,+ 112, 115, 118, 248, 53, 69, 80, 88, 95, 102,+};+static const int8_t SILK_CB_LAGS_STAGE2[44] = {+ 0, 2, -1, -1, -1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0,+ 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,+ 0, 0, -1, 2, 1, 0, 1, 1, 0, 0, -1, -1,+};+static const int8_t SILK_CB_LAGS_STAGE2_10_MS[6] = {+ 0, 1, 0, 0, 0, 1,+};+static const int8_t SILK_CB_LAGS_STAGE3[136] = {+ 0, 0, 1, -1, 0, 1, -1, 0, -1, 1, -2, 2, -2, -2, 2, -3,+ 2, 3, -3, -4, 3, -4, 4, 4, -5, 5, -6, -5, 6, -7, 6, 5,+ 8, -9, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0,+ 1, -1, 0, 1, -1, -1, 1, -1, 2, 1, -1, 2, -2, -2, 2, -2,+ 2, 2, 3, -3, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,+ 0, 1, -1, 1, 0, 0, 2, 1, -1, 2, -1, -1, 2, -1, 2, 2,+ -1, 3, -2, -2, -2, 3, 0, 1, 0, 0, 1, 0, 1, -1, 2, -1,+ 2, -1, 2, 3, -2, 3, -2, -2, 4, 4, -3, 5, -3, -4, 6, -4,+ 6, 5, -5, 8, -6, -5, -7, 9,+};+static const int8_t SILK_CB_LAGS_STAGE3_10_MS[24] = {+ 0, 0, 1, -1, 1, -1, 2, -2, 2, -2, 3, -3, 0, 1, 0, 1,+ -1, 2, -1, 2, -2, 3, -2, 3,+};+static const int16_t SILK_LSF_COS_Q12[129] = {+ 8192, 8190, 8182, 8170, 8152, 8130, 8104, 8072, 8034, 7994, 7946, 7896, 7840, 7778, 7714, 7644,+ 7568, 7490, 7406, 7318, 7226, 7128, 7026, 6922, 6812, 6698, 6580, 6458, 6332, 6204, 6070, 5934,+ 5792, 5648, 5502, 5352, 5198, 5040, 4880, 4718, 4552, 4382, 4212, 4038, 3862, 3684, 3502, 3320,+ 3136, 2948, 2760, 2570, 2378, 2186, 1990, 1794, 1598, 1400, 1202, 1002, 802, 602, 402, 202,+ 0, -202, -402, -602, -802, -1002, -1202, -1400, -1598, -1794, -1990, -2186, -2378, -2570, -2760, -2948,+ -3136, -3320, -3502, -3684, -3862, -4038, -4212, -4382, -4552, -4718, -4880, -5040, -5198, -5352, -5502, -5648,+ -5792, -5934, -6070, -6204, -6332, -6458, -6580, -6698, -6812, -6922, -7026, -7128, -7226, -7318, -7406, -7490,+ -7568, -7644, -7714, -7778, -7840, -7896, -7946, -7994, -8034, -8072, -8104, -8130, -8152, -8170, -8182, -8190,+ -8192,+};+static const int16_t SILK_FRAC_FIR_12[48] = {+ 189, -600, 617, 30567,+ 117, -159, -1070, 29704,+ 52, 221, -2392, 28276,+ -4, 529, -3350, 26341,+ -48, 758, -3956, 23973,+ -80, 905, -4235, 21254,+ -99, 972, -4222, 18278,+ -107, 967, -3957, 15143,+ -103, 896, -3487, 11950,+ -91, 773, -2865, 8798,+ -71, 611, -2143, 5784,+ -46, 425, -1375, 2996,+};+static const uint8_t SILK_NLSF_CB1_NB_MB_Q8[320] = {+ 12, 35, 60, 83, 108, 132, 157, 180, 206, 228, 15, 32, 55, 77, 101, 125,+ 151, 175, 201, 225, 19, 42, 66, 89, 114, 137, 162, 184, 209, 230, 12, 25,+ 50, 72, 97, 120, 147, 172, 200, 223, 26, 44, 69, 90, 114, 135, 159, 180,+ 205, 225, 13, 22, 53, 80, 106, 130, 156, 180, 205, 228, 15, 25, 44, 64,+ 90, 115, 142, 168, 196, 222, 19, 24, 62, 82, 100, 120, 145, 168, 190, 214,+ 22, 31, 50, 79, 103, 120, 151, 170, 203, 227, 21, 29, 45, 65, 106, 124,+ 150, 171, 196, 224, 30, 49, 75, 97, 121, 142, 165, 186, 209, 229, 19, 25,+ 52, 70, 93, 116, 143, 166, 192, 219, 26, 34, 62, 75, 97, 118, 145, 167,+ 194, 217, 25, 33, 56, 70, 91, 113, 143, 165, 196, 223, 21, 34, 51, 72,+ 97, 117, 145, 171, 196, 222, 20, 29, 50, 67, 90, 117, 144, 168, 197, 221,+ 22, 31, 48, 66, 95, 117, 146, 168, 196, 222, 24, 33, 51, 77, 116, 134,+ 158, 180, 200, 224, 21, 28, 70, 87, 106, 124, 149, 170, 194, 217, 26, 33,+ 53, 64, 83, 117, 152, 173, 204, 225, 27, 34, 65, 95, 108, 129, 155, 174,+ 210, 225, 20, 26, 72, 99, 113, 131, 154, 176, 200, 219, 34, 43, 61, 78,+ 93, 114, 155, 177, 205, 229, 23, 29, 54, 97, 124, 138, 163, 179, 209, 229,+ 30, 38, 56, 89, 118, 129, 158, 178, 200, 231, 21, 29, 49, 63, 85, 111,+ 142, 163, 193, 222, 27, 48, 77, 103, 133, 158, 179, 196, 215, 232, 29, 47,+ 74, 99, 124, 151, 176, 198, 220, 237, 33, 42, 61, 76, 93, 121, 155, 174,+ 207, 225, 29, 53, 87, 112, 136, 154, 170, 188, 208, 227, 24, 30, 52, 84,+ 131, 150, 166, 186, 203, 229, 37, 48, 64, 84, 104, 118, 156, 177, 201, 230,+};+static const int16_t SILK_NLSF_CB1_WGHT_Q9[320] = {+ 2897, 2314, 2314, 2314, 2287, 2287, 2314, 2300, 2327, 2287, 2888, 2580, 2394, 2367, 2314, 2274,+ 2274, 2274, 2274, 2194, 2487, 2340, 2340, 2314, 2314, 2314, 2340, 2340, 2367, 2354, 3216, 2766,+ 2340, 2340, 2314, 2274, 2221, 2207, 2261, 2194, 2460, 2474, 2367, 2394, 2394, 2394, 2394, 2367,+ 2407, 2314, 3479, 3056, 2127, 2207, 2274, 2274, 2274, 2287, 2314, 2261, 3282, 3141, 2580, 2394,+ 2247, 2221, 2207, 2194, 2194, 2114, 4096, 3845, 2221, 2620, 2620, 2407, 2314, 2394, 2367, 2074,+ 3178, 3244, 2367, 2221, 2553, 2434, 2340, 2314, 2167, 2221, 3338, 3488, 2726, 2194, 2261, 2460,+ 2354, 2367, 2207, 2101, 2354, 2420, 2327, 2367, 2394, 2420, 2420, 2420, 2460, 2367, 3779, 3629,+ 2434, 2527, 2367, 2274, 2274, 2300, 2207, 2048, 3254, 3225, 2713, 2846, 2447, 2327, 2300, 2300,+ 2274, 2127, 3263, 3300, 2753, 2806, 2447, 2261, 2261, 2247, 2127, 2101, 2873, 2981, 2633, 2367,+ 2407, 2354, 2194, 2247, 2247, 2114, 3225, 3197, 2633, 2580, 2274, 2181, 2247, 2221, 2221, 2141,+ 3178, 3310, 2740, 2407, 2274, 2274, 2274, 2287, 2194, 2114, 3141, 3272, 2460, 2061, 2287, 2500,+ 2367, 2487, 2434, 2181, 3507, 3282, 2314, 2700, 2647, 2474, 2367, 2394, 2340, 2127, 3423, 3535,+ 3038, 3056, 2300, 1950, 2221, 2274, 2274, 2274, 3404, 3366, 2087, 2687, 2873, 2354, 2420, 2274,+ 2474, 2540, 3760, 3488, 1950, 2660, 2897, 2527, 2394, 2367, 2460, 2261, 3028, 3272, 2740, 2888,+ 2740, 2154, 2127, 2287, 2234, 2247, 3695, 3657, 2025, 1969, 2660, 2700, 2580, 2500, 2327, 2367,+ 3207, 3413, 2354, 2074, 2888, 2888, 2340, 2487, 2247, 2167, 3338, 3366, 2846, 2780, 2327, 2154,+ 2274, 2287, 2114, 2061, 2327, 2300, 2181, 2167, 2181, 2367, 2633, 2700, 2700, 2553, 2407, 2434,+ 2221, 2261, 2221, 2221, 2340, 2420, 2607, 2700, 3038, 3244, 2806, 2888, 2474, 2074, 2300, 2314,+ 2354, 2380, 2221, 2154, 2127, 2287, 2500, 2793, 2793, 2620, 2580, 2367, 3676, 3713, 2234, 1838,+ 2181, 2753, 2726, 2673, 2513, 2207, 2793, 3160, 2726, 2553, 2846, 2513, 2181, 2394, 2221, 2181,+};+static const uint8_t SILK_NLSF_CB1_ICDF_NB_MB[64] = {+ 212, 178, 148, 129, 108, 96, 85, 82, 79, 77, 61, 59, 57, 56, 51, 49,+ 48, 45, 42, 41, 40, 38, 36, 34, 31, 30, 21, 12, 10, 3, 1, 0,+ 255, 245, 244, 236, 233, 225, 217, 203, 190, 176, 175, 161, 149, 136, 125, 114,+ 102, 91, 81, 71, 60, 52, 43, 35, 28, 20, 19, 18, 12, 11, 5, 0,+};+static const uint8_t SILK_NLSF_PRED_NB_MB_Q8[18] = {+ 179, 138, 140, 148, 151, 149, 153, 151, 163, 116, 67, 82, 59, 92, 72, 100,+ 89, 92,+};+static const uint8_t SILK_NLSF_CB2_SELECT_NB_MB[160] = {+ 16, 0, 0, 0, 0, 99, 66, 36, 36, 34, 36, 34, 34, 34, 34, 83,+ 69, 36, 52, 34, 116, 102, 70, 68, 68, 176, 102, 68, 68, 34, 65, 85,+ 68, 84, 36, 116, 141, 152, 139, 170, 132, 187, 184, 216, 137, 132, 249, 168,+ 185, 139, 104, 102, 100, 68, 68, 178, 218, 185, 185, 170, 244, 216, 187, 187,+ 170, 244, 187, 187, 219, 138, 103, 155, 184, 185, 137, 116, 183, 155, 152, 136,+ 132, 217, 184, 184, 170, 164, 217, 171, 155, 139, 244, 169, 184, 185, 170, 164,+ 216, 223, 218, 138, 214, 143, 188, 218, 168, 244, 141, 136, 155, 170, 168, 138,+ 220, 219, 139, 164, 219, 202, 216, 137, 168, 186, 246, 185, 139, 116, 185, 219,+ 185, 138, 100, 100, 134, 100, 102, 34, 68, 68, 100, 68, 168, 203, 221, 218,+ 168, 167, 154, 136, 104, 70, 164, 246, 171, 137, 139, 137, 155, 218, 219, 139,+};+static const uint8_t SILK_NLSF_CB2_ICDF_NB_MB[72] = {+ 255, 254, 253, 238, 14, 3, 2, 1, 0, 255, 254, 252, 218, 35, 3, 2,+ 1, 0, 255, 254, 250, 208, 59, 4, 2, 1, 0, 255, 254, 246, 194, 71,+ 10, 2, 1, 0, 255, 252, 236, 183, 82, 8, 2, 1, 0, 255, 252, 235,+ 180, 90, 17, 2, 1, 0, 255, 248, 224, 171, 97, 30, 4, 1, 0, 255,+ 254, 236, 173, 95, 37, 7, 1, 0,+};+static const int16_t SILK_NLSF_DELTA_MIN_NB_MB_Q15[11] = {+ 250, 3, 6, 3, 3, 3, 4, 3, 3, 3, 461,+};+static const uint8_t SILK_NLSF_CB1_WB_Q8[512] = {+ 7, 23, 38, 54, 69, 85, 100, 116, 131, 147, 162, 178, 193, 208, 223, 239,+ 13, 25, 41, 55, 69, 83, 98, 112, 127, 142, 157, 171, 187, 203, 220, 236,+ 15, 21, 34, 51, 61, 78, 92, 106, 126, 136, 152, 167, 185, 205, 225, 240,+ 10, 21, 36, 50, 63, 79, 95, 110, 126, 141, 157, 173, 189, 205, 221, 237,+ 17, 20, 37, 51, 59, 78, 89, 107, 123, 134, 150, 164, 184, 205, 224, 240,+ 10, 15, 32, 51, 67, 81, 96, 112, 129, 142, 158, 173, 189, 204, 220, 236,+ 8, 21, 37, 51, 65, 79, 98, 113, 126, 138, 155, 168, 179, 192, 209, 218,+ 12, 15, 34, 55, 63, 78, 87, 108, 118, 131, 148, 167, 185, 203, 219, 236,+ 16, 19, 32, 36, 56, 79, 91, 108, 118, 136, 154, 171, 186, 204, 220, 237,+ 11, 28, 43, 58, 74, 89, 105, 120, 135, 150, 165, 180, 196, 211, 226, 241,+ 6, 16, 33, 46, 60, 75, 92, 107, 123, 137, 156, 169, 185, 199, 214, 225,+ 11, 19, 30, 44, 57, 74, 89, 105, 121, 135, 152, 169, 186, 202, 218, 234,+ 12, 19, 29, 46, 57, 71, 88, 100, 120, 132, 148, 165, 182, 199, 216, 233,+ 17, 23, 35, 46, 56, 77, 92, 106, 123, 134, 152, 167, 185, 204, 222, 237,+ 14, 17, 45, 53, 63, 75, 89, 107, 115, 132, 151, 171, 188, 206, 221, 240,+ 9, 16, 29, 40, 56, 71, 88, 103, 119, 137, 154, 171, 189, 205, 222, 237,+ 16, 19, 36, 48, 57, 76, 87, 105, 118, 132, 150, 167, 185, 202, 218, 236,+ 12, 17, 29, 54, 71, 81, 94, 104, 126, 136, 149, 164, 182, 201, 221, 237,+ 15, 28, 47, 62, 79, 97, 115, 129, 142, 155, 168, 180, 194, 208, 223, 238,+ 8, 14, 30, 45, 62, 78, 94, 111, 127, 143, 159, 175, 192, 207, 223, 239,+ 17, 30, 49, 62, 79, 92, 107, 119, 132, 145, 160, 174, 190, 204, 220, 235,+ 14, 19, 36, 45, 61, 76, 91, 108, 121, 138, 154, 172, 189, 205, 222, 238,+ 12, 18, 31, 45, 60, 76, 91, 107, 123, 138, 154, 171, 187, 204, 221, 236,+ 13, 17, 31, 43, 53, 70, 83, 103, 114, 131, 149, 167, 185, 203, 220, 237,+ 17, 22, 35, 42, 58, 78, 93, 110, 125, 139, 155, 170, 188, 206, 224, 240,+ 8, 15, 34, 50, 67, 83, 99, 115, 131, 146, 162, 178, 193, 209, 224, 239,+ 13, 16, 41, 66, 73, 86, 95, 111, 128, 137, 150, 163, 183, 206, 225, 241,+ 17, 25, 37, 52, 63, 75, 92, 102, 119, 132, 144, 160, 175, 191, 212, 231,+ 19, 31, 49, 65, 83, 100, 117, 133, 147, 161, 174, 187, 200, 213, 227, 242,+ 18, 31, 52, 68, 88, 103, 117, 126, 138, 149, 163, 177, 192, 207, 223, 239,+ 16, 29, 47, 61, 76, 90, 106, 119, 133, 147, 161, 176, 193, 209, 224, 240,+ 15, 21, 35, 50, 61, 73, 86, 97, 110, 119, 129, 141, 175, 198, 218, 237,+};+static const int16_t SILK_NLSF_CB1_WB_WGHT_Q9[512] = {+ 3657, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2963, 2963, 2925, 2846,+ 3216, 3085, 2972, 3056, 3056, 3010, 3010, 3010, 2963, 2963, 3010, 2972, 2888, 2846, 2846, 2726,+ 3920, 4014, 2981, 3207, 3207, 2934, 3056, 2846, 3122, 3244, 2925, 2846, 2620, 2553, 2780, 2925,+ 3516, 3197, 3010, 3103, 3019, 2888, 2925, 2925, 2925, 2925, 2888, 2888, 2888, 2888, 2888, 2753,+ 5054, 5054, 2934, 3573, 3385, 3056, 3085, 2793, 3160, 3160, 2972, 2846, 2513, 2540, 2753, 2888,+ 4428, 4149, 2700, 2753, 2972, 3010, 2925, 2846, 2981, 3019, 2925, 2925, 2925, 2925, 2888, 2726,+ 3620, 3019, 2972, 3056, 3056, 2873, 2806, 3056, 3216, 3047, 2981, 3291, 3291, 2981, 3310, 2991,+ 5227, 5014, 2540, 3338, 3526, 3385, 3197, 3094, 3376, 2981, 2700, 2647, 2687, 2793, 2846, 2673,+ 5081, 5174, 4615, 4428, 2460, 2897, 3047, 3207, 3169, 2687, 2740, 2888, 2846, 2793, 2846, 2700,+ 3122, 2888, 2963, 2925, 2925, 2925, 2925, 2963, 2963, 2963, 2963, 2925, 2925, 2963, 2963, 2963,+ 4202, 3207, 2981, 3103, 3010, 2888, 2888, 2925, 2972, 2873, 2916, 3019, 2972, 3010, 3197, 2873,+ 3760, 3760, 3244, 3103, 2981, 2888, 2925, 2888, 2972, 2934, 2793, 2793, 2846, 2888, 2888, 2660,+ 3854, 4014, 3207, 3122, 3244, 2934, 3047, 2963, 2963, 3085, 2846, 2793, 2793, 2793, 2793, 2580,+ 3845, 4080, 3357, 3516, 3094, 2740, 3010, 2934, 3122, 3085, 2846, 2846, 2647, 2647, 2846, 2806,+ 5147, 4894, 3225, 3845, 3441, 3169, 2897, 3413, 3451, 2700, 2580, 2673, 2740, 2846, 2806, 2753,+ 4109, 3789, 3291, 3160, 2925, 2888, 2888, 2925, 2793, 2740, 2793, 2740, 2793, 2846, 2888, 2806,+ 5081, 5054, 3047, 3545, 3244, 3056, 3085, 2944, 3103, 2897, 2740, 2740, 2740, 2846, 2793, 2620,+ 4309, 4309, 2860, 2527, 3207, 3376, 3376, 3075, 3075, 3376, 3056, 2846, 2647, 2580, 2726, 2753,+ 3056, 2916, 2806, 2888, 2740, 2687, 2897, 3103, 3150, 3150, 3216, 3169, 3056, 3010, 2963, 2846,+ 4375, 3882, 2925, 2888, 2846, 2888, 2846, 2846, 2888, 2888, 2888, 2846, 2888, 2925, 2888, 2846,+ 2981, 2916, 2916, 2981, 2981, 3056, 3122, 3216, 3150, 3056, 3010, 2972, 2972, 2972, 2925, 2740,+ 4229, 4149, 3310, 3347, 2925, 2963, 2888, 2981, 2981, 2846, 2793, 2740, 2846, 2846, 2846, 2793,+ 4080, 4014, 3103, 3010, 2925, 2925, 2925, 2888, 2925, 2925, 2846, 2846, 2846, 2793, 2888, 2780,+ 4615, 4575, 3169, 3441, 3207, 2981, 2897, 3038, 3122, 2740, 2687, 2687, 2687, 2740, 2793, 2700,+ 4149, 4269, 3789, 3657, 2726, 2780, 2888, 2888, 3010, 2972, 2925, 2846, 2687, 2687, 2793, 2888,+ 4215, 3554, 2753, 2846, 2846, 2888, 2888, 2888, 2925, 2925, 2888, 2925, 2925, 2925, 2963, 2888,+ 5174, 4921, 2261, 3432, 3789, 3479, 3347, 2846, 3310, 3479, 3150, 2897, 2460, 2487, 2753, 2925,+ 3451, 3685, 3122, 3197, 3357, 3047, 3207, 3207, 2981, 3216, 3085, 2925, 2925, 2687, 2540, 2434,+ 2981, 3010, 2793, 2793, 2740, 2793, 2846, 2972, 3056, 3103, 3150, 3150, 3150, 3103, 3010, 3010,+ 2944, 2873, 2687, 2726, 2780, 3010, 3432, 3545, 3357, 3244, 3056, 3010, 2963, 2925, 2888, 2846,+ 3019, 2944, 2897, 3010, 3010, 2972, 3019, 3103, 3056, 3056, 3010, 2888, 2846, 2925, 2925, 2888,+ 3920, 3967, 3010, 3197, 3357, 3216, 3291, 3291, 3479, 3704, 3441, 2726, 2181, 2460, 2580, 2607,+};+static const uint8_t SILK_NLSF_CB1_ICDF_WB[64] = {+ 225, 204, 201, 184, 183, 175, 158, 154, 153, 135, 119, 115, 113, 110, 109, 99,+ 98, 95, 79, 68, 52, 50, 48, 45, 43, 32, 31, 27, 18, 10, 3, 0,+ 255, 251, 235, 230, 212, 201, 196, 182, 167, 166, 163, 151, 138, 124, 110, 104,+ 90, 78, 76, 70, 69, 57, 45, 34, 24, 21, 11, 6, 5, 4, 3, 0,+};+static const uint8_t SILK_NLSF_PRED_WB_Q8[30] = {+ 175, 148, 160, 176, 178, 173, 174, 164, 177, 174, 196, 182, 198, 192, 182, 68,+ 62, 66, 60, 72, 117, 85, 90, 118, 136, 151, 142, 160, 142, 155,+};+static const uint8_t SILK_NLSF_CB2_SELECT_WB[256] = {+ 0, 0, 0, 0, 0, 0, 0, 1, 100, 102, 102, 68, 68, 36, 34, 96,+ 164, 107, 158, 185, 180, 185, 139, 102, 64, 66, 36, 34, 34, 0, 1, 32,+ 208, 139, 141, 191, 152, 185, 155, 104, 96, 171, 104, 166, 102, 102, 102, 132,+ 1, 0, 0, 0, 0, 16, 16, 0, 80, 109, 78, 107, 185, 139, 103, 101,+ 208, 212, 141, 139, 173, 153, 123, 103, 36, 0, 0, 0, 0, 0, 0, 1,+ 48, 0, 0, 0, 0, 0, 0, 32, 68, 135, 123, 119, 119, 103, 69, 98,+ 68, 103, 120, 118, 118, 102, 71, 98, 134, 136, 157, 184, 182, 153, 139, 134,+ 208, 168, 248, 75, 189, 143, 121, 107, 32, 49, 34, 34, 34, 0, 17, 2,+ 210, 235, 139, 123, 185, 137, 105, 134, 98, 135, 104, 182, 100, 183, 171, 134,+ 100, 70, 68, 70, 66, 66, 34, 131, 64, 166, 102, 68, 36, 2, 1, 0,+ 134, 166, 102, 68, 34, 34, 66, 132, 212, 246, 158, 139, 107, 107, 87, 102,+ 100, 219, 125, 122, 137, 118, 103, 132, 114, 135, 137, 105, 171, 106, 50, 34,+ 164, 214, 141, 143, 185, 151, 121, 103, 192, 34, 0, 0, 0, 0, 0, 1,+ 208, 109, 74, 187, 134, 249, 159, 137, 102, 110, 154, 118, 87, 101, 119, 101,+ 0, 2, 0, 36, 36, 66, 68, 35, 96, 164, 102, 100, 36, 0, 2, 33,+ 167, 138, 174, 102, 100, 84, 2, 2, 100, 107, 120, 119, 36, 197, 24, 0,+};+static const uint8_t SILK_NLSF_CB2_ICDF_WB[72] = {+ 255, 254, 253, 244, 12, 3, 2, 1, 0, 255, 254, 252, 224, 38, 3, 2,+ 1, 0, 255, 254, 251, 209, 57, 4, 2, 1, 0, 255, 254, 244, 195, 69,+ 4, 2, 1, 0, 255, 251, 232, 184, 84, 7, 2, 1, 0, 255, 254, 240,+ 186, 86, 14, 2, 1, 0, 255, 254, 239, 178, 91, 30, 5, 1, 0, 255,+ 248, 227, 177, 100, 19, 2, 1, 0,+};+static const int16_t SILK_NLSF_DELTA_MIN_WB_Q15[17] = {+ 100, 3, 40, 3, 3, 3, 5, 14, 14, 10, 11, 3, 8, 9, 7, 3,+ 347,+};+#endifdiff --git a/src/common/audio_vorbis.c b/src/common/audio_vorbis.cnew file mode 100644index 0000000..37ae628--- /dev/null+++ b/src/common/audio_vorbis.c@@ -0,0 +1,1027 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_vorbis.c - Vorbis I (the Xiph.Org specification; audio_ogg.h): the+ * three headers - identification, comments (skipped), setup with its+ * codebooks (Huffman codes from their lengths, ordered or sparse, vector+ * lookups of type 1 and 2), floors (type 0, LSP, and type 1, line+ * segments), residues (types 0, 1 and 2), mappings with their channel+ * coupling and modes - then every audio packet: its mode and window, the+ * floor and residue of each channel, the inverse coupling, the floor+ * times the residue, the inverse MDCT (audio_fft.c), the window, the+ * overlap with the packet before; the start and the end trimmed by the+ * granule positions. Bits are read least significant first. The channels+ * are averaged.+ */+#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "audio_fft.h"+#include "audio_ogg.h"++/* ---- bits, least significant first ---- */++struct vbr {+ const uint8_t *p;+ size_t n, pos;+ int eop; /* read past the end of the packet */+};++static uint32_t vb(struct vbr *b, int k)+{+ if (!k)+ return 0;+ if (b->pos + (size_t)k > b->n * 8) {+ b->eop = 1;+ b->pos = b->n * 8;+ return 0;+ }+ uint64_t w = 0;+ size_t at = b->pos >> 3;+ for (int i = 7; i >= 0; i--)+ w = w << 8 | (at + (size_t)i < b->n ? b->p[at + (size_t)i] : 0);+ uint32_t v =+ (uint32_t)(w >> (b->pos & 7)) & (k == 32 ? 0xFFFFFFFFu : (1u << k) - 1);+ b->pos += (size_t)k;+ return v;+}++static int ilog(uint32_t v)+{+ int r = 0;+ while (v) {+ r++;+ v >>= 1;+ }+ return r;+}++static float f32_unpack(uint32_t x)+{+ double m = x & 0x1FFFFF;+ int e = (int)((x & 0x7FE00000u) >> 21);+ if (x & 0x80000000u)+ m = -m;+ return (float)ldexp(m, e - 788);+}++/* ---- codebooks ---- */++#define LUT 10++struct cb {+ int dim, entries, used;+ uint8_t *len;+ uint32_t *code; /* the codewords bit-reversed, as read */+ int *longs, n_longs; /* entries with codes longer than LUT bits */+ int16_t lut[1 << LUT];+ float *vq; /* entries x dim */+};++static void cb_free(struct cb *c)+{+ free(c->len);+ free(c->code);+ free(c->longs);+ free(c->vq);+}++static uint32_t rev(uint32_t v, int n)+{+ uint32_t r = 0;+ for (int i = 0; i < n; i++)+ r = r << 1 | (v >> i & 1);+ return r;+}++/* the codewords from the lengths, as the specification assigns them */+static int cb_codes(struct cb *c)+{+ uint32_t marker[33] = {0};+ c->code = calloc((size_t)c->entries, sizeof *c->code);+ c->longs = malloc(sizeof(int) * (size_t)c->entries);+ if (!c->code || !c->longs)+ return -1;+ for (int i = 0; i < 1 << LUT; i++)+ c->lut[i] = -1;+ int used = 0, only = -1;+ for (int i = 0; i < c->entries; i++)+ if (c->len[i]) {+ used++;+ only = i;+ }+ c->used = used;+ if (used == 1) { /* one entry: a code of one bit, 0 */+ c->len[only] = 1;+ c->code[only] = 0;+ for (int i = 0; i < 1 << LUT; i += 2)+ c->lut[i] = (int16_t)only;+ return 0;+ }+ for (int i = 0; i < c->entries; i++) {+ int l = c->len[i];+ if (!l)+ continue;+ uint32_t entry = marker[l];+ if (l < 32 && (entry >> l)) /* overspecified */+ return -1;+ c->code[i] = rev(entry, l);+ for (int j = l; j > 0; j--) {+ if (marker[j] & 1) {+ marker[j] = j == 1 ? marker[1] + 1 : marker[j - 1] << 1;+ break;+ }+ marker[j]++;+ }+ for (int j = l + 1; j < 33; j++) {+ if ((marker[j] >> 1) == entry) {+ entry = marker[j];+ marker[j] = marker[j - 1] << 1;+ } else {+ break;+ }+ }+ if (l <= LUT) {+ for (uint32_t hi = 0; hi < 1u << (LUT - l); hi++)+ c->lut[c->code[i] | hi << l] = (int16_t)i;+ } else {+ c->longs[c->n_longs++] = i;+ }+ }+ return 0;+}++static int cb_read(const struct cb *c, struct vbr *b)+{+ if (!c->used) {+ b->eop = 1;+ return -1;+ }+ uint32_t peek = 0;+ size_t left = b->n * 8 - b->pos;+ int k = left < 32 ? (int)left : 32;+ struct vbr t = *b;+ peek = vb(&t, k);+ int e = c->lut[peek & ((1u << LUT) - 1)];+ if (e >= 0) {+ if ((size_t)c->len[e] > left) {+ b->eop = 1;+ return -1;+ }+ b->pos += c->len[e];+ return e;+ }+ for (int i = 0; i < c->n_longs; i++) {+ int x = c->longs[i], l = c->len[x];+ if (l <= k &&+ (peek & (l == 32 ? 0xFFFFFFFFu : (1u << l) - 1)) == c->code[x]) {+ b->pos += (size_t)l;+ return x;+ }+ }+ b->eop = 1;+ return -1;+}++static long lookup1(int entries, int dim)+{+ long r = (long)floor(exp(log((double)entries) / dim));+ while (pow((double)r + 1, dim) <= entries)+ r++;+ while (r > 0 && pow((double)r, dim) > entries)+ r--;+ return r;+}++static int cb_setup(struct cb *c, struct vbr *b)+{+ if (vb(b, 24) != 0x564342)+ return -1;+ c->dim = (int)vb(b, 16);+ c->entries = (int)vb(b, 24);+ if (!c->dim || !c->entries || c->entries > (1 << 24))+ return -1;+ c->len = calloc((size_t)c->entries, 1);+ if (!c->len)+ return -1;+ if (vb(b, 1)) { /* ordered */+ int cur = 0, l = (int)vb(b, 5) + 1;+ while (cur < c->entries) {+ int num = (int)vb(b, ilog((uint32_t)(c->entries - cur)));+ if (cur + num > c->entries || l > 32)+ return -1;+ memset(c->len + cur, l, (size_t)num);+ cur += num;+ l++;+ if (b->eop)+ return -1;+ }+ } else {+ int sparse = (int)vb(b, 1);+ for (int i = 0; i < c->entries; i++)+ c->len[i] = !sparse || vb(b, 1) ? (uint8_t)(vb(b, 5) + 1) : 0;+ }+ if (b->eop || cb_codes(c))+ return -1;+ int type = (int)vb(b, 4);+ if (type == 0)+ return b->eop ? -1 : 0;+ if (type > 2)+ return -1;+ float min = f32_unpack(vb(b, 32)), delta = f32_unpack(vb(b, 32));+ int bits = (int)vb(b, 4) + 1, seq = (int)vb(b, 1);+ long nv =+ type == 1 ? lookup1(c->entries, c->dim) : (long)c->entries * c->dim;+ if (nv <= 0 || nv > (1 << 26))+ return -1;+ uint32_t *mul = malloc(sizeof(uint32_t) * (size_t)nv);+ c->vq = malloc(sizeof(float) * (size_t)c->entries * (size_t)c->dim);+ if (!mul || !c->vq) {+ free(mul);+ return -1;+ }+ for (long i = 0; i < nv; i++)+ mul[i] = vb(b, bits);+ for (int e = 0; e < c->entries; e++) {+ float last = 0;+ long div = 1;+ for (int i = 0; i < c->dim; i++) {+ long off = type == 1 ? (e / div) % nv : (long)e * c->dim + i;+ float v = (float)mul[off] * delta + min + last;+ if (seq)+ last = v;+ c->vq[(size_t)e * c->dim + i] = v;+ div *= nv;+ }+ }+ free(mul);+ return b->eop ? -1 : 0;+}++/* ---- the setup ---- */++struct floor_t {+ int type;+ /* type 0 */+ int order, rate, bark_size, amp_bits, amp_off, nbooks, books[16];+ /* type 1 */+ int parts, pclass[32], cdim[16], csub[16], cmaster[16], sbooks[16][8];+ int mult, values, x[65], sorted[65], low[65], high[65];+};++struct residue {+ int type, begin, end, psize, classes, classbook;+ int books[64][8];+};++struct mapping {+ int submaps, steps, mag[256], ang[256], mux[256];+ int sfloor[16], sres[16];+};++struct vorbis {+ int ch;+ uint32_t rate;+ int bs[2];+ int ncb, nfloor, nres, nmap, nmode;+ struct cb *cb;+ struct floor_t *floor;+ struct residue *res;+ struct mapping *map;+ int mode_block[64], mode_map[64];+ struct aimdct *mdct[2];+ float *win[2][2][2]; /* [blockflag][prev][next] */+};++static int floor_setup(struct vorbis *v, struct floor_t *f, struct vbr *b)+{+ f->type = (int)vb(b, 16);+ if (f->type == 0) {+ f->order = (int)vb(b, 8);+ f->rate = (int)vb(b, 16);+ f->bark_size = (int)vb(b, 16);+ f->amp_bits = (int)vb(b, 6);+ f->amp_off = (int)vb(b, 8);+ f->nbooks = (int)vb(b, 4) + 1;+ for (int i = 0; i < f->nbooks; i++)+ if ((f->books[i] = (int)vb(b, 8)) >= v->ncb)+ return -1;+ return f->order < 1 || !f->bark_size || !f->rate ? -1 : 0;+ }+ if (f->type != 1)+ return -1;+ f->parts = (int)vb(b, 5);+ int maxc = -1;+ for (int i = 0; i < f->parts; i++) {+ f->pclass[i] = (int)vb(b, 4);+ if (f->pclass[i] > maxc)+ maxc = f->pclass[i];+ }+ for (int c = 0; c <= maxc; c++) {+ f->cdim[c] = (int)vb(b, 3) + 1;+ f->csub[c] = (int)vb(b, 2);+ if (f->csub[c] && (f->cmaster[c] = (int)vb(b, 8)) >= v->ncb)+ return -1;+ for (int j = 0; j < 1 << f->csub[c]; j++)+ if ((f->sbooks[c][j] = (int)vb(b, 8) - 1) >= v->ncb)+ return -1;+ }+ f->mult = (int)vb(b, 2) + 1;+ int rb = (int)vb(b, 4);+ f->x[0] = 0;+ f->x[1] = 1 << rb;+ f->values = 2;+ for (int i = 0; i < f->parts; i++)+ for (int j = 0; j < f->cdim[f->pclass[i]]; j++) {+ if (f->values >= 65)+ return -1;+ f->x[f->values++] = (int)vb(b, rb);+ }+ /* the order of the X values, and each one's neighbours before it */+ for (int i = 0; i < f->values; i++)+ f->sorted[i] = i;+ for (int i = 1; i < f->values; i++)+ for (int j = i; j > 0 && f->x[f->sorted[j - 1]] > f->x[f->sorted[j]];+ j--) {+ int t = f->sorted[j];+ f->sorted[j] = f->sorted[j - 1];+ f->sorted[j - 1] = t;+ }+ for (int i = 2; i < f->values; i++) {+ int lo = 0, hi = 1;+ for (int j = 0; j < i; j++) {+ if (f->x[j] < f->x[i] && f->x[j] > f->x[lo])+ lo = j;+ if (f->x[j] > f->x[i] && f->x[j] < f->x[hi])+ hi = j;+ }+ f->low[i] = lo;+ f->high[i] = hi;+ }+ return b->eop ? -1 : 0;+}++static int residue_setup(struct vorbis *v, struct residue *r, struct vbr *b)+{+ r->type = (int)vb(b, 16);+ if (r->type > 2)+ return -1;+ r->begin = (int)vb(b, 24);+ r->end = (int)vb(b, 24);+ r->psize = (int)vb(b, 24) + 1;+ r->classes = (int)vb(b, 6) + 1;+ r->classbook = (int)vb(b, 8);+ if (r->classbook >= v->ncb)+ return -1;+ int cascade[64];+ for (int i = 0; i < r->classes; i++) {+ int low = (int)vb(b, 3), high = vb(b, 1) ? (int)vb(b, 5) : 0;+ cascade[i] = high * 8 + low;+ }+ for (int i = 0; i < r->classes; i++)+ for (int j = 0; j < 8; j++) {+ r->books[i][j] = -1;+ if (cascade[i] & (1 << j))+ if ((r->books[i][j] = (int)vb(b, 8)) >= v->ncb)+ return -1;+ }+ return b->eop ? -1 : 0;+}++static int mapping_setup(struct vorbis *v, struct mapping *m, struct vbr *b)+{+ if (vb(b, 16) != 0)+ return -1;+ m->submaps = vb(b, 1) ? (int)vb(b, 4) + 1 : 1;+ m->steps = 0;+ if (vb(b, 1)) {+ m->steps = (int)vb(b, 8) + 1;+ int bits = ilog((uint32_t)v->ch - 1);+ for (int i = 0; i < m->steps; i++) {+ m->mag[i] = (int)vb(b, bits);+ m->ang[i] = (int)vb(b, bits);+ if (m->mag[i] == m->ang[i] || m->mag[i] >= v->ch ||+ m->ang[i] >= v->ch)+ return -1;+ }+ }+ if (vb(b, 2))+ return -1;+ for (int c = 0; c < v->ch; c++) {+ m->mux[c] = m->submaps > 1 ? (int)vb(b, 4) : 0;+ if (m->mux[c] >= m->submaps)+ return -1;+ }+ for (int i = 0; i < m->submaps; i++) {+ vb(b, 8);+ m->sfloor[i] = (int)vb(b, 8);+ m->sres[i] = (int)vb(b, 8);+ if (m->sfloor[i] >= v->nfloor || m->sres[i] >= v->nres)+ return -1;+ }+ return b->eop ? -1 : 0;+}++static int setup(struct vorbis *v, const uint8_t *p, size_t n)+{+ struct vbr b = {.p = p, .n = n, .pos = 7 * 8};+ if (n < 7 || p[0] != 5 || memcmp(p + 1, "vorbis", 6))+ return -1;+ v->ncb = (int)vb(&b, 8) + 1;+ v->cb = calloc((size_t)v->ncb, sizeof *v->cb);+ if (!v->cb)+ return -1;+ for (int i = 0; i < v->ncb; i++)+ if (cb_setup(&v->cb[i], &b))+ return -1;+ int times = (int)vb(&b, 6) + 1;+ for (int i = 0; i < times; i++)+ if (vb(&b, 16))+ return -1;+ v->nfloor = (int)vb(&b, 6) + 1;+ v->floor = calloc((size_t)v->nfloor, sizeof *v->floor);+ if (!v->floor)+ return -1;+ for (int i = 0; i < v->nfloor; i++)+ if (floor_setup(v, &v->floor[i], &b))+ return -1;+ v->nres = (int)vb(&b, 6) + 1;+ v->res = calloc((size_t)v->nres, sizeof *v->res);+ if (!v->res)+ return -1;+ for (int i = 0; i < v->nres; i++)+ if (residue_setup(v, &v->res[i], &b))+ return -1;+ v->nmap = (int)vb(&b, 6) + 1;+ v->map = calloc((size_t)v->nmap, sizeof *v->map);+ if (!v->map)+ return -1;+ for (int i = 0; i < v->nmap; i++)+ if (mapping_setup(v, &v->map[i], &b))+ return -1;+ v->nmode = (int)vb(&b, 6) + 1;+ for (int i = 0; i < v->nmode; i++) {+ v->mode_block[i] = (int)vb(&b, 1);+ if (vb(&b, 16) || vb(&b, 16))+ return -1;+ v->mode_map[i] = (int)vb(&b, 8);+ if (v->mode_map[i] >= v->nmap)+ return -1;+ }+ return b.eop || !vb(&b, 1) ? -1 : 0;+}++/* ---- the floors ---- */++static float inv_db(int y)+{+ if (y < 0)+ y = 0;+ if (y > 255)+ y = 255;+ return (float)pow(10.0, (y - 255) * 7.0 / 256.0);+}++static int render_point(int x0, int y0, int x1, int y1, int x)+{+ int dy = y1 - y0, adx = x1 - x0, ady = dy < 0 ? -dy : dy;+ int off = ady * (x - x0) / adx;+ return dy < 0 ? y0 - off : y0 + off;+}++static void render_line(int x0, int y0, int x1, int y1, int *v, int n)+{+ int dy = y1 - y0, adx = x1 - x0, ady = dy < 0 ? -dy : dy;+ int base = dy / adx, sy = dy < 0 ? base - 1 : base + 1;+ int err = 0, y = y0;+ ady -= (base < 0 ? -base : base) * adx;+ if (x0 < n)+ v[x0] = y;+ for (int x = x0 + 1; x < x1 && x < n; x++) {+ err += ady;+ if (err >= adx) {+ err -= adx;+ y += sy;+ } else {+ y += base;+ }+ v[x] = y;+ }+}++/* floor 1 of a channel into out (n / 2 values): 0, or 1 when the channel+ is unused in this packet */+static int floor1(struct vorbis *v, const struct floor_t *f, struct vbr *b,+ int half, float *out, int *iv)+{+ if (!vb(b, 1))+ return 1;+ static const int RANGE[4] = {256, 128, 86, 64};+ int range = RANGE[f->mult - 1], bits = ilog((uint32_t)range - 1);+ int y[65], fy[65], step2[65];+ y[0] = (int)vb(b, bits);+ y[1] = (int)vb(b, bits);+ int off = 2;+ for (int i = 0; i < f->parts; i++) {+ int c = f->pclass[i], cdim = f->cdim[c], cbits = f->csub[c];+ int csub = (1 << cbits) - 1, cval = 0;+ if (cbits) {+ cval = cb_read(&v->cb[f->cmaster[c]], b);+ if (cval < 0)+ return 1;+ }+ for (int j = 0; j < cdim; j++) {+ int book = f->sbooks[c][cval & csub];+ cval >>= cbits;+ int val = 0;+ if (book >= 0 && (val = cb_read(&v->cb[book], b)) < 0)+ return 1;+ y[off++] = val;+ }+ }+ if (b->eop)+ return 1;+ fy[0] = y[0];+ fy[1] = y[1];+ step2[0] = step2[1] = 1;+ for (int i = 2; i < f->values; i++) {+ int lo = f->low[i], hi = f->high[i];+ int pred = render_point(f->x[lo], fy[lo], f->x[hi], fy[hi], f->x[i]);+ int val = y[i], hroom = range - pred, lroom = pred;+ int room = (hroom < lroom ? hroom : lroom) * 2;+ if (val) {+ step2[lo] = step2[hi] = step2[i] = 1;+ if (val >= room)+ fy[i] =+ hroom > lroom ? val - lroom + pred : pred - val + hroom - 1;+ else+ fy[i] = val & 1 ? pred - (val + 1) / 2 : pred + val / 2;+ } else {+ step2[i] = 0;+ fy[i] = pred;+ }+ }+ int lx = 0, ly = fy[0] * f->mult, hx = 0, hy = 0;+ for (int k = 1; k < f->values; k++) {+ int i = f->sorted[k];+ if (!step2[i])+ continue;+ hy = fy[i] * f->mult;+ hx = f->x[i];+ render_line(lx, ly, hx, hy, iv, half);+ lx = hx;+ ly = hy;+ }+ if (hx < half)+ render_line(hx, hy, half, hy, iv, half);+ for (int i = 0; i < half; i++)+ out[i] = inv_db(iv[i]);+ return 0;+}++static double bark(double x)+{+ return 13.1 * atan(.00074 * x) + 2.24 * atan(.0000000185 * x * x) ++ .0001 * x;+}++static int floor0(struct vorbis *v, const struct floor_t *f, struct vbr *b,+ int half, float *out)+{+ int amp = (int)vb(b, f->amp_bits);+ if (amp <= 0 || b->eop)+ return 1;+ int book = (int)vb(b, ilog((uint32_t)f->nbooks));+ if (book >= f->nbooks)+ return 1;+ const struct cb *c = &v->cb[f->books[book]];+ if (!c->vq)+ return 1;+ float coef[256 + 16];+ int n = 0;+ float last = 0;+ while (n < f->order) {+ int e = cb_read(c, b);+ if (e < 0)+ return 1;+ for (int k = 0; k < c->dim && n < f->order + 16; k++)+ coef[n++] = c->vq[(size_t)e * c->dim + k] + last;+ last = coef[n - 1];+ }+ double nyq = bark(.5 * f->rate);+ int prev = -2;+ float val = 0;+ for (int i = 0; i < half; i++) {+ int map = (int)floor(bark((double)f->rate * i / (2.0 * half)) *+ f->bark_size / nyq);+ if (map > f->bark_size - 1)+ map = f->bark_size - 1;+ if (map != prev) {+ double w = M_PI * map / f->bark_size, cw = cos(w), p, q;+ if (f->order & 1) {+ p = 1 - cw * cw;+ q = .25;+ for (int j = 0; j < (f->order - 1) / 2; j++)+ p *= 4 * (cos(coef[2 * j + 1]) - cw) *+ (cos(coef[2 * j + 1]) - cw);+ for (int j = 0; j < (f->order + 1) / 2; j++)+ q *= 4 * (cos(coef[2 * j]) - cw) * (cos(coef[2 * j]) - cw);+ } else {+ p = (1 - cw) / 2;+ q = (1 + cw) / 2;+ for (int j = 0; j < f->order / 2; j++) {+ p *= 4 * (cos(coef[2 * j + 1]) - cw) *+ (cos(coef[2 * j + 1]) - cw);+ q *= 4 * (cos(coef[2 * j]) - cw) * (cos(coef[2 * j]) - cw);+ }+ }+ val = (float)exp(+ .11512925 *+ (amp * f->amp_off / (((1 << f->amp_bits) - 1) * sqrt(p + q)) -+ f->amp_off));+ prev = map;+ }+ out[i] = val;+ }+ return 0;+}++/* ---- the residues ---- */++static void residue(struct vorbis *v, const struct residue *r, struct vbr *b,+ float **vec, const int *dnd, int nch, int half, int *cls)+{+ int size = r->type == 2 ? half * nch : half;+ int begin = r->begin < size ? r->begin : size;+ int end = r->end < size ? r->end : size;+ int ntoread = end - begin, parts = ntoread / r->psize;+ const struct cb *ccb = &v->cb[r->classbook];+ int cpw = ccb->dim;+ if (ntoread <= 0)+ return;+ float *tmp = NULL;+ float *vv[256];+ int dd[256], nv = nch;+ if (r->type == 2) { /* the channels interleaved into one vector */+ int any = 0;+ for (int j = 0; j < nch; j++)+ any |= !dnd[j];+ if (!any)+ return;+ tmp = calloc((size_t)size, sizeof(float));+ if (!tmp)+ return;+ vv[0] = tmp;+ dd[0] = 0;+ nv = 1;+ } else {+ for (int j = 0; j < nch; j++) {+ vv[j] = vec[j];+ dd[j] = dnd[j];+ }+ }+ for (int pass = 0; pass < 8; pass++) {+ int pc = 0;+ while (pc < parts) {+ if (pass == 0)+ for (int j = 0; j < nv; j++) {+ if (dd[j])+ continue;+ int t = cb_read(ccb, b);+ if (t < 0)+ goto done;+ for (int i = cpw - 1; i >= 0; i--) {+ if (i + pc < parts)+ cls[j * parts + i + pc] = t % r->classes;+ t /= r->classes;+ }+ }+ for (int i = 0; i < cpw && pc < parts; i++, pc++)+ for (int j = 0; j < nv; j++) {+ if (dd[j])+ continue;+ int book = r->books[cls[j * parts + pc]][pass];+ if (book < 0)+ continue;+ const struct cb *c = &v->cb[book];+ if (!c->vq)+ goto done;+ int off = begin + pc * r->psize, dim = c->dim;+ if (r->type == 0) {+ int step = r->psize / dim;+ for (int s = 0; s < step; s++) {+ int e = cb_read(c, b);+ if (e < 0)+ goto done;+ for (int k = 0; k < dim; k++)+ vv[j][off + s + k * step] +=+ c->vq[(size_t)e * dim + k];+ }+ } else {+ for (int s = 0; s < r->psize;) {+ int e = cb_read(c, b);+ if (e < 0)+ goto done;+ for (int k = 0; k < dim && s < r->psize; k++, s++)+ vv[j][off + s] += c->vq[(size_t)e * dim + k];+ }+ }+ }+ }+ }+done:+ if (tmp) {+ for (int i = 0; i < half; i++)+ for (int j = 0; j < nch; j++)+ vec[j][i] = tmp[i * nch + j];+ free(tmp);+ }+}++/* ---- windows ---- */++static float *make_window(int n, int bs0, int prev, int next, int longb)+{+ float *w = malloc(sizeof(float) * (size_t)n);+ if (!w)+ return NULL;+ int ls, le, ln, rs, re, rn;+ if (longb && !prev) {+ ls = n / 4 - bs0 / 4;+ le = n / 4 + bs0 / 4;+ ln = bs0 / 2;+ } else {+ ls = 0;+ le = n / 2;+ ln = n / 2;+ }+ if (longb && !next) {+ rs = n * 3 / 4 - bs0 / 4;+ re = n * 3 / 4 + bs0 / 4;+ rn = bs0 / 2;+ } else {+ rs = n / 2;+ re = n;+ rn = n / 2;+ }+ for (int i = 0; i < n; i++) {+ double x;+ if (i < ls || i >= re)+ x = 0;+ else if (i < le) {+ double s = sin((i - ls + .5) / ln * M_PI / 2);+ x = sin(M_PI / 2 * s * s);+ } else if (i < rs)+ x = 1;+ else {+ double s = sin((i - rs + .5) / rn * M_PI / 2 + M_PI / 2);+ x = sin(M_PI / 2 * s * s);+ }+ w[i] = (float)x;+ }+ return w;+}++static void vorbis_free(struct vorbis *v)+{+ for (int i = 0; v->cb && i < v->ncb; i++)+ cb_free(&v->cb[i]);+ free(v->cb);+ free(v->floor);+ free(v->res);+ free(v->map);+ for (int i = 0; i < 2; i++)+ aimdct_free(v->mdct[i]);+ for (int i = 0; i < 8; i++)+ free(v->win[i >> 2][(i >> 1) & 1][i & 1]);+}++/* ---- the stream ---- */++int janas_vorbis_stream(const struct ogg_packets *o, struct janas_audio *a,+ char *why, size_t wn)+{+ struct vorbis v = {0};+ const uint8_t *id = o->data + o->off[0];+ int rc = -1;+ float *out = NULL, **prev = NULL, **vec = NULL, **pcm = NULL;+ float *floorv = NULL;+ int *iv = NULL, *cls = NULL, *noresid = NULL, *dnd = NULL;+ if (o->count < 3 || o->len[0] < 30 || id[0] != 1 ||+ memcmp(id + 1, "vorbis", 6) || id[7] | id[8] | id[9] | id[10]) {+ snprintf(why, wn, "a Vorbis stream without its headers");+ return -1;+ }+ v.ch = id[11];+ v.rate = (uint32_t)id[12] | (uint32_t)id[13] << 8 | (uint32_t)id[14] << 16 |+ (uint32_t)id[15] << 24;+ v.bs[0] = 1 << (id[28] & 15);+ v.bs[1] = 1 << (id[28] >> 4);+ if (!v.ch || !v.rate || v.bs[0] < 64 || v.bs[1] > 8192 ||+ v.bs[0] > v.bs[1] || !(id[29] & 1)) {+ snprintf(why, wn, "a Vorbis stream of sizes out of range");+ return -1;+ }+ if (setup(&v, o->data + o->off[2], o->len[2])) {+ snprintf(why, wn, "a Vorbis stream whose setup does not read");+ goto end;+ }+ for (int i = 0; i < 2; i++)+ if (!(v.mdct[i] = aimdct_new(v.bs[i])))+ goto oom;+ for (int bl = 0; bl < 2; bl++)+ for (int p = 0; p < 2; p++)+ for (int nx = 0; nx < 2; nx++)+ if (!(v.win[bl][p][nx] =+ make_window(v.bs[bl], v.bs[0], p, nx, bl)))+ goto oom;+ int maxn = v.bs[1], half = maxn / 2;+ prev = calloc((size_t)v.ch, sizeof *prev);+ vec = calloc((size_t)v.ch, sizeof *vec);+ pcm = calloc((size_t)v.ch, sizeof *pcm);+ floorv = malloc(sizeof(float) * (size_t)half);+ iv = malloc(sizeof(int) * (size_t)half);+ cls = malloc(sizeof(int) * (size_t)half * (size_t)v.ch);+ noresid = malloc(sizeof(int) * (size_t)v.ch);+ dnd = malloc(sizeof(int) * (size_t)v.ch);+ if (!prev || !vec || !pcm || !floorv || !iv || !cls || !noresid || !dnd)+ goto oom;+ for (int c = 0; c < v.ch; c++)+ if (!(prev[c] = calloc((size_t)maxn, sizeof(float))) ||+ !(vec[c] = malloc(sizeof(float) * (size_t)half)) ||+ !(pcm[c] = malloc(sizeof(float) * (size_t)maxn)))+ goto oom;+ size_t got = 0, cap = 0;+ int pn = 0; /* the previous block's size, 0 before the first */+ int64_t first_granule = -1;+ size_t first_page_end = 0; /* samples out when its page ended */+ int64_t last_granule = -1;+ int pages = 0; /* with a granule position, after the first */+ for (size_t pk = 3; pk < o->count; pk++) {+ struct vbr b = {.p = o->data + o->off[pk], .n = o->len[pk]};+ if (!o->len[pk] || vb(&b, 1))+ continue; /* not audio */+ int mode = (int)vb(&b, ilog((uint32_t)v.nmode - 1));+ if (mode >= v.nmode || b.eop)+ continue;+ int bl = v.mode_block[mode], n = v.bs[bl], pf = 0, nf = 0;+ if (bl) {+ pf = (int)vb(&b, 1);+ nf = (int)vb(&b, 1);+ }+ const struct mapping *m = &v.map[v.mode_map[mode]];+ int h2 = n / 2;+ for (int c = 0; c < v.ch; c++) {+ const struct floor_t *f = &v.floor[m->sfloor[m->mux[c]]];+ float *fl = pcm[c]; /* the floor curve, kept here a while */+ noresid[c] = f->type == 1 ? floor1(&v, f, &b, h2, fl, iv)+ : floor0(&v, f, &b, h2, fl);+ memset(vec[c], 0, sizeof(float) * (size_t)h2);+ }+ /* a channel whose floor is unused stays silent, though coupling+ may have its residue read */+ memcpy(dnd, noresid, sizeof(int) * (size_t)v.ch);+ for (int s = 0; s < m->steps; s++)+ if (!noresid[m->mag[s]] || !noresid[m->ang[s]])+ noresid[m->mag[s]] = noresid[m->ang[s]] = 0;+ for (int sm = 0; sm < m->submaps; sm++) {+ float *cv[256];+ int cd[256], k = 0;+ for (int c = 0; c < v.ch; c++)+ if (m->mux[c] == sm) {+ cv[k] = vec[c];+ cd[k++] = noresid[c];+ }+ residue(&v, &v.res[m->sres[sm]], &b, cv, cd, k, h2, cls);+ }+ for (int s = m->steps - 1; s >= 0; s--) {+ float *M = vec[m->mag[s]], *A = vec[m->ang[s]];+ for (int i = 0; i < h2; i++) {+ float mg = M[i], an = A[i], nm, na;+ if (mg > 0) {+ if (an > 0) {+ nm = mg;+ na = mg - an;+ } else {+ na = mg;+ nm = mg + an;+ }+ } else {+ if (an > 0) {+ nm = mg;+ na = mg + an;+ } else {+ na = mg;+ nm = mg - an;+ }+ }+ M[i] = nm;+ A[i] = na;+ }+ }+ for (int c = 0; c < v.ch; c++) {+ float *fl = pcm[c];+ if (dnd[c])+ memset(vec[c], 0, sizeof(float) * (size_t)h2);+ else+ for (int i = 0; i < h2; i++)+ vec[c][i] *= fl[i];+ aimdct_run(v.mdct[bl], vec[c], pcm[c]);+ const float *w = v.win[bl][pf][nf];+ for (int i = 0; i < n; i++)+ pcm[c][i] *= w[i];+ }+ /* from the previous block's centre to this one's */+ if (pn) {+ int len = pn / 4 + n / 4;+ if (got + (size_t)len > JANAS_AUDIO_MAX_SAMPLES) {+ snprintf(why, wn, "a Vorbis stream longer than this reads");+ goto end;+ }+ if (got + (size_t)len > cap) {+ size_t c2 = cap ? cap * 2 : 1 << 18;+ while (c2 < got + (size_t)len)+ c2 *= 2;+ float *q = realloc(out, sizeof(float) * c2);+ if (!q)+ goto oom;+ out = q;+ cap = c2;+ }+ for (int k = 0; k < len; k++) {+ double s = 0;+ int pi = pn / 2 + k, ci = n / 4 - pn / 4 + k;+ for (int c = 0; c < v.ch; c++)+ s += (pi < pn ? prev[c][pi] : 0) ++ (ci >= 0 && ci < n ? pcm[c][ci] : 0);+ out[got + (size_t)k] = (float)(s / v.ch);+ }+ got += (size_t)len;+ }+ for (int c = 0; c < v.ch; c++)+ memcpy(prev[c], pcm[c], sizeof(float) * (size_t)n);+ pn = n;+ if (o->granule[pk] >= 0) {+ if (first_granule < 0) {+ first_granule = o->granule[pk];+ first_page_end = got;+ } else {+ pages++;+ }+ last_granule = o->granule[pk];+ }+ }+ /* the start: a first page ending before the samples it gave began+ later than zero; the end: the last granule position. A stream of a+ single page is only trimmed at its end, as libvorbisfile does. */+ size_t skip = 0, total = got;+ if (first_granule >= 0 && pages > 0 &&+ (int64_t)first_page_end > first_granule)+ skip = first_page_end - (size_t)first_granule;+ if (pages == 0 && last_granule >= 0) {+ if ((uint64_t)last_granule < total)+ total = (size_t)last_granule;+ } else if (last_granule >= 0 && first_granule >= 0) {+ int64_t want = last_granule - (first_granule - (int64_t)first_page_end ++ (int64_t)skip);+ if (want >= 0 && (size_t)want + skip < total)+ total = (size_t)want + skip;+ }+ if (skip > total)+ skip = total;+ if (total - skip == 0) {+ snprintf(why, wn, "a Vorbis stream without sound");+ goto end;+ }+ memmove(out, out + skip, sizeof(float) * (total - skip));+ a->pcm = out;+ a->n = total - skip;+ a->rate = v.rate;+ out = NULL;+ rc = 0;+ goto end;+oom:+ snprintf(why, wn, "out of memory");+end:+ free(out);+ for (int c = 0; c < v.ch; c++) {+ if (prev)+ free(prev[c]);+ if (vec)+ free(vec[c]);+ if (pcm)+ free(pcm[c]);+ }+ free(prev);+ free(vec);+ free(pcm);+ free(floorv);+ free(iv);+ free(cls);+ free(noresid);+ free(dnd);+ vorbis_free(&v);+ return rc;+}diff --git a/src/common/audio_wav.c b/src/common/audio_wav.cnew file mode 100644index 0000000..c052888--- /dev/null+++ b/src/common/audio_wav.c@@ -0,0 +1,123 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio_wav.c - a WAV file (audio.h): the RIFF chunks walked, "fmt " read+ * (PCM 8, 16, 24 or 32 bits, float 32 or 64; WAVE_FORMAT_EXTENSIBLE by+ * its subformat), the samples of "data" averaged over the channels. A+ * data chunk longer than the file (a stream whose length was never+ * written) is read to the end of the file.+ */+#include "audio.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++static uint32_t le16(const uint8_t *p)+{+ return (uint32_t)p[0] | (uint32_t)p[1] << 8;+}++static uint32_t le32(const uint8_t *p)+{+ return le16(p) | le16(p + 2) << 16;+}++static double sample(const uint8_t *p, int bits, int fl)+{+ if (fl) {+ if (bits == 32) {+ float f;+ memcpy(&f, p, 4);+ return f;+ }+ double d;+ memcpy(&d, p, 8);+ return d;+ }+ switch (bits) {+ case 8:+ return (p[0] - 128) / 128.0;+ case 16:+ return (int16_t)le16(p) / 32768.0;+ case 24: {+ int32_t v = (int32_t)(le16(p) | (uint32_t)p[2] << 16);+ if (v & 0x800000)+ v -= 0x1000000;+ return v / 8388608.0;+ }+ default:+ return (int32_t)le32(p) / 2147483648.0;+ }+}++int janas_audio_wav(const uint8_t *d, size_t n, struct janas_audio *a,+ char *why, size_t wn)+{+ if (n < 12 || memcmp(d, "RIFF", 4) || memcmp(d + 8, "WAVE", 4))+ return 1;+ uint32_t fmt = 0, ch = 0, rate = 0, bits = 0, align = 0;+ const uint8_t *data = NULL;+ size_t dn = 0;+ for (size_t at = 12; at + 8 <= n;) {+ uint32_t len = le32(d + at + 4);+ const uint8_t *body = d + at + 8;+ size_t room = n - at - 8;+ if (!memcmp(d + at, "fmt ", 4)) {+ if (len < 16 || len > room) {+ snprintf(why, wn, "a WAV file whose format is cut short");+ return -1;+ }+ fmt = le16(body);+ ch = le16(body + 2);+ rate = le32(body + 4);+ align = le16(body + 12);+ bits = le16(body + 14);+ if (fmt == 0xFFFE && len >= 40)+ fmt = le16(body + 24); /* the subformat's GUID begins so */+ } else if (!memcmp(d + at, "data", 4)) {+ data = body;+ dn = len <= room ? len : room;+ break;+ }+ if (len > room)+ break;+ at += 8 + (size_t)len + (len & 1);+ }+ int fl = fmt == 3;+ if (!fmt || !data) {+ snprintf(why, wn, "a WAV file without %s", fmt ? "samples" : "format");+ return -1;+ }+ if ((fmt != 1 && fmt != 3) || !ch || ch > 64 || !rate || rate > 768000 ||+ (fl ? bits != 32 && bits != 64+ : bits != 8 && bits != 16 && bits != 24 && bits != 32) ||+ align != ch * (bits / 8)) {+ snprintf(why, wn,+ "a WAV file of a kind this does not read (format %u, %u "+ "channels, %u bits)",+ fmt, ch, bits);+ return -1;+ }+ size_t frames = dn / align;+ if (frames > JANAS_AUDIO_MAX_SAMPLES) {+ snprintf(why, wn, "a WAV file longer than this reads");+ return -1;+ }+ a->pcm = malloc(sizeof(float) * (frames ? frames : 1));+ if (!a->pcm) {+ snprintf(why, wn, "out of memory");+ return -1;+ }+ uint32_t bs = bits / 8;+ for (size_t f = 0; f < frames; f++) {+ const uint8_t *p = data + f * align;+ double s = 0;+ for (uint32_t c = 0; c < ch; c++)+ s += sample(p + c * bs, (int)bits, fl);+ a->pcm[f] = (float)(s / ch);+ }+ a->n = frames;+ a->rate = rate;+ return 0;+}diff --git a/src/common/deflate.c b/src/common/deflate.cnew file mode 100644index 0000000..633f31c--- /dev/null+++ b/src/common/deflate.c@@ -0,0 +1,456 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * deflate.c - the size of the zlib stream zlib's compress() makes at its+ * default level (6: lazy matching, chains of 128, good 8, lazy 16, nice+ * 128, a window of 32 KiB, hash chains of 15 bits, blocks of up to 16383+ * symbols), to the byte: its matches found as zlib's deflate_slow finds+ * them, its Huffman trees built as zlib's trees.c builds them (the heap's+ * order, the depth that breaks ties, the lengths limited to 15 and 7 as+ * zlib limits them), each block stored, with the fixed codes or with its+ * own as zlib chooses. What OpenAI's Whisper measures a text's repetition+ * with (its compression ratio).+ */+#include "deflate.h"++#include <stdlib.h>+#include <string.h>++#define MIN_MATCH 3+#define MAX_MATCH 258+#define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)+#define W_SIZE 32768+#define W_MASK (W_SIZE - 1)+#define MAX_DIST (W_SIZE - MIN_LOOKAHEAD)+#define HASH_BITS 15+#define HASH_SIZE (1 << HASH_BITS)+#define HASH_MASK (HASH_SIZE - 1)+#define HASH_SHIFT ((HASH_BITS + MIN_MATCH - 1) / MIN_MATCH)+#define TOO_FAR 4096+#define LIT_BUFSIZE 16384+#define GOOD_LENGTH 8+#define MAX_LAZY 16+#define NICE_LENGTH 128+#define MAX_CHAIN 128++#define LITERALS 256+#define END_BLOCK 256+#define L_CODES (LITERALS + 1 + 29)+#define D_CODES 30+#define BL_CODES 19+#define HEAP_SIZE (2 * L_CODES + 1)+#define MAX_BITS 15+#define REP_3_6 16+#define REPZ_3_10 17+#define REPZ_11_138 18++static const int EXTRA_LBITS[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,+ 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};+static const int EXTRA_DBITS[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3,+ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,+ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};+static const int EXTRA_BLBITS[19] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,+ 0, 0, 0, 0, 0, 0, 2, 3, 7};+static const uint8_t BL_ORDER[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5,+ 11, 4, 12, 3, 13, 2, 14, 1, 15};+static const int BASE_LENGTH[29] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 10,+ 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,+ 64, 80, 96, 112, 128, 160, 192, 224, 255};+static const int BASE_DIST[30] = {+ 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,+ 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,+ 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576};++/* a tree's node: its frequency, and its parent until its length replaces+ it (zlib's union) */+struct node {+ uint32_t freq;+ uint16_t dl; /* dad, then len */+};++struct desc {+ struct node *tree;+ int max_code;+ const struct node *stree; /* the fixed code's lengths, or NULL */+ const int *extra;+ int base, elems, max_length;+};++struct z {+ const uint8_t *win;+ size_t n;+ uint32_t *head, *prev;+ uint32_t ins_h;+ size_t strstart, lookahead, match_start, prev_match;+ unsigned match_length, prev_length;+ int match_available;+ struct node dyn_l[HEAP_SIZE], dyn_d[2 * D_CODES + 1], bl[2 * BL_CODES + 1];+ struct node stat_l[L_CODES + 2], stat_d[D_CODES];+ int heap[2 * L_CODES + 1], heap_len, heap_max;+ uint8_t depth[2 * L_CODES + 1];+ uint16_t bl_count[MAX_BITS + 1];+ uint64_t opt_len, static_len;+ unsigned sym_next;+ size_t block_start;+ uint64_t bits; /* written so far */+};++static int length_code(unsigned len)+{+ for (int c = 28; c >= 0; c--)+ if (len >= (unsigned)BASE_LENGTH[c])+ return c;+ return 0;+}++static int dist_code(unsigned dist)+{+ for (int c = 29; c >= 0; c--)+ if (dist >= (unsigned)BASE_DIST[c])+ return c;+ return 0;+}++static void init_block(struct z *s)+{+ for (int i = 0; i < L_CODES; i++)+ s->dyn_l[i].freq = 0;+ for (int i = 0; i < D_CODES; i++)+ s->dyn_d[i].freq = 0;+ for (int i = 0; i < BL_CODES; i++)+ s->bl[i].freq = 0;+ s->dyn_l[END_BLOCK].freq = 1;+ s->opt_len = s->static_len = 0;+ s->sym_next = 0;+}++#define SMALLER(t, n, m) \+ ((t)[n].freq < (t)[m].freq || \+ ((t)[n].freq == (t)[m].freq && s->depth[n] <= s->depth[m]))++static void pqdownheap(struct z *s, struct node *tree, int k)+{+ int v = s->heap[k], j = k << 1;+ while (j <= s->heap_len) {+ if (j < s->heap_len && SMALLER(tree, s->heap[j + 1], s->heap[j]))+ j++;+ if (SMALLER(tree, v, s->heap[j]))+ break;+ s->heap[k] = s->heap[j];+ k = j;+ j <<= 1;+ }+ s->heap[k] = v;+}++static void gen_bitlen(struct z *s, struct desc *d)+{+ struct node *tree = d->tree;+ int max_code = d->max_code, max_length = d->max_length, overflow = 0, h;+ for (int b = 0; b <= MAX_BITS; b++)+ s->bl_count[b] = 0;+ tree[s->heap[s->heap_max]].dl = 0;+ for (h = s->heap_max + 1; h < HEAP_SIZE; h++) {+ int n = s->heap[h];+ int bits = tree[tree[n].dl].dl + 1;+ if (bits > max_length) {+ bits = max_length;+ overflow++;+ }+ tree[n].dl = (uint16_t)bits;+ if (n > max_code)+ continue;+ s->bl_count[bits]++;+ int xbits = n >= d->base ? d->extra[n - d->base] : 0;+ uint64_t f = tree[n].freq;+ s->opt_len += f * (unsigned)(bits + xbits);+ if (d->stree)+ s->static_len += f * (unsigned)(d->stree[n].dl + xbits);+ }+ if (!overflow)+ return;+ do {+ int bits = max_length - 1;+ while (s->bl_count[bits] == 0)+ bits--;+ s->bl_count[bits]--;+ s->bl_count[bits + 1] += 2;+ s->bl_count[max_length]--;+ overflow -= 2;+ } while (overflow > 0);+ for (int bits = max_length; bits != 0; bits--) {+ int n = s->bl_count[bits];+ while (n != 0) {+ int m = s->heap[--h];+ if (m > max_code)+ continue;+ if ((unsigned)tree[m].dl != (unsigned)bits) {+ s->opt_len += ((uint64_t)bits - tree[m].dl) * tree[m].freq;+ tree[m].dl = (uint16_t)bits;+ }+ n--;+ }+ }+}++static void build_tree(struct z *s, struct desc *d)+{+ struct node *tree = d->tree;+ int elems = d->elems, max_code = -1, node;+ s->heap_len = 0;+ s->heap_max = HEAP_SIZE;+ for (int n = 0; n < elems; n++) {+ if (tree[n].freq) {+ s->heap[++s->heap_len] = max_code = n;+ s->depth[n] = 0;+ } else {+ tree[n].dl = 0;+ }+ }+ while (s->heap_len < 2) {+ node = s->heap[++s->heap_len] = max_code < 2 ? ++max_code : 0;+ tree[node].freq = 1;+ s->depth[node] = 0;+ s->opt_len--;+ if (d->stree)+ s->static_len -= d->stree[node].dl;+ }+ d->max_code = max_code;+ for (int n = s->heap_len / 2; n >= 1; n--)+ pqdownheap(s, tree, n);+ node = elems;+ do {+ int n = s->heap[1];+ s->heap[1] = s->heap[s->heap_len--];+ pqdownheap(s, tree, 1);+ int m = s->heap[1];+ s->heap[--s->heap_max] = n;+ s->heap[--s->heap_max] = m;+ tree[node].freq = tree[n].freq + tree[m].freq;+ s->depth[node] =+ (uint8_t)((s->depth[n] >= s->depth[m] ? s->depth[n] : s->depth[m]) ++ 1);+ tree[n].dl = tree[m].dl = (uint16_t)node;+ s->heap[1] = node++;+ pqdownheap(s, tree, 1);+ } while (s->heap_len >= 2);+ s->heap[--s->heap_max] = s->heap[1];+ gen_bitlen(s, d);+}++static void scan_tree(struct z *s, struct node *tree, int max_code)+{+ int prevlen = -1, nextlen = tree[0].dl, count = 0;+ int max_count = 7, min_count = 4;+ if (nextlen == 0)+ max_count = 138, min_count = 3;+ tree[max_code + 1].dl = 0xffff;+ for (int n = 0; n <= max_code; n++) {+ int curlen = nextlen;+ nextlen = tree[n + 1].dl;+ if (++count < max_count && curlen == nextlen)+ continue;+ else if (count < min_count)+ s->bl[curlen].freq += (uint32_t)count;+ else if (curlen != 0) {+ if (curlen != prevlen)+ s->bl[curlen].freq++;+ s->bl[REP_3_6].freq++;+ } else if (count <= 10)+ s->bl[REPZ_3_10].freq++;+ else+ s->bl[REPZ_11_138].freq++;+ count = 0;+ prevlen = curlen;+ if (nextlen == 0)+ max_count = 138, min_count = 3;+ else if (curlen == nextlen)+ max_count = 6, min_count = 3;+ else+ max_count = 7, min_count = 4;+ }+}++/* a block of the symbols tallied, of stored_len bytes from block_start */+static void flush_block(struct z *s, size_t stored_len, int last)+{+ struct desc l = {s->dyn_l, 0, s->stat_l, EXTRA_LBITS,+ LITERALS + 1, L_CODES, MAX_BITS};+ struct desc dd = {s->dyn_d, 0, s->stat_d, EXTRA_DBITS,+ 0, D_CODES, MAX_BITS};+ struct desc bl = {s->bl, 0, NULL, EXTRA_BLBITS, 0, BL_CODES, 7};+ build_tree(s, &l);+ build_tree(s, &dd);+ scan_tree(s, s->dyn_l, l.max_code);+ scan_tree(s, s->dyn_d, dd.max_code);+ build_tree(s, &bl);+ int max_blindex;+ for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--)+ if (s->bl[BL_ORDER[max_blindex]].dl != 0)+ break;+ s->opt_len += 3 * ((uint64_t)max_blindex + 1) + 5 + 5 + 4;+ uint64_t opt_lenb = (s->opt_len + 3 + 7) >> 3;+ uint64_t static_lenb = (s->static_len + 3 + 7) >> 3;+ if (static_lenb <= opt_lenb)+ opt_lenb = static_lenb;+ if (stored_len + 4 <= opt_lenb) {+ s->bits += 3;+ s->bits = (s->bits + 7) & ~(uint64_t)7; /* to a byte */+ s->bits += 32 + 8 * (uint64_t)stored_len;+ } else if (static_lenb == opt_lenb) {+ s->bits += 3 + s->static_len;+ } else {+ s->bits += 3 + s->opt_len;+ }+ init_block(s);+ if (last)+ s->bits = (s->bits + 7) & ~(uint64_t)7;+}++static void insert(struct z *s, size_t str, uint32_t *head)+{+ s->ins_h =+ ((s->ins_h << HASH_SHIFT) ^ s->win[str + MIN_MATCH - 1]) & HASH_MASK;+ *head = s->prev[str & W_MASK] = s->head[s->ins_h];+ s->head[s->ins_h] = (uint32_t)str;+}++static unsigned longest_match(struct z *s, uint32_t cur_match)+{+ unsigned chain = MAX_CHAIN;+ const uint8_t *scan = s->win + s->strstart;+ unsigned best_len = s->prev_length;+ unsigned nice = NICE_LENGTH;+ size_t limit = s->strstart > MAX_DIST ? s->strstart - MAX_DIST : 0;+ const uint8_t *strend = s->win + s->strstart + MAX_MATCH;+ if (s->prev_length >= GOOD_LENGTH)+ chain >>= 2;+ if (nice > s->lookahead)+ nice = (unsigned)s->lookahead;+ uint8_t scan_end1 = scan[best_len - 1], scan_end = scan[best_len];+ do {+ const uint8_t *match = s->win + cur_match;+ if (match[best_len] != scan_end || match[best_len - 1] != scan_end1 ||+ match[0] != scan[0] || match[1] != scan[1])+ continue;+ const uint8_t *sc = scan + 2, *mt = match + 2;+ while (sc < strend && *++sc == *++mt)+ ;+ unsigned len = (unsigned)(MAX_MATCH - (strend - sc));+ if (sc > strend)+ len = MAX_MATCH;+ if (len > best_len) {+ s->match_start = cur_match;+ best_len = len;+ if (len >= nice)+ break;+ scan_end1 = scan[best_len - 1];+ scan_end = scan[best_len];+ }+ } while ((cur_match = s->prev[cur_match & W_MASK]) > limit && --chain != 0);+ return best_len <= s->lookahead ? best_len : (unsigned)s->lookahead;+}++static void tally(struct z *s, unsigned dist, unsigned lc, size_t at)+{+ if (!dist) {+ s->dyn_l[lc].freq++;+ } else {+ dist--;+ s->dyn_l[length_code(lc) + LITERALS + 1].freq++;+ s->dyn_d[dist_code(dist)].freq++;+ }+ s->sym_next += 3;+ if (s->sym_next == (LIT_BUFSIZE - 1) * 3) {+ flush_block(s, at - s->block_start, 0);+ s->block_start = at;+ }+}++long janas_deflate_size(const uint8_t *p, size_t n)+{+ /* one window: what this needs, Whisper's texts being short */+ if (n > W_SIZE - MIN_LOOKAHEAD)+ return -1;+ struct z *s = calloc(1, sizeof *s);+ uint8_t *win = calloc(n + MAX_MATCH + MIN_MATCH + 8, 1);+ if (s)+ s->head = calloc(HASH_SIZE, sizeof *s->head);+ if (s)+ s->prev = calloc(W_SIZE, sizeof *s->prev);+ if (!s || !win || !s->head || !s->prev) {+ if (s) {+ free(s->head);+ free(s->prev);+ }+ free(s);+ free(win);+ return -1;+ }+ memcpy(win, p, n);+ s->win = win;+ s->n = n;+ /* the fixed codes' lengths */+ for (int i = 0; i < L_CODES + 2; i++)+ s->stat_l[i].dl = i < 144 ? 8 : i < 256 ? 9 : i < 280 ? 7 : 8;+ for (int i = 0; i < D_CODES; i++)+ s->stat_d[i].dl = 5;+ init_block(s);+ s->lookahead = n;+ s->match_length = s->prev_length = MIN_MATCH - 1;+ if (n >= MIN_MATCH) {+ s->ins_h = win[0];+ s->ins_h = ((s->ins_h << HASH_SHIFT) ^ win[1]) & HASH_MASK;+ }+ /* deflate_slow */+ while (s->lookahead > 0) {+ uint32_t hash_head = 0;+ if (s->lookahead >= MIN_MATCH)+ insert(s, s->strstart, &hash_head);+ s->prev_length = s->match_length;+ s->prev_match = s->match_start;+ s->match_length = MIN_MATCH - 1;+ if (hash_head != 0 && s->prev_length < MAX_LAZY &&+ s->strstart - hash_head <= MAX_DIST) {+ s->match_length = longest_match(s, hash_head);+ if (s->match_length == MIN_MATCH &&+ s->strstart - s->match_start > TOO_FAR)+ s->match_length = MIN_MATCH - 1;+ }+ if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {+ size_t max_insert = s->strstart + s->lookahead - MIN_MATCH;+ size_t at = s->strstart - 1;+ unsigned dist = (unsigned)(s->strstart - 1 - s->prev_match);+ unsigned len = s->prev_length - MIN_MATCH;+ s->lookahead -= s->prev_length - 1;+ s->prev_length -= 2;+ do {+ if (++s->strstart <= max_insert)+ insert(s, s->strstart, &hash_head);+ } while (--s->prev_length != 0);+ s->match_available = 0;+ s->match_length = MIN_MATCH - 1;+ s->strstart++;+ /* the symbols' block ends after the match */+ (void)at;+ tally(s, dist, len, s->strstart);+ } else if (s->match_available) {+ tally(s, 0, win[s->strstart - 1], s->strstart);+ s->strstart++;+ s->lookahead--;+ } else {+ s->match_available = 1;+ s->strstart++;+ s->lookahead--;+ }+ }+ if (s->match_available)+ tally(s, 0, win[s->strstart - 1], s->strstart);+ flush_block(s, s->strstart - s->block_start, 1);+ long size = 2 + (long)(s->bits / 8) + 4;+ free(s->head);+ free(s->prev);+ free(s);+ free(win);+ return size;+}diff --git a/src/common/deflate.h b/src/common/deflate.hnew file mode 100644index 0000000..6bf8c96--- /dev/null+++ b/src/common/deflate.h@@ -0,0 +1,17 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * deflate.h - the size of a zlib stream (deflate.c).+ */+#ifndef JANAS_COMMON_DEFLATE_H+#define JANAS_COMMON_DEFLATE_H++#include <stddef.h>+#include <stdint.h>++/* the bytes zlib's compress() makes of n bytes at its default level, to+ the byte; -1 when out of memory or longer than one window (32 KiB less+ 262 bytes) */+long janas_deflate_size(const uint8_t *p, size_t n);++#endifdiff --git a/src/common/env_names.c b/src/common/env_names.cindex 4cbd17e..1122f3f 100644--- a/src/common/env_names.c+++ b/src/common/env_names.c@@ -83,6 +83,7 @@ const char *const JANAS_ENV_NAMES[] = {"JANAS_ALPHAVANTAGE_KEY","JANAS_GPU_XCOH","JANAS_HC_MIX","JANAS_HEAD_BITS",+ "JANAS_IMAGE_MAX_TOKENS","JANAS_JANET_FFN","JANAS_JANET_REC","JANAS_JANET_REC_LAYERS",diff --git a/src/common/hf_fetch.c b/src/common/hf_fetch.cindex b832105..ac8a915 100644--- a/src/common/hf_fetch.c+++ b/src/common/hf_fetch.c@@ -79,8 +79,16 @@ static int get_url(const char *url, const int64_t *from, const int64_t *to,return -1;}snprintf(u.path, sizeof(u.path), "%s", loc);- } else if (janas_url_parse(loc, &u, err, err_len) != 0) {- return -1;+ } else {+ int was_tls = u.tls;+ if (janas_url_parse(loc, &u, err, err_len) != 0)+ return -1;+ if (was_tls && !u.tls) { /* never down to clear text */+ snprintf(err, err_len,+ "%s: a redirect from HTTPS to HTTP refused",+ u.host);+ return -1;+ }}continue;}diff --git a/src/common/hf_safetensors.c b/src/common/hf_safetensors.cnew file mode 100644index 0000000..8e571df--- /dev/null+++ b/src/common/hf_safetensors.c@@ -0,0 +1,174 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * hf_safetensors.c - safetensors read for the tools (hf_safetensors.h).+ */+#define _FILE_OFFSET_BITS 64+#include "hf_safetensors.h"++#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "llm/json.h"++int janas_st_open(struct janas_st *s, const char *path, char *err,+ size_t err_len)+{+ memset(s, 0, sizeof(*s));+ s->f = fopen(path, "rb");+ uint64_t n = 0;+ if (!s->f || fread(&n, 8, 1, s->f) != 1 || n > ((uint64_t)1 << 30)) {+ snprintf(err, err_len, "%s: not a safetensors file", path);+ janas_st_close(s);+ return -1;+ }+ char *json = malloc(n + 1);+ if (!json || fread(json, 1, n, s->f) != n) {+ free(json);+ snprintf(err, err_len, "%s: cannot read its header", path);+ janas_st_close(s);+ return -1;+ }+ char why[160];+ s->doc = janas_json_parse(json, n, why, sizeof why);+ free(json);+ if (!s->doc || janas_json_root(s->doc)->type != JANAS_JSON_OBJECT) {+ snprintf(err, err_len, "%s: its header is not JSON (%s)", path,+ s->doc ? "not an object" : why);+ janas_st_close(s);+ return -1;+ }+ s->data_start = 8 + n;+ if (fseeko(s->f, 0, SEEK_END) != 0 || ftello(s->f) < (off_t)s->data_start) {+ snprintf(err, err_len, "%s: shorter than its header", path);+ janas_st_close(s);+ return -1;+ }+ s->size = (uint64_t)ftello(s->f);+ return 0;+}++void janas_st_close(struct janas_st *s)+{+ if (s->f)+ fclose(s->f);+ janas_json_free(s->doc);+ memset(s, 0, sizeof(*s));+}++static int u64_of(const struct janas_json *v, uint64_t *out)+{+ if (!v || v->type != JANAS_JSON_NUMBER || !v->n || v->n > 20)+ return -1;+ uint64_t x = 0;+ for (size_t i = 0; i < v->n; i++) {+ if (v->s[i] < '0' || v->s[i] > '9' || x > (UINT64_MAX - 9) / 10)+ return -1;+ x = x * 10 + (uint64_t)(v->s[i] - '0');+ }+ *out = x;+ return 0;+}++int janas_st_find(const struct janas_st *s, const char *name,+ struct janas_st_tensor *t)+{+ const struct janas_json *e = janas_json_get(janas_json_root(s->doc), name);+ const struct janas_json *dt = janas_json_get(e, "dtype");+ const struct janas_json *sh = janas_json_get(e, "shape");+ const struct janas_json *of = janas_json_get(e, "data_offsets");+ memset(t, 0, sizeof(*t));+ size_t esize;+ if (janas_json_is(dt, "F32"))+ t->dtype = JANAS_ST_F32, esize = 4;+ else if (janas_json_is(dt, "F16"))+ t->dtype = JANAS_ST_F16, esize = 2;+ else if (janas_json_is(dt, "BF16"))+ t->dtype = JANAS_ST_BF16, esize = 2;+ else+ return -1;+ if (!sh || sh->type != JANAS_JSON_ARRAY || sh->n > 8 || !of ||+ of->type != JANAS_JSON_ARRAY || of->n != 2)+ return -1;+ t->count = 1;+ for (const struct janas_json *d = sh->child; d; d = d->next) {+ if (u64_of(d, &t->shape[t->dims]) || !t->shape[t->dims] ||+ __builtin_mul_overflow(t->count, t->shape[t->dims], &t->count))+ return -1;+ t->dims++;+ }+ if (u64_of(of->child, &t->begin) || u64_of(of->child->next, &t->end) ||+ t->end < t->begin || t->end > s->size - s->data_start ||+ t->count > UINT64_MAX / esize || t->end - t->begin != t->count * esize)+ return -1;+ return 0;+}++static void *read_raw(const struct janas_st *s, const struct janas_st_tensor *t)+{+ size_t n = (size_t)(t->end - t->begin);+ void *raw = malloc(n ? n : 1);+ if (!raw ||+ fseeko(s->f, (off_t)(s->data_start + t->begin), SEEK_SET) != 0 ||+ fread(raw, 1, n, s->f) != n) {+ free(raw);+ return NULL;+ }+ return raw;+}++static float half_to_float(uint16_t h)+{+ int e = h >> 10 & 31, m = h & 1023;+ float v = e == 0 ? ldexpf((float)m, -24)+ : e == 31 ? (m ? NAN : INFINITY)+ : ldexpf((float)(m | 1024), e - 25);+ return h & 0x8000 ? -v : v;+}++float *janas_st_floats(const struct janas_st *s,+ const struct janas_st_tensor *t)+{+ void *raw = read_raw(s, t);+ float *x = raw ? malloc(sizeof(float) * (size_t)t->count) : NULL;+ if (!x) {+ free(raw);+ return NULL;+ }+ for (uint64_t i = 0; i < t->count; i++) {+ if (t->dtype == JANAS_ST_F32) {+ memcpy(&x[i], (char *)raw + 4 * i, 4);+ } else {+ uint16_t h;+ memcpy(&h, (char *)raw + 2 * i, 2);+ if (t->dtype == JANAS_ST_BF16) {+ uint32_t u = (uint32_t)h << 16;+ memcpy(&x[i], &u, 4);+ } else {+ x[i] = half_to_float(h);+ }+ }+ }+ free(raw);+ return x;+}++uint16_t *janas_st_bf16(const struct janas_st *s,+ const struct janas_st_tensor *t)+{+ if (t->dtype == JANAS_ST_BF16)+ return read_raw(s, t);+ float *x = janas_st_floats(s, t);+ uint16_t *b = x ? malloc(2 * (size_t)t->count) : NULL;+ for (uint64_t i = 0; b && i < t->count; i++) {+ uint32_t u;+ memcpy(&u, &x[i], 4);+ if ((u & 0x7fffffff) > 0x7f800000) /* NaN stays NaN */+ b[i] = (uint16_t)(u >> 16 | 0x40);+ else+ b[i] = (uint16_t)((u + 0x7fff + (u >> 16 & 1)) >> 16);+ }+ free(x);+ return b;+}diff --git a/src/common/hf_safetensors.h b/src/common/hf_safetensors.hnew file mode 100644index 0000000..9848884--- /dev/null+++ b/src/common/hf_safetensors.h@@ -0,0 +1,52 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * hf_safetensors.h - a safetensors file (Hugging Face's checkpoints) read+ * for the tools (hf_safetensors.c): an 8-byte header length, a JSON header+ * naming each tensor's type, shape and byte range, the data. Every field+ * is checked against the file before it is used.+ */+#ifndef JANAS_COMMON_HF_SAFETENSORS_H+#define JANAS_COMMON_HF_SAFETENSORS_H++#include <stddef.h>+#include <stdint.h>+#include <stdio.h>++struct janas_json_doc;++struct janas_st {+ FILE *f;+ struct janas_json_doc *doc; /* the header */+ uint64_t data_start, size;+};++enum { JANAS_ST_F32, JANAS_ST_F16, JANAS_ST_BF16 };++struct janas_st_tensor {+ int dtype; /* JANAS_ST_... */+ int dims;+ uint64_t shape[8]; /* as the checkpoint gives it, outermost first;+ none for a scalar (dims 0, count 1) */+ uint64_t count; /* elements */+ uint64_t begin, end;+};++/* 0, or -1 with the reason in err. */+int janas_st_open(struct janas_st *s, const char *path, char *err,+ size_t err_len);+void janas_st_close(struct janas_st *s);+/* The tensor of that name: 0, or -1 when it is not there or its entry is+ not one this reads (F32, F16, BF16; one to eight dimensions; its bytes+ inside the file and as many as its shape says). */+int janas_st_find(const struct janas_st *s, const char *name,+ struct janas_st_tensor *t);+/* Its elements as float (malloc'd), or NULL. */+float *janas_st_floats(const struct janas_st *s,+ const struct janas_st_tensor *t);+/* Its elements as bfloat16 (malloc'd; F32 and F16 rounded to nearest even),+ or NULL. */+uint16_t *janas_st_bf16(const struct janas_st *s,+ const struct janas_st_tensor *t);++#endifdiff --git a/src/common/image.c b/src/common/image.cnew file mode 100644index 0000000..d93e118--- /dev/null+++ b/src/common/image.c@@ -0,0 +1,33 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * image.c - a picture's format told by its first bytes, and the decoder+ * of that format (image.h).+ */+#include "image.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++int janas_image_decode(const void *data, size_t n, struct janas_image *im,+ char *why, size_t wn)+{+ const uint8_t *p = data;+ memset(im, 0, sizeof(*im));+ if (n >= 8 && !memcmp(p, "\x89PNG\r\n\x1a\n", 8))+ return janas_png_decode(p, n, im, why, wn);+ if (n >= 3 && p[0] == 0xff && p[1] == 0xd8 && p[2] == 0xff)+ return janas_jpeg_decode(p, n, im, why, wn);+ if (n >= 12 && !memcmp(p, "RIFF", 4) && !memcmp(p + 8, "WEBP", 4))+ snprintf(why, wn, "WebP pictures are not read yet: send PNG or JPEG");+ else+ snprintf(why, wn, "not a picture this reads (PNG or JPEG)");+ return -1;+}++void janas_image_free(struct janas_image *im)+{+ free(im->rgb);+ memset(im, 0, sizeof(*im));+}diff --git a/src/common/image.h b/src/common/image.hnew file mode 100644index 0000000..535f465--- /dev/null+++ b/src/common/image.h@@ -0,0 +1,44 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * image.h - pictures read from their files (PNG, JPEG) into 8-bit RGB, as+ * the vision models take them: the pixels Pillow's convert("RGB") gives+ * (an alpha channel dropped, 16-bit samples their high byte, a 16-bit gray+ * clipped at 255), which is what the models' processors gave them. Janas's+ * own decoders (image_png.c, image_jpeg.c), with bounds on sizes so a file+ * cannot make them take more memory than the picture.+ */+#ifndef JANAS_COMMON_IMAGE_H+#define JANAS_COMMON_IMAGE_H++#include <stddef.h>+#include <stdint.h>++#define JANAS_IMAGE_MAX_SIDE 16384+#define JANAS_IMAGE_MAX_PIXELS ((size_t)64 << 20)++struct janas_image {+ int w, h;+ uint8_t *rgb; /* w * h * 3 bytes, rows top to bottom */+};++/* n bytes of a PNG or a JPEG: 0 with *im filled (free it with+ janas_image_free), -1 with the reason in why. */+int janas_image_decode(const void *data, size_t n, struct janas_image *im,+ char *why, size_t wn);+void janas_image_free(struct janas_image *im);++/* in resized to w x h into *out (image_resize.c) as Pillow's+ resize(BICUBIC) does - the filter widened when shrinking, so every source+ pixel counts - which is what the vision processors use: 0, or -1 on+ memory or a size of 0. */+int janas_image_resize(const struct janas_image *in, int w, int h,+ struct janas_image *out);++/* the formats, called by janas_image_decode */+int janas_png_decode(const uint8_t *p, size_t n, struct janas_image *im,+ char *why, size_t wn);+int janas_jpeg_decode(const uint8_t *p, size_t n, struct janas_image *im,+ char *why, size_t wn);++#endifdiff --git a/src/common/image_jpeg.c b/src/common/image_jpeg.cnew file mode 100644index 0000000..2179219--- /dev/null+++ b/src/common/image_jpeg.c@@ -0,0 +1,856 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * image_jpeg.c - JPEG (ITU T.81) into 8-bit RGB (image.h): baseline and+ * extended sequential and progressive Huffman coding, 8-bit samples, one+ * component (gray) or three (YCbCr, or RGB when the file says so), any+ * sampling factors, restart intervals.+ *+ * Every coefficient of the picture is decoded first (progressive scans+ * refine them in place), then each block is dequantized and turned into+ * samples by an integer inverse DCT, the color planes are brought to full+ * size and converted. The arithmetic is chosen to give the pixels the+ * common decoders give (Pillow, through libjpeg): the IDCT is the+ * separable integer form of the Loeffler-Ligtenberg-Moschytz transform+ * with 13-bit constants and two extra bits between the passes; planes+ * sampled at half width, half height or both are brought up by+ * interpolating with the neighbours at 3:1 ("fancy" upsampling), other+ * ratios by repeating samples; YCbCr goes to RGB with the JFIF+ * coefficients in 16-bit fixed point.+ */+#include "image.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++static const uint8_t ZZ[64] = {+ 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,+ 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,+ 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,+ 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63};++struct huff {+ int ok;+ uint8_t val[256];+ int mincode[17], maxcode[18], valptr[17];+};++struct comp {+ int id, h, v, tq;+ int td, ta; /* the scan's tables */+ int bw, bh; /* blocks across and down, padded to whole MCUs */+ int cw, ch; /* blocks the component's samples cover */+ int dc_pred;+ int16_t *coef; /* bw * bh blocks of 64, in natural order */+ uint8_t *plane;+};++struct jpg {+ const uint8_t *p;+ size_t n, pos;+ uint32_t bits;+ int nbits;+ int marker; /* a marker met inside entropy data */+ int w, h, nc, hmax, vmax, mcux, mcuy, progressive;+ struct comp c[3];+ uint16_t q[4][64];+ struct huff dc[4], ac[4];+ int restart;+ int eobrun;+ int adobe, transform;+ const char *err;+};++/* ---- bits ---- */++static int bit(struct jpg *j)+{+ if (!j->nbits) {+ int b = 0;+ if (!j->marker && j->pos < j->n) {+ b = j->p[j->pos];+ if (b == 0xff) {+ int nx = j->pos + 1 < j->n ? j->p[j->pos + 1] : 0;+ if (nx == 0) {+ j->pos += 2; /* a stuffed 0xff */+ } else {+ j->marker = nx; /* the data ends: zeros from here */+ b = 0;+ }+ } else {+ j->pos++;+ }+ }+ j->bits = (uint32_t)b;+ j->nbits = 8;+ }+ j->nbits--;+ return (int)(j->bits >> j->nbits) & 1;+}++static int receive(struct jpg *j, int s)+{+ int v = 0;+ for (int i = 0; i < s; i++)+ v = v << 1 | bit(j);+ return v;+}++static int extend(int v, int s)+{+ return s && v < (1 << (s - 1)) ? v - (1 << s) + 1 : v;+}++static int decode(struct jpg *j, const struct huff *h)+{+ if (!h->ok) {+ j->err = "a scan uses a Huffman table the file did not give";+ return 0;+ }+ int code = 0;+ for (int l = 1; l <= 16; l++) {+ code = code << 1 | bit(j);+ if (code <= h->maxcode[l])+ return h->val[h->valptr[l] + code - h->mincode[l]];+ }+ j->err = "a bad Huffman code";+ return 0;+}++/* ---- markers ---- */++static int u16(const uint8_t *p)+{+ return p[0] << 8 | p[1];+}++static void dht(struct jpg *j, const uint8_t *d, size_t len)+{+ while (len > 0 && !j->err) {+ if (len < 17) {+ j->err = "a bad Huffman table";+ return;+ }+ int tc = d[0] >> 4, th = d[0] & 15;+ if (tc > 1 || th > 3) {+ j->err = "a bad Huffman table";+ return;+ }+ struct huff *h = tc ? &j->ac[th] : &j->dc[th];+ int count = 0, code = 0, k = 0;+ for (int l = 1; l <= 16; l++)+ count += d[l];+ if (count > 256 || (size_t)(17 + count) > len) {+ j->err = "a bad Huffman table";+ return;+ }+ memcpy(h->val, d + 17, (size_t)count);+ for (int l = 1; l <= 16; l++) {+ h->valptr[l] = k;+ h->mincode[l] = code;+ code += d[l];+ k += d[l];+ h->maxcode[l] = d[l] ? code - 1 : -1;+ code <<= 1;+ }+ h->maxcode[17] = 0x7fffffff;+ h->ok = 1;+ d += 17 + count;+ len -= (size_t)(17 + count);+ }+}++static void dqt(struct jpg *j, const uint8_t *d, size_t len)+{+ while (len > 0) {+ int pq = d[0] >> 4, tq = d[0] & 15;+ size_t need = 1 + (pq ? 128 : 64);+ if (pq > 1 || tq > 3 || len < need) {+ j->err = "a bad quantization table";+ return;+ }+ for (int i = 0; i < 64; i++)+ j->q[tq][ZZ[i]] = (uint16_t)(pq ? u16(d + 1 + 2 * i) : d[1 + i]);+ d += need;+ len -= need;+ }+}++static void sof(struct jpg *j, const uint8_t *d, size_t len)+{+ if (len < 6 || d[0] != 8) {+ j->err = "a JPEG of other than 8-bit samples";+ return;+ }+ j->h = u16(d + 1);+ j->w = u16(d + 3);+ j->nc = d[5];+ if (j->nc != 1 && j->nc != 3) {+ j->err = "a JPEG of other than one or three components (CMYK?)";+ return;+ }+ if (len < 6 + 3 * (size_t)j->nc || !j->w || !j->h) {+ j->err = "a bad frame header";+ return;+ }+ if (j->w > JANAS_IMAGE_MAX_SIDE || j->h > JANAS_IMAGE_MAX_SIDE ||+ (size_t)j->w * (size_t)j->h > JANAS_IMAGE_MAX_PIXELS) {+ j->err = "a picture larger than allowed (16384 a side, 64 Mpixel)";+ return;+ }+ j->hmax = j->vmax = 1;+ for (int i = 0; i < j->nc; i++) {+ struct comp *c = &j->c[i];+ c->id = d[6 + 3 * i];+ c->h = d[7 + 3 * i] >> 4;+ c->v = d[7 + 3 * i] & 15;+ c->tq = d[8 + 3 * i];+ if (c->h < 1 || c->h > 4 || c->v < 1 || c->v > 4 || c->tq > 3) {+ j->err = "a bad frame header";+ return;+ }+ if (c->h > j->hmax)+ j->hmax = c->h;+ if (c->v > j->vmax)+ j->vmax = c->v;+ }+ j->mcux = (j->w + 8 * j->hmax - 1) / (8 * j->hmax);+ j->mcuy = (j->h + 8 * j->vmax - 1) / (8 * j->vmax);+ /* every block costs at least a bit of entropy-coded data: a header+ claiming more blocks than the file can hold is refused before the+ memory is taken */+ size_t blocks = 0;+ for (int i = 0; i < j->nc; i++)+ blocks += (size_t)j->mcux * (size_t)j->c[i].h * (size_t)j->mcuy *+ (size_t)j->c[i].v;+ if (blocks > 8 * j->n + 4096) {+ j->err = "a picture larger than its data";+ return;+ }+ for (int i = 0; i < j->nc; i++) {+ struct comp *c = &j->c[i];+ int sw = (j->w * c->h + j->hmax - 1) / j->hmax;+ int sh = (j->h * c->v + j->vmax - 1) / j->vmax;+ c->cw = (sw + 7) / 8;+ c->ch = (sh + 7) / 8;+ c->bw = j->mcux * c->h;+ c->bh = j->mcuy * c->v;+ c->coef = calloc((size_t)c->bw * (size_t)c->bh * 64, sizeof(int16_t));+ if (!c->coef) {+ j->err = "out of memory";+ return;+ }+ }+}++/* ---- entropy-coded blocks ---- */++static void block_seq(struct jpg *j, struct comp *c, int16_t *b)+{+ int t = decode(j, &j->dc[c->td]);+ if (t > 11) {+ j->err = "a bad DC difference";+ return;+ }+ c->dc_pred += extend(receive(j, t), t);+ b[0] = (int16_t)c->dc_pred;+ for (int k = 1; k < 64 && !j->err;) {+ int rs = decode(j, &j->ac[c->ta]), r = rs >> 4, s = rs & 15;+ if (s) {+ k += r;+ if (k > 63) {+ j->err = "a bad AC run";+ return;+ }+ b[ZZ[k++]] = (int16_t)extend(receive(j, s), s);+ } else if (r == 15) {+ k += 16;+ } else {+ break;+ }+ }+}++static void dc_first(struct jpg *j, struct comp *c, int16_t *b, int al)+{+ int t = decode(j, &j->dc[c->td]);+ if (t > 11) {+ j->err = "a bad DC difference";+ return;+ }+ c->dc_pred += extend(receive(j, t), t);+ b[0] = (int16_t)(c->dc_pred * (1 << al));+}++static void dc_refine(struct jpg *j, int16_t *b, int al)+{+ if (bit(j))+ b[0] = (int16_t)(b[0] | (1 << al));+}++static void ac_first(struct jpg *j, struct comp *c, int16_t *b, int ss, int se,+ int al)+{+ if (j->eobrun) {+ j->eobrun--;+ return;+ }+ for (int k = ss; k <= se && !j->err;) {+ int rs = decode(j, &j->ac[c->ta]), r = rs >> 4, s = rs & 15;+ if (s) {+ k += r;+ if (k > se) {+ j->err = "a bad AC run";+ return;+ }+ b[ZZ[k++]] = (int16_t)(extend(receive(j, s), s) * (1 << al));+ } else if (r == 15) {+ k += 16;+ } else {+ j->eobrun = (1 << r) - 1;+ if (r)+ j->eobrun += receive(j, r);+ break;+ }+ }+}++/* the coefficients already non-zero in k.. get their correction bits;+ with zeros to skip (r >= 0) it stops at the r+1-th zero (its index) */+static int refine_nonzero(struct jpg *j, int16_t *b, int k, int se, int r,+ int p1)+{+ for (; k <= se; k++) {+ int16_t *c = &b[ZZ[k]];+ if (*c) {+ if (bit(j) && (*c & p1) == 0)+ *c = (int16_t)(*c >= 0 ? *c + p1 : *c - p1);+ } else if (r >= 0) {+ if (r == 0)+ return k;+ r--;+ }+ }+ return k;+}++static void ac_refine(struct jpg *j, struct comp *c, int16_t *b, int ss, int se,+ int al)+{+ int p1 = 1 << al;+ int k = ss;+ if (!j->eobrun) {+ while (k <= se && !j->err) {+ int rs = decode(j, &j->ac[c->ta]), r = rs >> 4, s = rs & 15;+ int value = 0;+ if (s) {+ if (s != 1) {+ j->err = "a bad refinement";+ return;+ }+ value = bit(j) ? p1 : -p1;+ } else if (r != 15) {+ j->eobrun = 1 << r;+ if (r)+ j->eobrun += receive(j, r);+ break; /* the rest of the band below, as end of band */+ }+ /* r zeros skipped (the non-zero ones refined on the way),+ then the new coefficient (or 16 zeros) */+ k = refine_nonzero(j, b, k, se, r, p1);+ if (k > se) {+ if (s)+ j->err = "a bad refinement";+ return;+ }+ if (s)+ b[ZZ[k]] = (int16_t)value;+ k++;+ }+ }+ if (j->eobrun) {+ refine_nonzero(j, b, k, se, -1, p1);+ j->eobrun--;+ }+}++/* ---- scans ---- */++static int expect_restart(struct jpg *j)+{+ /* to the marker: RSTn expected */+ j->nbits = 0;+ if (!j->marker) {+ while (j->pos + 1 < j->n &&+ !(j->p[j->pos] == 0xff && j->p[j->pos + 1] != 0 &&+ j->p[j->pos + 1] != 0xff))+ j->pos++;+ if (j->pos + 1 < j->n)+ j->marker = j->p[j->pos + 1];+ }+ if (j->marker < 0xd0 || j->marker > 0xd7)+ return -1;+ j->pos += 2;+ j->marker = 0;+ j->eobrun = 0;+ for (int i = 0; i < j->nc; i++)+ j->c[i].dc_pred = 0;+ return 0;+}++static void one_block(struct jpg *j, struct comp *c, int16_t *b, int ss, int se,+ int ah, int al)+{+ if (!j->progressive)+ block_seq(j, c, b);+ else if (ss == 0)+ ah ? dc_refine(j, b, al) : dc_first(j, c, b, al);+ else+ ah ? ac_refine(j, c, b, ss, se, al) : ac_first(j, c, b, ss, se, al);+}++static void sos(struct jpg *j, const uint8_t *d, size_t len)+{+ if (!j->nc || len < 1) {+ j->err = "a scan before the frame";+ return;+ }+ int ns = d[0];+ if (ns < 1 || ns > j->nc || len < 4 + 2 * (size_t)ns) {+ j->err = "a bad scan header";+ return;+ }+ struct comp *sc[3];+ for (int i = 0; i < ns; i++) {+ int id = d[1 + 2 * i];+ sc[i] = NULL;+ for (int k = 0; k < j->nc; k++)+ if (j->c[k].id == id)+ sc[i] = &j->c[k];+ if (!sc[i]) {+ j->err = "a scan of a component the frame does not have";+ return;+ }+ sc[i]->td = d[2 + 2 * i] >> 4;+ sc[i]->ta = d[2 + 2 * i] & 15;+ if (sc[i]->td > 3 || sc[i]->ta > 3) {+ j->err = "a bad scan header";+ return;+ }+ }+ int ss = d[1 + 2 * ns], se = d[2 + 2 * ns];+ int ah = d[3 + 2 * ns] >> 4, al = d[3 + 2 * ns] & 15;+ if (j->progressive ? (ss > se || se > 63 || (ss == 0 && se) ||+ (ss && ns != 1) || al > 13)+ : (ss || se != 63 || ah || al)) {+ j->err = "a bad scan header";+ return;+ }+ j->pos += len; /* the data follows */+ j->bits = 0;+ j->nbits = 0;+ j->marker = 0;+ j->eobrun = 0;+ for (int i = 0; i < j->nc; i++)+ j->c[i].dc_pred = 0;+ int todo = j->restart;+ if (ns == 1) { /* not interleaved: the component's own blocks */+ struct comp *c = sc[0];+ for (int by = 0; by < c->ch && !j->err; by++)+ for (int bx = 0; bx < c->cw && !j->err; bx++) {+ if (j->restart && !todo) {+ if (expect_restart(j) != 0) {+ j->err = "a restart marker missing";+ return;+ }+ todo = j->restart;+ }+ one_block(j, c,+ c->coef ++ ((size_t)by * (size_t)c->bw + (size_t)bx) * 64,+ ss, se, ah, al);+ todo--;+ }+ } else {+ for (int my = 0; my < j->mcuy && !j->err; my++)+ for (int mx = 0; mx < j->mcux && !j->err; mx++) {+ if (j->restart && !todo) {+ if (expect_restart(j) != 0) {+ j->err = "a restart marker missing";+ return;+ }+ todo = j->restart;+ }+ for (int i = 0; i < ns; i++) {+ struct comp *c = sc[i];+ for (int v = 0; v < c->v; v++)+ for (int h = 0; h < c->h; h++) {+ size_t by = (size_t)(my * c->v + v);+ size_t bx = (size_t)(mx * c->h + h);+ one_block(j, c,+ c->coef + (by * (size_t)c->bw + bx) * 64,+ ss, se, ah, al);+ }+ }+ todo--;+ }+ }+ /* past the data, to the next marker */+ j->nbits = 0;+ while (j->pos + 1 < j->n &&+ !(j->p[j->pos] == 0xff && j->p[j->pos + 1] != 0 &&+ !(j->p[j->pos + 1] >= 0xd0 && j->p[j->pos + 1] <= 0xd7)))+ j->pos++;+}++/* ---- samples ---- */++#define CB 13 /* bits of the constants */+#define P1 2 /* bits kept between the passes */+#define F(x) ((int64_t)((x) * (1 << CB) + 0.5))+#define F16(x) ((int32_t)((x) * 65536 + 0.5)) /* the color constants */++static uint8_t clamp(int64_t v)+{+ return (uint8_t)(v < 0 ? 0 : v > 255 ? 255 : v);+}++/* one 8x8 block: dequantized coefficients in, samples out (stride s); in+ 64 bits, as libjpeg-turbo's long: a corrupt file's coefficients cannot+ overflow it */+static void idct(const int16_t *in, const uint16_t *q, uint8_t *out, size_t s)+{+ int64_t ws[64];+ for (int x = 0; x < 8; x++) { /* columns */+ int64_t c[8];+ for (int y = 0; y < 8; y++)+ c[y] = (int64_t)in[y * 8 + x] * q[y * 8 + x];+ int64_t z1 = (c[2] + c[6]) * F(0.541196100);+ int64_t t2 = z1 + c[6] * -F(1.847759065);+ int64_t t3 = z1 + c[2] * F(0.765366865);+ int64_t t0 = (c[0] + c[4]) * (1 << CB);+ int64_t t1 = (c[0] - c[4]) * (1 << CB);+ int64_t e10 = t0 + t3, e13 = t0 - t3, e11 = t1 + t2, e12 = t1 - t2;+ int64_t o0 = c[7], o1 = c[5], o2 = c[3], o3 = c[1];+ int64_t a1 = o0 + o3, a2 = o1 + o2, a3 = o0 + o2, a4 = o1 + o3;+ int64_t z5 = (a3 + a4) * F(1.175875602);+ o0 *= F(0.298631336);+ o1 *= F(2.053119869);+ o2 *= F(3.072711026);+ o3 *= F(1.501321110);+ a1 *= -F(0.899976223);+ a2 *= -F(2.562915447);+ a3 = a3 * -F(1.961570560) + z5;+ a4 = a4 * -F(0.390180644) + z5;+ o0 += a1 + a3;+ o1 += a2 + a4;+ o2 += a2 + a3;+ o3 += a1 + a4;+ const int sh = CB - P1;+ const int64_t rd = 1 << (sh - 1);+ ws[0 * 8 + x] = (e10 + o3 + rd) >> sh;+ ws[7 * 8 + x] = (e10 - o3 + rd) >> sh;+ ws[1 * 8 + x] = (e11 + o2 + rd) >> sh;+ ws[6 * 8 + x] = (e11 - o2 + rd) >> sh;+ ws[2 * 8 + x] = (e12 + o1 + rd) >> sh;+ ws[5 * 8 + x] = (e12 - o1 + rd) >> sh;+ ws[3 * 8 + x] = (e13 + o0 + rd) >> sh;+ ws[4 * 8 + x] = (e13 - o0 + rd) >> sh;+ }+ for (int y = 0; y < 8; y++) { /* rows */+ const int64_t *c = ws + y * 8;+ int64_t z1 = (c[2] + c[6]) * F(0.541196100);+ int64_t t2 = z1 + c[6] * -F(1.847759065);+ int64_t t3 = z1 + c[2] * F(0.765366865);+ int64_t t0 = (c[0] + c[4]) * (1 << CB);+ int64_t t1 = (c[0] - c[4]) * (1 << CB);+ int64_t e10 = t0 + t3, e13 = t0 - t3, e11 = t1 + t2, e12 = t1 - t2;+ int64_t o0 = c[7], o1 = c[5], o2 = c[3], o3 = c[1];+ int64_t a1 = o0 + o3, a2 = o1 + o2, a3 = o0 + o2, a4 = o1 + o3;+ int64_t z5 = (a3 + a4) * F(1.175875602);+ o0 *= F(0.298631336);+ o1 *= F(2.053119869);+ o2 *= F(3.072711026);+ o3 *= F(1.501321110);+ a1 *= -F(0.899976223);+ a2 *= -F(2.562915447);+ a3 = a3 * -F(1.961570560) + z5;+ a4 = a4 * -F(0.390180644) + z5;+ o0 += a1 + a3;+ o1 += a2 + a4;+ o2 += a2 + a3;+ o3 += a1 + a4;+ const int sh = CB + P1 + 3;+ const int64_t rd = 1 << (sh - 1);+ uint8_t *o = out + (size_t)y * s;+ o[0] = clamp(((e10 + o3 + rd) >> sh) + 128);+ o[7] = clamp(((e10 - o3 + rd) >> sh) + 128);+ o[1] = clamp(((e11 + o2 + rd) >> sh) + 128);+ o[6] = clamp(((e11 - o2 + rd) >> sh) + 128);+ o[2] = clamp(((e12 + o1 + rd) >> sh) + 128);+ o[5] = clamp(((e12 - o1 + rd) >> sh) + 128);+ o[3] = clamp(((e13 + o0 + rd) >> sh) + 128);+ o[4] = clamp(((e13 - o0 + rd) >> sh) + 128);+ }+}++/* the component's samples at full size, row y (w samples) into out */+static void full_row(const struct jpg *j, const struct comp *c, int y,+ uint8_t *out)+{+ int fx = j->hmax / c->h, fy = j->vmax / c->v;+ size_t pw = (size_t)c->bw * 8;+ /* the component's real size, as the decoders see it */+ int sw = (j->w * c->h + j->hmax - 1) / j->hmax;+ int sh = (j->h * c->v + j->vmax - 1) / j->vmax;+ /* interpolated where the decoders interpolate: half width only when+ the plane is wider than two samples */+ int fancy_h = fx == 2 && j->hmax % c->h == 0 && sw > 2;+ int fancy_v = fy == 2 && j->vmax % c->v == 0 && (fx == 1 || sw > 2);+ int sy = y / fy;+ const uint8_t *row = c->plane + (size_t)sy * pw;+ if (fx == 1 && fy == 1) {+ memcpy(out, row, (size_t)j->w);+ return;+ }+ if (fancy_h && fancy_v && fy == 2 && fx == 2) {+ /* 2x2: rows mixed 3:1 with the one above or below, then columns */+ int ny =+ y % 2 ? (sy + 1 < sh ? sy + 1 : sh - 1) : (sy > 0 ? sy - 1 : 0);+ const uint8_t *r1 = c->plane + (size_t)ny * pw;+ int last = 0, cur = row[0] * 3 + r1[0];+ int nx = sw > 1 ? row[1] * 3 + r1[1] : cur;+ uint8_t *o = out;+ int ow = 0;+ if (sw == 1) {+ o[0] = (uint8_t)((cur * 4 + 8) >> 4);+ if (j->w > 1)+ o[1] = (uint8_t)((cur * 4 + 7) >> 4);+ return;+ }+ o[ow++] = (uint8_t)((cur * 4 + 8) >> 4);+ o[ow++] = (uint8_t)((cur * 3 + nx + 7) >> 4);+ last = cur;+ cur = nx;+ for (int x = 1; x < sw - 1; x++) {+ nx = row[x + 1] * 3 + r1[x + 1];+ if (ow < j->w)+ o[ow++] = (uint8_t)((cur * 3 + last + 8) >> 4);+ if (ow < j->w)+ o[ow++] = (uint8_t)((cur * 3 + nx + 7) >> 4);+ last = cur;+ cur = nx;+ }+ if (ow < j->w)+ o[ow++] = (uint8_t)((cur * 3 + last + 8) >> 4);+ if (ow < j->w)+ o[ow++] = (uint8_t)((cur * 4 + 7) >> 4);+ return;+ }+ if (fancy_h && fx == 2 && fy == 1) {+ /* 2x1: columns mixed 3:1 with the left or right one */+ uint8_t *o = out;+ int ow = 0;+ if (sw == 1) {+ o[0] = row[0];+ if (j->w > 1)+ o[1] = row[0];+ return;+ }+ o[ow++] = row[0];+ o[ow++] = (uint8_t)((row[0] * 3 + row[1] + 2) >> 2);+ for (int x = 1; x < sw - 1; x++) {+ int v = row[x] * 3;+ if (ow < j->w)+ o[ow++] = (uint8_t)((v + row[x - 1] + 1) >> 2);+ if (ow < j->w)+ o[ow++] = (uint8_t)((v + row[x + 1] + 2) >> 2);+ }+ if (ow < j->w)+ o[ow++] = (uint8_t)((row[sw - 1] * 3 + row[sw - 2] + 1) >> 2);+ if (ow < j->w)+ o[ow++] = row[sw - 1];+ return;+ }+ if (fancy_v && fy == 2 && fx == 1) {+ /* 1x2: rows mixed 3:1 with the one above or below */+ int ny =+ y % 2 ? (sy + 1 < sh ? sy + 1 : sh - 1) : (sy > 0 ? sy - 1 : 0);+ const uint8_t *r1 = c->plane + (size_t)ny * pw;+ int bias = y % 2 ? 2 : 1;+ for (int x = 0; x < j->w; x++)+ out[x] = (uint8_t)((row[x] * 3 + r1[x] + bias) >> 2);+ return;+ }+ for (int x = 0; x < j->w; x++) /* other ratios: samples repeated */+ out[x] = row[x / fx];+}++static int finish(struct jpg *j, struct janas_image *im)+{+ for (int i = 0; i < j->nc; i++) {+ struct comp *c = &j->c[i];+ size_t pw = (size_t)c->bw * 8;+ c->plane = malloc(pw * (size_t)c->bh * 8);+ if (!c->plane)+ return -1;+ for (int by = 0; by < c->bh; by++)+ for (int bx = 0; bx < c->bw; bx++)+ idct(c->coef + ((size_t)by * (size_t)c->bw + (size_t)bx) * 64,+ j->q[c->tq],+ c->plane + (size_t)by * 8 * pw + (size_t)bx * 8, pw);+ }+ im->rgb = malloc((size_t)j->w * (size_t)j->h * 3);+ uint8_t *rows = malloc((size_t)j->w * 3);+ if (!im->rgb || !rows) {+ free(rows);+ return -1;+ }+ /* RGB when the file says so: an Adobe marker without transform, or+ components named R, G, B */+ int rgb = j->nc == 3 &&+ ((j->adobe && j->transform == 0) ||+ (j->c[0].id == 'R' && j->c[1].id == 'G' && j->c[2].id == 'B'));+ int32_t cr_r[256], cb_b[256], cr_g[256], cb_g[256];+ for (int i = 0; i < 256; i++) {+ int x = i - 128;+ const int32_t half = 1 << 15;+ cr_r[i] = (F16(1.40200) * x + half) >> 16;+ cb_b[i] = (F16(1.77200) * x + half) >> 16;+ cr_g[i] = -F16(0.71414) * x;+ cb_g[i] = -F16(0.34414) * x + half;+ }+ for (int y = 0; y < j->h; y++) {+ for (int i = 0; i < j->nc; i++)+ full_row(j, &j->c[i], y, rows + (size_t)i * (size_t)j->w);+ uint8_t *o = im->rgb + (size_t)y * (size_t)j->w * 3;+ for (int x = 0; x < j->w; x++, o += 3) {+ int Y = rows[x];+ if (j->nc == 1) {+ o[0] = o[1] = o[2] = (uint8_t)Y;+ } else if (rgb) {+ o[0] = (uint8_t)Y;+ o[1] = rows[(size_t)j->w + (size_t)x];+ o[2] = rows[2 * (size_t)j->w + (size_t)x];+ } else {+ int cb = rows[(size_t)j->w + (size_t)x];+ int cr = rows[2 * (size_t)j->w + (size_t)x];+ o[0] = clamp(Y + cr_r[cr]);+ o[1] = clamp(Y + ((cb_g[cb] + cr_g[cr]) >> 16));+ o[2] = clamp(Y + cb_b[cb]);+ }+ }+ }+ free(rows);+ im->w = j->w;+ im->h = j->h;+ return 0;+}++int janas_jpeg_decode(const uint8_t *p, size_t n, struct janas_image *im,+ char *why, size_t wn)+{+ struct jpg *j = calloc(1, sizeof(*j));+ memset(im, 0, sizeof(*im));+ if (!j) {+ snprintf(why, wn, "out of memory");+ return -1;+ }+ j->p = p;+ j->n = n;+ j->pos = 2;+ int frame = 0, ended = 0, scans = 0;+ while (!j->err && !ended) {+ /* the next marker (fill bytes 0xff allowed) */+ while (j->pos < n && p[j->pos] != 0xff)+ j->pos++;+ while (j->pos < n && p[j->pos] == 0xff)+ j->pos++;+ if (j->pos >= n) {+ if (!scans)+ j->err = "the file ends before its picture";+ break;+ }+ int m = p[j->pos++];+ if (m == 0xd9) {+ ended = 1;+ break;+ }+ if (m == 0xd8 || (m >= 0xd0 && m <= 0xd7) || m == 0x01)+ continue;+ if (n - j->pos < 2) {+ j->err = "the file ends inside a marker";+ break;+ }+ size_t len = (size_t)u16(p + j->pos);+ if (len < 2 || len > n - j->pos) {+ j->err = "a marker runs past the end of the file";+ break;+ }+ const uint8_t *d = p + j->pos + 2;+ size_t dl = len - 2;+ switch (m) {+ case 0xc0:+ case 0xc1:+ case 0xc2:+ if (frame) {+ j->err = "two frames in one file";+ break;+ }+ frame = 1;+ j->progressive = m == 0xc2;+ sof(j, d, dl);+ break;+ case 0xc4:+ dht(j, d, dl);+ break;+ case 0xdb:+ dqt(j, d, dl);+ break;+ case 0xdd:+ j->restart = dl >= 2 ? u16(d) : 0;+ break;+ case 0xee: /* Adobe: whether YCbCr is used */+ if (dl >= 12 && !memcmp(d, "Adobe", 5)) {+ j->adobe = 1;+ j->transform = d[11];+ }+ break;+ case 0xda:+ if (!frame) {+ j->err = "a scan before the frame";+ break;+ }+ j->pos += 2;+ sos(j, d, dl);+ scans++;+ continue; /* sos moved pos past the scan's data */+ default:+ if ((m >= 0xc3 && m <= 0xcf) && m != 0xc4 && m != 0xc8 && m != 0xcc)+ j->err = "a JPEG coding this does not read (lossless or "+ "arithmetic)";+ }+ j->pos += len;+ }+ int rc = -1;+ if (!j->err && !scans)+ j->err = "no picture in the file";+ if (!j->err) {+ rc = finish(j, im);+ if (rc != 0)+ j->err = "out of memory";+ }+ if (j->err) {+ snprintf(why, wn, "JPEG: %s", j->err);+ free(im->rgb);+ memset(im, 0, sizeof(*im));+ rc = -1;+ }+ for (int i = 0; i < 3; i++) {+ free(j->c[i].coef);+ free(j->c[i].plane);+ }+ free(j);+ return rc;+}diff --git a/src/common/image_png.c b/src/common/image_png.cnew file mode 100644index 0000000..e4f7997--- /dev/null+++ b/src/common/image_png.c@@ -0,0 +1,275 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * image_png.c - PNG (ISO/IEC 15948) into 8-bit RGB (image.h): every color+ * type and bit depth, palettes, Adam7 interlacing. The chunks are walked+ * with their lengths checked, the pixel data inflated into a buffer of+ * exactly the size the header says (inflate.c), the rows unfiltered, the+ * samples made RGB; transparency (tRNS, alpha) is dropped. The result is+ Pillow's convert("RGB"), pixel for pixel.+ */+#include "image.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "inflate.h"++static uint32_t be32(const uint8_t *p)+{+ return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 |+ p[3];+}++static int paeth(int a, int b, int c)+{+ int p = a + b - c, pa = abs(p - a), pb = abs(p - b), pc = abs(p - c);+ return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;+}++/* rows of rb bytes (each after its filter byte), bpp bytes a pixel for the+ filters: unfiltered in place, 0 or -1 */+static int unfilter(uint8_t *d, int rows, size_t rb, int bpp)+{+ uint8_t *prev = NULL;+ for (int y = 0; y < rows; y++) {+ uint8_t f = d[0], *r = d + 1;+ for (size_t i = 0; i < rb; i++) {+ int a = i >= (size_t)bpp ? r[i - (size_t)bpp] : 0;+ int b = prev ? prev[i] : 0;+ int c = prev && i >= (size_t)bpp ? prev[i - (size_t)bpp] : 0;+ switch (f) {+ case 0:+ break;+ case 1:+ r[i] = (uint8_t)(r[i] + a);+ break;+ case 2:+ r[i] = (uint8_t)(r[i] + b);+ break;+ case 3:+ r[i] = (uint8_t)(r[i] + ((a + b) >> 1));+ break;+ case 4:+ r[i] = (uint8_t)(r[i] + paeth(a, b, c));+ break;+ default:+ return -1;+ }+ }+ prev = r;+ d += rb + 1;+ }+ return 0;+}++struct png {+ int w, h, depth, ctype, channels;+ uint8_t pal[256][3];+ int npal;+};++/* sample k of a row (depth bits each), scaled to 8 bits unless it is a+ palette index */+static int sample(const struct png *g, const uint8_t *row, size_t k)+{+ switch (g->depth) {+ case 16:+ /* the high byte; a lone gray channel clipped at 255 instead, as+ Pillow reads it (mode I;16), so the vision models get what their+ processors gave them in training */+ if (g->ctype == 0) {+ int v = row[2 * k] << 8 | row[2 * k + 1];+ return v > 255 ? 255 : v;+ }+ return row[2 * k];+ case 8:+ return row[k];+ default: {+ size_t bit = k * (size_t)g->depth;+ int v = (row[bit >> 3] >> (8 - g->depth - (int)(bit & 7))) &+ ((1 << g->depth) - 1);+ if (g->ctype == 3)+ return v;+ return v * 255 / ((1 << g->depth) - 1);+ }+ }+}++/* row y of a pass of pw pixels into the picture, at x0 + x * dx */+static void put_row(const struct png *g, const uint8_t *row, int pw,+ uint8_t *dst, int x0, int dx)+{+ for (int x = 0; x < pw; x++) {+ uint8_t *o = dst + (size_t)(x0 + x * dx) * 3;+ size_t s = (size_t)x * (size_t)g->channels;+ switch (g->ctype) {+ case 0:+ case 4: { /* gray, gray + alpha */+ int v = sample(g, row, s);+ o[0] = o[1] = o[2] = (uint8_t)v;+ break;+ }+ case 2:+ case 6: /* RGB, RGBA */+ o[0] = (uint8_t)sample(g, row, s);+ o[1] = (uint8_t)sample(g, row, s + 1);+ o[2] = (uint8_t)sample(g, row, s + 2);+ break;+ default: { /* palette */+ int i = sample(g, row, s);+ if (i < g->npal)+ memcpy(o, g->pal[i], 3);+ else+ o[0] = o[1] = o[2] = 0;+ }+ }+ }+}++static size_t row_bytes(const struct png *g, int w)+{+ return ((size_t)w * (size_t)g->channels * (size_t)g->depth + 7) / 8;+}++int janas_png_decode(const uint8_t *p, size_t n, struct janas_image *im,+ char *why, size_t wn)+{+ static const uint8_t SIG[8] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'};+ memset(im, 0, sizeof(*im));+ if (n < 8 || memcmp(p, SIG, 8)) {+ snprintf(why, wn, "not a PNG");+ return -1;+ }+ struct png g = {0};+ uint8_t *idat = NULL;+ size_t nidat = 0;+ int have_hdr = 0, interlace = 0, ended = 0;+ size_t pos = 8;+ const char *err = NULL;+ while (!err && !ended) {+ if (n - pos < 12) {+ err = "the file ends inside a chunk";+ break;+ }+ uint32_t len = be32(p + pos);+ const uint8_t *type = p + pos + 4, *d = p + pos + 8;+ if (len > n - pos - 12) {+ err = "a chunk runs past the end of the file";+ break;+ }+ if (!memcmp(type, "IHDR", 4)) {+ if (len != 13 || have_hdr) {+ err = "a bad header";+ break;+ }+ g.w = (int)be32(d) > 0 ? (int)be32(d) : 0;+ g.h = (int)be32(d + 4) > 0 ? (int)be32(d + 4) : 0;+ g.depth = d[8];+ g.ctype = d[9];+ interlace = d[12];+ static const int CH[7] = {1, 0, 3, 1, 2, 0, 4};+ g.channels = g.ctype <= 6 ? CH[g.ctype] : 0;+ int dep = g.depth;+ int ok =+ g.channels && d[10] == 0 && d[11] == 0 && interlace < 2 &&+ (g.ctype == 0+ ? dep == 1 || dep == 2 || dep == 4 || dep == 8 || dep == 16+ : g.ctype == 3 ? dep == 1 || dep == 2 || dep == 4 || dep == 8+ : dep == 8 || dep == 16);+ if (!ok)+ err = "a color type and depth PNG does not have";+ else if (!g.w || !g.h || g.w > JANAS_IMAGE_MAX_SIDE ||+ g.h > JANAS_IMAGE_MAX_SIDE ||+ (size_t)g.w * (size_t)g.h > JANAS_IMAGE_MAX_PIXELS)+ err = "a picture larger than allowed (16384 a side, 64 "+ "Mpixel)";+ have_hdr = 1;+ } else if (!memcmp(type, "PLTE", 4)) {+ if (len % 3 || len > 768) {+ err = "a bad palette";+ break;+ }+ g.npal = (int)len / 3;+ memcpy(g.pal, d, len);+ } else if (!memcmp(type, "IDAT", 4)) {+ uint8_t *t = realloc(idat, nidat + len + 1);+ if (!t) {+ err = "out of memory";+ break;+ }+ idat = t;+ memcpy(idat + nidat, d, len);+ nidat += len;+ } else if (!memcmp(type, "IEND", 4)) {+ ended = 1;+ } else if (!(type[0] & 0x20)) {+ err = "a chunk a reader must know and this one does not";+ }+ pos += 12 + (size_t)len;+ }+ if (!err && (!have_hdr || !nidat))+ err = "no picture in the file";+ if (!err && g.ctype == 3 && !g.npal)+ err = "a palette picture without its palette";+ /* the inflated data: each pass's rows, each with its filter byte */+ static const int AX[7] = {0, 4, 0, 2, 0, 1, 0},+ AY[7] = {0, 0, 4, 0, 2, 0, 1};+ static const int DX[7] = {8, 8, 4, 4, 2, 2, 1},+ DY[7] = {8, 8, 8, 4, 4, 2, 2};+ int passes = interlace ? 7 : 1;+ size_t raw = 0;+ for (int i = 0; !err && i < passes; i++) {+ int pw = interlace ? (g.w - AX[i] + DX[i] - 1) / DX[i] : g.w;+ int ph = interlace ? (g.h - AY[i] + DY[i] - 1) / DY[i] : g.h;+ if (pw > 0 && ph > 0)+ raw += (size_t)ph * (row_bytes(&g, pw) + 1);+ }+ /* DEFLATE packs at most about 1032 bytes into one: a header claiming+ more pixels than the data can hold is refused before the memory is+ taken */+ if (!err && raw / 1032 > nidat + 64)+ err = "a picture larger than its data";+ uint8_t *px = err ? NULL : malloc(raw ? raw : 1);+ im->rgb = err ? NULL : malloc((size_t)g.w * (size_t)g.h * 3);+ if (!err && (!px || !im->rgb))+ err = "out of memory";+ char zwhy[128];+ if (!err && janas_zlib_inflate(idat, nidat, px, raw, zwhy, sizeof zwhy)) {+ snprintf(why, wn, "%s", zwhy);+ err = why;+ }+ int bpp = (g.channels * g.depth + 7) / 8;+ uint8_t *d = px;+ for (int i = 0; !err && i < passes; i++) {+ int pw = interlace ? (g.w - AX[i] + DX[i] - 1) / DX[i] : g.w;+ int ph = interlace ? (g.h - AY[i] + DY[i] - 1) / DY[i] : g.h;+ if (pw <= 0 || ph <= 0)+ continue;+ size_t rb = row_bytes(&g, pw);+ if (unfilter(d, ph, rb, bpp) != 0) {+ err = "a row with an unknown filter";+ break;+ }+ for (int y = 0; y < ph; y++) {+ int yy = interlace ? AY[i] + y * DY[i] : y;+ put_row(&g, d + (size_t)y * (rb + 1) + 1, pw,+ im->rgb + (size_t)yy * (size_t)g.w * 3,+ interlace ? AX[i] : 0, interlace ? DX[i] : 1);+ }+ d += (size_t)ph * (rb + 1);+ }+ free(idat);+ free(px);+ if (err) {+ if (err != why)+ snprintf(why, wn, "PNG: %s", err);+ free(im->rgb);+ memset(im, 0, sizeof(*im));+ return -1;+ }+ im->w = g.w;+ im->h = g.h;+ return 0;+}diff --git a/src/common/image_resize.c b/src/common/image_resize.cnew file mode 100644index 0000000..fdcd761--- /dev/null+++ b/src/common/image_resize.c@@ -0,0 +1,144 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * image_resize.c - bicubic resampling of 8-bit RGB (image.h), as Pillow's+ * resize(BICUBIC) computes it, so a vision model gets the pixels its+ * processor gave it in training: separable, rows first then columns, each+ * pass rounded to 8 bits; for each output sample the source span around its+ * centre, the cubic kernel (a = -0.5) widened by the scale when shrinking,+ * the weights normalised to sum 1 and turned into 22-bit fixed point.+ */+#include "image.h"++#include <math.h>+#include <stdlib.h>+#include <string.h>++#define PB 22 /* bits of the weights */++static double cubic(double x)+{+ const double a = -0.5;+ x = fabs(x);+ if (x < 1)+ return ((a + 2) * x - (a + 3)) * x * x + 1;+ if (x < 2)+ return (((x - 5) * x + 8) * x - 4) * a;+ return 0;+}++/* each output sample's first source sample and count, and its weights */+struct taps {+ int ksize;+ int *first, *count;+ int32_t *w; /* out * ksize */+};++static int taps_make(struct taps *t, int in, int out)+{+ double scale = (double)in / out;+ double fs = scale < 1 ? 1 : scale;+ double support = 2.0 * fs;+ t->ksize = (int)ceil(support) * 2 + 1;+ t->first = malloc(sizeof(int) * (size_t)out);+ t->count = malloc(sizeof(int) * (size_t)out);+ t->w = calloc((size_t)out * (size_t)t->ksize, sizeof(int32_t));+ double *k = malloc(sizeof(double) * (size_t)t->ksize);+ if (!t->first || !t->count || !t->w || !k) {+ free(k);+ return -1;+ }+ for (int x = 0; x < out; x++) {+ double centre = (x + 0.5) * scale, sum = 0;+ int lo = (int)(centre - support + 0.5), hi;+ if (lo < 0)+ lo = 0;+ hi = (int)(centre + support + 0.5);+ if (hi > in)+ hi = in;+ int n = hi - lo;+ for (int i = 0; i < n; i++) {+ k[i] = cubic((i + lo - centre + 0.5) / fs);+ sum += k[i];+ }+ for (int i = 0; i < n; i++) {+ double v = sum != 0 ? k[i] / sum : k[i];+ t->w[(size_t)x * (size_t)t->ksize + (size_t)i] =+ (int32_t)(v < 0 ? -0.5 + v * (1 << PB) : 0.5 + v * (1 << PB));+ }+ t->first[x] = lo;+ t->count[x] = n;+ }+ free(k);+ return 0;+}++static void taps_free(struct taps *t)+{+ free(t->first);+ free(t->count);+ free(t->w);+}++static uint8_t clip(int64_t v)+{+ v >>= PB;+ return (uint8_t)(v < 0 ? 0 : v > 255 ? 255 : v);+}++int janas_image_resize(const struct janas_image *in, int w, int h,+ struct janas_image *out)+{+ memset(out, 0, sizeof(*out));+ if (w <= 0 || h <= 0 || in->w <= 0 || in->h <= 0)+ return -1;+ struct taps tx = {0}, ty = {0};+ uint8_t *mid = NULL;+ int rc = -1;+ if (taps_make(&tx, in->w, w) || taps_make(&ty, in->h, h))+ goto done;+ /* the rows the columns pass reads: those of any output row's span */+ int y0 = ty.first[0], y1 = ty.first[h - 1] + ty.count[h - 1];+ mid = malloc((size_t)w * (size_t)(y1 - y0) * 3);+ out->rgb = malloc((size_t)w * (size_t)h * 3);+ if (!mid || !out->rgb)+ goto done;+ for (int y = y0; y < y1; y++) { /* rows */+ const uint8_t *src = in->rgb + (size_t)y * (size_t)in->w * 3;+ uint8_t *dst = mid + (size_t)(y - y0) * (size_t)w * 3;+ for (int x = 0; x < w; x++) {+ const int32_t *k = tx.w + (size_t)x * (size_t)tx.ksize;+ for (int c = 0; c < 3; c++) {+ int64_t s = (int64_t)1 << (PB - 1);+ for (int i = 0; i < tx.count[x]; i++)+ s += (int64_t)src[(size_t)(tx.first[x] + i) * 3 + c] * k[i];+ dst[(size_t)x * 3 + c] = clip(s);+ }+ }+ }+ for (int y = 0; y < h; y++) { /* columns */+ const int32_t *k = ty.w + (size_t)y * (size_t)ty.ksize;+ uint8_t *dst = out->rgb + (size_t)y * (size_t)w * 3;+ for (int x = 0; x < w * 3; x++) {+ int64_t s = (int64_t)1 << (PB - 1);+ for (int i = 0; i < ty.count[y]; i++)+ s += (int64_t)+ mid[(size_t)(ty.first[y] - y0 + i) * (size_t)w * 3 ++ (size_t)x] *+ k[i];+ dst[x] = clip(s);+ }+ }+ out->w = w;+ out->h = h;+ rc = 0;+done:+ if (rc) {+ free(out->rgb);+ memset(out, 0, sizeof(*out));+ }+ free(mid);+ taps_free(&tx);+ taps_free(&ty);+ return rc;+}diff --git a/src/common/inflate.c b/src/common/inflate.cnew file mode 100644index 0000000..0c83a49--- /dev/null+++ b/src/common/inflate.c@@ -0,0 +1,264 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * inflate.c - DEFLATE (RFC 1951) in a zlib wrapping (RFC 1950): stored,+ * fixed and dynamic Huffman blocks. Codes are decoded canonically, a bit+ * at a time against the count of codes of each length - plain, and fast+ * enough for the pictures it is for. Every read is bounded: broken data+ * ends with a reason, never past the input or the output.+ */+#include "inflate.h"++#include <pthread.h>+#include <stdio.h>+#include <string.h>++struct in {+ const uint8_t *p;+ size_t n, pos;+ uint32_t bits;+ int nbits;+ int bad;+ uint8_t *out;+ size_t cap, len;+};++static int bit(struct in *s)+{+ if (!s->nbits) {+ if (s->pos >= s->n) {+ s->bad = 1;+ return 0;+ }+ s->bits = s->p[s->pos++];+ s->nbits = 8;+ }+ int b = (int)(s->bits & 1);+ s->bits >>= 1;+ s->nbits--;+ return b;+}++static uint32_t bits(struct in *s, int n)+{+ uint32_t v = 0;+ for (int i = 0; i < n; i++)+ v |= (uint32_t)bit(s) << i;+ return v;+}++/* a canonical Huffman code: how many codes of each length, the symbols in+ code order */+struct huff {+ short count[16];+ short sym[320];+};++/* 0, or -1 when the lengths do not make a code */+static int build(struct huff *h, const uint8_t *len, int n)+{+ memset(h->count, 0, sizeof h->count);+ for (int i = 0; i < n; i++)+ h->count[len[i]]++;+ if (h->count[0] == n)+ return 0; /* no codes: an empty distance code is allowed */+ int left = 1;+ for (int l = 1; l < 16; l++) {+ left <<= 1;+ left -= h->count[l];+ if (left < 0)+ return -1; /* too many codes */+ }+ short offs[16];+ offs[1] = 0;+ for (int l = 1; l < 15; l++)+ offs[l + 1] = (short)(offs[l] + h->count[l]);+ for (int i = 0; i < n; i++)+ if (len[i])+ h->sym[offs[len[i]]++] = (short)i;+ return 0;+}++static int decode(struct in *s, const struct huff *h)+{+ int code = 0, first = 0, index = 0;+ for (int l = 1; l < 16; l++) {+ code |= bit(s);+ int c = h->count[l];+ if (code - c < first)+ return h->sym[index + (code - first)];+ index += c;+ first += c;+ first <<= 1;+ code <<= 1;+ if (s->bad)+ return -1;+ }+ s->bad = 1;+ return -1;+}++static const short LBASE[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13,+ 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,+ 67, 83, 99, 115, 131, 163, 195, 227, 258};+static const short LEXT[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,+ 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};+static const short DBASE[30] = {+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25,+ 33, 49, 65, 97, 129, 193, 257, 385, 513, 769,+ 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};+static const short DEXT[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3,+ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,+ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};++/* the symbols of one compressed block: 0, or -1 */+static int codes(struct in *s, const struct huff *lc, const struct huff *dc)+{+ for (;;) {+ int sym = decode(s, lc);+ if (s->bad || sym < 0)+ return -1;+ if (sym < 256) {+ if (s->len >= s->cap)+ return -1;+ s->out[s->len++] = (uint8_t)sym;+ } else if (sym == 256) {+ return 0;+ } else {+ sym -= 257;+ if (sym >= 29)+ return -1;+ size_t len = (size_t)LBASE[sym] + bits(s, LEXT[sym]);+ int ds = decode(s, dc);+ if (s->bad || ds < 0 || ds >= 30)+ return -1;+ size_t dist = (size_t)DBASE[ds] + bits(s, DEXT[ds]);+ if (dist > s->len || len > s->cap - s->len)+ return -1;+ for (size_t i = 0; i < len; i++, s->len++)+ s->out[s->len] = s->out[s->len - dist];+ }+ }+}++static int stored(struct in *s)+{+ s->nbits = 0; /* to the byte */+ if (s->n - s->pos < 4)+ return -1;+ unsigned len = s->p[s->pos] | (unsigned)s->p[s->pos + 1] << 8;+ unsigned nlen = s->p[s->pos + 2] | (unsigned)s->p[s->pos + 3] << 8;+ s->pos += 4;+ if ((len ^ 0xffff) != nlen || s->n - s->pos < len || s->cap - s->len < len)+ return -1;+ memcpy(s->out + s->len, s->p + s->pos, len);+ s->pos += len;+ s->len += len;+ return 0;+}++static struct huff fixed_lc, fixed_dc;++static void fixed_make(void)+{+ uint8_t l[288];+ int i = 0;+ for (; i < 144; i++)+ l[i] = 8;+ for (; i < 256; i++)+ l[i] = 9;+ for (; i < 280; i++)+ l[i] = 7;+ for (; i < 288; i++)+ l[i] = 8;+ build(&fixed_lc, l, 288);+ for (i = 0; i < 30; i++)+ l[i] = 5;+ build(&fixed_dc, l, 30);+}++static int fixed(struct in *s)+{+ static pthread_once_t once = PTHREAD_ONCE_INIT;+ pthread_once(&once, fixed_make); /* the same every time: made once */+ return codes(s, &fixed_lc, &fixed_dc);+}++static int dynamic(struct in *s)+{+ int nlen = (int)bits(s, 5) + 257, ndist = (int)bits(s, 5) + 1;+ int ncode = (int)bits(s, 4) + 4;+ if (nlen > 286 || ndist > 30 || s->bad)+ return -1;+ static const uint8_t ORDER[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5,+ 11, 4, 12, 3, 13, 2, 14, 1, 15};+ uint8_t len[320] = {0};+ for (int i = 0; i < ncode; i++)+ len[ORDER[i]] = (uint8_t)bits(s, 3);+ struct huff h, lc, dc;+ if (build(&h, len, 19) != 0)+ return -1;+ /* the lengths of both codes, run-length coded */+ int k = 0;+ while (k < nlen + ndist) {+ int sym = decode(s, &h);+ if (s->bad || sym < 0)+ return -1;+ if (sym < 16) {+ len[k++] = (uint8_t)sym;+ continue;+ }+ uint8_t v = 0;+ int rep;+ if (sym == 16) {+ if (!k)+ return -1;+ v = len[k - 1];+ rep = 3 + (int)bits(s, 2);+ } else if (sym == 17) {+ rep = 3 + (int)bits(s, 3);+ } else {+ rep = 11 + (int)bits(s, 7);+ }+ if (k + rep > nlen + ndist)+ return -1;+ while (rep--)+ len[k++] = v;+ }+ if (!len[256]) /* no end of block */+ return -1;+ if (build(&lc, len, nlen) != 0 || build(&dc, len + nlen, ndist) != 0)+ return -1;+ return codes(s, &lc, &dc);+}++int janas_zlib_inflate(const uint8_t *p, size_t n, uint8_t *out, size_t cap,+ char *why, size_t wn)+{+ if (n < 2 || (p[0] & 0x0f) != 8 || ((p[0] << 8) | p[1]) % 31 ||+ (p[1] & 0x20)) {+ snprintf(why, wn, "not zlib data");+ return -1;+ }+ struct in s = {.p = p, .n = n, .pos = 2, .out = out, .cap = cap};+ int last;+ do {+ last = bit(&s);+ int type = (int)bits(&s, 2);+ int rc = s.bad ? -1+ : type == 0 ? stored(&s)+ : type == 1 ? fixed(&s)+ : type == 2 ? dynamic(&s)+ : -1;+ if (rc != 0 || s.bad) {+ snprintf(why, wn, "broken compressed data");+ return -1;+ }+ } while (!last);+ if (s.len != cap) {+ snprintf(why, wn, "%zu bytes of pixels where %zu were expected", s.len,+ cap);+ return -1;+ }+ return 0;+}diff --git a/src/common/inflate.h b/src/common/inflate.hnew file mode 100644index 0000000..c73b7f3--- /dev/null+++ b/src/common/inflate.h@@ -0,0 +1,18 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * inflate.h - DEFLATE data (RFC 1951) inside its zlib wrapping (RFC 1950)+ * made whole again (inflate.c), as PNG keeps its pixels.+ */+#ifndef JANAS_COMMON_INFLATE_H+#define JANAS_COMMON_INFLATE_H++#include <stddef.h>+#include <stdint.h>++/* The n bytes of zlib data at p into out, exactly cap bytes expected:+ 0, or -1 (data broken, or not cap bytes) with the reason in why. */+int janas_zlib_inflate(const uint8_t *p, size_t n, uint8_t *out, size_t cap,+ char *why, size_t wn);++#endifdiff --git a/src/common/mcp_client.c b/src/common/mcp_client.cindex da31021..f3078fe 100644--- a/src/common/mcp_client.c+++ b/src/common/mcp_client.c@@ -508,6 +508,16 @@ int32_t janas_mcp_open_command(const char *name, int32_t argc,int32_t janas_mcp_open_url(const char *name, const char *url, int32_t n_headers,const char *const *headers, int32_t seconds,janas_mcp **out)+{+ return janas_mcp_open_url_policy(name, url, n_headers, headers, seconds,+ NULL, out);+}++int32_t janas_mcp_open_url_policy(const char *name, const char *url,+ int32_t n_headers, const char *const *headers,+ int32_t seconds,+ const struct janas_net_policy *policy,+ janas_mcp **out){if (!out || !url || n_headers < 0 || (n_headers > 0 && !headers))return janas_mcp_fail(JANAS_MCP_EINVAL, "invalid argument");@@ -522,6 +532,7 @@ int32_t janas_mcp_open_url(const char *name, const char *url, int32_t n_headers,janas_mcp_close(m);return janas_mcp_fail(JANAS_MCP_EINVAL, "%s", err);}+ m->url.policy = policy;for (int32_t i = 0; i < n_headers; i++)if (!headers[i] || strpbrk(headers[i], "\r\n") ||!strchr(headers[i], ':')) {diff --git a/src/common/mcp_client.h b/src/common/mcp_client.hindex 8476452..de97dca 100644--- a/src/common/mcp_client.h+++ b/src/common/mcp_client.h@@ -75,6 +75,14 @@ enum {/* mcp_client_http.c: a request (id >= 0) or notification over HTTP, itsanswer into *a as in the stdio transport; REFUSED says the serverrefused it at the HTTP level (m->http_status). */+/* janas_mcp_open_url with connections only where policy allows (kept by+ the caller while the client lives; NULL: anywhere), for a server that+ reaches the MCP servers its clients name. */+int32_t janas_mcp_open_url_policy(const char *name, const char *url,+ int32_t n_headers, const char *const *headers,+ int32_t seconds,+ const struct janas_net_policy *policy,+ janas_mcp **out);int janas_mcp_http_exchange(struct janas_mcp *m, const char *method, int64_t id,const char *body, size_t n, double ms,struct janas_rpc_msg *a);diff --git a/src/common/mcp_client_http.c b/src/common/mcp_client_http.cindex 1955e0c..8f2b82a 100644--- a/src/common/mcp_client_http.c+++ b/src/common/mcp_client_http.c@@ -183,7 +183,7 @@ int janas_mcp_param_headers(struct janas_mcp *m, const struct janas_mcp_tool *t,/* The header lines of a request, into an array of pointers into b. */static size_t build_headers(struct janas_mcp *m, const char *method,- struct janas_buf *b, const char **h, size_t cap)+ struct janas_buf *b, const char ***out){janas_buf_puts(b, "Content-Type: application/json\n""Accept: application/json, text/event-stream\n");@@ -205,6 +205,14 @@ static size_t build_headers(struct janas_mcp *m, const char *method,janas_buf_printf(b, "%s\n", *c);if (b->oom)return 0;+ /* as many as there are lines: a call's own never push the configured+ ones (an Authorization) out */+ size_t cap = 0;+ for (size_t i = 0; i < b->n; i++)+ cap += b->p[i] == '\n';+ const char **h = malloc(cap * sizeof(*h));+ if (!h)+ return 0;size_t n = 0;for (char *l = b->p; l < b->p + b->n && n < cap;) {char *nl = memchr(l, '\n', b->n - (size_t)(l - b->p));@@ -212,6 +220,7 @@ static size_t build_headers(struct janas_mcp *m, const char *method,h[n++] = l;l = nl + 1;}+ *out = h;return n;}@@ -243,8 +252,8 @@ int janas_mcp_http_exchange(struct janas_mcp *m, const char *method, int64_t id,struct janas_rpc_msg *a){struct janas_buf hb = {0};- const char *h[64];- size_t nh = build_headers(m, method, &hb, h, 64);+ const char **h = NULL;+ size_t nh = build_headers(m, method, &hb, &h);if (!nh) {janas_buf_free(&hb);return NOMEM;@@ -255,6 +264,7 @@ int janas_mcp_http_exchange(struct janas_mcp *m, const char *method, int64_t id,int rc = janas_http_request(&c, &m->url, "POST", h, nh, body, n,ms < 0 ? -1 : (int)ms,(volatile int *)&m->cancel, err, sizeof(err));+ free(h);janas_buf_free(&hb);m->http_status = 0;if (rc != 0) {diff --git a/src/common/mcp_http.c b/src/common/mcp_http.cindex 069d4b3..4064e2c 100644--- a/src/common/mcp_http.c+++ b/src/common/mcp_http.c@@ -358,8 +358,14 @@ static int connect_to(const struct janas_url *u, double end,snprintf(err, err_len, "%s: %s", u->host, gai_strerror(e));return -1;}- int fd = -1;+ int fd = -1, refused = 0, tried = 0;for (struct addrinfo *a = res; a && fd < 0; a = a->ai_next) {+ /* the address checked is the address connected to */+ if (!janas_net_allowed(u->policy, u->host, u->port, a->ai_addr)) {+ refused++;+ continue;+ }+ tried++;fd = janas_sock_open(a->ai_family, a->ai_socktype, a->ai_protocol);if (fd < 0)continue;@@ -378,6 +384,10 @@ static int connect_to(const struct janas_url *u, double end,fd = -1;}freeaddrinfo(res);+ if (fd < 0 && refused && !tried)+ snprintf(err, err_len,+ "%s:%s: not a public address, and not among those allowed",+ u->host, u->port);return fd;}diff --git a/src/common/mcp_http.h b/src/common/mcp_http.hindex 9c1d68d..da0d6a8 100644--- a/src/common/mcp_http.h+++ b/src/common/mcp_http.h@@ -15,6 +15,7 @@#include <stddef.h>#include <stdint.h>+#include "common/mcp_policy.h"#include "common/mcp_tls.h"#include "llm/json.h"@@ -24,6 +25,8 @@ struct janas_url {char port[8];char path[4096]; /* with the query, "/" at least (a signed CDNaddress runs past a thousand) */+ /* where a connection may go (mcp_policy.h); NULL, as parsed: anywhere */+ const struct janas_net_policy *policy;};/* 0, or -1 with the reason in err. Only http:// and https://. */diff --git a/src/common/mcp_policy.c b/src/common/mcp_policy.cnew file mode 100644index 0000000..ed67d76--- /dev/null+++ b/src/common/mcp_policy.c@@ -0,0 +1,136 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * mcp_policy.c - where a connection made on someone else's word may go+ * (see mcp_policy.h). The ranges are IANA's special-purpose registries.+ */+#include "common/mcp_policy.h"++#include <netinet/in.h>+#include <stdint.h>+#include <string.h>+#include <strings.h>+#include <sys/socket.h>++static int v4_public(uint32_t a)+{+ static const struct {+ uint32_t net;+ int bits;+ } bad[] = {+ {0x00000000u, 8}, /* this network */+ {0x0a000000u, 8}, /* private */+ {0x64400000u, 10}, /* shared, carrier-grade NAT */+ {0x7f000000u, 8}, /* loopback */+ {0xa9fe0000u, 16}, /* link-local, the clouds' metadata */+ {0xac100000u, 12}, /* private */+ {0xc0000000u, 24}, /* protocol assignments */+ {0xc0000200u, 24}, /* documentation */+ {0xc0586300u, 24}, /* 6to4 relays */+ {0xc0a80000u, 16}, /* private */+ {0xc6120000u, 15}, /* benchmarking */+ {0xc6336400u, 24}, /* documentation */+ {0xcb007100u, 24}, /* documentation */+ {0xe0000000u, 4}, /* multicast */+ {0xf0000000u, 4}, /* reserved, broadcast */+ };+ for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++)+ if ((a >> (32 - bad[i].bits)) == (bad[i].net >> (32 - bad[i].bits)))+ return 0;+ return 1;+}++static uint32_t be32(const uint8_t *p)+{+ return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 |+ p[3];+}++static int v6_public(const uint8_t *b)+{+ static const uint8_t zero[12] = {0};+ static const uint8_t mapped[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255};+ static const uint8_t nat64[12] = {0, 0x64, 0xff, 0x9b};+ if (!memcmp(b, mapped, 12) || !memcmp(b, nat64, 12))+ return v4_public(be32(b + 12));+ if (!memcmp(b, zero, 12)) /* ::, ::1, IPv4-compatible (deprecated) */+ return 0;+ if (b[0] == 0x20 && b[1] == 0x02) /* 6to4: the IPv4 address after */+ return v4_public(be32(b + 2));+ if ((b[0] & 0xfe) == 0xfc) /* unique local */+ return 0;+ if (b[0] == 0xfe && (b[1] & 0xc0) == 0x80) /* link-local */+ return 0;+ if (b[0] == 0xfe && (b[1] & 0xc0) == 0xc0) /* site-local (deprecated) */+ return 0;+ if (b[0] == 0xff) /* multicast */+ return 0;+ if (b[0] == 0x20 && b[1] == 0x01 && b[2] == 0x0d && b[3] == 0xb8)+ return 0; /* documentation */+ if (b[0] == 0x01 && b[1] == 0x00 && !memcmp(b + 2, zero, 6))+ return 0; /* discard */+ if (b[0] == 0x20 && b[1] == 0x01 && b[2] == 0x00 && b[3] == 0x00)+ return 0; /* Teredo: an IPv4 address behind, hidden */+ if (b[0] == 0x20 && b[1] == 0x01 && b[2] == 0x00 && (b[3] & 0xf0) == 0x20)+ return 0; /* ORCHIDv2 */+ if (b[0] == 0x20 && b[1] == 0x01 && b[2] == 0x00 && b[3] == 0x02 &&+ b[4] == 0 && b[5] == 0)+ return 0; /* benchmarking */+ if (b[0] == 0x5f) /* 5f00::/16, segment routing */+ return 0;+ return 1;+}++int janas_addr_public(const struct sockaddr *sa)+{+ if (!sa)+ return 0;+ if (sa->sa_family == AF_INET) {+ const struct sockaddr_in *s = (const struct sockaddr_in *)sa;+ return v4_public(be32((const uint8_t *)&s->sin_addr));+ }+ if (sa->sa_family == AF_INET6) {+ const struct sockaddr_in6 *s = (const struct sockaddr_in6 *)sa;+ return v6_public((const uint8_t *)&s->sin6_addr);+ }+ return 0;+}++/* Does an entry ("host", "host:port", "[v6]:port", "[v6]") name host:port? */+static int entry_names(const char *e, const char *host, const char *port)+{+ const char *h = e, *he, *p = NULL;+ if (*e == '[') {+ h = e + 1;+ he = strchr(h, ']');+ if (!he)+ return 0;+ if (he[1] == ':')+ p = he + 2;+ else if (he[1])+ return 0;+ } else {+ he = strrchr(e, ':');+ if (he && strchr(e, ':') != he) /* a bare v6 address, no port */+ he = NULL;+ if (he)+ p = he + 1;+ else+ he = e + strlen(e);+ }+ size_t n = (size_t)(he - h);+ if (strlen(host) != n || strncasecmp(h, host, n) != 0)+ return 0;+ return !p || !strcmp(p, port);+}++int janas_net_allowed(const struct janas_net_policy *p, const char *host,+ const char *port, const struct sockaddr *sa)+{+ if (!p || !p->public_only || janas_addr_public(sa))+ return 1;+ for (size_t i = 0; i < p->n_allow; i++)+ if (entry_names(p->allow[i], host, port))+ return 1;+ return 0;+}diff --git a/src/common/mcp_policy.h b/src/common/mcp_policy.hnew file mode 100644index 0000000..74508f7--- /dev/null+++ b/src/common/mcp_policy.h@@ -0,0 +1,36 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * mcp_policy.h - where a connection made on someone else's word may go.+ * janas-server reaches the MCP servers a request names: left free, a+ * request could make it talk to this machine's own services, the local+ * network's, or a cloud's metadata address. With a policy, the HTTP client+ * (mcp_http.c) checks every address a name resolves to and connects only+ * to one that passed, so a name that resolves to a public address when+ * checked and to a private one when used (DNS rebinding) gains nothing.+ */+#ifndef JANAS_COMMON_MCP_POLICY_H+#define JANAS_COMMON_MCP_POLICY_H++#include <stddef.h>++struct sockaddr;++struct janas_net_policy {+ int public_only; /* refuse loopback, private, link-local, reserved... */+ size_t n_allow; /* allowed all the same: "host" or "host:port", the */+ char **allow; /* host a name or a literal address ([v6] with a port) */+};++/* 1 when the address is one of the Internet's: not loopback, private+ (10/8, 172.16/12, 192.168/16, fc00::/7), shared (100.64/10), link-local,+ multicast, documentation, benchmarking or otherwise reserved; an IPv4+ address inside an IPv6 one (mapped, NAT64, 6to4) judged as itself. */+int janas_addr_public(const struct sockaddr *sa);++/* Whether a connection to host:port at address sa may be made (p NULL:+ any). */+int janas_net_allowed(const struct janas_net_policy *p, const char *host,+ const char *port, const struct sockaddr *sa);++#endifdiff --git a/src/common/mcp_tls.c b/src/common/mcp_tls.cindex b7a8653..04a5ff5 100644--- a/src/common/mcp_tls.c+++ b/src/common/mcp_tls.c@@ -226,6 +226,24 @@ long janas_tls_write(struct janas_tls *t, const void *p, size_t n)return -1;}+void janas_tls_lock(void)+{+ pthread_mutex_lock(&lock);+}++void janas_tls_unlock(void)+{+ pthread_mutex_unlock(&lock);+}++int janas_tls_crypto(void)+{+ static int started;+ if (!started && psa_crypto_init() == PSA_SUCCESS)+ started = 1;+ return started ? 0 : -1;+}+void janas_tls_close(struct janas_tls *t){if (!t)diff --git a/src/common/mcp_tls.h b/src/common/mcp_tls.hindex 490cd23..feebaf2 100644--- a/src/common/mcp_tls.h+++ b/src/common/mcp_tls.h@@ -31,4 +31,11 @@ long janas_tls_read(struct janas_tls *t, void *p, size_t n);long janas_tls_write(struct janas_tls *t, const void *p, size_t n);void janas_tls_close(struct janas_tls *t);+/* The one lock every use of Mbed TLS in the process takes (it has no+ threading of its own): janas-server's TLS takes it too. janas_tls_crypto+ starts the crypto library once, under it: 0, or -1. */+void janas_tls_lock(void);+void janas_tls_unlock(void);+int janas_tls_crypto(void);+#endifdiff --git a/src/common/md5.c b/src/common/md5.cnew file mode 100644index 0000000..434fc36--- /dev/null+++ b/src/common/md5.c@@ -0,0 +1,111 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * md5.c - MD5 as RFC 1321 gives it (see md5.h).+ */+#include "common/md5.h"++#include <string.h>++static const uint32_t K[64] = {+ 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a,+ 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,+ 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340,+ 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,+ 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,+ 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,+ 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,+ 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,+ 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,+ 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,+ 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};++static const uint8_t R[64] = {+ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,+ 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,+ 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,+ 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};++static void block(uint32_t h[4], const uint8_t *p)+{+ uint32_t m[16];+ for (int i = 0; i < 16; i++)+ m[i] = (uint32_t)p[4 * i] | (uint32_t)p[4 * i + 1] << 8 |+ (uint32_t)p[4 * i + 2] << 16 | (uint32_t)p[4 * i + 3] << 24;+ uint32_t a = h[0], b = h[1], c = h[2], d = h[3];+ for (int i = 0; i < 64; i++) {+ uint32_t f;+ int g;+ if (i < 16) {+ f = (b & c) | (~b & d);+ g = i;+ } else if (i < 32) {+ f = (d & b) | (~d & c);+ g = (5 * i + 1) % 16;+ } else if (i < 48) {+ f = b ^ c ^ d;+ g = (3 * i + 5) % 16;+ } else {+ f = c ^ (b | ~d);+ g = (7 * i) % 16;+ }+ uint32_t t = d;+ d = c;+ c = b;+ uint32_t x = a + f + K[i] + m[g];+ b = b + (x << R[i] | x >> (32 - R[i]));+ a = t;+ }+ h[0] += a;+ h[1] += b;+ h[2] += c;+ h[3] += d;+}++void janas_md5_init(struct janas_md5 *s)+{+ s->h[0] = 0x67452301;+ s->h[1] = 0xefcdab89;+ s->h[2] = 0x98badcfe;+ s->h[3] = 0x10325476;+ s->len = 0;+ s->n = 0;+}++void janas_md5_update(struct janas_md5 *s, const void *data, size_t n)+{+ const uint8_t *p = data;+ s->len += n;+ if (s->n) {+ size_t k = 64 - s->n < n ? 64 - s->n : n;+ memcpy(s->buf + s->n, p, k);+ s->n += k;+ p += k;+ n -= k;+ if (s->n < 64)+ return;+ block(s->h, s->buf);+ s->n = 0;+ }+ for (; n >= 64; p += 64, n -= 64)+ block(s->h, p);+ memcpy(s->buf, p, n);+ s->n = n;+}++void janas_md5_hex(struct janas_md5 *s, char out[33])+{+ uint64_t bits = s->len * 8;+ uint8_t pad[72] = {0x80};+ size_t k = s->n < 56 ? 56 - s->n : 120 - s->n;+ for (int i = 0; i < 8; i++)+ pad[k + (size_t)i] = (uint8_t)(bits >> (8 * i));+ janas_md5_update(s, pad, k + 8);+ static const char hex[] = "0123456789abcdef";+ for (int i = 0; i < 16; i++) {+ uint8_t b = (uint8_t)(s->h[i / 4] >> (8 * (i % 4)));+ out[2 * i] = hex[b >> 4];+ out[2 * i + 1] = hex[b & 15];+ }+ out[32] = 0;+}diff --git a/src/common/md5.h b/src/common/md5.hnew file mode 100644index 0000000..58267b5--- /dev/null+++ b/src/common/md5.h@@ -0,0 +1,25 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * md5.h - MD5 (RFC 1321), only to check an upload against the checksum a+ * client sends with it, as OpenAI's Uploads API asks: not for security.+ */+#ifndef JANAS_COMMON_MD5_H+#define JANAS_COMMON_MD5_H++#include <stddef.h>+#include <stdint.h>++struct janas_md5 {+ uint32_t h[4];+ uint64_t len; /* bytes hashed */+ uint8_t buf[64];+ size_t n; /* bytes waiting in buf */+};++void janas_md5_init(struct janas_md5 *s);+void janas_md5_update(struct janas_md5 *s, const void *data, size_t n);+/* The digest as 32 lowercase hex digits and a NUL. */+void janas_md5_hex(struct janas_md5 *s, char out[33]);++#endifdiff --git a/src/common/npu.c b/src/common/npu.cindex ee1c345..878a7e2 100644--- a/src/common/npu.c+++ b/src/common/npu.c@@ -82,7 +82,8 @@ struct janas_npu {ze_command_queue_handle_t q;ze_graph_dditable_ext_t *g;uint16_t cmajor, cminor;- uint32_t driver_version;+ uint32_t driver_version, vendor_id, device_id;+ int driver_known; /* its version read: else no cache, which keys on it */char name[256], *cache;void *allocs[NPU_MAX_ALLOCS];int nallocs;@@ -99,6 +100,7 @@ struct janas_npu {struct janas_npu_mm *ka;pthread_t ka_thread;int ka_started;+ int ka_failed; /* once: what it made is freed with the NPU, not again */atomic_int ka_on, ka_stop;/* off, the thread waits here rather than wake every half millisecond:2000 times a second beside the cores writing a reply */@@ -179,9 +181,13 @@ struct janas_npu *janas_npu_create(char *err, size_t err_len)goto fail;}memcpy(u->name, dp.name, sizeof u->name - 1);+ u->vendor_id = dp.vendorId;+ u->device_id = dp.deviceId;ze_driver_properties_t drp = {.stype = ZE_STYPE_DRIVER_PROPERTIES};- if (!u->zeDriverGetProperties(u->drv, &drp))+ if (!u->zeDriverGetProperties(u->drv, &drp)) {u->driver_version = drp.driverVersion;+ u->driver_known = 1;+ }ze_simple_desc_t cd = {.stype = ZE_STYPE_CONTEXT_DESC};if (u->zeContextCreate(u->drv, &cd, &u->ctx)) {snprintf(err, err_len, "NPU context refused");@@ -286,8 +292,9 @@ int janas_npu_set_cache(struct janas_npu *u, const char *dir)return u->cache ? 0 : -1;}-/* the cache's file of a model: its SHA-256 with the driver's and the- compiler's versions */+/* the cache's file of a model: its SHA-256 with the device's ids and the+ driver's and the compiler's versions (a graph compiled for one NPU is+ not for another) */static void cache_path(const struct janas_npu *u, const uint8_t *model,size_t bytes, char *path, size_t len){@@ -295,6 +302,8 @@ static void cache_path(const struct janas_npu *u, const uint8_t *model,char hex[65];janas_sha256_init(&h);janas_sha256_update(&h, model, bytes);+ janas_sha256_update(&h, &u->vendor_id, sizeof u->vendor_id);+ janas_sha256_update(&h, &u->device_id, sizeof u->device_id);janas_sha256_update(&h, &u->driver_version, sizeof u->driver_version);janas_sha256_update(&h, &u->cmajor, sizeof u->cmajor);janas_sha256_update(&h, &u->cminor, sizeof u->cminor);@@ -327,8 +336,11 @@ static void cache_store(struct janas_npu *u, ze_graph_handle_t graph,if (u->g->pfnGetNativeBinary(graph, &n, NULL) || !n)return;uint8_t *b = malloc(n);+ /* its own name: two NPUs of a process (a model and its drafts) may+ store the same graph at once */+ static _Atomic unsigned seq;char tmp[4200];- snprintf(tmp, sizeof tmp, "%s.%d.tmp", path, (int)getpid());+ snprintf(tmp, sizeof tmp, "%s.%d.%u.tmp", path, (int)getpid(), ++seq);FILE *f =b && !u->g->pfnGetNativeBinary(graph, &n, b) ? fopen(tmp, "wb") : NULL;if (f) {@@ -509,7 +521,7 @@ static struct janas_npu_mm *from_ir(struct janas_npu *u, struct npu_ir *irp,double t0 = now();ze_result_t r = 1;char path[4096];- if (u->cache) {+ if (u->cache && u->driver_known) {cache_path(u, model, bytes, path, sizeof path);size_t nb = 0;uint8_t *blob = read_file(path, &nb);@@ -527,7 +539,7 @@ static struct janas_npu_mm *from_ir(struct janas_npu *u, struct npu_ir *irp,}if (r) {r = u->g->pfnCreate2(u->ctx, u->dev, &gd, &p->graph);- if (!r && u->cache)+ if (!r && u->cache && u->driver_known)cache_store(u, p->graph, path);}free(model);@@ -732,6 +744,11 @@ int janas_npu_keep_awake(struct janas_npu *u, int on)}if (!on)return 0;+ if (u->ka_failed) {+ atomic_store(&u->ka_on, 0);+ return -1;+ }+ u->ka_failed = 1; /* until it is running *//* the tiny product, its fence moved to a queue of its own */char err[64];ze_command_queue_desc_t qd = {.stype = ZE_STYPE_COMMAND_QUEUE_DESC};@@ -767,12 +784,15 @@ int janas_npu_keep_awake(struct janas_npu *u, int on)if (slot < 0 || (!registered && atexit(stop_all_awake)) ||pthread_create(&u->ka_thread, NULL, keep_awake, u)) {pthread_mutex_unlock(&awake_lock);+ pthread_cond_destroy(&u->ka_cv);+ pthread_mutex_destroy(&u->ka_mx);atomic_store(&u->ka_on, 0);return -1;}registered = 1;awake_npus[slot] = u;u->ka_started = 1;+ u->ka_failed = 0;pthread_mutex_unlock(&awake_lock);return 0;}diff --git a/src/common/pool.c b/src/common/pool.cindex 5f70835..5194a55 100644--- a/src/common/pool.c+++ b/src/common/pool.c@@ -226,12 +226,18 @@ struct janas_pool *janas_pool_create(int n_threads, const int *cpus)pthread_cond_init(&p->wake_idle, NULL);for (int i = 1; i < n_threads; i++) {struct worker_arg *wa = malloc(sizeof(*wa));- if (!wa)- abort();- wa->p = p;- wa->tid = i;- if (pthread_create(&p->threads[i], NULL, worker, wa) != 0)- abort();+ if (wa) {+ wa->p = p;+ wa->tid = i;+ }+ if (!wa || pthread_create(&p->threads[i], NULL, worker, wa) != 0) {+ /* a library does not end its host: the threads started stop,+ and the caller hears NULL */+ free(wa);+ p->n_threads = i;+ janas_pool_destroy(p);+ return NULL;+ }}return p;}diff --git a/src/common/proc_win.c b/src/common/proc_win.cindex 986af1b..5260f7d 100644--- a/src/common/proc_win.c+++ b/src/common/proc_win.c@@ -60,9 +60,9 @@ static void putc_(struct str *s, char c)/* One argument quoted as CommandLineToArgvW and the C runtime read it:backslashes are literal except before a quote. */-static void quote(struct str *s, const char *a)+static void quote_as(struct str *s, const char *a, int always){- if (*a && !strpbrk(a, " \t\n\v\"")) {+ if (!always && *a && !strpbrk(a, " \t\n\v\"")) {put(s, a, strlen(a));return;}@@ -91,6 +91,11 @@ static void quote(struct str *s, const char *a)putc_(s, '"');}+static void quote(struct str *s, const char *a)+{+ quote_as(s, a, 0);+}+static int ends_with(const char *s, const char *tail){size_t n = strlen(s), t = strlen(tail);@@ -193,17 +198,31 @@ int janas_proc_start(struct janas_proc *p, const char *const argv[],return -1;}int script = ends_with(prog, ".cmd") || ends_with(prog, ".bat");+ /* cmd.exe reads the line before the program does: & | < > ^ ( ) are+ inert only inside quotes, so every argument is quoted; a quote of+ the argument's own would end that, and % (and ! with delayed+ expansion) expands even inside: those are refused, never passed+ (BatBadBut) */+ for (size_t i = 1; script && argv[i]; i++)+ if (strpbrk(argv[i], "\"%!\r\n")) {+ snprintf(err, err_len,+ "cannot start %s: an argument holds a character "+ "cmd.exe would act on (\" %% ! or a newline)",+ argv[0]);+ errno = EINVAL;+ return -1;+ }struct str cmd = {0};if (script) {/* cmd.exe /s /c "the whole line": its first and last quote go */put(&cmd, "cmd.exe /d /s /c \"", 18);- quote(&cmd, prog);+ quote_as(&cmd, prog, 1);} else {quote(&cmd, prog);}for (size_t i = 1; argv[i]; i++) {putc_(&cmd, ' ');- quote(&cmd, argv[i]);+ quote_as(&cmd, argv[i], script);}if (script)putc_(&cmd, '"');diff --git a/src/common/sandbox.c b/src/common/sandbox.cnew file mode 100644index 0000000..347cc8f--- /dev/null+++ b/src/common/sandbox.c@@ -0,0 +1,612 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * sandbox.c - the cage of sandbox.h.+ *+ * The caller runs itself again (/proc/self/exe JANAS_SANDBOX_ARG ...), so+ * the cage is built by a fresh process with a single thread, where every+ * call is safe. That process unshares a user namespace (mapping only its+ * own uid and gid) with mount, PID, network, IPC and UTS namespaces, and+ * forks the cage's first process, PID 1 of the new namespace, which:+ * - builds a root in memory (tmpfs): /usr and the links or directories+ * beside it (/bin, /lib, /lib64...) read-only, a few files of /etc the+ * programs need (the linker's cache, the alternatives, fonts, python's+ * and matplotlib's settings) read-only, the data directory writable at+ * /mnt/data, a /tmp in memory of bounded size, a /proc of its own, a+ * /dev with null, zero, full, random and urandom only;+ * - moves into it (pivot_root), drops the old root, makes the root+ * read-only;+ * - sets its limits (address space, file size, open files, no core);+ * - forbids new privileges and installs a seccomp filter: no new+ * processes (fork, vfork, clone without CLONE_THREAD; clone3 answered+ * ENOSYS so the C library makes its threads with clone), no mounts,+ * namespaces, tracing, keys, BPF, perf, userfaultfd, io_uring, kernel+ * modules, reboot or clock changes;+ * - runs the program with the environment it was given.+ * Both processes die with their parent (PR_SET_PDEATHSIG), and every+ * process of the namespace dies with its PID 1, so the cage never outlives+ * the program that made it. The network namespace has no interface up.+ */+#include "sandbox.h"++#include <stdio.h>+#include <string.h>++#if defined(__linux__) && (defined(__x86_64__) || defined(__aarch64__))++#include <errno.h>+#include <fcntl.h>+#include <linux/audit.h>+#include <linux/filter.h>+#include <linux/seccomp.h>+#include <sched.h>+#include <signal.h>+#include <spawn.h>+#include <stdlib.h>+#include <sys/mount.h>+#include <sys/prctl.h>+#include <sys/resource.h>+#include <sys/stat.h>+#include <sys/statvfs.h>+#include <sys/syscall.h>+#include <sys/wait.h>+#include <time.h>+#include <unistd.h>++extern char **environ;++/* ---- the builder (a fresh process: plain calls are safe) ---- */++static void die(const char *what)+{+ dprintf(2, "janas sandbox: %s: %s\n", what, strerror(errno));+ _exit(127);+}++static void write_file(const char *path, const char *text)+{+ int fd = open(path, O_WRONLY | O_CLOEXEC);+ if (fd < 0 || write(fd, text, strlen(text)) != (ssize_t)strlen(text))+ die(path);+ close(fd);+}++/* mkdir -p */+static void make_dirs(const char *path)+{+ char p[4096];+ snprintf(p, sizeof p, "%s", path);+ for (char *s = p + 1; *s; s++)+ if (*s == '/') {+ *s = 0;+ if (mkdir(p, 0755) != 0 && errno != EEXIST)+ die(p);+ *s = '/';+ }+ if (mkdir(p, 0755) != 0 && errno != EEXIST)+ die(p);+}++/* An empty file to bind a file onto (its directories made). */+static void make_file(const char *path)+{+ char d[4096];+ snprintf(d, sizeof d, "%s", path);+ char *slash = strrchr(d, '/');+ if (slash && slash != d) {+ *slash = 0;+ make_dirs(d);+ }+ int fd = open(path, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);+ if (fd < 0)+ die(path);+ close(fd);+}++/* src bound at dst, read-only unless rw; the flags the kernel keeps locked+ in a user namespace (nosuid, nodev, noexec, atime) kept. */+static void bind(const char *src, const char *dst, int rw)+{+ if (mount(src, dst, NULL, MS_BIND | MS_REC, NULL) != 0)+ die(dst);+ struct statvfs v;+ unsigned long fl = MS_BIND | MS_REMOUNT | MS_NOSUID | MS_NODEV;+ if (statvfs(dst, &v) == 0) {+ if (v.f_flag & ST_NOEXEC)+ fl |= MS_NOEXEC;+ if (v.f_flag & ST_NOATIME)+ fl |= MS_NOATIME;+ if (v.f_flag & ST_NODIRATIME)+ fl |= MS_NODIRATIME;+ if (v.f_flag & ST_RELATIME)+ fl |= MS_RELATIME;+ }+ if (!rw)+ fl |= MS_RDONLY;+ if (mount(NULL, dst, NULL, fl, NULL) != 0)+ die(dst);+}++/* Host path p (a directory or a file) bound read-only at root + p, if it+ is there. */+static void bind_host(const char *root, const char *p)+{+ struct stat st;+ if (stat(p, &st) != 0)+ return;+ char dst[4096];+ snprintf(dst, sizeof dst, "%s%s", root, p);+ if (S_ISDIR(st.st_mode))+ make_dirs(dst);+ else+ make_file(dst);+ bind(p, dst, 0);+}++/* /bin, /lib... beside /usr: the same link, or the directory read-only */+static void top_entry(const char *root, const char *name)+{+ char p[256], dst[4096], target[4096];+ snprintf(p, sizeof p, "/%s", name);+ struct stat st;+ if (lstat(p, &st) != 0)+ return;+ snprintf(dst, sizeof dst, "%s/%s", root, name);+ if (S_ISLNK(st.st_mode)) {+ ssize_t k = readlink(p, target, sizeof target - 1);+ if (k < 0)+ die(p);+ target[k] = 0;+ if (symlink(target, dst) != 0)+ die(dst);+ } else if (S_ISDIR(st.st_mode)) {+ make_dirs(dst);+ bind(p, dst, 0);+ }+}++static int pivot_root_(const char *new_root, const char *put_old)+{+ return (int)syscall(SYS_pivot_root, new_root, put_old);+}++/* ---- seccomp ---- */++#if defined(__x86_64__)+#define ARCH AUDIT_ARCH_X86_64+#else+#define ARCH AUDIT_ARCH_AARCH64+#endif++#define DENY(nr) \+ BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, (nr), 0, 1), \+ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | EPERM)++static void seccomp_on(void)+{+ struct sock_filter f[] = {+ /* the architecture this was built for, nothing else */+ BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch)),+ BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ARCH, 1, 0),+ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),+ BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),+#if defined(__x86_64__)+ /* no x32 calls */+ BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 0x40000000, 0, 1),+ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),+ DENY(SYS_fork),+ DENY(SYS_vfork),+ DENY(SYS_iopl),+ DENY(SYS_ioperm),+#endif+ /* clone: threads only */+ BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_clone, 0, 4),+ BPF_STMT(BPF_LD | BPF_W | BPF_ABS,+ offsetof(struct seccomp_data, args[0])),+ BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, CLONE_THREAD, 1, 0),+ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | EPERM),+ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),+#ifdef SYS_clone3+ BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_clone3, 0, 1),+ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | ENOSYS),+#endif+ DENY(SYS_ptrace),+ DENY(SYS_mount),+ DENY(SYS_umount2),+ DENY(SYS_pivot_root),+ DENY(SYS_chroot),+ DENY(SYS_unshare),+ DENY(SYS_setns),+ DENY(SYS_bpf),+ DENY(SYS_keyctl),+ DENY(SYS_add_key),+ DENY(SYS_request_key),+ DENY(SYS_perf_event_open),+ DENY(SYS_userfaultfd),+ DENY(SYS_init_module),+ DENY(SYS_finit_module),+ DENY(SYS_delete_module),+ DENY(SYS_kexec_load),+#ifdef SYS_kexec_file_load+ DENY(SYS_kexec_file_load),+#endif+ DENY(SYS_reboot),+ DENY(SYS_swapon),+ DENY(SYS_swapoff),+ DENY(SYS_acct),+ DENY(SYS_settimeofday),+ DENY(SYS_clock_settime),+ DENY(SYS_open_by_handle_at),+ DENY(SYS_name_to_handle_at),+ DENY(SYS_quotactl),+ DENY(SYS_personality),+ DENY(SYS_process_vm_readv),+ DENY(SYS_process_vm_writev),+ DENY(SYS_kcmp),+#ifdef SYS_io_uring_setup+ DENY(SYS_io_uring_setup),+ DENY(SYS_io_uring_enter),+ DENY(SYS_io_uring_register),+#endif+#ifdef SYS_fsopen+ DENY(SYS_fsopen),+ DENY(SYS_fsconfig),+ DENY(SYS_fsmount),+ DENY(SYS_fspick),+ DENY(SYS_move_mount),+ DENY(SYS_open_tree),+#endif+#ifdef SYS_mount_setattr+ DENY(SYS_mount_setattr),+#endif+ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),+ };+ struct sock_fprog prog = {.len = (unsigned short)(sizeof f / sizeof f[0]),+ .filter = f};+ if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0)+ die("no_new_privs");+ if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) != 0)+ die("seccomp");+}++static void limit(int what, uint64_t v)+{+ struct rlimit r = {.rlim_cur = (rlim_t)v, .rlim_max = (rlim_t)v};+ if (setrlimit(what, &r) != 0)+ die("setrlimit");+}++/* argv: JANAS_SANDBOX_ARG data mem file tmp n_ro ro... -- program args */+void janas_sandbox_main(int argc, char **argv)+{+ if (argc < 2 || strcmp(argv[1], JANAS_SANDBOX_ARG) != 0)+ return;+ if (argc < 8)+ _exit(127);+ const char *data = argv[2];+ uint64_t mem = strtoull(argv[3], NULL, 10);+ uint64_t fsz = strtoull(argv[4], NULL, 10);+ uint64_t tmp = strtoull(argv[5], NULL, 10);+ int n_ro = atoi(argv[6]);+ if (n_ro < 0 || 7 + n_ro + 1 >= argc || strcmp(argv[7 + n_ro], "--"))+ _exit(127);+ char **ro = argv + 7;+ char **prog = argv + 8 + n_ro;++ if (prctl(PR_SET_PDEATHSIG, SIGKILL) != 0 || getppid() == 1)+ _exit(127);+ for (int fd = 3; fd < 1024; fd++)+ close(fd);+ if (access(data, F_OK) != 0)+ die(data);+ if (n_ro > 64)+ _exit(127);+ uid_t uid = getuid();+ gid_t gid = getgid();+ if (unshare(CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWPID | CLONE_NEWNET |+ CLONE_NEWIPC | CLONE_NEWUTS) != 0)+ die("unshare");+ char map[64];+ write_file("/proc/self/setgroups", "deny");+ snprintf(map, sizeof map, "%u %u 1\n", (unsigned)uid, (unsigned)uid);+ write_file("/proc/self/uid_map", map);+ snprintf(map, sizeof map, "%u %u 1\n", (unsigned)gid, (unsigned)gid);+ write_file("/proc/self/gid_map", map);++ pid_t b = fork();+ if (b < 0)+ die("fork");+ if (b > 0) { /* the first process: waits, and gives its end back */+ int st = 0;+ while (waitpid(b, &st, 0) < 0 && errno == EINTR)+ ;+ _exit(WIFEXITED(st) ? WEXITSTATUS(st) : 128 + WTERMSIG(st));+ }+ /* PID 1 of the cage */+ if (prctl(PR_SET_PDEATHSIG, SIGKILL) != 0)+ die("pdeathsig");+ if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL) != 0)+ die("private /");+ /* the sources, opened in this mount namespace (a bind from another is+ refused) before /tmp is covered */+ int data_fd = open(data, O_PATH | O_DIRECTORY | O_CLOEXEC);+ if (data_fd < 0)+ die(data);+ int ro_fd[64];+ for (int i = 0; i < n_ro; i++)+ if ((ro_fd[i] = open(ro[i], O_PATH | O_CLOEXEC)) < 0)+ die(ro[i]);++ const char *root = "/tmp";+ if (mount("tmpfs", root, "tmpfs", MS_NOSUID | MS_NODEV,+ "mode=0755,size=16m") != 0)+ die("root tmpfs");+ char p[4096], src[64];+ snprintf(p, sizeof p, "%s/usr", root);+ make_dirs(p);+ bind("/usr", p, 0);+ static const char *const top[] = {"bin", "sbin", "lib",+ "lib32", "lib64", "libx32"};+ for (size_t i = 0; i < sizeof top / sizeof top[0]; i++)+ top_entry(root, top[i]);+ static const char *const etc[] = {+ "/etc/ld.so.cache", "/etc/ld.so.conf", "/etc/ld.so.conf.d",+ "/etc/alternatives", "/etc/localtime", "/etc/fonts",+ "/etc/matplotlibrc", "/etc/mime.types", "/etc/python3",+ "/etc/python3.11", "/etc/python3.12", "/etc/python3.13",+ "/etc/python3.14"};+ snprintf(p, sizeof p, "%s/etc", root);+ make_dirs(p);+ for (size_t i = 0; i < sizeof etc / sizeof etc[0]; i++)+ bind_host(root, etc[i]);+ /* the data, writable */+ snprintf(p, sizeof p, "%s/mnt/data", root);+ make_dirs(p);+ snprintf(src, sizeof src, "/proc/self/fd/%d", data_fd);+ bind(src, p, 1);+ /* more paths, read-only where they are */+ for (int i = 0; i < n_ro; i++) {+ struct stat st;+ if (fstat(ro_fd[i], &st) != 0)+ die(ro[i]);+ snprintf(p, sizeof p, "%s%s", root, ro[i]);+ if (S_ISDIR(st.st_mode))+ make_dirs(p);+ else+ make_file(p);+ snprintf(src, sizeof src, "/proc/self/fd/%d", ro_fd[i]);+ bind(src, p, 0);+ }+ /* /tmp in memory */+ char opt[64];+ snprintf(p, sizeof p, "%s/tmp", root);+ make_dirs(p);+ snprintf(opt, sizeof opt, "mode=1777,size=%llu",+ (unsigned long long)(tmp ? tmp : (uint64_t)256 << 20));+ if (mount("tmpfs", p, "tmpfs", MS_NOSUID | MS_NODEV, opt) != 0)+ die("/tmp");+ /* /dev: a few devices */+ snprintf(p, sizeof p, "%s/dev", root);+ make_dirs(p);+ if (mount("tmpfs", p, "tmpfs", MS_NOSUID | MS_NOEXEC, "mode=0755") != 0)+ die("/dev");+ static const char *const dev[] = {"null", "zero", "full", "random",+ "urandom"};+ for (size_t i = 0; i < sizeof dev / sizeof dev[0]; i++) {+ char h[64];+ snprintf(h, sizeof h, "/dev/%s", dev[i]);+ snprintf(p, sizeof p, "%s/dev/%s", root, dev[i]);+ make_file(p);+ if (mount(h, p, NULL, MS_BIND, NULL) != 0)+ die(h);+ }+ static const char *const links[][2] = {{"/proc/self/fd", "fd"},+ {"/proc/self/fd/0", "stdin"},+ {"/proc/self/fd/1", "stdout"},+ {"/proc/self/fd/2", "stderr"}};+ for (size_t i = 0; i < sizeof links / sizeof links[0]; i++) {+ snprintf(p, sizeof p, "%s/dev/%s", root, links[i][1]);+ if (symlink(links[i][0], p) != 0)+ die(p);+ }+ snprintf(p, sizeof p, "%s/proc", root);+ make_dirs(p);+ if (mount("proc", p, "proc", MS_NOSUID | MS_NODEV | MS_NOEXEC, NULL) != 0)+ die("/proc");+ /* into the new root */+ snprintf(p, sizeof p, "%s/oldroot", root);+ make_dirs(p);+ if (pivot_root_(root, p) != 0)+ die("pivot_root");+ if (chdir("/") != 0 || umount2("/oldroot", MNT_DETACH) != 0 ||+ rmdir("/oldroot") != 0)+ die("old root");+ if (mount(NULL, "/", NULL, MS_REMOUNT | MS_RDONLY | MS_NOSUID | MS_NODEV,+ NULL) != 0)+ die("read-only root");+ if (sethostname("sandbox", 7) != 0)+ die("hostname");+ if (chdir("/mnt/data") != 0)+ die("/mnt/data");+ limit(RLIMIT_AS, mem ? mem : (uint64_t)2 << 30);+ limit(RLIMIT_FSIZE, fsz ? fsz : (uint64_t)1 << 30);+ limit(RLIMIT_NOFILE, 256);+ limit(RLIMIT_CORE, 0);+ seccomp_on();+ execve(prog[0], prog, environ);+ die(prog[0]);+}++/* ---- the caller ---- */++int janas_sandbox_spawn(const struct janas_sandbox_cfg *c, char *const argv[],+ char *const envp[], struct janas_sandbox *sb, char *why,+ size_t n)+{+ int nro = 0;+ while (c->ro && c->ro[nro])+ nro++;+ int na = 0;+ while (argv[na])+ na++;+ if (nro > 64) {+ snprintf(why, n, "too many read-only paths");+ return -1;+ }+ char num[3][24], cnt[16];+ snprintf(num[0], sizeof num[0], "%llu", (unsigned long long)c->mem_bytes);+ snprintf(num[1], sizeof num[1], "%llu", (unsigned long long)c->file_bytes);+ snprintf(num[2], sizeof num[2], "%llu", (unsigned long long)c->tmp_bytes);+ snprintf(cnt, sizeof cnt, "%d", nro);+ char **args = calloc((size_t)(9 + nro + na), sizeof(char *));+ if (!args) {+ snprintf(why, n, "out of memory");+ return -1;+ }+ int k = 0;+ args[k++] = "janas-sandbox";+ args[k++] = JANAS_SANDBOX_ARG;+ args[k++] = (char *)c->data_dir;+ args[k++] = num[0];+ args[k++] = num[1];+ args[k++] = num[2];+ args[k++] = cnt;+ for (int i = 0; i < nro; i++)+ args[k++] = (char *)c->ro[i];+ args[k++] = "--";+ for (int i = 0; i < na; i++)+ args[k++] = argv[i];+ args[k] = NULL;+ int in[2], out[2], err[2];+ if (pipe2(in, O_CLOEXEC) != 0) {+ free(args);+ snprintf(why, n, "pipe: %s", strerror(errno));+ return -1;+ }+ if (pipe2(out, O_CLOEXEC) != 0) {+ close(in[0]), close(in[1]);+ free(args);+ snprintf(why, n, "pipe: %s", strerror(errno));+ return -1;+ }+ if (pipe2(err, O_CLOEXEC) != 0) {+ close(in[0]), close(in[1]), close(out[0]), close(out[1]);+ free(args);+ snprintf(why, n, "pipe: %s", strerror(errno));+ return -1;+ }+ posix_spawn_file_actions_t fa;+ posix_spawn_file_actions_init(&fa);+ posix_spawn_file_actions_adddup2(&fa, in[0], 0);+ posix_spawn_file_actions_adddup2(&fa, out[1], 1);+ posix_spawn_file_actions_adddup2(&fa, err[1], 2);+ pid_t pid;+ int rc = posix_spawn(&pid, "/proc/self/exe", &fa, NULL, args, envp);+ posix_spawn_file_actions_destroy(&fa);+ free(args);+ close(in[0]);+ close(out[1]);+ close(err[1]);+ if (rc != 0) {+ close(in[1]), close(out[0]), close(err[0]);+ snprintf(why, n, "cannot start the cage: %s", strerror(rc));+ return -1;+ }+ sb->pid = pid;+ sb->in = in[1];+ sb->out = out[0];+ sb->err = err[0];+ return 0;+}++void janas_sandbox_kill(struct janas_sandbox *sb)+{+ if (sb->pid > 0) {+ kill(sb->pid, SIGKILL); /* its PID 1 dies with it, and all the rest */+ while (waitpid(sb->pid, NULL, 0) < 0 && errno == EINTR)+ ;+ }+ if (sb->in >= 0)+ close(sb->in);+ if (sb->out >= 0)+ close(sb->out);+ if (sb->err >= 0)+ close(sb->err);+ sb->pid = 0;+ sb->in = sb->out = sb->err = -1;+}++int janas_sandbox_available(char *why, size_t n)+{+ static int known = -1;+ static char reason[256];+ if (known < 0) {+ /* a cage built for true, read back */+ char dir[] = "/tmp/janas-sandbox-XXXXXX";+ struct janas_sandbox sb;+ char *argv[] = {"/usr/bin/true", NULL};+ char *envp[] = {NULL};+ struct janas_sandbox_cfg c = {.data_dir = dir};+ known = 0;+ if (!mkdtemp(dir)) {+ snprintf(reason, sizeof reason, "no temporary directory");+ } else if (janas_sandbox_spawn(&c, argv, envp, &sb, reason,+ sizeof reason) == 0) {+ close(sb.in);+ int st = 0;+ while (waitpid(sb.pid, &st, 0) < 0 && errno == EINTR)+ ;+ char msg[200] = "";+ ssize_t k = read(sb.err, msg, sizeof msg - 1);+ msg[k > 0 ? k : 0] = 0;+ char *nl = strchr(msg, '\n');+ if (nl)+ *nl = 0;+ close(sb.out);+ close(sb.err);+ known = WIFEXITED(st) && WEXITSTATUS(st) == 0;+ if (!known)+ snprintf(reason, sizeof reason, "%s",+ msg[0] ? msg+ : "the cage could not be built (are user "+ "namespaces allowed?)");+ }+ rmdir(dir);+ }+ if (!known)+ snprintf(why, n, "%s", reason);+ return known;+}++#else /* not Linux */++void janas_sandbox_main(int argc, char **argv)+{+ (void)argc;+ (void)argv;+}++int janas_sandbox_available(char *why, size_t n)+{+ snprintf(why, n, "the cage is built on Linux only, for now");+ return 0;+}++int janas_sandbox_spawn(const struct janas_sandbox_cfg *c, char *const argv[],+ char *const envp[], struct janas_sandbox *sb, char *why,+ size_t n)+{+ (void)c;+ (void)argv;+ (void)envp;+ (void)sb;+ snprintf(why, n, "the cage is built on Linux only, for now");+ return -1;+}++void janas_sandbox_kill(struct janas_sandbox *sb)+{+ (void)sb;+}++#endifdiff --git a/src/common/sandbox.h b/src/common/sandbox.hnew file mode 100644index 0000000..a273d97--- /dev/null+++ b/src/common/sandbox.h@@ -0,0 +1,58 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * sandbox.h - a cage for programs Janas runs but did not write (the code a+ * model writes for code_interpreter): Linux namespaces of its own (user,+ * mount, PID, network, IPC, UTS) without privileges, a root of its own+ * with the system's /usr read-only and one directory writable, no network,+ * no other process to see, no new processes (seccomp: threads only),+ * limits on memory, file sizes and open files.+ *+ * The cage is built by the program itself run again: janas_sandbox_spawn+ * runs /proc/self/exe with janas_sandbox_exec's first argument, and a+ * program offering the cage calls janas_sandbox_main first thing in main.+ * Linux only (x86-64 and AArch64); elsewhere janas_sandbox_available says+ * why not.+ */+#ifndef JANAS_COMMON_SANDBOX_H+#define JANAS_COMMON_SANDBOX_H++#include <stddef.h>+#include <stdint.h>+#include <sys/types.h>++#define JANAS_SANDBOX_ARG "--janas-sandbox-exec"++struct janas_sandbox_cfg {+ const char *data_dir; /* host directory, writable at /mnt/data */+ const char *const *ro; /* more host paths, read-only at the same path+ (NULL-terminated; may be NULL) */+ uint64_t mem_bytes; /* address space; 0: 2 GiB */+ uint64_t file_bytes; /* largest file written; 0: 1 GiB */+ uint64_t tmp_bytes; /* the /tmp in memory; 0: 256 MiB */+};++struct janas_sandbox {+ pid_t pid; /* the cage's first process */+ int in, out, err; /* its standard input, output and error */+};++/* Whether a cage can be built here: 1, or 0 with the reason. */+int janas_sandbox_available(char *why, size_t n);++/* argv (argv[0] a path inside the cage, as /usr/bin/python3) run in a cage+ with envp as its whole environment: 0 with sb filled, -1 with the+ reason. The cage dies with its caller. */+int janas_sandbox_spawn(const struct janas_sandbox_cfg *c, char *const argv[],+ char *const envp[], struct janas_sandbox *sb, char *why,+ size_t n);++/* The cage killed (and its pipes closed). */+void janas_sandbox_kill(struct janas_sandbox *sb);++/* In main, first: when argv[1] is JANAS_SANDBOX_ARG this process is a cage+ being built, and the function never returns; otherwise it returns at+ once. */+void janas_sandbox_main(int argc, char **argv);++#endifdiff --git a/src/get/steps.c b/src/get/steps.cindex 21e22b6..d3ce1ff 100644--- a/src/get/steps.c+++ b/src/get/steps.c@@ -80,15 +80,22 @@ int get_fetch(const char *repo, const char *path, const char *dir,}printf(" %s from %s, %.2f GB\n", path, repo, (double)size / 1e9);fflush(stdout);+ /* written aside and named only once checked: a download stopped+ halfway is never taken for the file. A partial one left under the+ final name by an older janas-get goes on aside. */+ char part[4200];+ snprintf(part, sizeof(part), "%s.part", local);+ if (access(part, F_OK) != 0 && access(local, F_OK) == 0)+ rename(local, part);struct bar b = {.t0 = now()};- FILE *f = fopen(local, "rb");+ FILE *f = fopen(part, "rb");if (f) {fseek(f, 0, SEEK_END);long at = ftell(f);b.from = at > 0 ? (uint64_t)at : 0;fclose(f);}- if (janas_hf_download(repo, NULL, path, local, size, got, progress, &b, err,+ if (janas_hf_download(repo, NULL, path, part, size, got, progress, &b, err,sizeof(err)) != 0) {fprintf(stderr, "\njanas-get: %s (run again to resume)\n", err);return -1;@@ -99,10 +106,19 @@ int get_fetch(const char *repo, const char *path, const char *dir,"janas-get: %s is not the file it should be (SHA-256 %s, ""not %s); deleted\n",local, got, expect);- remove(local);+ remove(part);return -1;}- printf(" SHA-256 checked%s\n", want ? " against MODELS.md" : "");+ if (rename(part, local) != 0) {+ fprintf(stderr, "janas-get: cannot name %s\n", local);+ return -1;+ }+ if (expect[0])+ printf(" SHA-256 checked%s\n", want ? " against MODELS.md" : "");+ else /* a small file kept in git, not LFS: Hugging Face lists no hash */+ printf(" not checked: Hugging Face publishes no SHA-256 for it "+ "(SHA-256 %s)\n",+ got);return 0;}diff --git a/src/llm/api.c b/src/llm/api.cindex 2ff1236..41cb357 100644--- a/src/llm/api.c+++ b/src/llm/api.c@@ -33,6 +33,7 @@#include "llm/quant.h"#include "llm/tokenizer.h"#include "llm/chat.h"+#include "llm/vision.h"#include "llm/metrics.h"static _Thread_local char last_error[256];@@ -329,7 +330,7 @@ static uint64_t npu_need(const struct janas_jns *j)}static void mem_need(const struct janas_jns *j, uint32_t n_ctx, int mtp_file,- struct mem_need *e)+ uint32_t min_block, struct mem_need *e){char arch[32] = "";janas_jns_meta_str(j, "general.architecture", arch, sizeof(arch));@@ -399,7 +400,8 @@ static void mem_need(const struct janas_jns *j, uint32_t n_ctx, int mtp_file,snprintf(key, sizeof(key), "%.32s.attention.sliding_window", arch);if (janas_jns_meta_u32(j, key, &window) == 0 && window) {const char *rg = getenv("JANAS_SWA_RING");- ring = janas_attn_ring_positions(window, janas_llm_prefill_block(j));+ ring = janas_attn_ring_positions(window,+ janas_llm_prefill_block(j, min_block));if (ring >= n_ctx || (rg && atoi(rg) == 0))ring = 0;}@@ -500,7 +502,7 @@ static void mem_need(const struct janas_jns *j, uint32_t n_ctx, int mtp_file,}snprintf(key, sizeof(key), "%.32s.expert_used_count", arch);janas_jns_meta_u32(j, key, &k_used);- uint64_t blk = janas_llm_prefill_block(j);+ uint64_t blk = janas_llm_prefill_block(j, min_block);if (blk < 256)blk = 256;uint64_t scratch =@@ -575,10 +577,11 @@ int32_t janas_llm_open(const char *path, const struct janas_llm_params *pp,janas_jns_meta_str(&j, "general.architecture", arch, sizeof(arch));int32_t rc = 0;/* byte-level BPE with Qwen's or Llama 3's pre-tokenizers, or Gemma 4's */- int known = (strcmp(kind, "gpt2") == 0 &&- (strcmp(pre, "qwen2") == 0 || strcmp(pre, "qwen35") == 0 ||- strcmp(pre, "llama-bpe") == 0)) ||- strcmp(kind, "gemma4") == 0;+ int known =+ (strcmp(kind, "gpt2") == 0 &&+ (strcmp(pre, "qwen2") == 0 || strcmp(pre, "qwen35") == 0 ||+ strcmp(pre, "llama-bpe") == 0 || strcmp(pre, "pixtral") == 0)) ||+ strcmp(kind, "gemma4") == 0;if (!known)rc = janas_api_fail(JANAS_LLM_EMODEL, "tokenizer %s/%s not supported",kind, pre);@@ -587,7 +590,10 @@ int32_t janas_llm_open(const char *path, const struct janas_llm_params *pp,elserc = find_format(llm, &j);struct mem_need e;- mem_need(&j, p.n_ctx, p.mtp_path != NULL, &e);+ /* a vision encoder beside it whose pictures attend both ways: the+ prefill block holds a picture whole */+ uint32_t pic_block = janas_api_picture_block(path);+ mem_need(&j, p.n_ctx, p.mtp_path != NULL, pic_block, &e);janas_jns_close(&j);if (rc != 0) {janas_tokenizer_destroy(llm->tok);@@ -683,8 +689,14 @@ int32_t janas_llm_open(const char *path, const struct janas_llm_params *pp,.expert_bits = p.expert_bits,.attn_scores = p.attn_scores,.head_bits = p.head_bits,+ .min_quality = p.min_quality < JANAS_LLM_QUALITY_LOW ||+ p.min_quality > JANAS_LLM_QUALITY_HIGH+ ? JANAS_LLM_QUALITY_LOW+ : p.min_quality,.no_npu = no_npu,- .ple_ram = table_ram};+ .ple_ram = table_ram,+ .min_block = pic_block};+ llm->path = strdup(path);llm->m = janas_llm_model_load(path, &mo, err, sizeof(err));if (!llm->m) {janas_tokenizer_destroy(llm->tok);@@ -799,7 +811,10 @@ int32_t janas_llm_estimate(const char *path, const struct janas_llm_params *pp,if (janas_jns_open(path, &j, err, sizeof(err)) != 0)return janas_api_fail(JANAS_LLM_EOPEN, "%s: %s", path, err);struct mem_need e;- mem_need(&j, p.n_ctx, p.mtp_path != NULL, &e);+ /* a vision encoder beside it whose pictures attend both ways: the+ prefill block holds a picture whole */+ uint32_t pic_block = janas_api_picture_block(path);+ mem_need(&j, p.n_ctx, p.mtp_path != NULL, pic_block, &e);janas_jns_close(&j);if (fixed)*fixed = mem_fixed(&e, &p);@@ -834,6 +849,8 @@ void janas_llm_close(janas_llm *llm)janas_llm_model_free(llm->draft);janas_llm_model_free(llm->m);janas_tokenizer_destroy(llm->tok);+ janas_vision_close(llm->vis);+ free(llm->path);free(llm);}diff --git a/src/llm/api_chat.c b/src/llm/api_chat.cindex aa8a3c7..752d1ed 100644--- a/src/llm/api_chat.c+++ b/src/llm/api_chat.c@@ -144,6 +144,8 @@ void janas_llm_chat_destroy(janas_llm_chat *c)free(c->memo[i].ids);}free(c->rtext);+ janas_api_pics_free(c->pics, c->n_pics);+ free(c->idpics);free(c);}@@ -515,6 +517,7 @@ static int recap(janas_llm_chat *c)memcpy(keep, c->ids, n_keep * sizeof(int32_t));}c->n_ids = c->n_text = 0;+ c->n_idpics = 0;int err = 0;if (!c->closed)err |= janas_api_add_special(c, c->llm->im_end);@@ -619,6 +622,7 @@ int32_t janas_llm_chat_prepare(janas_llm_chat *c, int32_t *done, int32_t *total)if (janas_llm_session_length(c->s) > 0 || c->replying)return 0; /* the conversation has started */c->n_ids = c->n_text = 0;+ c->n_idpics = 0;int err = janas_api_begin_sequence(c);if (c->system || c->tools_sys || c->llm->format == FORMAT_GEMMA ||janas_api_think_note(c))@@ -848,6 +852,7 @@ static int32_t send_turn(janas_llm_chat *c, const char *text, size_t n,}janas_api_reply_clear(c);c->n_ids = c->n_text = 0;+ c->n_idpics = 0;int err = ans.oom;/* (ans is freed once written, below) */if (gemma_answers) {@@ -888,7 +893,8 @@ static int32_t send_turn(janas_llm_chat *c, const char *text, size_t n,err |= janas_api_add_special(c, c->llm->im_end) ||janas_api_end_turn_nl(c);} else {- err |= janas_api_add_message(c, "user", text, n);+ /* the pictures queued: before the text, as the template has it */+ err |= janas_api_add_user(c, text, n, c->pics, c->n_pics);}err |= janas_api_flush_text(c);size_t reply_off = c->n_ids;@@ -935,6 +941,16 @@ static int32_t send_turn(janas_llm_chat *c, const char *text, size_t n,return janas_api_fail(JANAS_LLM_EFULL,"the context is full (%u tokens)", c->llm->n_ctx);}+ /* the pictures' embeddings where their tokens are */+ if (c->n_idpics) {+ int bad = janas_api_media_register(c, at, 0);+ c->n_idpics = 0;+ janas_api_pics_free(c->pics, c->n_pics);+ c->pics = NULL;+ c->n_pics = 0;+ if (bad)+ return janas_api_fail(JANAS_LLM_ENOMEM, "out of memory");+ }if (janas_api_begin_reply(c, at + (uint32_t)reply_off,at + (uint32_t)c->n_ids, at) != 0)return janas_api_fail(JANAS_LLM_ENOMEM, "out of memory");diff --git a/src/llm/api_image.c b/src/llm/api_image.cnew file mode 100644index 0000000..cdc778f--- /dev/null+++ b/src/llm/api_image.c@@ -0,0 +1,193 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * api_image.c - pictures in a conversation (janas_llm_chat_image): read by+ * the model's vision encoder (vision.c) from the file beside the model -+ * the .vision.jns whose name is the longest prefix of the model's+ * (qwen3.5-0.8b.vision.jns serves qwen3.5-0.8b-q4km.jns) - and queued for+ * the next user message, sent or added. In the prompt a picture is its+ * start token (<|vision_start|>, Gemma's <|image>), one image token+ * (<|image_pad|>, <|image|>) a token of its grid and its end token, before+ * the message's text as the chat templates put it - ChatML's turn and+ * Gemma's alike;+ * once the prompt is in the session the model takes the pictures'+ * embeddings at those places (model_media.c). Every token id of a picture+ * is the same whatever the picture, so no computed prefix is reused past+ * the first picture, and a conversation with pictures is not kept.+ */+#include "chat.h"++#include <dirent.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "common/image.h"+#include "jns.h"+#include "model.h"+#include "tokenizer.h"+#include "vision.h"++#define MAX_IMAGE_TOKENS 1024 /* a picture's tokens at most: 1 Mpixel */++/* The encoder's file beside the model at path into best (empty: none) */+static void vision_path(const char *path, char *best, size_t best_len)+{+ char dir[4096];+ best[0] = 0;+ const char *slash = strrchr(path, '/');+ const char *base = slash ? slash + 1 : path;+ if (slash)+ snprintf(dir, sizeof dir, "%.*s", (int)(slash - path), path);+ else+ snprintf(dir, sizeof dir, ".");+ size_t best_stem = 0;+ DIR *d = opendir(dir);+ struct dirent *e;+ while (d && (e = readdir(d))) {+ size_t n = strlen(e->d_name), sfx = strlen(".vision.jns");+ if (n <= sfx || strcmp(e->d_name + n - sfx, ".vision.jns"))+ continue;+ size_t stem = n - sfx;+ if (stem > best_stem && !strncmp(base, e->d_name, stem)) {+ best_stem = stem;+ snprintf(best, best_len, "%s/%s", dir, e->d_name);+ }+ }+ if (d)+ closedir(d);+}++uint32_t janas_api_picture_block(const char *path)+{+ char best[8448];+ vision_path(path, best, sizeof best);+ struct janas_jns j;+ char err[256];+ uint32_t both = 0, most = 0;+ if (!best[0] || janas_jns_open(best, &j, err, sizeof err) != 0)+ return 0;+ janas_jns_meta_u32(&j, "vision.bidirectional", &both);+ janas_jns_meta_u32(&j, "vision.max_soft_tokens", &most);+ janas_jns_close(&j);+ return both && most && most <= 4096 ? (most + 63) / 64 * 64 : 0;+}++/* The encoder beside the model: 0, or -1 with the reason in the library's+ error. */+static int vision_of(janas_llm *llm)+{+ if (llm->vis)+ return 0;+ if (!llm->path)+ return janas_api_fail(JANAS_LLM_EMODEL, "no vision encoder"), -1;+ char best[8448];+ vision_path(llm->path, best, sizeof best);+ if (!best[0])+ return janas_api_fail(JANAS_LLM_EMODEL,+ "this model has no vision encoder beside it "+ "(a <model>.vision.jns, made by hf2jns_vision)"),+ -1;+ char err[300];+ llm->vis = janas_vision_open(best, err, sizeof err);+ if (!llm->vis)+ return janas_api_fail(JANAS_LLM_EMODEL, "%s", err), -1;+ if (janas_vision_dim(llm->vis) != janas_llm_model_n_embd(llm->m)) {+ janas_vision_close(llm->vis);+ llm->vis = NULL;+ return janas_api_fail(JANAS_LLM_EMODEL,+ "%s: its embeddings are not this model's width",+ best),+ -1;+ }+ return 0;+}++int32_t janas_llm_chat_image(janas_llm_chat *c, const void *data, int64_t n)+{+ if (!c || !data || n <= 0)+ return janas_api_fail(JANAS_LLM_EINVAL, "invalid argument");+ if (c->llm->format != FORMAT_CHATML && c->llm->format != FORMAT_GEMMA)+ return janas_api_fail(JANAS_LLM_EMODEL,+ "pictures are read with ChatML and Gemma "+ "models only, for now");+ if (vision_of(c->llm) != 0)+ return JANAS_LLM_EMODEL;+ struct janas_image im;+ char why[200];+ if (janas_image_decode(data, (size_t)n, &im, why, sizeof why) != 0)+ return janas_api_fail(JANAS_LLM_EINVAL, "%s", why);+ int max = MAX_IMAGE_TOKENS;+ const char *env = getenv("JANAS_IMAGE_MAX_TOKENS");+ if (env && atoi(env) > 0)+ max = atoi(env);+ float *embd;+ int nx, ny;+ int rc = janas_vision_encode(c->llm->vis, &im, max,+ janas_llm_model_pool(c->llm->m), &embd, &nx,+ &ny, why, sizeof why);+ janas_image_free(&im);+ if (rc != 0)+ return janas_api_fail(JANAS_LLM_ENOMEM, "%s", why);+ struct cpic *t = realloc(c->pics, sizeof(*t) * (c->n_pics + 1));+ if (!t) {+ free(embd);+ return janas_api_fail(JANAS_LLM_ENOMEM, "out of memory");+ }+ c->pics = t;+ c->pics[c->n_pics++] =+ (struct cpic){.embd = embd, .nx = (uint32_t)nx, .ny = (uint32_t)ny};+ return JANAS_LLM_OK;+}++void janas_api_pics_free(struct cpic *p, size_t n)+{+ for (size_t i = 0; i < n; i++)+ free(p[i].embd);+ free(p);+}++int janas_api_add_user(janas_llm_chat *c, const char *text, size_t n,+ const struct cpic *pics, size_t n_pics)+{+ if (!n_pics)+ return janas_api_add_message(c, "user", text, n);+ const janas_llm *llm = c->llm;+ int32_t img, vs, ve;+ janas_vision_tokens(llm->vis, &img, &vs, &ve);+ int err = janas_api_add_special(c, llm->im_start) ||+ janas_api_add_text(c, "user\n", 5);+ for (size_t i = 0; i < n_pics && !err; i++) {+ err |= janas_api_add_special(c, vs);+ struct ppic *t = realloc(c->idpics, sizeof(*t) * (c->n_idpics + 1));+ if (!t)+ return -1;+ c->idpics = t;+ c->idpics[c->n_idpics++] =+ (struct ppic){.off = (uint32_t)c->n_ids, .pic = &pics[i]};+ for (uint32_t k = 0; k < pics[i].nx * pics[i].ny && !err; k++)+ err |= janas_api_add_special(c, img);+ err |= janas_api_add_special(c, ve);+ }+ return err || janas_api_add_text(c, text, n) ||+ janas_api_add_special(c, llm->im_end) || janas_api_end_turn_nl(c);+}++uint32_t janas_api_media_first(const janas_llm_chat *c)+{+ return c->n_idpics ? c->idpics[0].off : UINT32_MAX;+}++int janas_api_media_register(janas_llm_chat *c, uint32_t base, uint32_t from)+{+ for (size_t i = 0; i < c->n_idpics; i++) {+ const struct ppic *p = &c->idpics[i];+ if (p->off < from)+ continue; /* in the session already, as it was */+ if (janas_llm_model_media_add(c->llm->m, base + p->off, p->pic->embd,+ p->pic->nx, p->pic->ny,+ janas_vision_both_ways(c->llm->vis)) != 0)+ return -1;+ }+ return 0;+}diff --git a/src/llm/api_load.c b/src/llm/api_load.cindex 9288ec2..7ddcfe0 100644--- a/src/llm/api_load.c+++ b/src/llm/api_load.c@@ -21,6 +21,7 @@ static void free_msgs(janas_llm_chat *c)free((char *)m->calls[k].args);}free(m->calls);+ janas_api_pics_free(m->pics, m->n_pics);}c->n_msgs = 0;}@@ -73,6 +74,13 @@ int32_t janas_llm_chat_add(janas_llm_chat *c, int32_t role, const char *text,if (!t)return janas_api_fail(JANAS_LLM_ENOMEM, "out of memory");c->msgs[c->n_msgs++] = (struct cmsg){.role = role, .text = t, .n = n};+ if (role == JANAS_LLM_ROLE_USER && c->n_pics) {+ /* the pictures queued go with it */+ c->msgs[c->n_msgs - 1].pics = c->pics;+ c->msgs[c->n_msgs - 1].n_pics = c->n_pics;+ c->pics = NULL;+ c->n_pics = 0;+ }return JANAS_LLM_OK;}@@ -235,7 +243,7 @@ static int render(janas_llm_chat *c, uint32_t *reply_off)err |= janas_api_flush_text(c);c->last_at = (uint32_t)c->n_ids; /* the last one's, at the end */err |= janas_api_note_turn(c, (uint32_t)c->n_ids);- err |= janas_api_add_message(c, "user", m->text, m->n);+ err |= janas_api_add_user(c, m->text, m->n, m->pics, m->n_pics);break;case JANAS_LLM_ROLE_ASSISTANT: {if (llm->tools == JANAS_TOOLS_GEMMA && (m->n_calls || gemma_open)) {@@ -314,10 +322,15 @@ static int32_t load_ids(janas_llm_chat *c, uint32_t reply_off)c->n_ids, c->llm->n_ctx);if (c->llm->mode == JANAS_LLM_MODE_AUTO)janas_api_apply_mode(c->llm);- janas_api_keep_pick(c);+ /* a picture's tokens are the same whatever the picture: nothing+ computed is taken past the first one, nor kept */+ uint32_t media = janas_api_media_first(c);+ if (media == UINT32_MAX)+ janas_api_keep_pick(c);const int32_t *h;uint32_t hn = janas_llm_session_tokens(c->s, &h), same = 0;- while (same < hn && same < c->n_ids && h[same] == c->ids[same])+ while (same < hn && same < c->n_ids && same < media &&+ h[same] == c->ids[same])same++;/* the last token must be read for its logits: when it has been, keepone less (a prompt kept before its reply has not: it is kept whole,@@ -339,6 +352,8 @@ static int32_t load_ids(janas_llm_chat *c, uint32_t reply_off)return janas_api_fail(JANAS_LLM_EFULL,"the context is full (%u tokens)", c->llm->n_ctx);}+ if (media != UINT32_MAX && janas_api_media_register(c, 0, 0) != 0)+ return janas_api_fail(JANAS_LLM_ENOMEM, "out of memory");c->dropped = 0;c->loaded = (uint32_t)c->n_ids;if (janas_api_begin_reply(c, reply_off, (uint32_t)c->n_ids, same) != 0)@@ -346,7 +361,7 @@ static int32_t load_ids(janas_llm_chat *c, uint32_t reply_off)/* the prompt is kept once read, which janas_llm_chat_next does a blockat a time: reading it here would leave the caller without word foras long as it takes */- c->keep_prompt = 1;+ c->keep_prompt = media == UINT32_MAX;return JANAS_LLM_OK;}@@ -363,6 +378,7 @@ int32_t janas_llm_chat_run(janas_llm_chat *c)c->tools_tokens = 0; /* until a system message with tools says */janas_api_reply_clear(c);c->n_ids = c->n_text = 0;+ c->n_idpics = 0;c->n_turn = 0;c->sys_tokens = 0;free(c->system);@@ -391,11 +407,13 @@ int32_t janas_llm_chat_count(janas_llm_chat *c, int32_t *n)if (turns)memcpy(saved, c->turn, turns * sizeof(*saved));c->n_ids = c->n_text = 0;+ c->n_idpics = 0;c->n_turn = 0;uint32_t reply_off;int err = render(c, &reply_off) || janas_api_flush_text(c);*n = (int32_t)c->n_ids;c->n_ids = c->n_text = 0;+ c->n_idpics = 0;c->sys_tokens = sys;c->n_turn = 0;for (size_t i = 0; i < turns && !err; i++)@@ -437,6 +455,7 @@ int32_t janas_llm_chat_prompt(janas_llm_chat *c, const char *text, int32_t len)c->tools_tokens = 0;janas_api_reply_clear(c);c->n_ids = c->n_text = 0;+ c->n_idpics = 0;c->n_turn = 0;c->sys_tokens = 0;/* Llama 3's text starts with <|begin_of_text|>, as its tokenizer adds */@@ -465,6 +484,7 @@ int32_t janas_llm_chat_infill(janas_llm_chat *c, const char *prefix,c->tools_tokens = 0;janas_api_reply_clear(c);c->n_ids = c->n_text = 0;+ c->n_idpics = 0;c->n_turn = 0;c->sys_tokens = 0;int err =diff --git a/src/llm/api_speech.c b/src/llm/api_speech.cnew file mode 100644index 0000000..e3d2720--- /dev/null+++ b/src/llm/api_speech.c@@ -0,0 +1,383 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * api_speech.c - speech to text in the public API (janas/llm.h): a Whisper+ * model (whisper.h) and the last transcription it made.+ */+#include <stdlib.h>+#include <math.h>+#include <string.h>+#include <unistd.h>++#include "api_speech.h"+#include "chat.h"+#include "common/audio.h"+#include "tokenizer.h"++char *janas_speech_beside(const char *path, const char *file)+{+ const char *slash = strrchr(path, '/');+ size_t dl = slash ? (size_t)(slash - path) + 1 : 0, fl = strlen(file);+ char *p = malloc(dl + fl + 1);+ if (!p)+ return NULL;+ memcpy(p, path, dl);+ memcpy(p + dl, file, fl + 1);+ if (access(p, R_OK) != 0) {+ free(p);+ return NULL;+ }+ return p;+}++void janas_llm_speech_params_default(struct janas_llm_speech_params *p)+{+ if (!p)+ return;+ memset(p, 0, sizeof *p);+ p->size = sizeof *p;+}++int32_t janas_llm_speech_open(const char *path,+ const struct janas_llm_speech_params *p,+ janas_llm_speech **out)+{+ if (!path || !out || (p && p->size != sizeof *p))+ return janas_api_fail(JANAS_LLM_EINVAL, "invalid argument");+ *out = NULL;+ janas_llm_speech *s = calloc(1, sizeof *s);+ if (!s)+ return janas_api_fail(JANAS_LLM_ENOMEM, "out of memory");+ char err[512];+ s->w = janas_whisper_open(path, p ? p->threads : 0, err, sizeof err);+ if (!s->w) {+ free(s);+ return janas_api_fail(JANAS_LLM_EOPEN, "%s", err);+ }+ /* the voice activity detector: named, or beside the model */+ if (p && p->vad_model) {+ s->vad = janas_vad_open(p->vad_model, err, sizeof err);+ if (!s->vad) {+ janas_llm_speech_close(s);+ return janas_api_fail(JANAS_LLM_EOPEN, "%s", err);+ }+ } else {+ char *vp = janas_speech_beside(path, "silero-vad.jns");+ if (vp)+ s->vad = janas_vad_open(vp, err, sizeof err);+ free(vp);+ }+ /* the speaker model for diarization: named, or beside the model */+ if (p && p->speaker_model) {+ s->speaker =+ janas_speaker_open(p->speaker_model, p->threads, err, sizeof err);+ if (!s->speaker) {+ janas_llm_speech_close(s);+ return janas_api_fail(JANAS_LLM_EOPEN, "%s", err);+ }+ } else {+ char *sp = janas_speech_beside(path, "wespeaker-resnet34.jns");+ if (sp)+ s->speaker =+ janas_speaker_open(sp, p ? p->threads : 0, err, sizeof err);+ free(sp);+ }+ *out = s;+ return JANAS_LLM_OK;+}++void janas_llm_speech_close(janas_llm_speech *s)+{+ if (!s)+ return;+ janas_whisper_result_free(&s->r);+ janas_speech_turns_free(s);+ janas_whisper_close(s->w);+ janas_vad_close(s->vad);+ janas_speaker_close(s->speaker);+ free(s);+}++void janas_llm_transcribe_params_default(struct janas_llm_transcribe_params *p)+{+ if (!p)+ return;+ memset(p, 0, sizeof *p);+ p->size = sizeof *p;+ p->vad_threshold = 0.5f;+ p->vad_prefix_ms = 300;+ p->vad_silence_ms = 200;+}++struct cb {+ void (*fn)(void *user, int32_t segment);+ void *user;+};++static void on_segment(void *user, uint32_t index)+{+ const struct cb *c = user;+ c->fn(c->user, (int32_t)index);+}++int32_t janas_llm_speech_transcribe(janas_llm_speech *s, const void *data,+ int64_t n,+ const struct janas_llm_transcribe_params *p)+{+ if (!s || !data || n <= 0 || (p && p->size != sizeof *p) ||+ (p && (p->temperature < 0 || p->temperature > 1)))+ return janas_api_fail(JANAS_LLM_EINVAL, "invalid argument");+ janas_whisper_result_free(&s->r);+ struct janas_audio a;+ char err[512];+ if (janas_audio_decode(data, (size_t)n, &a, err, sizeof err) != 0)+ return janas_api_fail(JANAS_LLM_EINVAL, "%s", err);+ if (janas_audio_resample(&a, s->w->rate) != 0) {+ janas_audio_free(&a);+ return janas_api_fail(JANAS_LLM_ENOMEM, "out of memory");+ }+ struct janas_whisper_opts o = {0};+ struct cb cb = {0};+ if (p) {+ o.language = p->language;+ o.translate = p->translate != 0;+ o.prompt = p->prompt;+ o.temperature = p->temperature;+ o.seed = p->seed;+ o.words = p->words != 0 || p->diarize != 0;+ if (p->on_segment) {+ o.on_segment = on_segment;+ o.user = &cb;+ cb.fn = p->on_segment;+ cb.user = p->user;+ }+ o.stop = (volatile int *)p->stop;+ }+ if (o.language && o.language[0] &&+ janas_whisper_lang_index(s->w, o.language) < 0 && s->w->multilingual) {+ janas_audio_free(&a);+ return janas_api_fail(JANAS_LLM_EINVAL,+ "a language this model does not know: %s",+ o.language);+ }+ /* chunking: the stretches of speech the detector finds */+ size_t *regions = NULL;+ if (p && p->chunking) {+ if (!s->vad) {+ janas_audio_free(&a);+ return janas_api_fail(JANAS_LLM_EINVAL,+ "chunking needs a voice activity detector: "+ "silero-vad.jns (made by silero2jns) beside "+ "the speech model, or vad_model");+ }+ struct janas_vad_params vp = {.threshold = 0.5f,+ .min_speech_ms = 250,+ .min_silence_ms = 200,+ .speech_pad_ms = 300};+ float *src = a.pcm, *norm = NULL;+ if (p->chunking == 2) {+ if (p->vad_threshold > 0)+ vp.threshold = p->vad_threshold;+ if (p->vad_silence_ms > 0)+ vp.min_silence_ms = (uint32_t)p->vad_silence_ms;+ if (p->vad_prefix_ms >= 0)+ vp.speech_pad_ms = (uint32_t)p->vad_prefix_ms;+ } else if ((norm = malloc(sizeof(float) * (a.n ? a.n : 1)))) {+ /* auto: the loudness normalized for the detector, its peak at+ 0.9 */+ float peak = 0;+ for (size_t i = 0; i < a.n; i++)+ peak = fabsf(a.pcm[i]) > peak ? fabsf(a.pcm[i]) : peak;+ float g = peak > 0 ? 0.9f / peak : 1;+ for (size_t i = 0; i < a.n; i++)+ norm[i] = a.pcm[i] * g;+ src = norm;+ }+ float *probs = NULL;+ long np = janas_vad_probs(s->vad, src, a.n, &probs);+ long ns =+ np < 0 ? -1+ : janas_vad_segments(s->vad, probs, np, a.n, &vp, ®ions);+ free(probs);+ free(norm);+ if (ns < 0) {+ janas_audio_free(&a);+ return janas_api_fail(JANAS_LLM_ENOMEM, "out of memory");+ }+ static const size_t NONE[2] = {0, 0};+ o.regions = ns ? regions : NONE;+ o.n_regions = ns ? (size_t)ns : 1;+ }+ janas_speech_turns_free(s);+ if (p && p->diarize && !s->speaker) {+ free(regions);+ janas_audio_free(&a);+ return janas_api_fail(JANAS_LLM_EINVAL,+ "diarization needs a speaker model: "+ "wespeaker-resnet34.jns (made by wespeaker2jns) "+ "beside the speech model, or speaker_model");+ }+ int rc =+ janas_whisper_transcribe(s->w, a.pcm, a.n, &o, &s->r, err, sizeof err);+ if (rc == 0 && p && p->diarize &&+ janas_speech_diarize(s, a.pcm, a.n, p->chunking ? o.regions : NULL,+ p->chunking ? o.n_regions : 0, p, err,+ sizeof err) != 0)+ rc = -1;+ free(regions);+ janas_audio_free(&a);+ if (rc != 0)+ return janas_api_fail(strstr(err, "memory") ? JANAS_LLM_ENOMEM+ : strstr(err, "English only") ? JANAS_LLM_EINVAL+ : JANAS_LLM_EFAIL,+ "%s", err);+ return JANAS_LLM_OK;+}++int32_t janas_llm_speech_text(const janas_llm_speech *s, char *buf, int32_t cap,+ int32_t *len)+{+ if (!s)+ return janas_api_fail(JANAS_LLM_EINVAL, "no model");+ const char *t = s->r.text ? s->r.text : "";+ return janas_api_copy_out(t, strlen(t), buf, cap, len);+}++int32_t janas_llm_speech_language(const janas_llm_speech *s, char *buf,+ int32_t cap, int32_t *len)+{+ if (!s)+ return janas_api_fail(JANAS_LLM_EINVAL, "no model");+ return janas_api_copy_out(s->r.language, strlen(s->r.language), buf, cap,+ len);+}++const char *janas_llm_speech_language_name(const char *code)+{+ return code ? janas_whisper_lang_name(code) : NULL;+}++double janas_llm_speech_duration(const janas_llm_speech *s)+{+ return s ? s->r.duration : 0;+}++int32_t janas_llm_speech_segments(const janas_llm_speech *s)+{+ return s ? (int32_t)s->r.n_segs : 0;+}++int32_t janas_llm_speech_segment(const janas_llm_speech *s, int32_t i,+ double *start, double *end, char *buf,+ int32_t cap, int32_t *len)+{+ if (!s || i < 0 || (uint32_t)i >= s->r.n_segs)+ return janas_api_fail(JANAS_LLM_EINVAL, "no such segment");+ const struct janas_whisper_seg *g = &s->r.segs[i];+ if (start)+ *start = g->t0;+ if (end)+ *end = g->t1;+ if (!buf && !len)+ return JANAS_LLM_OK;+ return janas_api_copy_out(g->text, strlen(g->text), buf, cap, len);+}++int32_t janas_llm_speech_segment_info(const janas_llm_speech *s, int32_t i,+ float *avg_logprob, float *no_speech_prob,+ float *temperature,+ float *compression_ratio, int32_t *ids,+ int32_t max, int32_t *n)+{+ if (!s || i < 0 || (uint32_t)i >= s->r.n_segs || max < 0)+ return janas_api_fail(JANAS_LLM_EINVAL, "no such segment");+ const struct janas_whisper_seg *g = &s->r.segs[i];+ if (avg_logprob)+ *avg_logprob = g->avg_logprob;+ if (no_speech_prob)+ *no_speech_prob = g->no_speech_prob;+ if (temperature)+ *temperature = g->temperature;+ if (compression_ratio)+ *compression_ratio = g->compression_ratio;+ if (n)+ *n = (int32_t)g->n_tokens;+ if (ids)+ memcpy(ids, g->tokens,+ sizeof(int32_t) *+ ((uint32_t)max < g->n_tokens ? (uint32_t)max : g->n_tokens));+ return ids && (uint32_t)max < g->n_tokens+ ? janas_api_fail(JANAS_LLM_ESMALL, "buffer too small")+ : JANAS_LLM_OK;+}++int32_t janas_llm_speech_words(const janas_llm_speech *s)+{+ if (!s)+ return 0;+ uint32_t n = 0;+ for (uint32_t i = 0; i < s->r.n_segs; i++)+ n += s->r.segs[i].n_words;+ return (int32_t)n;+}++int32_t janas_llm_speech_word(const janas_llm_speech *s, int32_t i,+ double *start, double *end, float *probability,+ char *buf, int32_t cap, int32_t *len)+{+ if (!s || i < 0)+ return janas_api_fail(JANAS_LLM_EINVAL, "no such word");+ uint32_t k = (uint32_t)i;+ for (uint32_t g = 0; g < s->r.n_segs; g++) {+ const struct janas_whisper_seg *sg = &s->r.segs[g];+ if (k >= sg->n_words) {+ k -= sg->n_words;+ continue;+ }+ const struct janas_whisper_word *w = &sg->words[k];+ if (start)+ *start = w->t0;+ if (end)+ *end = w->t1;+ if (probability)+ *probability = w->probability;+ if (!buf && !len)+ return JANAS_LLM_OK;+ return janas_api_copy_out(w->text, strlen(w->text), buf, cap, len);+ }+ return janas_api_fail(JANAS_LLM_EINVAL, "no such word");+}++int32_t janas_llm_speech_segment_logprobs(const janas_llm_speech *s, int32_t i,+ float *lp, int32_t max, int32_t *n)+{+ if (!s || i < 0 || (uint32_t)i >= s->r.n_segs || max < 0)+ return janas_api_fail(JANAS_LLM_EINVAL, "no such segment");+ const struct janas_whisper_seg *g = &s->r.segs[i];+ if (n)+ *n = (int32_t)g->n_tokens;+ if (lp)+ memcpy(lp, g->logprobs,+ sizeof(float) *+ ((uint32_t)max < g->n_tokens ? (uint32_t)max : g->n_tokens));+ return lp && (uint32_t)max < g->n_tokens+ ? janas_api_fail(JANAS_LLM_ESMALL, "buffer too small")+ : JANAS_LLM_OK;+}++int32_t janas_llm_speech_token(const janas_llm_speech *s, int32_t id, char *buf,+ int32_t cap, int32_t *len)+{+ if (!s || id < 0 || (uint32_t)id >= s->w->vocab)+ return janas_api_fail(JANAS_LLM_EINVAL, "no such token");+ char tmp[256];+ size_t k = janas_tokenizer_decode(s->w->tk, id, tmp, sizeof tmp);+ if (k > sizeof tmp)+ k = sizeof tmp;+ return janas_api_copy_out(tmp, k, buf, cap, len);+}++int32_t janas_llm_speech_token_is_text(const janas_llm_speech *s, int32_t id)+{+ return s && id >= 0 && id < s->w->eot;+}diff --git a/src/llm/api_speech.h b/src/llm/api_speech.hnew file mode 100644index 0000000..d5c5e86--- /dev/null+++ b/src/llm/api_speech.h@@ -0,0 +1,47 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * api_speech.h - the speech model of the public API (api_speech.c) and+ * its diarization (api_speech_diar.c): a Whisper model, its voice activity+ * detector and speaker model, the last transcription and its turns.+ */+#ifndef JANAS_LLM_API_SPEECH_H+#define JANAS_LLM_API_SPEECH_H++#include "janas/llm.h"+#include "speaker.h"+#include "vad.h"+#include "whisper.h"++/* a speaker's turn: consecutive words of one speaker */+struct janas_turn {+ double t0, t1;+ char *text;+ char *speaker; /* a known speaker's name, or "A", "B"... */+};++struct janas_llm_speech {+ struct janas_whisper *w;+ struct janas_whisper_result r;+ struct janas_vad *vad; /* or NULL */+ struct janas_speaker *speaker; /* or NULL */+ struct janas_turn *turns;+ uint32_t n_turns;+};++/* the model beside path (its directory) named file, opened by open, or+ NULL when there is none */+char *janas_speech_beside(const char *path, const char *file);++/* the speakers of the transcription just made (s->r, its words' times)+ of pcm (n samples at 16 kHz), from the stretches of speech regions (NULL:+ found here, or the whole recording without a detector): its turns in+ s->turns. 0, or -1 with the reason in err (also a known speaker's+ recording that cannot be read) */+int janas_speech_diarize(janas_llm_speech *s, const float *pcm, size_t n,+ const size_t *regions, size_t n_regions,+ const struct janas_llm_transcribe_params *p, char *err,+ size_t err_len);+void janas_speech_turns_free(janas_llm_speech *s);++#endifdiff --git a/src/llm/api_speech_diar.c b/src/llm/api_speech_diar.cnew file mode 100644index 0000000..6101052--- /dev/null+++ b/src/llm/api_speech_diar.c@@ -0,0 +1,256 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * api_speech_diar.c - who speaks when, in the public API: the+ * transcription's stretches of speech (the voice activity detector's, or+ * the whole recording) diarized (diarize.c), the known speakers'+ * recordings embedded, each word given the speaker of the windows that+ * overlap it most, the words joined into turns - consecutive words of one+ * speaker, a pause of more than 2 s or a segment past 20 s of the turn+ * starting another - named after the+ * known speakers or "A", "B"... as they first speak.+ */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "api_speech.h"+#include "chat.h"+#include "common/audio.h"++#define MAX_KNOWN 8+#define REF_FRAMES 3000 /* 30 s of a known speaker at most */++void janas_speech_turns_free(janas_llm_speech *s)+{+ for (uint32_t i = 0; i < s->n_turns; i++) {+ free(s->turns[i].text);+ free(s->turns[i].speaker);+ }+ free(s->turns);+ s->turns = NULL;+ s->n_turns = 0;+}++/* a known speaker's recording embedded: 0, or -1 with why */+static int embed_ref(janas_llm_speech *s, const void *data, int64_t n,+ float *emb, char *err, size_t err_len)+{+ struct janas_audio a;+ char why[256];+ if (!data || n <= 0 ||+ janas_audio_decode(data, (size_t)n, &a, why, sizeof why) != 0) {+ snprintf(err, err_len, "a known speaker's recording: %s",+ data && n > 0 ? why : "none given");+ return -1;+ }+ int rc = -1;+ uint32_t frames = 0;+ float *fb = NULL;+ if (janas_audio_resample(&a, 16000) == 0)+ fb = janas_speaker_fbank(a.pcm, a.n, &frames);+ if (!fb)+ snprintf(err, err_len, "out of memory");+ else if (janas_speaker_embed(s->speaker, fb, 0,+ frames < REF_FRAMES ? frames : REF_FRAMES,+ emb) != 0)+ snprintf(err, err_len,+ "a known speaker's recording too short (at least 0.1 s)");+ else+ rc = 0;+ free(fb);+ janas_audio_free(&a);+ return rc;+}++int janas_speech_diarize(janas_llm_speech *s, const float *pcm, size_t n,+ const size_t *regions, size_t n_regions,+ const struct janas_llm_transcribe_params *p, char *err,+ size_t err_len)+{+ int nk = p->n_speakers > 0 ? p->n_speakers : 0;+ if (nk > MAX_KNOWN) {+ snprintf(err, err_len, "at most %d known speakers", MAX_KNOWN);+ return -1;+ }+ if (nk && (!p->speaker_refs || !p->speaker_ref_bytes)) {+ snprintf(err, err_len, "known speakers without their recordings");+ return -1;+ }+ float ref[MAX_KNOWN * 256];+ for (int k = 0; k < nk; k++)+ if (embed_ref(s, p->speaker_refs[k], p->speaker_ref_bytes[k],+ ref + 256 * k, err, err_len) != 0)+ return -1;+ /* the stretches of speech: the chunking's, the detector's, or all */+ size_t *found = NULL, whole[2] = {0, n};+ if (!regions) {+ regions = whole;+ n_regions = 1;+ if (s->vad) {+ float *probs = NULL;+ long np = janas_vad_probs(s->vad, pcm, n, &probs);+ struct janas_vad_params vp = {.threshold = 0.5f,+ .min_speech_ms = 250,+ .min_silence_ms = 100,+ .speech_pad_ms = 30};+ long ns =+ np < 0 ? -1+ : janas_vad_segments(s->vad, probs, np, n, &vp, &found);+ free(probs);+ if (ns < 0) {+ snprintf(err, err_len, "out of memory");+ return -1;+ }+ regions = found;+ n_regions = (size_t)ns;+ }+ }+ struct janas_diar d;+ int rc = janas_diarize(s->speaker, pcm, n, regions, n_regions, ref,+ (uint32_t)nk, 0, &d);+ free(found);+ if (rc != 0) {+ snprintf(err, err_len, "out of memory");+ return -1;+ }+ /* each word's speaker: the windows overlapping it, weighed by how much */+ uint32_t total = 0;+ for (uint32_t g = 0; g < s->r.n_segs; g++)+ total += s->r.segs[g].n_words;+ int *wsp = malloc(sizeof(int) * (total ? total : 1));+ double *vote = calloc(d.n_speakers ? d.n_speakers : 1, sizeof(double));+ char **label = calloc(d.n_speakers ? d.n_speakers : 1, sizeof(char *));+ s->turns = calloc(total ? total : 1, sizeof *s->turns);+ if (!wsp || !vote || !label || !s->turns)+ goto oom;+ uint32_t k = 0;+ for (uint32_t g = 0; g < s->r.n_segs; g++)+ for (uint32_t i = 0; i < s->r.segs[g].n_words; i++, k++) {+ const struct janas_whisper_word *wd = &s->r.segs[g].words[i];+ double a = wd->t0 * 16000, b = wd->t1 * 16000;+ memset(vote, 0, sizeof(double) * (d.n_speakers ? d.n_speakers : 1));+ int best = -1;+ double bv = 0, near = 1e300;+ int nearest = d.n_win ? d.speaker[0] : 0;+ for (uint32_t j = 0; j < d.n_win; j++) {+ double ws = (double)d.win[2 * j], we = (double)d.win[2 * j + 1];+ double o = (b < we ? b : we) - (a > ws ? a : ws);+ if (o > 0)+ vote[d.speaker[j]] += o;+ double c = (ws + we) / 2 - (a + b) / 2;+ if (c < 0)+ c = -c;+ if (c < near) {+ near = c;+ nearest = d.speaker[j];+ }+ }+ for (uint32_t q = 0; q < d.n_speakers; q++)+ if (vote[q] > bv) {+ bv = vote[q];+ best = (int)q;+ }+ wsp[k] = best >= 0 ? best : nearest;+ }+ /* the turns */+ uint32_t nt = 0, unknown = 0;+ double last_end = -1e300;+ int cur = -1;+ k = 0;+ for (uint32_t g = 0; g < s->r.n_segs; g++)+ for (uint32_t i = 0; i < s->r.segs[g].n_words; i++, k++) {+ const struct janas_whisper_word *wd = &s->r.segs[g].words[i];+ /* a new turn: another speaker, a pause of more than 2 s, or a+ segment's start once the turn is longer than 20 s */+ if (wsp[k] != cur || wd->t0 - last_end > 2.0 ||+ (i == 0 && nt && wd->t0 - s->turns[nt - 1].t0 > 20.0)) {+ cur = wsp[k];+ struct janas_turn *t = &s->turns[nt++];+ t->t0 = wd->t0;+ if (!label[cur]) {+ char name[16];+ if (d.n_speakers && d.known[cur] >= 0 && p->speaker_names &&+ p->speaker_names[d.known[cur]]) {+ label[cur] = strdup(p->speaker_names[d.known[cur]]);+ } else {+ if (unknown < 26)+ snprintf(name, sizeof name, "%c", 'A' + unknown);+ else+ snprintf(name, sizeof name, "S%u", unknown + 1);+ unknown++;+ label[cur] = strdup(name);+ }+ if (!label[cur])+ goto oom;+ }+ t->speaker = strdup(label[cur]);+ t->text = strdup("");+ if (!t->speaker || !t->text)+ goto oom;+ }+ struct janas_turn *t = &s->turns[nt - 1];+ size_t a = strlen(t->text), b = strlen(wd->text);+ char *x = realloc(t->text, a + b + 1);+ if (!x)+ goto oom;+ memcpy(x + a, wd->text, b + 1);+ t->text = x;+ t->t1 = wd->t1;+ last_end = wd->t1;+ }+ s->n_turns = nt;+ for (uint32_t i = 0; i < nt; i++) { /* without the leading space */+ char *t = s->turns[i].text, *b = t;+ while (*b == ' ')+ b++;+ memmove(t, b, strlen(b) + 1);+ }+ for (uint32_t q = 0; q < d.n_speakers; q++)+ free(label[q]);+ free(label);+ free(vote);+ free(wsp);+ janas_diar_free(&d);+ return 0;+oom:+ s->n_turns = total;+ janas_speech_turns_free(s);+ if (label)+ for (uint32_t q = 0; q < d.n_speakers; q++)+ free(label[q]);+ free(label);+ free(vote);+ free(wsp);+ janas_diar_free(&d);+ snprintf(err, err_len, "out of memory");+ return -1;+}++int32_t janas_llm_speech_turns(const janas_llm_speech *s)+{+ return s ? (int32_t)s->n_turns : 0;+}++int32_t janas_llm_speech_turn(const janas_llm_speech *s, int32_t i,+ double *start, double *end, char *speaker,+ int32_t speaker_cap, int32_t *speaker_len,+ char *buf, int32_t cap, int32_t *len)+{+ if (!s || i < 0 || (uint32_t)i >= s->n_turns)+ return janas_api_fail(JANAS_LLM_EINVAL, "no such turn");+ const struct janas_turn *t = &s->turns[i];+ if (start)+ *start = t->t0;+ if (end)+ *end = t->t1;+ if (speaker || speaker_len) {+ int32_t rc = janas_api_copy_out(t->speaker, strlen(t->speaker), speaker,+ speaker_cap, speaker_len);+ if (rc != JANAS_LLM_OK)+ return rc;+ }+ if (!buf && !len)+ return JANAS_LLM_OK;+ return janas_api_copy_out(t->text, strlen(t->text), buf, cap, len);+}diff --git a/src/llm/arch.c b/src/llm/arch.cindex 3709eef..d731f8d 100644--- a/src/llm/arch.c+++ b/src/llm/arch.c@@ -39,6 +39,11 @@ static const struct janas_arch archs[] = {.shared_expert = 1,.vmap_mod = 1,.hc = 1},+ /* NVIDIA's Nemotron-H with LatentMoE (Nemotron 3.5 Bolt) */+ {.name = "nemotron_h_moe",+ .rec = JANAS_REC_MAMBA2,+ .no_qk_norm = 1,+ .nh = 1},/* its scores are not scaled down, and an eight-bit key moves them: ateight bits llama.cpp agrees with its own f16 cache at 169 of 200 mostlikely tokens */diff --git a/src/llm/arch.h b/src/llm/arch.hindex 957e665..fb21555 100644--- a/src/llm/arch.h+++ b/src/llm/arch.h@@ -12,9 +12,10 @@/* The recurrent layers, if any: Gated DeltaNet in two layouts. */enum janas_rec {- JANAS_REC_NONE, /* attention in every layer */- JANAS_REC_NEXT, /* qwen3next: q|k|v|z and b|a from two fused matrices */- JANAS_REC_Q35 /* qwen35: q|k|v, z, alpha, beta from four */+ JANAS_REC_NONE, /* attention in every layer */+ JANAS_REC_NEXT, /* qwen3next: q|k|v|z and b|a from two fused matrices */+ JANAS_REC_Q35, /* qwen35: q|k|v, z, alpha, beta from four */+ JANAS_REC_MAMBA2 /* nemotron_h_moe: Mamba-2 (model_mamba.c) */};struct janas_arch {@@ -46,6 +47,12 @@ struct janas_arch {at the layers the file names, the DeltaNet's output gated by asigmoid instead of SiLU, and the attention's sparse selection */int hc;+ /* Nemotron-H (nemotron_h_moe, model_nh.c): each layer one part, its+ norm and residual of its own - a Mamba-2 mixer, an attention without+ RoPE or a mixture - the mixture's experts in a latent space (down+ from d_model before them, up after), relu^2 without a gate, routed+ by sigmoids with a bias for the choice */+ int nh;};/* The architecture called name, or NULL if Janas does not know it. */diff --git a/src/llm/attention.c b/src/llm/attention.cindex 9539721..e861c6b 100644--- a/src/llm/attention.c+++ b/src/llm/attention.c@@ -16,8 +16,10 @@#include <immintrin.h>#endif+#include "common/sysinfo.h"#include "jmath.h"#include "quant.h"+#include "tuner.h"#include "vmath.h"#define ATT_CHUNK 256 /* positions per work item */@@ -39,21 +41,20 @@ struct janas_attn {int16_t *qi; /* q to 16 bits: MAX_BLOCK x n_head x hd */float *qs; /* its scale, one per token and head */int n_threads;- int scores; /* JANAS_ATTN_FLOAT or JANAS_ATTN_INT16, resolved */- int scores_auto; /* 1: the engine chose it */- float scale; /* of the scores: 1 / sqrt(head_dim) unless set */- int kv16; /* keys and values of sixteen bits, not eight */- int ring; /* position p in row p mod n_ctx (with a window) */- uint32_t window; /* a token sees the last window positions; 0: all */- atomic_uint *done; /* chunks finished, per (KV head, subgroup) */- /* the engine's choice (scores_auto): the two ways measured on the- calls themselves, a token's and a block's apart, every TUNE_BAND- positions (attn_tune_*) */- struct {- uint32_t band, calls;- double t[2]; /* seconds over the calls each way: float, int16 */- int pick;- } tune[2];+ int scores; /* JANAS_ATTN_FLOAT or JANAS_ATTN_INT16, resolved */+ int scores_auto; /* 1: the engine chose it */+ float scale; /* of the scores: 1 / sqrt(head_dim) unless set */+ int kv16; /* keys and values of sixteen bits, not eight */+ int ring; /* position p in row p mod n_ctx (with a window) */+ uint32_t window; /* a token sees the last window positions; 0: all */+ const uint32_t *last; /* the next call's last position a token sees+ (janas_attn_set_last); NULL: its own */+ atomic_uint *done; /* chunks finished, per (KV head, subgroup) */+ /* the engine's choice (scores_auto): the sixteen-bit query from this+ position on, a token's calls [0] and a block's [1] (UINT32_MAX:+ never), measured once before the first call (attn_calibrate) */+ uint32_t from16[2];+ int calibrated;};struct attn_job {@@ -65,11 +66,19 @@ struct attn_job {const float *ks, *vs; /* one scale per position and KV head */float *out;uint32_t n, pos0, n_chunks, split;- uint32_t c0; /* the first chunk any token of the call sees */- int fuse; /* 1: the last chunk of a subgroup merges it */- atomic_uint next; /* next item to claim */+ uint32_t c0; /* the first chunk any token of the call sees */+ const uint32_t *last; /* token j sees up to last[j]; NULL: pos0 + j */+ int fuse; /* 1: the last chunk of a subgroup merges it */+ atomic_uint next; /* next item to claim */};+/* The last position token j of a call sees: its own, or beyond it in a+ span that attends both ways (a picture's tokens) */+static uint32_t last_seen(const struct attn_job *a, uint32_t j)+{+ return a->last ? a->last[j] : a->pos0 + j;+}+/* The partial of chunk c for token j and query head g of KV head kvh:largest score, sum of weights, then head_dim weighted values. */static float *att_partial(const struct janas_attn *A, uint32_t kvh, uint32_t c,@@ -624,8 +633,8 @@ static void attn_item(struct attn_job *a, uint32_t it, float *sc, int avx,uint32_t sub = it % a->split, c = a->c0 + it / a->split % span;uint32_t kvh = it / a->split / span, g0 = sub * gs;uint32_t t0 = c * ATT_CHUNK, t1 = t0 + ATT_CHUNK;- if (t1 > a->pos0 + a->n)- t1 = a->pos0 + a->n;+ if (t1 > last_seen(a, a->n - 1) + 1)+ t1 = last_seen(a, a->n - 1) + 1;uint32_t nt = t1 - t0;/* sixteen-bit weights for the values too (att_wquant_ref) */const int fastv = A->scores == JANAS_ATTN_INT16 && !A->kv16;@@ -637,9 +646,13 @@ static void attn_item(struct attn_job *a, uint32_t it, float *sc, int avx,const int8_t *V = a->v + row0;const float *ks = a->ks + r0;const float *vs = a->vs + r0;- /* token j sees positions up to pos0 + j: tokens before jlo see- nothing of this chunk */+ /* token j sees positions up to pos0 + j (or last[j], never less and+ never less than the token before's): tokens before jlo see nothing+ of this chunk */uint32_t jlo = t0 > a->pos0 ? t0 - a->pos0 : 0;+ if (a->last)+ for (jlo = 0; jlo < a->n && a->last[jlo] < t0; jlo++)+ ;for (uint32_t j = 0; j < jlo && j < a->n; j++)for (uint32_t g = g0; g < g0 + gs; g++) {float *pm = att_partial(A, kvh, c, j, g);@@ -704,7 +717,7 @@ static void attn_item(struct attn_job *a, uint32_t it, float *sc, int avx,/* per slot: largest visible score, weights and their sum */for (uint32_t sl = 0; sl < ns; sl++) {uint32_t j = jlo + sl / gs;- uint32_t vis = a->pos0 + j + 1 - t0; /* visible positions */+ uint32_t vis = last_seen(a, j) + 1 - t0; /* visible positions */float *pm = att_partial(A, kvh, c, j, g0 + sl % gs);if (A->window && a->pos0 + j + 1 > A->window) {/* a sliding window: the positions before its start weigh@@ -844,7 +857,7 @@ static void merge_one(struct attn_job *a, uint32_t j, uint32_t h)uint32_t hd = A->hd, qd = A->n_head * hd;uint32_t group = A->n_head / A->n_kv;uint32_t kvh = h / group, g = h % group;- uint32_t nc = (a->pos0 + j + ATT_CHUNK) / ATT_CHUNK;+ uint32_t nc = (last_seen(a, j) + ATT_CHUNK) / ATT_CHUNK;float mx = -INFINITY, sum = 0.0f;for (uint32_t c = a->c0; c < nc; c++) {float v = att_partial(A, kvh, c, j, g)[0];@@ -994,8 +1007,6 @@ struct janas_attn *janas_attn_create(uint32_t n_head, uint32_t n_head_kv,sizeof(int16_t));A->qs = malloc((size_t)JANAS_ATTN_MAX_BLOCK * n_head * sizeof(float));}- for (int c = 0; c < 2; c++)- A->tune[c].band = UINT32_MAX;if (!A->part || !A->scr || !A->done || (i16 && (!A->qi || !A->qs))) {janas_attn_destroy(A);return NULL;@@ -1008,6 +1019,11 @@ void janas_attn_set_scale(struct janas_attn *a, float scale)a->scale = scale;}+void janas_attn_set_last(struct janas_attn *a, const uint32_t *last)+{+ a->last = last;+}+void janas_attn_set_window(struct janas_attn *a, uint32_t window){a->window = window;@@ -1056,20 +1072,21 @@ void janas_attn_destroy(struct janas_attn *A)}/*- * The engine's choice of the scores' way, made on this machine: in float- * over the query as it is, or over the query at sixteen bits (an integer- * sum, a relative 3e-5 of error where the eight-bit keys carry 4e-3: the- * most likely token the same at 2,961 of 3,200 positions against llama.cpp- * as at 2,963 in float, Qwen3 0.6B and 4B, Qwen3.5 0.8B and 2B, two texts- * each, 26 Oct 2026). Which is faster depends on the processor (AVX-VNNI- * or not, its cores) and grows with the position, so every TUNE_BAND- * positions TUNE_TRY calls each way, a token's calls and a block's apart- * (the first way of a band alternating), and the faster for the rest of- * the band. JANAS_ATTN=float or int16 fixes it.+ * The attention's scores: over the query as it is, or over the query at+ * sixteen bits (an integer sum, a relative 3e-5 of error where the eight-bit+ * keys carry 4e-3: the most likely token the same at 2,961 of 3,200+ * positions against llama.cpp as at 2,963 in float, Qwen3 0.6B and 4B,+ * Qwen3.5 0.8B and 2B, two texts each, 26 Oct 2026). Which is faster+ * depends on the processor (AVX-VNNI or not, its cores) and grows with the+ * position. Measured on the calls themselves (until 28 Oct 2026) the way+ * changed between passes, and so did the results; now it is measured once,+ * before the first call, on made-up keys of this attention's geometry at+ * contexts of 1,024 positions and up (a token's calls to 65,536, a block's+ * of 64 to 16,384): the sixteen-bit way from the shortest at which it wins+ * there and at every longer one. The result is kept in ~/.cache/janas+ * (attn.txt) by processor, threads and geometry, so every session uses the+ * same. JANAS_ATTN=float or int16 fixes it.*/-#define TUNE_BAND (16 * ATT_CHUNK)-#define TUNE_TRY 8-static double attn_now(void){struct timespec ts;@@ -1077,30 +1094,140 @@ static double attn_now(void)return (double)ts.tv_sec + ts.tv_nsec * 1e-9;}-/* this call's way (JANAS_ATTN_FLOAT or _INT16) */-static int attn_tune_pick(struct janas_attn *A, int c, uint32_t pos0)+static void run_way(struct janas_attn *A, struct janas_pool *pool,+ const float *q, const int8_t *k, const float *ks,+ const int8_t *v, const float *vs, uint32_t n, uint32_t pos0,+ float *out);++static void calib_key(const struct janas_attn *A, char *key, size_t len)+{+ char cpu[128];+ janas_cpu_name(cpu, sizeof(cpu));+ for (char *c = cpu; *c; c++)+ if (*c == ' ' || *c == '\t')+ *c = '_';+ snprintf(key, len, "%s|%d|%u/%u/%u|%u|%u", cpu, A->n_threads, A->n_head,+ A->n_kv, A->hd, A->window, A->n_ctx);+}++static int calib_load(struct janas_attn *A, const char *key)+{+ char path[1024], line[512];+ if (janas_cache_dir(path, sizeof(path), 0) != 0)+ return -1;+ strcat(path, "/attn.txt");+ FILE *f = fopen(path, "r");+ if (!f)+ return -1;+ int found = -1;+ while (fgets(line, sizeof(line), f)) {+ char k[400];+ unsigned a, b;+ if (sscanf(line, "%399s %u %u", k, &a, &b) == 3 && !strcmp(k, key)) {+ A->from16[0] = a;+ A->from16[1] = b;+ found = 0;+ }+ }+ fclose(f);+ return found;+}++static void calib_save(const struct janas_attn *A, const char *key){- uint32_t band = pos0 / TUNE_BAND;- if (band != A->tune[c].band) {- A->tune[c].band = band;- A->tune[c].calls = 0;- A->tune[c].t[0] = A->tune[c].t[1] = 0.0;+ char path[1024];+ if (janas_cache_dir(path, sizeof(path), 1) != 0)+ return;+ strcat(path, "/attn.txt");+ FILE *f = fopen(path, "a");+ if (!f)+ return;+ fprintf(f, "%s %u %u\n", key, A->from16[0], A->from16[1]);+ fclose(f);+}++/* seconds of the faster of three calls of this way */+static double time_way(struct janas_attn *A, struct janas_pool *pool, int way,+ const float *q, const int8_t *k, const float *ks,+ const int8_t *v, const float *vs, uint32_t n,+ uint32_t pos0, float *out)+{+ double best = 1e30;+ A->scores = way;+ for (int r = 0; r < 3; r++) {+ double t0 = attn_now();+ run_way(A, pool, q, k, ks, v, vs, n, pos0, out);+ double t = attn_now() - t0;+ if (t < best)+ best = t;}- uint32_t k = A->tune[c].calls;- if (k >= 2 * TUNE_TRY)- return A->tune[c].pick;- int first = (int)(band & 1u), way = k < TUNE_TRY ? first : !first;- return way ? JANAS_ATTN_INT16 : JANAS_ATTN_FLOAT;+ return best;}-static void attn_tune_seen(struct janas_attn *A, int c, int mode, double s)+static void attn_calibrate(struct janas_attn *A, struct janas_pool *pool){- if (A->tune[c].calls >= 2 * TUNE_TRY)+ char key[512];+ A->calibrated = 1;+ A->from16[0] = A->from16[1] = UINT32_MAX;+ calib_key(A, key, sizeof(key));+ if (calib_load(A, key) == 0)return;- A->tune[c].t[mode == JANAS_ATTN_INT16] += s;- if (++A->tune[c].calls == 2 * TUNE_TRY)- A->tune[c].pick = A->tune[c].t[1] < A->tune[c].t[0] ? JANAS_ATTN_INT16- : JANAS_ATTN_FLOAT;+ uint32_t nb = JANAS_ATTN_MAX_BLOCK < 64 ? JANAS_ATTN_MAX_BLOCK : 64;+ /* made-up rows only as far as measured: the layout's stride shortened+ for the while (a ring keeps its own) */+ uint32_t keep_ctx = A->n_ctx, keep_chunks = A->chunks;+ if (!A->ring && A->n_ctx > 65536 + nb) {+ A->n_ctx = 65536 + nb;+ A->chunks = (A->n_ctx + ATT_CHUNK - 1) / ATT_CHUNK;+ }+ uint32_t top = A->n_ctx;+ size_t cells = (size_t)A->n_kv * A->n_ctx;+ int8_t *k = malloc(cells * A->hd), *v = malloc(cells * A->hd);+ float *ks = malloc(cells * sizeof(float)),+ *vs = malloc(cells * sizeof(float));+ size_t qn = (size_t)nb * A->n_head * A->hd;+ float *q = malloc(qn * sizeof(float)), *out = malloc(qn * sizeof(float));+ if (!k || !v || !ks || !vs || !q || !out)+ goto done;+ uint32_t r = 12345;+ for (size_t i = 0; i < cells * A->hd; i++) {+ r = r * 1103515245u + 12345u;+ k[i] = (int8_t)((r >> 16) % 255 - 127);+ v[i] = (int8_t)((r >> 8) % 255 - 127);+ }+ for (size_t i = 0; i < cells; i++)+ ks[i] = vs[i] = 0.01f;+ for (size_t i = 0; i < qn; i++) {+ r = r * 1103515245u + 12345u;+ q[i] = (float)((int)(r >> 16) % 2001 - 1000) / 500.0f;+ }+ for (int c = 0; c < 2; c++) {+ uint32_t n = c ? nb : 1, limit = c ? 16384 : 65536;+ uint32_t from = UINT32_MAX;+ for (uint32_t p = 1024; p + n <= top && p <= limit; p *= 2) {+ double tf =+ time_way(A, pool, JANAS_ATTN_FLOAT, q, k, ks, v, vs, n, p, out);+ double ti =+ time_way(A, pool, JANAS_ATTN_INT16, q, k, ks, v, vs, n, p, out);+ if (ti < tf) {+ if (from == UINT32_MAX)+ from = p;+ } else {+ from = UINT32_MAX; /* it must win at every longer one */+ }+ }+ A->from16[c] = from;+ }+ calib_save(A, key);+done:+ A->n_ctx = keep_ctx;+ A->chunks = keep_chunks;+ free(k);+ free(v);+ free(ks);+ free(vs);+ free(q);+ free(out);}void janas_attn_run(struct janas_attn *A, struct janas_pool *pool,@@ -1108,15 +1235,27 @@ void janas_attn_run(struct janas_attn *A, struct janas_pool *pool,const int8_t *v, const float *vs, uint32_t n, uint32_t pos0,float *out){- /* the engine's choice: measured (attn_tune_pick) */- int tc = n > 1, tuned = A->scores_auto && A->qi && !A->kv16;- double t0 = 0.0;- if (tuned) {- A->scores = attn_tune_pick(A, tc, pos0);- t0 = attn_now();+ /* the engine's choice: measured once (attn_calibrate) */+ if (A->scores_auto && A->qi && !A->kv16) {+ if (!A->calibrated)+ attn_calibrate(A, pool);+ A->scores =+ pos0 >= A->from16[n > 1] ? JANAS_ATTN_INT16 : JANAS_ATTN_FLOAT;}+ run_way(A, pool, q, k, ks, v, vs, n, pos0, out);+}++static void run_way(struct janas_attn *A, struct janas_pool *pool,+ const float *q, const int8_t *k, const float *ks,+ const int8_t *v, const float *vs, uint32_t n, uint32_t pos0,+ float *out)+{+ /* the positions seen: up to the last token's, or past it */+ const uint32_t *last = A->last;+ A->last = NULL;+ uint32_t end = last ? last[n - 1] + 1 : pos0 + n;/* enough items for every thread: split the query heads of a group */- uint32_t n_chunks = (pos0 + n + ATT_CHUNK - 1) / ATT_CHUNK, split = 1;+ uint32_t n_chunks = (end + ATT_CHUNK - 1) / ATT_CHUNK, split = 1;uint32_t group = A->n_head / A->n_kv;/* with a window, the chunks wholly before the first token's are neverread: at a long context most of them */@@ -1143,6 +1282,7 @@ void janas_attn_run(struct janas_attn *A, struct janas_pool *pool,.pos0 = pos0,.n_chunks = n_chunks,.c0 = c0,+ .last = last,.split = split};job.qi = A->qi;job.qs = A->qs;@@ -1172,6 +1312,4 @@ void janas_attn_run(struct janas_attn *A, struct janas_pool *pool,janas_pool_run(pool, attn_worker, &job);if (!job.fuse)janas_pool_run(pool, merge_worker, &job);- if (tuned)- attn_tune_seen(A, tc, A->scores, attn_now() - t0);}diff --git a/src/llm/attention.h b/src/llm/attention.hindex a5a0a1f..9e129bc 100644--- a/src/llm/attention.h+++ b/src/llm/attention.h@@ -1,7 +1,8 @@/* SPDX-License-Identifier: GPL-3.0-or-laterCopyright (C) 2026 Maurizio Cammalleri *//*- * attention.h - causal grouped-query attention over an eight-bit KV cache.+ * attention.h - causal grouped-query attention over an eight-bit KV cache+ * (spans that attend both ways set call by call).** The keys and values of one layer are laid out n_head_kv x n_ctx x* head_dim as signed bytes, with one float scale per position and KV head@@ -85,6 +86,16 @@ void janas_attn_set_ring(struct janas_attn *a, int on);whose keys and values are written before any of them is attended. */uint32_t janas_attn_ring_positions(uint32_t window, uint32_t n);+/*+ * For the next call only: token j sees the positions up to last[j] instead+ * of up to its own (pos0 + j), last[j] >= pos0 + j and never less than+ * last[j - 1] - the tokens of a span that attends both ways, a picture's+ * in Gemma 4 12B and 26B; the keys and values up to the largest must be in+ * the cache. A window still starts from the token's own position. NULL:+ * causal, as every call without it.+ */+void janas_attn_set_last(struct janas_attn *a, const uint32_t *last);+/* What the scores are actually computed with: FLOAT or INT16. */int janas_attn_scores_mode(const struct janas_attn *a);/* 1 when the engine chose it rather than the caller. */diff --git a/src/llm/chat.h b/src/llm/chat.hindex a767cfc..2ee6724 100644--- a/src/llm/chat.h+++ b/src/llm/chat.h@@ -33,8 +33,22 @@ enum chat_format {FORMAT_LLAMA3,};+/* a picture read by the vision encoder (api_image.c): its embeddings and+ its grid of tokens; and where one is in the prompt being built */+struct cpic {+ float *embd;+ uint32_t nx, ny;+};+struct ppic {+ uint32_t off; /* of its first token in ids */+ const struct cpic *pic;+};+struct janas_llm {struct janas_llm_model *m;+ char *path; /* the model's file */+ struct janas_vision *vis; /* its vision encoder, opened at the first+ picture (api_image.c) */struct janas_tokenizer *tok;int format;int32_t im_start, im_end;@@ -115,6 +129,11 @@ struct janas_llm_chat {/* the message being built: ids, and plain text not yet tokenized */int32_t *ids;size_t n_ids, cap_ids;+ /* pictures queued for the next user message, and those in ids */+ struct cpic *pics;+ size_t n_pics;+ struct ppic *idpics;+ size_t n_idpics;char *text;size_t n_text, cap_text;/* counters */@@ -169,6 +188,8 @@ struct janas_llm_chat {size_t n;struct janas_tool_call *calls; /* name and args owned here */size_t n_calls;+ struct cpic *pics; /* a user message's pictures, owned here */+ size_t n_pics;} *msgs;size_t n_msgs, cap_msgs;@@ -260,6 +281,16 @@ int janas_api_flush_text(janas_llm_chat *c);int janas_api_add_special(janas_llm_chat *c, int32_t id);/* The newline after a turn's end where the format has one. */int janas_api_end_turn_nl(janas_llm_chat *c);+/* api_image.c */+int janas_api_add_user(janas_llm_chat *c, const char *text, size_t n,+ const struct cpic *pics, size_t n_pics);+uint32_t janas_api_media_first(const janas_llm_chat *c);+/* The prefill block a model at path needs for its pictures: 0, or (a+ vision encoder beside it whose pictures attend both ways) their most+ tokens rounded up to 64 */+uint32_t janas_api_picture_block(const char *path);+int janas_api_media_register(janas_llm_chat *c, uint32_t base, uint32_t from);+void janas_api_pics_free(struct cpic *p, size_t n);int janas_api_add_message(janas_llm_chat *c, const char *role, const char *text,size_t n);int janas_api_add_system(janas_llm_chat *c, const char *sys, size_t n);diff --git a/src/llm/diarize.c b/src/llm/diarize.cnew file mode 100644index 0000000..fb6520f--- /dev/null+++ b/src/llm/diarize.c@@ -0,0 +1,524 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * diarize.c - who speaks when: the stretches of speech cut into windows of+ * 1.5 s every 0.75 s (a shorter stretch one window), each embedded+ * (speaker.c) and normalized, grouped by WeSpeaker's spectral clustering:+ * the cosine affinities pruned to each window's nearest, the Laplacian's+ * smallest eigenvalues' largest gap for the number of speakers, k-means on+ * its eigenvectors. Known speakers' embeddings take the groups nearest+ * them, one each, where their cosine is above 0.5. Groups numbered in the+ * order they first speak.+ */+#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "speaker.h"++#define DIM 256+#define WIN_FRAMES 150+#define STEP_FRAMES 75++void janas_diar_free(struct janas_diar *d)+{+ free(d->win);+ free(d->speaker);+ free(d->known);+ memset(d, 0, sizeof *d);+}++static void normalize(float *v)+{+ double s = 0;+ for (int i = 0; i < DIM; i++)+ s += (double)v[i] * v[i];+ float k = s > 0 ? (float)(1 / sqrt(s)) : 0;+ for (int i = 0; i < DIM; i++)+ v[i] *= k;+}++static float cosine(const float *a, const float *b)+{+ double s = 0, na = 0, nb = 0;+ for (int i = 0; i < DIM; i++) {+ s += (double)a[i] * b[i];+ na += (double)a[i] * a[i];+ nb += (double)b[i] * b[i];+ }+ return na > 0 && nb > 0 ? (float)(s / sqrt(na * nb)) : 0;+}++/* the eigenvalues (ascending, into d) and eigenvectors (columns of a, n x+ n row-major) of a symmetric matrix a: Householder's reduction to a+ tridiagonal and the implicit QL iterations (EISPACK's tred2 and tql2) */+static int eigh(double *a, int n, double *d)+{+ double *e = calloc((size_t)n, sizeof *e);+ if (!e)+ return -1;+#define A(i, j) a[(size_t)(i) * n + (j)]+ for (int i = n - 1; i > 0; i--) {+ int l = i - 1;+ double h = 0, scale = 0;+ if (l > 0) {+ for (int k = 0; k <= l; k++)+ scale += fabs(A(i, k));+ if (scale == 0) {+ e[i] = A(i, l);+ } else {+ for (int k = 0; k <= l; k++) {+ A(i, k) /= scale;+ h += A(i, k) * A(i, k);+ }+ double f = A(i, l);+ double g = f >= 0 ? -sqrt(h) : sqrt(h);+ e[i] = scale * g;+ h -= f * g;+ A(i, l) = f - g;+ f = 0;+ for (int j = 0; j <= l; j++) {+ A(j, i) = A(i, j) / h;+ g = 0;+ for (int k = 0; k <= j; k++)+ g += A(j, k) * A(i, k);+ for (int k = j + 1; k <= l; k++)+ g += A(k, j) * A(i, k);+ e[j] = g / h;+ f += e[j] * A(i, j);+ }+ double hh = f / (h + h);+ for (int j = 0; j <= l; j++) {+ f = A(i, j);+ e[j] = g = e[j] - hh * f;+ for (int k = 0; k <= j; k++)+ A(j, k) -= f * e[k] + g * A(i, k);+ }+ }+ } else {+ e[i] = A(i, l);+ }+ d[i] = h;+ }+ d[0] = 0;+ e[0] = 0;+ for (int i = 0; i < n; i++) {+ int l = i - 1;+ if (d[i] != 0) {+ for (int j = 0; j <= l; j++) {+ double g = 0;+ for (int k = 0; k <= l; k++)+ g += A(i, k) * A(k, j);+ for (int k = 0; k <= l; k++)+ A(k, j) -= g * A(k, i);+ }+ }+ d[i] = A(i, i);+ A(i, i) = 1;+ for (int j = 0; j <= l; j++)+ A(j, i) = A(i, j) = 0;+ }+ /* tql2 */+ for (int i = 1; i < n; i++)+ e[i - 1] = e[i];+ e[n - 1] = 0;+ for (int l = 0; l < n; l++) {+ int iter = 0, m;+ do {+ for (m = l; m < n - 1; m++) {+ double dd = fabs(d[m]) + fabs(d[m + 1]);+ if (fabs(e[m]) <= 1e-15 * dd)+ break;+ }+ if (m != l) {+ if (iter++ == 60) {+ free(e);+ return -1;+ }+ double g = (d[l + 1] - d[l]) / (2 * e[l]);+ double r = hypot(g, 1);+ g = d[m] - d[l] + e[l] / (g + (g >= 0 ? fabs(r) : -fabs(r)));+ double sn = 1, c = 1, pp = 0;+ int i;+ for (i = m - 1; i >= l; i--) {+ double f = sn * e[i], b = c * e[i];+ e[i + 1] = r = hypot(f, g);+ if (r == 0) {+ d[i + 1] -= pp;+ e[m] = 0;+ break;+ }+ sn = f / r;+ c = g / r;+ g = d[i + 1] - pp;+ r = (d[i] - g) * sn + 2 * c * b;+ pp = sn * r;+ d[i + 1] = g + pp;+ g = c * r - b;+ for (int k = 0; k < n; k++) {+ f = A(k, i + 1);+ A(k, i + 1) = sn * A(k, i) + c * f;+ A(k, i) = c * A(k, i) - sn * f;+ }+ }+ if (r == 0 && i >= l)+ continue;+ d[l] -= pp;+ e[l] = g;+ e[m] = 0;+ }+ } while (m != l);+ }+ /* ascending, the vectors with them */+ for (int i = 0; i < n - 1; i++) {+ int k = i;+ for (int j = i + 1; j < n; j++)+ if (d[j] < d[k])+ k = j;+ if (k != i) {+ double t = d[i];+ d[i] = d[k];+ d[k] = t;+ for (int r = 0; r < n; r++) {+ t = A(r, i);+ A(r, i) = A(r, k);+ A(r, k) = t;+ }+ }+ }+#undef A+ free(e);+ return 0;+}++static uint64_t rng_next(uint64_t *s)+{+ *s ^= *s << 13;+ *s ^= *s >> 7;+ *s ^= *s << 17;+ return *s;+}++/* k-means of n points of k dimensions into k groups: k-means++ starts, ten+ of them, the one of least inertia kept */+static void kmeans(const double *x, int n, int k, uint32_t *label)+{+ double *c = malloc(sizeof(double) * (size_t)k * k);+ double *dist2 = malloc(sizeof(double) * (size_t)n);+ uint32_t *lab = malloc(sizeof(uint32_t) * (size_t)n);+ if (!c || !dist2 || !lab) {+ for (int i = 0; i < n; i++)+ label[i] = 0;+ free(c);+ free(dist2);+ free(lab);+ return;+ }+ uint64_t seed = 0x9e3779b97f4a7c15ull;+ double best = INFINITY;+ for (int init = 0; init < 10; init++) {+ /* k-means++ */+ int first = (int)(rng_next(&seed) % (uint64_t)n);+ memcpy(c, x + (size_t)first * k, sizeof(double) * (size_t)k);+ for (int j = 1; j < k; j++) {+ double tot = 0;+ for (int i = 0; i < n; i++) {+ double m = INFINITY;+ for (int q = 0; q < j; q++) {+ double s2 = 0;+ for (int t = 0; t < k; t++) {+ double dd = x[(size_t)i * k + t] - c[(size_t)q * k + t];+ s2 += dd * dd;+ }+ m = s2 < m ? s2 : m;+ }+ dist2[i] = m;+ tot += m;+ }+ double r =+ (double)(rng_next(&seed) >> 11) / 9007199254740992.0 * tot;+ int pick = n - 1;+ for (int i = 0; i < n; i++) {+ r -= dist2[i];+ if (r <= 0) {+ pick = i;+ break;+ }+ }+ memcpy(c + (size_t)j * k, x + (size_t)pick * k,+ sizeof(double) * (size_t)k);+ }+ /* Lloyd's iterations */+ double inertia = 0;+ for (int it = 0; it < 300; it++) {+ int moved = 0;+ inertia = 0;+ for (int i = 0; i < n; i++) {+ double m = INFINITY;+ uint32_t bq = 0;+ for (int q = 0; q < k; q++) {+ double s2 = 0;+ for (int t = 0; t < k; t++) {+ double dd = x[(size_t)i * k + t] - c[(size_t)q * k + t];+ s2 += dd * dd;+ }+ if (s2 < m) {+ m = s2;+ bq = (uint32_t)q;+ }+ }+ if (it == 0 || lab[i] != bq)+ moved = 1;+ lab[i] = bq;+ inertia += m;+ }+ if (!moved)+ break;+ for (int q = 0; q < k; q++) {+ int cnt = 0;+ double *cq = c + (size_t)q * k;+ memset(cq, 0, sizeof(double) * (size_t)k);+ for (int i = 0; i < n; i++)+ if (lab[i] == (uint32_t)q) {+ cnt++;+ for (int t = 0; t < k; t++)+ cq[t] += x[(size_t)i * k + t];+ }+ for (int t = 0; t < k && cnt; t++)+ cq[t] /= cnt;+ }+ }+ if (inertia < best) {+ best = inertia;+ memcpy(label, lab, sizeof(uint32_t) * (size_t)n);+ }+ }+ free(c);+ free(dist2);+ free(lab);+}++/* WeSpeaker's spectral clustering of n normalized embeddings: 0, or -1+ when out of memory */+static int spectral(const float *emb, uint32_t nw, uint32_t max_spk,+ uint32_t *label)+{+ int n = (int)nw;+ if (n <= 2) {+ for (int i = 0; i < n; i++)+ label[i] = 0;+ return 0;+ }+ double *m = malloc(sizeof(double) * (size_t)n * n);+ double *row = malloc(sizeof(double) * (size_t)n);+ double *d = malloc(sizeof(double) * (size_t)n);+ int *idx = malloc(sizeof(int) * (size_t)n);+ if (!m || !row || !d || !idx) {+ free(m);+ free(row);+ free(d);+ free(idx);+ return -1;+ }+ int keep = n < 1000 ? (n - 10 > 2 ? 10 : n - 2) : n - (int)(0.99 * n);+ for (int i = 0; i < n; i++) {+ for (int j = 0; j < n; j++) {+ double s = 0;+ for (int t = 0; t < DIM; t++)+ s +=+ (double)emb[(size_t)i * DIM + t] * emb[(size_t)j * DIM + t];+ row[j] = 0.5 * (1 + s);+ idx[j] = j;+ }+ /* the keep largest to 1, the rest to 0 (a stable order: ties by+ index, as numpy's argsort) */+ for (int a = 0; a < keep; a++) {+ int b = a;+ for (int c = a + 1; c < n; c++)+ if (row[idx[c]] > row[idx[b]] ||+ (row[idx[c]] == row[idx[b]] && idx[c] > idx[b]))+ b = c;+ int t = idx[a];+ idx[a] = idx[b];+ idx[b] = t;+ }+ for (int j = 0; j < n; j++)+ m[(size_t)i * n + j] = 0;+ for (int a = 0; a < keep; a++)+ m[(size_t)i * n + idx[a]] = 1;+ }+ for (int i = 0; i < n; i++) /* symmetric, no self-loops: the Laplacian */+ for (int j = i; j < n; j++) {+ double v = 0.5 * (m[(size_t)i * n + j] + m[(size_t)j * n + i]);+ m[(size_t)i * n + j] = m[(size_t)j * n + i] = i == j ? 0 : -v;+ }+ for (int i = 0; i < n; i++) {+ double deg = 0;+ for (int j = 0; j < n; j++)+ deg -= m[(size_t)i * n + j];+ m[(size_t)i * n + i] = deg;+ }+ if (eigh(m, n, d) != 0) {+ free(m);+ free(row);+ free(d);+ free(idx);+ return -1;+ }+ int lim = (int)max_spk < n - 1 ? (int)max_spk : n - 1, k = 1;+ double gap = -1;+ for (int i = 0; i < lim; i++)+ if (d[i + 1] - d[i] > gap) {+ gap = d[i + 1] - d[i];+ k = i + 1;+ }+ double *x = malloc(sizeof(double) * (size_t)n * k);+ if (!x) {+ free(m);+ free(row);+ free(d);+ free(idx);+ return -1;+ }+ for (int i = 0; i < n; i++)+ for (int q = 0; q < k; q++)+ x[(size_t)i * k + q] = m[(size_t)i * n + q];+ kmeans(x, n, k, label);+ free(x);+ free(m);+ free(row);+ free(d);+ free(idx);+ return 0;+}++int janas_diarize(struct janas_speaker *s, const float *pcm, size_t n,+ const size_t *regions, size_t n_regions, const float *ref,+ uint32_t n_ref, uint32_t max_speakers, struct janas_diar *out)+{+ memset(out, 0, sizeof *out);+ uint32_t frames = 0;+ float *fb = janas_speaker_fbank(pcm, n, &frames);+ if (!fb)+ return -1;+ /* the windows, frames of the filter banks (10 ms each) */+ size_t cap = 64;+ uint32_t nw = 0;+ uint32_t(*w)[2] = malloc(sizeof *w * cap);+ float *emb = NULL;+ uint32_t *of = NULL;+ int rc = -1;+ if (!w)+ goto end;+ for (size_t r = 0; r < n_regions; r++) {+ uint32_t a = (uint32_t)(regions[2 * r] / 160);+ uint32_t b = (uint32_t)(regions[2 * r + 1] / 160);+ if (b > frames)+ b = frames;+ if (b <= a || b - a < 8)+ continue;+ for (uint32_t f = a;; f += STEP_FRAMES) {+ uint32_t e = f + WIN_FRAMES < b ? f + WIN_FRAMES : b;+ if (e - f < 8)+ break;+ if (nw == cap) {+ cap *= 2;+ uint32_t(*t)[2] = realloc(w, sizeof *w * cap);+ if (!t)+ goto end;+ w = t;+ }+ w[nw][0] = f;+ w[nw][1] = e;+ nw++;+ if (e == b)+ break;+ }+ }+ out->win = malloc(sizeof(size_t) * 2 * (nw ? nw : 1));+ out->speaker = malloc(sizeof(int) * (nw ? nw : 1));+ emb = malloc(sizeof(float) * DIM * (nw ? nw : 1));+ of = malloc(sizeof(uint32_t) * (nw ? nw : 1));+ if (!out->win || !out->speaker || !emb || !of)+ goto end;+ for (uint32_t i = 0; i < nw; i++) {+ out->win[2 * i] = (size_t)w[i][0] * 160;+ out->win[2 * i + 1] = (size_t)w[i][1] * 160 + 240;+ if (janas_speaker_embed(s, fb, w[i][0], w[i][1] - w[i][0],+ emb + (size_t)i * DIM) != 0)+ goto end;+ normalize(emb + (size_t)i * DIM);+ }+ out->n_win = nw;+ /* spectral clustering, as WeSpeaker's: the affinity of the cosine+ similarities kept to each window's nearest (10, or the top 1% past+ 1000 windows) and made symmetric, its Laplacian, as many groups as+ the largest gap among its smallest eigenvalues says, k-means on+ their eigenvectors */+ if (spectral(emb, nw, max_speakers ? max_speakers : 20, of) != 0)+ goto end;+ /* numbered in order of first appearance */+ int *label = malloc(sizeof(int) * (nw ? nw : 1));+ if (!label)+ goto end;+ for (uint32_t i = 0; i < nw; i++)+ label[i] = -1;+ uint32_t ns = 0;+ for (uint32_t i = 0; i < nw; i++) {+ if (label[of[i]] < 0)+ label[of[i]] = (int)ns++;+ out->speaker[i] = label[of[i]];+ }+ out->n_speakers = ns;+ /* the known speakers: each to the group nearest it */+ out->known = malloc(sizeof(int) * (ns ? ns : 1));+ float *gc = calloc((size_t)(ns ? ns : 1) * DIM, sizeof(float));+ if (!out->known || !gc) {+ free(label);+ free(gc);+ goto end;+ }+ for (uint32_t g = 0; g < ns; g++)+ out->known[g] = -1;+ for (uint32_t i = 0; i < nw; i++)+ for (int k = 0; k < DIM; k++)+ gc[(size_t)out->speaker[i] * DIM + k] += emb[(size_t)i * DIM + k];+ if (n_ref && ns) {+ char *ref_used = calloc(n_ref, 1);+ for (;;) {+ float best = 0.5f;+ int bg = -1, br = -1;+ for (uint32_t g = 0; g < ns; g++) {+ if (out->known[g] >= 0)+ continue;+ for (uint32_t r = 0; r < n_ref; r++) {+ if (ref_used && ref_used[r])+ continue;+ float c =+ cosine(gc + (size_t)g * DIM, ref + (size_t)r * DIM);+ if (c > best) {+ best = c;+ bg = (int)g;+ br = (int)r;+ }+ }+ }+ if (bg < 0 || !ref_used)+ break;+ out->known[bg] = br;+ ref_used[br] = 1;+ }+ free(ref_used);+ }+ free(gc);+ free(label);+ rc = 0;+end:+ if (rc != 0)+ janas_diar_free(out);+ free(fb);+ free(w);+ free(emb);+ free(of);+ return rc;+}diff --git a/src/llm/expert_cache.c b/src/llm/expert_cache.cindex 6ec1e83..3b72cde 100644--- a/src/llm/expert_cache.c+++ b/src/llm/expert_cache.c@@ -114,6 +114,7 @@ struct janas_expert_cache {int outstanding; /* begin called, finish not yet (caller thread only) */int stop;int batch_rc;+ int lost; /* a request found no slot for an expert: finish() fails it *//** Reads ahead (janas_expert_cache_prefetch), two sets: the engine reads@@ -478,9 +479,12 @@ static void layout(const struct janas_jns *j, uint32_t n_layer, uint32_t n_main,if (b > *m)*m = b;}- /* one size for all, the largest */+ /* one size for all, the largest (a file whose every slot is empty:+ none to hold) */uint64_t one = main > wide ? main : wide;- size_t n = (size_t)(budget / one);+ size_t n = one ? (size_t)(budget / one) : 0;+ if (!one)+ one = 1;*o = (struct layout){.slot = one, .n_slots = n < keys ? n : keys};if (wide <= main || main == 0)return;@@ -565,6 +569,10 @@ part_create(const struct janas_jns *j, uint32_t first, uint32_t n_layer,c->lhead = malloc(((size_t)n_layer + 1) * sizeof(int32_t));c->ltail = malloc(((size_t)n_layer + 1) * sizeof(int32_t));c->pf_flag = calloc(n_slots, 1);+ /* before any way out: janas_expert_cache_destroy destroys them */+ pthread_mutex_init(&c->api_lock, NULL);+ pthread_mutex_init(&c->lock, NULL);+ pthread_cond_init(&c->cond, NULL);if (c->fd < 0 || !c->arena || !c->where || !c->key || !c->prev ||!c->next || !c->stamp || !c->jobs || !c->lprev || !c->lnext ||!c->lhead || !c->ltail || !c->pf_flag) {@@ -588,9 +596,6 @@ part_create(const struct janas_jns *j, uint32_t first, uint32_t n_layer,if (!arena && getenv("JANAS_ARENA_HUGE") &&atoi(getenv("JANAS_ARENA_HUGE")) > 0)madvise(c->arena, arena_bytes, MADV_HUGEPAGE);- pthread_mutex_init(&c->api_lock, NULL);- pthread_mutex_init(&c->lock, NULL);- pthread_cond_init(&c->cond, NULL);/** Asynchronous reads, where the kernel has io_uring: for the batches it* is good at (see by_ring), begin() queues them itself, a system call,@@ -715,9 +720,9 @@ void janas_expert_cache_destroy(struct janas_expert_cache *c)pthread_cond_broadcast(&c->cond);pthread_mutex_unlock(&c->lock);pthread_join(c->coordinator, NULL);- pthread_mutex_destroy(&c->lock);- pthread_cond_destroy(&c->cond);}+ pthread_mutex_destroy(&c->lock);+ pthread_cond_destroy(&c->cond);janas_uring_destroy(c->ring);pthread_mutex_destroy(&c->api_lock);if (c->fd >= 0)@@ -747,13 +752,26 @@ size_t janas_expert_cache_slots(const struct janas_expert_cache *c)return c->n_slots;}-static const uint32_t *sort_counts;+static int by_key(const void *a, const void *b)+{+ uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b;+ return (x > y) - (x < y);+}-static int by_count(const void *a, const void *b)+/* order's n keys by ascending counts[key] (the most frequent loaded last),+ ties by key; with nothing shared between callers, as two caches may sort+ at once. Left as it is without memory for the sort. */+static void sort_by_count(uint32_t *order, size_t n, const uint32_t *counts){- uint32_t x = sort_counts[*(const uint32_t *)a];- uint32_t y = sort_counts[*(const uint32_t *)b];- return (x > y) - (x < y); /* ascending: the most frequent loaded last */+ uint64_t *k = n ? malloc(n * sizeof(*k)) : NULL;+ if (!k)+ return;+ for (size_t i = 0; i < n; i++)+ k[i] = (uint64_t)counts[order[i]] << 32 | order[i];+ qsort(k, n, sizeof(*k), by_key);+ for (size_t i = 0; i < n; i++)+ order[i] = (uint32_t)k[i];+ free(k);}/*@@ -838,9 +856,8 @@ int janas_expert_cache_warm_background(struct janas_expert_cache *c,for (size_t k = 0; k < n_keys; k++)if (c->warm_counts[k])c->warm_order[n++] = (uint32_t)k;- sort_counts = c->warm_counts;- qsort(c->warm_order, n, sizeof(uint32_t), by_count);- /* by_count puts the least used first: read the most used first here */+ sort_by_count(c->warm_order, n, c->warm_counts);+ /* the least used come first: read the most used first here */for (size_t i = 0; i < n / 2; i++) {uint32_t t = c->warm_order[i];c->warm_order[i] = c->warm_order[n - 1 - i];@@ -983,8 +1000,7 @@ int janas_expert_cache_warm(struct janas_expert_cache *c,for (size_t k = 0; k < n_keys; k++)if (counts[k])order[n++] = (uint32_t)k;- sort_counts = counts;- qsort(order, n, sizeof(uint32_t), by_count);+ sort_by_count(order, n, counts);size_t first = n > c->n_slots ? n - c->n_slots : 0;int rc = 0;for (size_t i = first; i < n && rc == 0; i++) {@@ -1169,14 +1185,25 @@ static int begin_req(struct janas_expert_cache *c, uint32_t layer,continue;}s[i] = load(c, layer, ids[i]);+ if (s[i] == NONE) {+ /* every free slot held by reads ahead: settled, and+ free to go */+ for (int p = 0; p < 2; p++)+ pf_settle(c, p);+ s[i] = load(c, layer, ids[i]);+ }c->st.misses++;queued++;}c->st.requests += n;+ int lost = 0;+ for (size_t i = 0; i < n; i++)+ lost |= s[i] == NONE;for (size_t i = 0; i < n; i++)slots[i] =s[i] != NONE ? c->arena + (size_t)s[i] * c->slot_bytes : NULL;c->outstanding = 1;+ c->lost = lost; /* a slot not found is NULL, never to be read */if (start_jobs(c) != 0)c->batch_rc = -1; /* finish() reports it, and waits all the same */return queued;@@ -1221,9 +1248,11 @@ int janas_expert_cache_finish(struct janas_expert_cache *c)}double t0 = now();int rc = wait_jobs(c);- if (c->via_ring && c->batch_rc) /* the submission itself failed */+ if ((c->via_ring && c->batch_rc) || c->lost) /* the submission itself+ failed, or a slot */rc = -1;c->batch_rc = 0;+ c->lost = 0;c->outstanding = 0;c->st.wait_seconds += now() - t0;pthread_mutex_unlock(&c->api_lock);diff --git a/src/llm/generate.c b/src/llm/generate.cindex 2e7f737..5df2c7d 100644--- a/src/llm/generate.c+++ b/src/llm/generate.c@@ -480,6 +480,7 @@ void janas_llm_session_destroy(struct janas_llm_session *s)void janas_llm_session_reset(struct janas_llm_session *s){+ janas_llm_model_media_drop(s->m, 0); /* the pictures go with it */s->dpos_set = 0;s->n = s->n_kv = s->ret = s->call_start = 0;s->n_mdraft = 0;@@ -653,6 +654,7 @@ int janas_llm_session_truncate(struct janas_llm_session *s, uint32_t n)return -1;janas_sampler_rewind(s->smp, s->n - s->ret); /* drawn, never returned */s->n = s->ret = n;+ janas_llm_model_media_drop(s->m, n); /* pictures past the cut */if (s->n_kv > kv)s->n_kv = kv;if (s->call_start > s->n_kv)@@ -725,6 +727,7 @@ int janas_llm_session_prefill_step(struct janas_llm_session *s)uint32_t most = janas_llm_model_max_block(m);if (b > most)b = most;+ b = janas_llm_model_media_cut(m, p, b);if (janas_llm_model_forward(m, s->hist + p, b, p, s->logits, 0) != 0)return -1;s->call_start = p;diff --git a/src/llm/gguf.c b/src/llm/gguf.cindex a054731..e1ed94a 100644--- a/src/llm/gguf.c+++ b/src/llm/gguf.c@@ -32,9 +32,9 @@ int janas_gguf_type(uint32_t type, uint64_t *blck, uint64_t *size){6, 32, 22}, {7, 32, 24}, {8, 32, 34}, {10, 256, 84},{11, 256, 110}, {12, 256, 144}, {13, 256, 176}, {14, 256, 210},{15, 256, 292}, {20, 32, 18}, {21, 256, 110}, {23, 256, 136},- {30, 1, 2}, {39, 32, 17}, {42, 64, 18}, {18, 256, 98},- {16, 256, 66}, {17, 256, 74}, {22, 256, 82}, {29, 256, 56},- {19, 256, 50},+ {30, 1, 2}, {39, 32, 17}, {40, 64, 36}, {42, 64, 18},+ {18, 256, 98}, {16, 256, 66}, {17, 256, 74}, {22, 256, 82},+ {29, 256, 56}, {19, 256, 50},};for (size_t i = 0; i < sizeof(t) / sizeof(t[0]); i++)if (t[i].type == type) {@@ -196,6 +196,10 @@ int janas_gguf_parse(uint8_t *head, size_t n, uint64_t file_bytes,goto bad;for (uint32_t d = t->n_dims; d < 4; d++)t->dims[d] = 1;+ for (uint32_t d = 0; d < 4; d++)+ if (t->dims[d] == 0) /* readers divide by them */+ return fail(g, -1, err, err_len,+ "a tensor with an empty dimension");if (u32(&c, &t->type) || u64(&c, &t->offset))goto bad;uint64_t blck, size, count = 1, bytes;diff --git a/src/llm/gpu.c b/src/llm/gpu.cindex 982e225..29837d7 100644--- a/src/llm/gpu.c+++ b/src/llm/gpu.c@@ -367,7 +367,7 @@ static void *keep_alive(void *arg)}/* An empty dispatch (no blocks: nothing read; invocation 0 writes one float- at the start of the Y stage, unused), recorded once, for the keep-alive. */+ into a buffer of its own), recorded once, for the keep-alive. */static int record_keep_alive(struct janas_gpu *g){VkCommandBufferAllocateInfo cba = {@@ -382,7 +382,9 @@ static int record_keep_alive(struct janas_gpu *g)g->vkCreateFence(g->dev, &fci, NULL, &g->ka_fence) != VK_SUCCESS ||g->vkBeginCommandBuffer(g->ka_cb, &bb) != VK_SUCCESS)return -1;- VkDeviceAddress a = g->ys.addr + g->ys.bytes - 64;+ if (janas_g_make_stage(g, &g->kas, 64, 0) != 0)+ return -1;+ VkDeviceAddress a = g->kas.addr;struct push pc = {.rows = 1, .nb = 0, .nv = 1, .xs = 0, .ys = 0};pc.y[0] = pc.x[0] = pc.w[0] = (uint32_t)a;pc.y[1] = pc.x[1] = pc.w[1] = (uint32_t)(a >> 32);@@ -811,66 +813,74 @@ struct janas_gpu *janas_gpu_create(char *err, size_t err_len)set_err(err, err_len, "cannot create the shaders", r);goto fail;}- /* the subgroup-per-row shaders written so far */- if ((r = janas_g_make_pipeline_nv(g, janas_spv_q4k_row,- sizeof(janas_spv_q4k_row), &g->rm[0],- &g->rp[0], 0, 1)) != VK_SUCCESS) {- set_err(err, err_len, "cannot create the shaders", r);- goto fail;- }- if ((r = janas_g_make_pipeline_nv(g, janas_spv_q6k_row,- sizeof(janas_spv_q6k_row), &g->rm[1],- &g->rp[1], 0, 1)) != VK_SUCCESS) {- set_err(err, err_len, "cannot create the shaders", r);- goto fail;- }- if ((r = janas_g_make_pipeline_nv(g, janas_spv_q6kp_row,- sizeof(janas_spv_q6kp_row), &g->rm[2],- &g->rp[2], 0, 1)) != VK_SUCCESS) {- set_err(err, err_len, "cannot create the shaders", r);- goto fail;- }- if ((r = janas_g_make_pipeline_nv(- g, janas_spv_q6ka_row, sizeof(janas_spv_q6ka_row), &g->rm_q6ka,- &g->rp_q6ka, 0, 1)) != VK_SUCCESS) {- set_err(err, err_len, "cannot create the shaders", r);- goto fail;- }- /* the tiled copies (constant 2: the rows of a tile) */- if ((r = janas_g_make_pipeline_nv(g, janas_spv_q4k_row,- sizeof(janas_spv_q4k_row),- &g->rm_tile[0], &g->rp_tile[0],- JANAS_TILE, 1)) != VK_SUCCESS ||- (r = janas_g_make_pipeline_nv(g, janas_spv_q6ka_row,- sizeof(janas_spv_q6ka_row),- &g->rm_tile[1], &g->rp_tile[1],- JANAS_TILE, 1)) != VK_SUCCESS) {- set_err(err, err_len, "cannot create the shaders", r);- goto fail;- }- /* and for 2, 4 and 8 vectors at once */- {- const uint32_t *code[4] = {janas_spv_q4k_row, janas_spv_q6k_row,- janas_spv_q6kp_row, janas_spv_q6ka_row};- size_t bytes[4] = {- sizeof(janas_spv_q4k_row), sizeof(janas_spv_q6k_row),- sizeof(janas_spv_q6kp_row), sizeof(janas_spv_q6ka_row)};- for (int k = 0; k < 3; k++)- for (int i = 0; i < 4; i++)- if ((r = janas_g_make_pipeline_nv(- g, code[i], bytes[i], &g->rmv[k][i], &g->rpv[k][i],- 0, 2u << k)) != VK_SUCCESS) {- set_err(err, err_len, "cannot create the shaders", r);- goto fail;- }- }- /* Q4_K on a few vectors, a row an invocation (JANAS_GPU_MV=1, being- measured) */- if ((r = janas_g_make_pipeline_nv(- g, janas_spv_q4k_mv, sizeof(janas_spv_q4k_mv), &g->sm_mv,- &g->pipe_mv, 0, MV_NV)) != VK_SUCCESS) {- set_err(err, err_len, "cannot create the shaders", r);- goto fail;+ /* the subgroup-per-row shaders and q4k_mv take SG / 8 blocks a+ step: none where a subgroup has fewer than 8 lanes (their step+ would be 0, a loop without end), the products going the other+ ways */+ if (g->sg >= 8) {+ /* the subgroup-per-row shaders written so far */+ if ((r = janas_g_make_pipeline_nv(+ g, janas_spv_q4k_row, sizeof(janas_spv_q4k_row), &g->rm[0],+ &g->rp[0], 0, 1)) != VK_SUCCESS) {+ set_err(err, err_len, "cannot create the shaders", r);+ goto fail;+ }+ if ((r = janas_g_make_pipeline_nv(+ g, janas_spv_q6k_row, sizeof(janas_spv_q6k_row), &g->rm[1],+ &g->rp[1], 0, 1)) != VK_SUCCESS) {+ set_err(err, err_len, "cannot create the shaders", r);+ goto fail;+ }+ if ((r = janas_g_make_pipeline_nv(+ g, janas_spv_q6kp_row, sizeof(janas_spv_q6kp_row),+ &g->rm[2], &g->rp[2], 0, 1)) != VK_SUCCESS) {+ set_err(err, err_len, "cannot create the shaders", r);+ goto fail;+ }+ if ((r = janas_g_make_pipeline_nv(+ g, janas_spv_q6ka_row, sizeof(janas_spv_q6ka_row),+ &g->rm_q6ka, &g->rp_q6ka, 0, 1)) != VK_SUCCESS) {+ set_err(err, err_len, "cannot create the shaders", r);+ goto fail;+ }+ /* the tiled copies (constant 2: the rows of a tile) */+ if ((r = janas_g_make_pipeline_nv(g, janas_spv_q4k_row,+ sizeof(janas_spv_q4k_row),+ &g->rm_tile[0], &g->rp_tile[0],+ JANAS_TILE, 1)) != VK_SUCCESS ||+ (r = janas_g_make_pipeline_nv(g, janas_spv_q6ka_row,+ sizeof(janas_spv_q6ka_row),+ &g->rm_tile[1], &g->rp_tile[1],+ JANAS_TILE, 1)) != VK_SUCCESS) {+ set_err(err, err_len, "cannot create the shaders", r);+ goto fail;+ }+ /* and for 2, 4 and 8 vectors at once */+ {+ const uint32_t *code[4] = {janas_spv_q4k_row, janas_spv_q6k_row,+ janas_spv_q6kp_row,+ janas_spv_q6ka_row};+ size_t bytes[4] = {+ sizeof(janas_spv_q4k_row), sizeof(janas_spv_q6k_row),+ sizeof(janas_spv_q6kp_row), sizeof(janas_spv_q6ka_row)};+ for (int k = 0; k < 3; k++)+ for (int i = 0; i < 4; i++)+ if ((r = janas_g_make_pipeline_nv(+ g, code[i], bytes[i], &g->rmv[k][i],+ &g->rpv[k][i], 0, 2u << k)) != VK_SUCCESS) {+ set_err(err, err_len, "cannot create the shaders",+ r);+ goto fail;+ }+ }+ /* Q4_K on a few vectors, a row an invocation (JANAS_GPU_MV=1, being+ measured) */+ if ((r = janas_g_make_pipeline_nv(+ g, janas_spv_q4k_mv, sizeof(janas_spv_q4k_mv), &g->sm_mv,+ &g->pipe_mv, 0, MV_NV)) != VK_SUCCESS) {+ set_err(err, err_len, "cannot create the shaders", r);+ goto fail;+ }}/* ... and Q4_K, Q6_K, Q5_K and IQ4_XS on 2 to 8 with the lanes onthe vectors, where a subgroup is 8 lanes, one a vector */@@ -1005,8 +1015,8 @@ void janas_gpu_destroy(struct janas_gpu *g)int waited;janas_gpu_finish(g, &waited);}- struct stage *st[3] = {&g->xs, &g->ys, &g->ts};- for (int i = 0; i < 3; i++) {+ struct stage *st[4] = {&g->xs, &g->ys, &g->ts, &g->kas};+ for (int i = 0; i < 4; i++) {if (st[i]->buf)g->vkDestroyBuffer(g->dev, st[i]->buf, NULL);if (st[i]->mem)@@ -1381,6 +1391,7 @@ static int copy_to_device(struct janas_gpu *g, const void *p, size_t bytes)if (g->vkBindBufferMemory(g->dev, rg->buf, rg->mem, 0) != VK_SUCCESS)goto fail;g->local_bytes += req.size;+ rg->local = req.size;/* staging: 64 MiB of host memory, one chunk at a time */struct stage st = {0};size_t chunk = (size_t)64 << 20;@@ -1440,9 +1451,21 @@ static int copy_to_device(struct janas_gpu *g, const void *p, size_t bytes)fail:g->vkDestroyBuffer(g->dev, rg->buf, NULL);g->vkFreeMemory(g->dev, rg->mem, NULL);+ g->local_bytes -= rg->local;+ rg->local = 0;return -1;}+void janas_g_free_stage(struct janas_gpu *g, struct stage *s)+{+ if (s->buf)+ g->vkDestroyBuffer(g->dev, s->buf, NULL);+ if (s->mem)+ g->vkFreeMemory(g->dev, s->mem, NULL);+ g->local_bytes -= s->local;+ memset(s, 0, sizeof(*s));+}+int janas_g_make_local(struct janas_gpu *g, struct stage *s, size_t bytes){memset(s, 0, sizeof(*s));@@ -1476,6 +1499,7 @@ int janas_g_make_local(struct janas_gpu *g, struct stage *s, size_t bytes)return -1;}g->local_bytes += req.size;+ s->local = req.size;s->addr = janas_g_buffer_address(g, s->buf);s->bytes = bytes;return 0;@@ -1638,7 +1662,11 @@ int janas_gpu_import(struct janas_gpu *g, const void *p, size_t bytes)return import_refused(g, p, bytes, "buffer", r);VkMemoryRequirements req;g->vkGetBufferMemoryRequirements(g->dev, rg->buf, &req);- int t = janas_g_mem_type(g, hp.memoryTypeBits & req.memoryTypeBits, 0);+ /* coherent only: the host writes these pages and reads what the GPU+ wrote with no flush or invalidation in between (some ARM drivers+ offer cached, non-coherent types: refused, the copy way instead) */+ int t = janas_g_mem_type(g, hp.memoryTypeBits & req.memoryTypeBits,+ VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);VkImportMemoryHostPointerInfoEXT imp = {.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT,.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,@@ -1679,6 +1707,7 @@ void janas_g_drop_region(struct janas_gpu *g, const void *host)struct region *rg = &g->reg[--g->n_reg];g->vkDestroyBuffer(g->dev, rg->buf, NULL);g->vkFreeMemory(g->dev, rg->mem, NULL);+ g->local_bytes -= rg->local;memset(rg, 0, sizeof(*rg));}@@ -1900,6 +1929,7 @@ static int b32_kind(int type): type == JANAS_MXFP4 ? 4: type == JANAS_Q5_0 ? 5: type == JANAS_Q2_0 ? 6+ : type == JANAS_NVFP4 ? 7: -1;}diff --git a/src/llm/gpu_impl.h b/src/llm/gpu_impl.hindex 97cee12..b435421 100644--- a/src/llm/gpu_impl.h+++ b/src/llm/gpu_impl.h@@ -18,7 +18,7 @@/* the kinds of product shader (gpu.c's prod_index) */#define N_PROD 15/* the kinds of b32_matvec.comp (gpu.c's b32_kind) */-#define N_B32 7+#define N_B32 8/* the 256-block types of iq2_matvec, iq3xxs_matvec, iq1_matvec andq2k_matvec.comp (gpu.c's iqk_index) */#define N_IQK 7@@ -127,6 +127,7 @@ struct region {GPU writes there the CPU reads. 0: a copy in a card's own memory(copy_to_device), which the GPU's writes never leave */int host_shared;+ VkDeviceSize local; /* of the card's own memory, given back with it */};struct stage {@@ -135,7 +136,8 @@ struct stage {uint8_t *map;VkDeviceAddress addr;size_t bytes;- int coherent; /* else invalidated before the host reads it */+ int coherent; /* else invalidated before the host reads it */+ VkDeviceSize local; /* of the card's own memory (janas_g_make_local) */};struct pending {@@ -250,13 +252,16 @@ struct janas_gpu {struct region reg[MAX_REGIONS];int n_reg;struct stage xs, ys;+ struct stage kas; /* the keep-alive's float: its own, never a stage in+ use (another thread writes it, unordered) */struct pending pend[MAX_PENDING];size_t n_pend;int busy, broken;char name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];uint32_t sg; /* subgroup size the shader runs with */int discrete; /* weights copied to its memory, not shared */- VkDeviceSize local_bytes; /* taken of its own memory, never given back */+ VkDeviceSize local_bytes; /* taken of its own memory, less what was+ given back */int host_import; /* VK_EXT_external_memory_host available */int sg_control; /* the size is required (else the device's fixed one) */uint32_t sg_min, sg_max; /* the sizes it may be given (sg_control) */@@ -341,6 +346,9 @@ void janas_g_drop_region(struct janas_gpu *g, const void *host);int janas_g_mem_type(const struct janas_gpu *g, uint32_t bits,VkMemoryPropertyFlags want);VkDeviceAddress janas_g_buffer_address(struct janas_gpu *g, VkBuffer b);+/* A stage's buffer and memory freed, what it took of the card's own memory+ given back; the stage zeroed. */+void janas_g_free_stage(struct janas_gpu *g, struct stage *s);int janas_g_make_stage(struct janas_gpu *g, struct stage *s, size_t bytes,int cached);VkResult janas_g_make_pipeline(struct janas_gpu *g, const uint32_t *code,diff --git a/src/llm/gpu_token.c b/src/llm/gpu_token.cindex ab83268..f3e50da 100644--- a/src/llm/gpu_token.c+++ b/src/llm/gpu_token.c@@ -239,6 +239,9 @@ static int check_model(const struct janas_gpu *g,/* feed-forwards may end inside a group of 256 (qwen4exp's 640): theirdown matrices in blocks of 32, checked by janas_gpu_can andcheck_ffn */+ /* its shaders add in clusters of 8 lanes and use the row shaders */+ if (g->sg < 8)+ return fail(err, len, "subgroups of fewer than 8 lanes");if (md->dm % JANAS_QK || qd % JANAS_QK || md->ff % 32 || md->dm > 8192 ||qd > 8192 || md->hd > 512 || md->hd % 16 || md->n_rot > md->hd ||md->n_kv == 0 || md->n_head % md->n_kv)@@ -256,7 +259,10 @@ static int check_model(const struct janas_gpu *g,: 0;if (!ga || ly->hd > md->hd || ly->hd % 16 || ly->n_rot > ly->hd ||ly->n_rot % 2 || ly->n_kv > md->n_kv ||- ga * ATT_RUN > ATT_MAXS_G(ga) || ly->cap == 0 ||+ ga * ATT_RUN > ATT_MAXS_G(ga) ||+ /* sixteen-bit values: a thread 8 numbers of the G heads, at+ most 256 threads (attn_decode.comp's SH would be 0) */+ (md->kv16 && ga * ly->hd > 2048) || ly->cap == 0 ||ly->cap > md->cap ||(ly->swa ? ly->n_rot > md->n_rot_swa : ly->n_rot > md->n_rot) ||(md->kv16 && (md->ix_heads || md->n_rec)))@@ -371,8 +377,7 @@ static int make_buf(struct janas_gpu *g, struct janas_gpu_tok *t, size_t bytes)if (janas_g_make_local(g, &d, bytes) != 0)return janas_g_make_stage(g, &t->buf, bytes, 1);if (janas_g_make_stage(g, &t->mirror, bytes, 1) != 0) {- g->vkDestroyBuffer(g->dev, d.buf, NULL);- g->vkFreeMemory(g->dev, d.mem, NULL);+ janas_g_free_stage(g, &d);return -1;}t->buf = d;@@ -552,7 +557,9 @@ struct janas_gpu_tok *janas_gpu_tok_create(struct janas_gpu *g,t->mvid_n = mv ? (uint32_t)atoi(mv) : (uint32_t)nb;if (t->mvid_n > nb)t->mvid_n = (uint32_t)nb;- size_t nt = np / 16, nm = (size_t)t->mvid_n * md->k / MVID_NV;+ /* the tables for a whole block by rows, whatever mvid_n: Q5_1 and+ Q8_0, which have no tiles, go by rows at any size */+ size_t nt = np / 16, nm = (size_t)nb * md->k / MVID_NV;if (nm > nt)nt = nm;t->o_tab1 = take(&at, (t->tiles1 + 2 * (nt + ne)) * 16);@@ -770,6 +777,8 @@ void janas_gpu_tok_destroy(struct janas_gpu *g, struct janas_gpu_tok *t)g->vkFreeCommandBuffers(g->dev, g->cp, 1, &t->cb[n][a][h]);janas_gt_free_segments(g, t, &t->seg_cb[n][a][h]);}+ if (t->cb_rows)+ g->vkFreeCommandBuffers(g->dev, g->cp, 1, &t->cb_rows);VkPipeline p[18] = {t->p_nq, t->p_qk, t->p_at, t->p_am, t->p_gc, t->p_gs,t->p_gl, t->p_gr, t->p_gg, t->p_tk, t->p_ga, t->p_cb,t->p_sm, t->p_gb, t->p_gx, t->p_pl, t->p_at2, t->p_pa};@@ -791,14 +800,8 @@ void janas_gpu_tok_destroy(struct janas_gpu *g, struct janas_gpu_tok *t)if (s[i])g->vkDestroyShaderModule(g->dev, s[i], NULL);}- if (t->buf.buf)- g->vkDestroyBuffer(g->dev, t->buf.buf, NULL);- if (t->buf.mem)- g->vkFreeMemory(g->dev, t->buf.mem, NULL);- if (t->mirror.buf)- g->vkDestroyBuffer(g->dev, t->mirror.buf, NULL);- if (t->mirror.mem)- g->vkFreeMemory(g->dev, t->mirror.mem, NULL);+ janas_g_free_stage(g, &t->buf);+ janas_g_free_stage(g, &t->mirror);free(t);}diff --git a/src/llm/gpu_token_impl.h b/src/llm/gpu_token_impl.hindex 47cb882..60cd036 100644--- a/src/llm/gpu_token_impl.h+++ b/src/llm/gpu_token_impl.h@@ -186,10 +186,11 @@ _Static_assert(sizeof(struct push_at) <= JANAS_G_PUSH_BYTES, "push size");#define ATT_MAXS_G(g) ((g) * ATT_RUN > ATT_MAXS ? (g) * ATT_RUN : ATT_MAXS)/* The query heads a workgroup of the attention takes, of the gt that sharea KV head of hd numbers: all of them, or (Gemma 4's full layers, 16 of- 512) the most of a divisor of gt that its shared memory holds; 0: none */+ 512) the most of a divisor of gt that its shared memory holds, at most+ 16 (the query scales' array, sqs); 0: none */static inline uint32_t janas_gt_att_g(uint32_t gt, uint32_t hd){- for (uint32_t g = gt; g >= 1; g--)+ for (uint32_t g = gt < 16 ? gt : 16; g >= 1; g--)if (gt % g == 0 && g * hd <= ATT_MAXQ_G(g))return g;return 0;diff --git a/src/llm/gpu_token_moe.c b/src/llm/gpu_token_moe.cindex 83c8424..723f1bc 100644--- a/src/llm/gpu_token_moe.c+++ b/src/llm/gpu_token_moe.c@@ -274,10 +274,7 @@ void janas_gt_tier_destroy(struct janas_gpu *g, struct janas_gpu_tok *t)struct stage *st = i < TIER_PIECES ? &t->tier[i]: i == TIER_PIECES ? &t->up: &t->at;- if (st->buf)- g->vkDestroyBuffer(g->dev, st->buf, NULL);- if (st->mem)- g->vkFreeMemory(g->dev, st->mem, NULL);+ janas_g_free_stage(g, st);}free(t->slot_of);free(t->uses);diff --git a/src/llm/gpu_token_rec.c b/src/llm/gpu_token_rec.cindex b882b6a..f12e78b 100644--- a/src/llm/gpu_token_rec.c+++ b/src/llm/gpu_token_rec.c@@ -296,6 +296,8 @@ int janas_gt_rec_attn(struct janas_gpu *g, struct janas_gpu_tok *t,if (md->gated_q) {size_t rb =(size_t)(md->dm / JANAS_QK) * janas_qtype_block_size(ly->wq.type);+ if (!rb) /* a repacked type (JANAS_GPU_REPACK): no rows to cut */+ return -1;for (uint32_t h = 0; h < md->n_head; h++) {struct janas_matvec_task q = ly->wq, gt = ly->wq;q.rows = gt.rows = hd;diff --git a/src/llm/jns.c b/src/llm/jns.cindex 79ea743..e2f0fec 100644--- a/src/llm/jns.c+++ b/src/llm/jns.c@@ -87,6 +87,7 @@ static int type_geometry(uint32_t type, uint64_t *blck, uint64_t *size){23, 256, 136}, /* IQ4_XS */{30, 1, 2}, /* BF16 */{39, 32, 17}, /* MXFP4 */+ {40, 64, 36}, /* NVFP4 */{42, 64, 18}, /* Q2_0 */{18, 256, 98}, /* IQ3_XXS */{16, 256, 66}, /* IQ2_XXS */@@ -443,9 +444,14 @@ int janas_jns_parse(const uint8_t *image, size_t bytes, uint64_t file_bytes,h->header_checksum)return fail(err, err_len, "header checksum mismatch");- if (h->n_layer < 1 || h->n_layer > JANAS_JNS_MAX_LAYERS ||- h->n_expert < 1 || h->n_expert > JANAS_JNS_MAX_EXPERTS ||- h->n_expert_used < 1 || h->n_expert_used > h->n_expert ||+ /* no layers and no experts: a file of resident tensors only (the+ vision encoder of tools/hf2jns_vision.c) */+ int resident_only =+ h->n_layer == 0 && h->n_expert == 0 && h->n_expert_used == 0;+ if ((!resident_only &&+ (h->n_layer < 1 || h->n_layer > JANAS_JNS_MAX_LAYERS ||+ h->n_expert < 1 || h->n_expert > JANAS_JNS_MAX_EXPERTS ||+ h->n_expert_used < 1 || h->n_expert_used > h->n_expert)) ||h->n_tensor > JANAS_JNS_MAX_TENSORS)return fail(err, err_len, "model dimensions out of range");@@ -494,9 +500,10 @@ int janas_jns_parse(const uint8_t *image, size_t bytes, uint64_t file_bytes,h->experts_offset < h->resident_offset + h->resident_bytes)return fail(err, err_len, "data regions out of place");- out->layers = malloc(array_bytes(h->n_layer, sizeof(*out->layers)));- out->expert_offset = malloc(slot_len);- out->expert_sum = malloc(slot_len);+ uint64_t lb = array_bytes(h->n_layer, sizeof(*out->layers));+ out->layers = malloc(lb ? lb : 1);+ out->expert_offset = malloc(slot_len ? slot_len : 1);+ out->expert_sum = malloc(slot_len ? slot_len : 1);out->tensors = malloc(dir_len ? dir_len : 1);out->metadata = malloc(h->metadata_bytes);if (!out->layers || !out->expert_offset || !out->expert_sum ||@@ -541,13 +548,31 @@ int janas_jns_parse(const uint8_t *image, size_t bytes, uint64_t file_bytes,uint64_t experts_end = h->experts_offset + h->experts_bytes;for (uint32_t l = 0; l < h->n_layer; l++) {const struct janas_jns_layer *ly = &out->layers[l];- if (ly->slot_bytes == 0 || ly->slot_bytes % JANAS_JNS_ALIGN) {+ /* a layer without experts (nemotron_h_moe's Mamba-2 and attention+ layers): an empty slot, no matrices, every expert absent */+ if (ly->slot_bytes == 0) {+ static const struct janas_jns_layer none;+ int empty = memcmp(ly, &none, sizeof(none)) == 0;+ for (uint32_t e = 0; empty && e < h->n_expert; e++)+ empty = out->expert_offset[(size_t)l * h->n_expert + e] == 0 &&+ out->expert_sum[(size_t)l * h->n_expert + e] == 0;+ if (!empty) {+ janas_jns_close(out);+ return fail(err, err_len, "layer %u: bad empty slot", l);+ }+ continue;+ }+ if (ly->slot_bytes % JANAS_JNS_ALIGN) {janas_jns_close(out);return fail(err, err_len, "layer %u: bad slot size", l);}for (int m = 0; m < 3; m++) {const struct janas_jns_matrix *mx = &ly->m[m];uint64_t blck, size, row_bytes, total;+ static const struct janas_jns_matrix no_matrix;+ /* no gate (nemotron_h_moe's experts: relu^2 on up alone) */+ if (m == JANAS_JNS_GATE && memcmp(mx, &no_matrix, sizeof(*mx)) == 0)+ continue;if (!type_geometry(mx->type, &blck, &size) || mx->rows == 0 ||mx->cols == 0 || mx->cols % blck ||__builtin_mul_overflow(mx->cols / blck, size, &row_bytes) ||@@ -605,7 +630,13 @@ int janas_jns_parse(const uint8_t *image, size_t bytes, uint64_t file_bytes,}}for (uint32_t e = 0; e < h->n_expert; e++) {- uint64_t off = out->expert_offset[(size_t)l * h->n_expert + e];+ size_t at = (size_t)l * h->n_expert + e;+ uint64_t off = out->expert_offset[at];+ /* absent: past the layer's own experts, a tail of them, each+ with a zero checksum (gguf2jns) */+ if (off == 0 && out->expert_sum[at] == 0 &&+ (e + 1 == h->n_expert || out->expert_offset[at + 1] == 0))+ continue;if (off % JANAS_JNS_ALIGN ||!inside(off, ly->slot_bytes, h->experts_offset, experts_end)) {janas_jns_close(out);@@ -616,9 +647,17 @@ int janas_jns_parse(const uint8_t *image, size_t bytes, uint64_t file_bytes,}for (uint64_t t = 0; t < h->n_tensor; t++) {const struct janas_jns_tensor *tn = &out->tensors[t];- uint64_t blck, size;- if (memchr(tn->name, 0, sizeof(tn->name)) == NULL || tn->n_dims < 1 ||- tn->n_dims > 4 || !type_geometry(tn->type, &blck, &size) ||+ uint64_t blck, size, rows = 1, want = 0;+ int dims_ok = tn->n_dims >= 1 && tn->n_dims <= 4;+ /* the bytes are what the shape says: the readers trust the dims */+ for (uint32_t d = 1; dims_ok && d < tn->n_dims; d++)+ dims_ok = tn->dims[d] > 0 &&+ !__builtin_mul_overflow(rows, tn->dims[d], &rows);+ if (memchr(tn->name, 0, sizeof(tn->name)) == NULL || !dims_ok ||+ !type_geometry(tn->type, &blck, &size) || tn->dims[0] == 0 ||+ tn->dims[0] % blck ||+ __builtin_mul_overflow(tn->dims[0] / blck, size, &want) ||+ __builtin_mul_overflow(want, rows, &want) || want != tn->bytes ||!inside(tn->offset, tn->bytes, h->resident_offset,h->resident_offset + h->resident_bytes)) {janas_jns_close(out);diff --git a/src/llm/jns_write.c b/src/llm/jns_write.cnew file mode 100644index 0000000..7110d79--- /dev/null+++ b/src/llm/jns_write.c@@ -0,0 +1,208 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * jns_write.c - a JNS file of resident tensors only (jns_write.h).+ */+#define _GNU_SOURCE+#include "jns_write.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <unistd.h>++#include "jns.h"++#define GG_I32 5+#define GG_STRING 8+#define GG_ARRAY 9++static uint64_t align_up(uint64_t x, uint64_t a)+{+ return (x + a - 1) / a * a;+}++int janas_jw_tensor(struct janas_jw *w, const char *name, uint32_t type,+ uint32_t n_dims, const uint64_t *dims, void *data,+ uint64_t bytes)+{+ if (strlen(name) >= 64 || n_dims < 1 || n_dims > 4 || !data) {+ free(data);+ return -1;+ }+ if (w->n == w->cap) {+ int cap = w->cap ? 2 * w->cap : 64;+ struct janas_jw_tensor *t = realloc(w->t, sizeof(*t) * (size_t)cap);+ if (!t) {+ free(data);+ return -1;+ }+ w->t = t;+ w->cap = cap;+ }+ struct janas_jw_tensor *t = &w->t[w->n++];+ memset(t, 0, sizeof *t);+ snprintf(t->name, sizeof t->name, "%s", name);+ t->type = type;+ t->n_dims = n_dims;+ memcpy(t->dims, dims, sizeof(uint64_t) * n_dims);+ t->data = data;+ t->bytes = bytes;+ return 0;+}++static void key(struct janas_jw *w, const char *k, uint32_t type)+{+ uint64_t n = strlen(k);+ janas_buf_put(&w->meta, (const char *)&n, 8);+ janas_buf_put(&w->meta, k, n);+ janas_buf_put(&w->meta, (const char *)&type, 4);+ w->n_meta++;+}++void janas_jw_u32(struct janas_jw *w, const char *k, uint32_t v)+{+ key(w, k, 4);+ janas_buf_put(&w->meta, (const char *)&v, 4);+}++void janas_jw_f32(struct janas_jw *w, const char *k, float v)+{+ key(w, k, 6);+ janas_buf_put(&w->meta, (const char *)&v, 4);+}++void janas_jw_str(struct janas_jw *w, const char *k, const char *v)+{+ key(w, k, GG_STRING);+ uint64_t n = strlen(v);+ janas_buf_put(&w->meta, (const char *)&n, 8);+ janas_buf_put(&w->meta, v, n);+}++void janas_jw_strings(struct janas_jw *w, const char *k, uint64_t n,+ const char *const *v, const uint64_t *lens)+{+ uint32_t et = GG_STRING;+ key(w, k, GG_ARRAY);+ janas_buf_put(&w->meta, (const char *)&et, 4);+ janas_buf_put(&w->meta, (const char *)&n, 8);+ for (uint64_t i = 0; i < n; i++) {+ uint64_t len = lens ? lens[i] : strlen(v[i]);+ janas_buf_put(&w->meta, (const char *)&len, 8);+ janas_buf_put(&w->meta, v[i], len);+ }+}++void janas_jw_ints(struct janas_jw *w, const char *k, uint64_t n,+ const int32_t *v)+{+ uint32_t et = GG_I32;+ key(w, k, GG_ARRAY);+ janas_buf_put(&w->meta, (const char *)&et, 4);+ janas_buf_put(&w->meta, (const char *)&n, 8);+ janas_buf_put(&w->meta, (const char *)v, 4 * n);+}++int janas_jw_write(struct janas_jw *w, const char *path, const char *arch,+ uint32_t d_model, uint64_t *file_bytes, char *err,+ size_t err_len)+{+ struct janas_buf mreg = {0};+ janas_buf_put(&mreg, (const char *)&w->n_meta, 8);+ janas_buf_put(&mreg, w->meta.p, w->meta.n);+ uint64_t A = JANAS_JNS_ALIGN, dir_off = A;+ uint64_t dir_len = sizeof(struct janas_jns_tensor) * (uint64_t)w->n;+ uint8_t *dirb = calloc(1, dir_len ? dir_len : 1);+ FILE *f = NULL;+ int rc = -1;+ if (w->meta.oom || mreg.oom || !dirb) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ /* layout: header, tensor directory, metadata, resident data */+ uint64_t meta_off = align_up(dir_off + dir_len, A);+ uint64_t res_off = align_up(meta_off + mreg.n, A), pos = res_off;+ for (int i = 0; i < w->n; i++) {+ w->t[i].offset = pos;+ pos = align_up(pos + w->t[i].bytes, A);+ }+ f = fopen(path, "w+b");+ if (!f || ftruncate(fileno(f), (off_t)pos)) {+ snprintf(err, err_len, "cannot create %s", path);+ goto done;+ }+ for (int i = 0; i < w->n; i++) {+ struct janas_jns_tensor t = {0};+ memcpy(t.name, w->t[i].name, sizeof t.name);+ t.type = w->t[i].type;+ t.n_dims = w->t[i].n_dims;+ memcpy(t.dims, w->t[i].dims, sizeof t.dims);+ t.offset = w->t[i].offset;+ t.bytes = w->t[i].bytes;+ memcpy(dirb + (size_t)i * sizeof t, &t, sizeof t);+ if (fseeko(f, (off_t)t.offset, SEEK_SET) ||+ fwrite(w->t[i].data, 1, t.bytes, f) != t.bytes)+ goto failed;+ }+ if (fseeko(f, (off_t)dir_off, SEEK_SET) ||+ fwrite(dirb, 1, dir_len, f) != dir_len ||+ fseeko(f, (off_t)meta_off, SEEK_SET) ||+ fwrite(mreg.p, 1, mreg.n, f) != mreg.n)+ goto failed;+ struct janas_jns_header h = {0};+ memcpy(h.magic, JANAS_JNS_MAGIC, 8);+ h.version = JANAS_JNS_VERSION;+ h.header_bytes = JANAS_JNS_ALIGN;+ h.file_bytes = pos;+ snprintf(h.arch, sizeof h.arch, "%s", arch);+ h.d_model = d_model;+ h.layer_table_offset = dir_off;+ h.expert_table_offset = dir_off;+ h.expert_sums_offset = dir_off;+ h.tensor_dir_offset = dir_off;+ h.n_tensor = (uint64_t)w->n;+ h.metadata_offset = meta_off;+ h.metadata_bytes = mreg.n;+ h.resident_offset = res_off;+ h.resident_bytes = pos - res_off;+ h.experts_offset = pos;+ h.tables_checksum = janas_fnv1a(dirb, dir_len, JANAS_FNV_INIT);+ h.metadata_checksum = janas_crc32(0, mreg.p, mreg.n);+ h.header_checksum = janas_fnv1a(&h, sizeof h, JANAS_FNV_INIT);+ if (fseeko(f, 0, SEEK_SET) || fwrite(&h, 1, sizeof h, f) != sizeof h)+ goto failed;+ int closed = fclose(f);+ f = NULL;+ if (closed != 0)+ goto failed;+ /* read back as the engine reads it */+ struct janas_jns j;+ char e[256];+ if (janas_jns_open(path, &j, e, sizeof e) != 0) {+ snprintf(err, err_len, "the file written does not read back: %s", e);+ goto done;+ }+ janas_jns_close(&j);+ if (file_bytes)+ *file_bytes = pos;+ rc = 0;+ goto done;+failed:+ snprintf(err, err_len, "write failed: %s", path);+done:+ if (f)+ fclose(f);+ free(dirb);+ free(mreg.p);+ return rc;+}++void janas_jw_free(struct janas_jw *w)+{+ for (int i = 0; i < w->n; i++)+ free(w->t[i].data);+ free(w->t);+ free(w->meta.p);+ memset(w, 0, sizeof *w);+}diff --git a/src/llm/jns_write.h b/src/llm/jns_write.hnew file mode 100644index 0000000..565ace1--- /dev/null+++ b/src/llm/jns_write.h@@ -0,0 +1,52 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * jns_write.h - a JNS file of resident tensors only (no layers, no+ * experts), as the converters of encoders write it (jns_write.c): the+ * tensors and the metadata gathered, then written at once and read back as+ * the engine reads it.+ */+#ifndef JANAS_LLM_JNS_WRITE_H+#define JANAS_LLM_JNS_WRITE_H++#include <stdint.h>++#include "json.h"++struct janas_jw_tensor {+ char name[64];+ uint32_t type, n_dims;+ uint64_t dims[4]; /* ggml order: the row length first */+ void *data; /* the writer's: freed with it */+ uint64_t bytes, offset;+};++struct janas_jw {+ struct janas_jw_tensor *t;+ int n, cap;+ struct janas_buf meta; /* GGUF's key/value encoding */+ uint64_t n_meta;+};++/* A tensor; data (malloc'd) becomes the writer's, freed on failure too.+ 0, or -1 (out of memory, a name of 64 bytes or more). */+int janas_jw_tensor(struct janas_jw *w, const char *name, uint32_t type,+ uint32_t n_dims, const uint64_t *dims, void *data,+ uint64_t bytes);+/* Metadata; a failure is remembered (oom) and reported by the write. */+void janas_jw_u32(struct janas_jw *w, const char *k, uint32_t v);+void janas_jw_f32(struct janas_jw *w, const char *k, float v);+void janas_jw_str(struct janas_jw *w, const char *k, const char *v);+/* an array of n strings, each of lens[i] bytes (lens NULL: NUL-ended) */+void janas_jw_strings(struct janas_jw *w, const char *k, uint64_t n,+ const char *const *v, const uint64_t *lens);+void janas_jw_ints(struct janas_jw *w, const char *k, uint64_t n,+ const int32_t *v);+/* The file: header (arch, d_model), tensor directory, metadata, data.+ 0, or -1 with the reason in err. */+int janas_jw_write(struct janas_jw *w, const char *path, const char *arch,+ uint32_t d_model, uint64_t *file_bytes, char *err,+ size_t err_len);+void janas_jw_free(struct janas_jw *w);++#endifdiff --git a/src/llm/model.c b/src/llm/model.cindex 041a3ce..cb1102b 100644--- a/src/llm/model.c+++ b/src/llm/model.c@@ -106,10 +106,37 @@ static void rope_table(float *cs, float *sn, uint32_t n, uint32_t pos0,}}+/* With pictures in the sequence: each position's three (time, row, column;+ model_media.c), each frequency taking the one the multimodal sections+ give it */+static void rope_media(struct janas_llm_model *m, uint32_t n, uint32_t pos0)+{+ uint32_t half = m->n_rot / 2;+ for (uint32_t j = 0; j < n; j++) {+ float p3[3];+ janas_m_media_pos(m, pos0 + j, p3);+ for (uint32_t i = 0; i < half; i++) {+ int axis = i % 3 == 1 && i < 3 * m->mrope[1] ? 1+ : i % 3 == 2 && i < 3 * m->mrope[2] ? 2+ : 0;+ float theta =+ p3[axis] *+ janas_powf(m->rope_base, -2.0f * (float)i / (float)m->n_rot);+ if (m->rope_freqs)+ theta /= m->rope_freqs[i];+ janas_sincosf(theta, &m->rope_sin[(size_t)j * half + i],+ &m->rope_cos[(size_t)j * half + i]);+ }+ }+}+static void set_rope(struct janas_llm_model *m, uint32_t n, uint32_t pos0){- rope_table(m->rope_cos, m->rope_sin, n, pos0, m->n_rot, m->rope_base,- m->rope_freqs);+ if (m->n_media && (m->mrope[0] || m->mrope[1] || m->mrope[2]))+ rope_media(m, n, pos0);+ else+ rope_table(m->rope_cos, m->rope_sin, n, pos0, m->n_rot, m->rope_base,+ m->rope_freqs);if (m->a->swa)rope_table(m->rope_cos_swa, m->rope_sin_swa, n, pos0, m->n_rot_swa,m->rope_base_swa, NULL);@@ -255,7 +282,10 @@ static void unrotate_row(void *row, float *scale, uint32_t hd, uint32_t half,int janas_llm_model_shift(struct janas_llm_model *m, uint32_t keep,uint32_t drop, uint32_t n_kv){- if (!drop || n_kv > m->n_ctx || keep + drop > n_kv || m->hd_max > 512)+ /* pictures' positions are not slid (yet): the caller reads the prompt+ again instead */+ if (!drop || n_kv > m->n_ctx || keep + drop > n_kv || m->hd_max > 512 ||+ m->n_media)return -1;/* qwen4exp: its n-gram history first (the one part that can refuse) */if (m->hc_n && janas_m_hc_shift(m, keep, drop, n_kv) != 0)@@ -267,7 +297,10 @@ int janas_llm_model_shift(struct janas_llm_model *m, uint32_t keep,for (uint32_t sl = 0; sl < slots; sl++) {uint32_t hd = m->kv_hd[sl], cap = m->kv_cap[sl];int swa = m->kv_swa[sl];- uint32_t n_rot = swa ? m->n_rot_swa : m->n_rot, half = n_rot / 2;+ /* Nemotron-H's attention has no RoPE (its n_rot is the buffers'):+ the keys move, nothing to turn back */+ uint32_t n_rot = m->a->nh ? 0 : swa ? m->n_rot_swa : m->n_rot;+ uint32_t half = n_rot / 2;float base = swa ? m->rope_base_swa : m->rope_base;const float *ff = swa ? NULL : m->rope_freqs;for (uint32_t i = 0; i < half; i++) {@@ -292,7 +325,7 @@ int janas_llm_model_shift(struct janas_llm_model *m, uint32_t keep,memmove(kz + keep, kz + keep + drop, move * sizeof(float));memmove(vz + keep, vz + keep + drop, move * sizeof(float));}- for (uint32_t p = keep; p < keep + move; p++)+ for (uint32_t p = keep; p < keep + move && half; p++)unrotate_row(k + p * row, kz + p, hd, half, cs, sn,m->kv16);continue;@@ -318,8 +351,9 @@ int janas_llm_model_shift(struct janas_llm_model *m, uint32_t keep,memcpy(v + dst * row, tv + src * row, row);kz[dst] = tkz[src];vz[dst] = tvz[src];- unrotate_row(k + dst * row, kz + dst, hd, half, cs, sn,- m->kv16);+ if (half)+ unrotate_row(k + dst * row, kz + dst, hd, half, cs, sn,+ m->kv16);}}}@@ -331,10 +365,20 @@ int janas_llm_model_shift(struct janas_llm_model *m, uint32_t keep,/* the recurrent state holds what it has seen, dropped tokens included -that is what carries the older text - but the position it stands atmoves with everything else */- if (m->base_pos != UINT32_MAX)- m->base_pos = m->base_pos >= keep + drop ? m->base_pos - drop- : m->base_pos > keep ? keep- : m->base_pos;+ if (m->base_pos != UINT32_MAX) {+ uint32_t b = m->base_pos, t = b + m->log_n;+ if (b >= keep + drop)+ m->base_pos = b - drop;+ else if (m->log_n && t > keep) {+ /* the log runs into what is dropped (or past it): replaying+ it would count the dropped tokens and land short of the+ tip, so the state goes on from the tip, the other buffer */+ m->base = 1 - m->base;+ m->base_pos = t >= keep + drop ? t - drop : keep;+ m->log_n = 0;+ } else if (b > keep)+ m->base_pos = keep;+ }return 0;}@@ -508,6 +552,27 @@ static int forward(struct janas_llm_model *m, const int32_t *tokens, uint32_t n,for (uint32_t j = 0; j < n; j++)if (tokens[j] < 0 || (uint32_t)tokens[j] >= m->n_vocab)return -1;+ uint32_t dm = m->d_model;+ size_t nbd = dm / JANAS_QK;+ for (uint32_t j = 0; j < n; j++) {+ /* a picture's token: its embedding from the encoder */+ const float *pic = m->n_media ? janas_m_media_row(m, pos0 + j) : NULL;+ if (pic)+ memcpy(m->x + (size_t)j * dm, pic, sizeof(float) * dm);+ else+ embed(m, tokens[j], m->x + (size_t)j * dm);+ }+ if (m->trace >= 2) {+ fprintf(stderr, "tokens:");+ for (uint32_t j = 0; j < n; j++)+ fprintf(stderr, " %d", tokens[j]);+ fprintf(stderr, "\n");+ trace_row(m, "embed", m->x + (size_t)(n - 1) * dm, dm);+ }+ /* what can fail before the state is touched: a failure leaves it as+ it was */+ if (m->ple_dim && janas_m_ple_inputs(m, tokens, n, pos0) != 0)+ return -1;/* from the start: nothing of the GPU's copy of the state matters, thehost's (the recurrent states zeroed below) is the one */if (m->gtok_dev && pos0 == 0)@@ -517,8 +582,7 @@ static int forward(struct janas_llm_model *m, const int32_t *tokens, uint32_t n,m->src = 0;m->replay = 0;memset(m->ssm_buf, 0,- (size_t)m->n_rec * m->n_vh * m->ds * m->ds *- sizeof(uint16_t));+ (size_t)m->n_rec * m->ssm_per * sizeof(uint16_t));memset(m->conv_buf, 0,(size_t)m->n_rec * m->conv_ch * (m->d_conv - 1) *sizeof(float));@@ -534,20 +598,6 @@ static int forward(struct janas_llm_model *m, const int32_t *tokens, uint32_t n,}m->base_pos = UINT32_MAX; /* invalid until this pass completes */}- uint32_t dm = m->d_model;- size_t nbd = dm / JANAS_QK;-- for (uint32_t j = 0; j < n; j++)- embed(m, tokens[j], m->x + (size_t)j * dm);- if (m->trace >= 2) {- fprintf(stderr, "tokens:");- for (uint32_t j = 0; j < n; j++)- fprintf(stderr, " %d", tokens[j]);- fprintf(stderr, "\n");- trace_row(m, "embed", m->x + (size_t)(n - 1) * dm, dm);- }- if (m->ple_dim && janas_m_ple_inputs(m, tokens, n) != 0)- return -1;set_rope(m, n, pos0);/* a token (or, with JANAS_GPU_BLOCK=1, a block) on the GPU, whole,where it can (model_gpu.c) */@@ -585,6 +635,12 @@ static int forward(struct janas_llm_model *m, const int32_t *tokens, uint32_t n,}return 0;}+ /* Nemotron-H: one part a layer (model_nh.c) */+ if (m->a->nh) {+ if (janas_m_nh_layers(m, n, pos0) != 0)+ return -1;+ goto output;+ }/* qwen4exp: its layers on the streams of the residual (model_hc.c) */if (m->hc_n) {if (janas_m_hc_layers(m, tokens, n, pos0) != 0)diff --git a/src/llm/model.h b/src/llm/model.hindex 2914166..cd30271 100644--- a/src/llm/model.h+++ b/src/llm/model.h@@ -49,6 +49,8 @@ struct janas_llm_options {choice to the engine: every bit when the experts fitin cache_bytes, four otherwise (environmentJANAS_EXPERT_BITS overrides) */+ int min_quality; /* JANAS_LLM_QUALITY_*: the lowest the engine's own+ choices go (expert bits, experts left out) */int head_bits; /* 4: the output head, if the file holds it at six bits(Q6_K), requantized to four (Q4_K) at load - faster, alittle less accurate; 0 or 6: as in the file@@ -61,6 +63,8 @@ struct janas_llm_options {experts (janas_llm_open); 0: read a row at a timefrom the file (environment JANAS_PLE_RAM=0/1overrides) */+ uint32_t min_block; /* the prefill block at least this (a picture whose+ tokens attend both ways goes in one pass) */};struct janas_llm_model;@@ -152,9 +156,9 @@ double janas_llm_model_pass_seconds(const struct janas_llm_model *m);* the expert cache) the GPU's fast prompt takes whole (gate and up Q4_K,* down Q4_K or Q6_K, plain or in planes; janas_gpu_fast_prompt on), else* 256;- * 64 .. 4096.+ * 64 .. 4096; at_least raises it (pictures in one pass).*/-uint32_t janas_llm_prefill_block(const struct janas_jns *j);+uint32_t janas_llm_prefill_block(const struct janas_jns *j, uint32_t at_least);/* The model's own layers in j: all of them less an MTP block in the file(qwen35moe: nextn_predict_layers, the last ones). */@@ -222,6 +226,30 @@ struct janas_llm_state *janas_llm_state_read(const struct janas_llm_model *m,FILE *f);/* One token at position pos: janas_llm_model_forward with n = 1. */+/*+ * Pictures in the sequence (model_media.c): the nx * ny embeddings+ * (d_model wide, row-major over the grid) taken for the tokens at+ * at .. at + nx * ny - 1 in place of theirs, with positions for the RoPE+ * of their row and column; the text after a picture goes on from its start+ * plus max(nx, ny), as Qwen3-VL's get_rope_index has it. both_ways: its+ * tokens see one another, the later ones too (Gemma 4 12B and 26B), so+ * a pass must hold the whole picture. _add: 0, or -1 (memory, overlap, no+ * room). _drop: those that begin at or after `from`+ * forgotten (one cut by it as well). _count: how many there are.+ */+int janas_llm_model_media_add(struct janas_llm_model *m, uint32_t at,+ const float *embd, uint32_t nx, uint32_t ny,+ int both_ways);+void janas_llm_model_media_drop(struct janas_llm_model *m, uint32_t from);+/* A pass of n tokens from pos0 shortened so that it does not cut a picture+ whose tokens attend both ways: ending before it, or taking it whole. */+uint32_t janas_llm_model_media_cut(const struct janas_llm_model *m,+ uint32_t pos0, uint32_t n);+uint32_t janas_llm_model_media_count(const struct janas_llm_model *m);+/* The model's compute threads, for work between its passes (a picture's+ encoding). */+struct janas_pool *janas_llm_model_pool(const struct janas_llm_model *m);+int janas_llm_model_decode(struct janas_llm_model *m, int32_t token,uint32_t pos, float *logits);diff --git a/src/llm/model_attn.c b/src/llm/model_attn.cindex 3b0974b..40cfbbc 100644--- a/src/llm/model_attn.c+++ b/src/llm/model_attn.c@@ -318,9 +318,16 @@ void janas_m_attn_core(struct janas_llm_model *m, const struct layer *ly,/* attention takes JANAS_LLM_MAX_BLOCK tokens a call; the keys and valuesof the whole block are in the cache already, and each piece sees thepositions up to its own end, as the block would */+ /* a picture whose tokens see one another: each sees to its end */+ int both = m->n_media && janas_m_media_both(m, pos0, n);+ if (both)+ janas_m_media_last(m, pos0, n, m->att_last);for (uint32_t s = 0; s < n; s += JANAS_LLM_MAX_BLOCK) {uint32_t len =n - s < JANAS_LLM_MAX_BLOCK ? n - s : JANAS_LLM_MAX_BLOCK;+ if (both)+ janas_attn_set_last(ly->swa ? m->attn_swa : m->attn,+ m->att_last + s);janas_attn_run(ly->swa ? m->attn_swa : m->attn, m->compute,m->q + (size_t)s * qd, kc, kcs, vc, vcs, len, pos0 + s,m->att + (size_t)s * qd);diff --git a/src/llm/model_ffn.c b/src/llm/model_ffn.cindex 8d1a634..5545141 100644--- a/src/llm/model_ffn.c+++ b/src/llm/model_ffn.c@@ -127,7 +127,10 @@ static void act_worker(void *arg, int tid, int n_threads)/* deterministic operations only, so that a GPU computing anexpert gives the same bits */uint32_t r = 0;- if (m->a->gelu) {+ if (m->a->nh) { /* relu^2 on up, no gate (Nemotron-H) */+ for (; r < ff; r++)+ h[r] = u[r] > 0.0f ? u[r] * u[r] : 0.0f;+ } else if (m->a->gelu) {#if defined(__x86_64__)if (m->avx2)r = gelu_mul_avx2(gt, u, h, ff);@@ -163,6 +166,7 @@ struct router_job {struct janas_llm_model *m;const uint16_t *w;uint32_t n;+ uint32_t ne; /* the layer's experts: the model's, or fewer (Nemotron-H) */};static float dot_f16_ref(const uint16_t *w, const float *x, uint32_t n)@@ -199,7 +203,7 @@ static void router_worker(void *arg, int tid, int n_threads)struct router_job *r = arg;struct janas_llm_model *m = r->m;uint32_t ne = m->n_expert, dm = m->d_model;- for (uint32_t e = (uint32_t)tid; e < ne; e += (uint32_t)n_threads)+ for (uint32_t e = (uint32_t)tid; e < r->ne; e += (uint32_t)n_threads)for (uint32_t j = 0; j < r->n; j++) {const float *x = m->xn + (size_t)j * dm;const uint16_t *w = r->w + (size_t)e * dm;@@ -250,8 +254,11 @@ static void run_experts(struct janas_llm_model *m,const struct expert_run *runs, uint32_t n_runs){double t_enter = janas_m_exp_tracing() ? now() : 0;- uint32_t dm = m->d_model, ff = m->ff_buf;+ /* dm: the experts' input and output width (the latent one of+ Nemotron-H's mixtures), xs their input */+ uint32_t dm = m->moe_w, ff = m->ff_buf;size_t nbd = dm / JANAS_QK, nbf = ff / JANAS_QK;+ const struct janas_block_q8k *xs = m->moe_xq;struct janas_matvec_task t1[2 * MAX_RUNS], t2[MAX_RUNS];struct janas_neuron_run nr[MAX_RUNS]; /* down by neuron: no product */uint32_t ng = 0, n1 = 0, n2 = 0, nn = 0;@@ -263,7 +270,7 @@ static void run_experts(struct janas_llm_model *m,uint32_t a = runs[r].a, cnt = runs[r].b - runs[r].a;for (uint32_t g = a; g < runs[r].b; g++)memcpy(m->xg + (size_t)g * nbd,- m->xq + (size_t)m->pair_tok[m->order[g]] * nbd,+ xs + (size_t)m->pair_tok[m->order[g]] * nbd,nbd * sizeof(*m->xg));if (nf_run < 0 && m->npu &&janas_m_npu_ffn_begin(m, runs[r].base, runs[r].plane, runs[r].mx,@@ -274,7 +281,7 @@ static void run_experts(struct janas_llm_model *m,}for (uint32_t g = a; g < runs[r].b; g++) {m->glist[ng++] = g;- m->hlen[g] = runs[r].mx[0].rows;+ m->hlen[g] = runs[r].mx[JANAS_JNS_UP].rows;if (m->nskipq)m->nskipq[g] = runs[r].mx[JANAS_JNS_DOWN].type == JANAS_Q6_K_PN;if (m->ana_exp) /* the Ana-Net probe: the pair's expert */@@ -283,6 +290,8 @@ static void run_experts(struct janas_llm_model *m,}for (int p = 0; p < 2; p++) {const struct janas_jns_matrix *mx = &runs[r].mx[p];+ if (mx->type == 0)+ continue; /* no gate */t1[n1++] = (struct janas_matvec_task){.type = (int)mx->type,.w = runs[r].base[p] + mx->offset,@@ -423,7 +432,7 @@ int janas_m_route_rank(struct janas_llm_model *m, uint32_t l, uint32_t n,return -1;/* xn and router are free between layers: l + 1 norms into xn first */janas_m_norm_quant(m, f32(m, ly->ffn_norm), 0, n);- struct router_job rj = {m, ly->router16, n};+ struct router_job rj = {m, ly->router16, n, ne};janas_pool_run(m->compute, router_worker, &rj); /* 512 x 2048 a layer */for (uint32_t j = 0; j < n; j++) {float *r = m->router + (size_t)j * ne;@@ -507,9 +516,48 @@ void janas_m_prefetch_block(struct janas_llm_model *m, uint32_t l, uint32_t n)m->phase[JANAS_PH_ROUTER] += now() - t0;}+/* Nemotron-H's choice for token j: sigmoid scores, the k best by score+ plus the layer's bias, their scores as weights, normalized to one (at+ least 2^-14 for the sum, as llama.cpp), times the model's scale. */+static void route_sigmoid(struct janas_llm_model *m, const struct layer *ly,+ uint32_t j)+{+ uint32_t ne = ly->n_exp, k = m->k_use;+ float *r = m->router + (size_t)j * m->n_expert, sel[4096];+ for (uint32_t e = 0; e < ne; e++) {+ r[e] = sigmoid(r[e]);+ sel[e] = r[e] + ly->exp_bias[e];+ }+ float wsum = 0.0f;+ for (uint32_t i = 0; i < k; i++) {+ uint32_t best = 0;+ float bv = -INFINITY;+ for (uint32_t e = 0; e < ne; e++)+ if (sel[e] > bv) {+ bv = sel[e];+ best = e;+ }+ sel[best] = -INFINITY; /* taken */+ uint32_t p = j * k + i;+ m->pair_tok[p] = j;+ m->pair_exp[p] = best;+ m->pair_w[p] = r[best];+ wsum += r[best];+ }+ if (wsum < 6.103515625e-5f)+ wsum = 6.103515625e-5f;+ for (uint32_t i = 0; i < k; i++)+ m->pair_w[j * k + i] = m->pair_w[j * k + i] / wsum * m->route_scale;+}+/* The router's choice for token j: softmax, top-k, normalized weights. */-static void route_tok(struct janas_llm_model *m, uint32_t j)+static void route_tok(struct janas_llm_model *m, const struct layer *ly,+ uint32_t j){+ if (ly && ly->exp_bias) {+ route_sigmoid(m, ly, j);+ return;+ }uint32_t ne = m->n_expert, k = m->k_use;float *r = m->router + (size_t)j * ne, mx = -INFINITY, sum = 0.0f;for (uint32_t e = 0; e < ne; e++)@@ -543,7 +591,7 @@ static void route_worker(void *arg, int tid, int n_threads){struct tok_job *t = arg;for (uint32_t j = (uint32_t)tid; j < t->n; j += (uint32_t)n_threads)- route_tok(t->m, j);+ route_tok(t->m, t->ly, j);}/* The experts' outputs added to the tokens: each token takes its own pairs@@ -559,12 +607,12 @@ static void axpy_worker(void *arg, int tid, int n_threads){struct axpy_job *a = arg;struct janas_llm_model *m = a->m;- uint32_t dm = m->d_model;+ uint32_t w = m->moe_w;for (uint32_t j = (uint32_t)tid; j < a->n; j += (uint32_t)n_threads)for (uint32_t c = a->first[j]; c < a->first[j + 1]; c++) {uint32_t g = a->list[c];- janas_axpy_f32(m->x + (size_t)j * dm, m->pair_w[m->order[g]],- m->dout + (size_t)g * dm, dm);+ janas_axpy_f32(m->moe_out + (size_t)j * w, m->pair_w[m->order[g]],+ m->dout + (size_t)g * w, w);}}@@ -591,9 +639,19 @@ static uint32_t skip_misses(struct janas_llm_model *m,wmax[m->pair_exp[p]] = 0.0f;mark[m->pair_exp[p]] = 0;}- for (uint32_t p = 0; p < np; p++)- if (m->pair_w[p] > wmax[m->pair_exp[p]])- wmax[m->pair_exp[p]] = m->pair_w[p];+ /* a weight against its token's sum: the router's may sum to a scale+ (Nemotron-H's 5.0), not to one */+ float tot = 0.0f;+ for (uint32_t p = 0; p < np; p++) {+ if (p % k == 0) {+ tot = 0.0f;+ for (uint32_t i = 0; i < k; i++)+ tot += m->pair_w[p + i];+ }+ float w = tot > 0.0f ? m->pair_w[p] / tot : 0.0f;+ if (w > wmax[m->pair_exp[p]])+ wmax[m->pair_exp[p]] = w;+ }uint32_t light[256];uint8_t in[256];size_t nl = 0;@@ -615,17 +673,20 @@ static uint32_t skip_misses(struct janas_llm_model *m,}uint32_t q = 0;for (uint32_t j = 0; j < n; j++) {- float kept = 0.0f;- for (uint32_t i = 0; i < k; i++)+ float kept = 0.0f, all = 0.0f;+ for (uint32_t i = 0; i < k; i++) {+ all += m->pair_w[j * k + i];if (mark[m->pair_exp[j * k + i]] != 2)kept += m->pair_w[j * k + i];+ }+ /* the weights left brought back to the sum the router gave */for (uint32_t i = 0; i < k; i++) {uint32_t p = j * k + i;if (mark[m->pair_exp[p]] == 2)continue;m->pair_tok[q] = j;m->pair_exp[q] = m->pair_exp[p];- m->pair_w[q] = m->pair_w[p] / kept;+ m->pair_w[q] = m->pair_w[p] / kept * all;q++;}}@@ -644,18 +705,43 @@ int janas_m_moe_block(struct janas_llm_model *m, const struct layer *ly,double t0 = now();uint32_t dm = m->d_model, ne = m->n_expert;uint32_t k = part == MOE_SHARED ? 0 : m->k_use;+ /* the experts' input and where their weighted outputs go: the block+ and x, or (Nemotron-H) the block brought down to the latent width+ and a sum there, brought up and added to x at the end */+ m->moe_w = dm;+ m->moe_xq = m->xq;+ m->moe_out = m->x;+ if (ly->lat_down) {+ uint32_t L = m->moe_lat;+ struct janas_matvec_task t = {.type = (int)ly->lat_down->type,+ .w = data(m, ly->lat_down),+ .x = m->xq,+ .y = m->lat,+ .rows = L,+ .cols = dm,+ .n_vec = n};+ janas_m_products(m, janas_m_gpu(m, 1), &t, 1);+ for (uint32_t j = 0; j < n; j++)+ janas_q8k_quantize(m->lat + (size_t)j * L,+ m->latq + (size_t)j * (L / JANAS_QK), L);+ memset(m->lat_acc, 0, (size_t)n * L * sizeof(float));+ m->moe_w = L;+ m->moe_xq = m->latq;+ m->moe_out = m->lat_acc;+ }uint32_t np = n * k;double t_route = 0;if (k) {- struct router_job rj = {m, ly->router16, n};+ struct router_job rj = {m, ly->router16, n,+ ly->n_exp ? ly->n_exp : m->n_expert};janas_pool_run(m->compute, router_worker, &rj);if (janas_m_exp_tracing())t_route = now();/* per token: softmax, top-k, normalized weights */- struct tok_job tj = {.m = m, .n = n};+ struct tok_job tj = {.m = m, .ly = ly, .n = n};if (n < 2)- route_tok(m, 0);+ route_tok(m, ly, 0);elsejanas_pool_run(m->compute, route_worker, &tj);/* an expert's own output scale, folded into its weight (Gemma 4) */@@ -664,7 +750,7 @@ int janas_m_moe_block(struct janas_llm_model *m, const struct layer *ly,m->pair_w[p] *= ly->exp_scale[m->pair_exp[p]];if (m->route_fn && cache == m->cache)m->route_fn(m->route_ctx, l, n, k, m->pair_exp);- /* the weights must be the router's own, summing to one a token */+ /* the weights must be the router's own, the same sum each token */if (m->miss_skip > 0 && cache && !ly->exp_scale)np = skip_misses(m, cache, l, n, k);}@@ -707,8 +793,15 @@ int janas_m_moe_block(struct janas_llm_model *m, const struct layer *ly,/* experts in chunks: compute those in RAM while the others load; theshared expert, resident, goes with the first of them */- for (uint32_t c0 = 0; c0 < nd || (c0 == 0 && nt > np); c0 += EXPERT_CHUNK) {- uint32_t cn = nd - c0 < EXPERT_CHUNK ? nd - c0 : EXPERT_CHUNK;+ /* a request is at most as many experts as the cache has slots (a+ small one: fewer than EXPERT_CHUNK) */+ uint32_t chunk = EXPERT_CHUNK;+ if (cache && janas_expert_cache_slots(cache) < chunk)+ chunk = (uint32_t)janas_expert_cache_slots(cache);+ if (!chunk)+ return -1;+ for (uint32_t c0 = 0; c0 < nd || (c0 == 0 && nt > np); c0 += chunk) {+ uint32_t cn = nd - c0 < chunk ? nd - c0 : chunk;const uint8_t *slots[EXPERT_CHUNK];uint8_t ready[EXPERT_CHUNK];if (cn) {@@ -756,15 +849,16 @@ int janas_m_moe_block(struct janas_llm_model *m, const struct layer *ly,if (nr)run_experts(m, runs, nr);}- if (nd <= c0 + EXPERT_CHUNK)+ if (nd <= c0 + chunk)break;}if (n < 2) {EX_T(ex_axpy, for (uint32_t g = 0; g < nt; g++) {uint32_t p = m->order[g];- janas_axpy_f32(m->x + (size_t)m->pair_tok[p] * dm, m->pair_w[p],- m->dout + (size_t)g * dm, dm);+ janas_axpy_f32(m->moe_out + (size_t)m->pair_tok[p] * m->moe_w,+ m->pair_w[p], m->dout + (size_t)g * m->moe_w,+ m->moe_w);});} else {/* the rows of each token, in increasing order: a counting sort */@@ -781,6 +875,25 @@ int janas_m_moe_block(struct janas_llm_model *m, const struct layer *ly,struct axpy_job aj = {m, first, list, n};EX_T(ex_axpy, janas_pool_run(m->compute, axpy_worker, &aj));}+ if (ly->lat_down) {+ uint32_t L = m->moe_lat;+ for (uint32_t j = 0; j < n; j++)+ janas_q8k_quantize(m->lat_acc + (size_t)j * L,+ m->latq + (size_t)j * (L / JANAS_QK), L);+ struct janas_matvec_task t = {.type = (int)ly->lat_up->type,+ .w = data(m, ly->lat_up),+ .x = m->latq,+ .y = m->xn,+ .rows = dm,+ .cols = L,+ .n_vec = n};+ janas_m_products(m, janas_m_gpu(m, 1), &t, 1);+ for (size_t i = 0; i < (size_t)n * dm; i++)+ m->x[i] += m->xn[i];+ m->moe_w = dm;+ m->moe_xq = m->xq;+ m->moe_out = m->x;+ }m->phase[JANAS_PH_EXPERTS] += now() - t1;return 0;}diff --git a/src/llm/model_gpu.c b/src/llm/model_gpu.cindex dc63c55..2f39231 100644--- a/src/llm/model_gpu.c+++ b/src/llm/model_gpu.c@@ -453,8 +453,7 @@ static int sync_state(struct janas_llm_model *m, int to_device)}if (m->a->rec) {size_t L = JANAS_LLM_MAX_BLOCK,- sb = 2 * (size_t)m->n_rec * m->n_vh * m->ds * m->ds *- sizeof(uint16_t),+ sb = 2 * (size_t)m->n_rec * m->ssm_per * sizeof(uint16_t),cb = 2 * (size_t)m->n_rec * m->conv_ch * (m->d_conv - 1) *sizeof(float),lm = (size_t)m->n_rec * L * m->conv_ch * sizeof(float),@@ -524,8 +523,7 @@ static int build(struct janas_llm_model *m, char *err, size_t len)if (!janas_gpu_shares_host(m->gpu) && !m->gtok_dev) {if (m->a->rec && !m->gtok_rec_in) {size_t L = JANAS_LLM_MAX_BLOCK,- sb = 2 * (size_t)m->n_rec * m->n_vh * m->ds * m->ds *- sizeof(uint16_t),+ sb = 2 * (size_t)m->n_rec * m->ssm_per * sizeof(uint16_t),cb = 2 * (size_t)m->n_rec * m->conv_ch * (m->d_conv - 1) *sizeof(float),lm = (size_t)m->n_rec * L * m->conv_ch * sizeof(float),@@ -849,6 +847,9 @@ int janas_m_gpu_token(struct janas_llm_model *m, uint32_t pos, uint32_t n,{if (m->gtok_state < 0)return -1;+ /* a picture whose tokens attend both ways: the CPU's attention */+ if (m->n_media && janas_m_media_both(m, pos, n))+ return -1;/* JANAS_GPU_TOKEN=1: tokens, and blocks with JANAS_GPU_BLOCK=1. With thefast prompt a prompt's blocks (from JANAS_GPU_FAST_N tokens) comehere by themselves, JANAS_GPU_BLOCK=0 aside: the whole block in onediff --git a/src/llm/model_impl.h b/src/llm/model_impl.hindex f1aa6e5..161f96f 100644--- a/src/llm/model_impl.h+++ b/src/llm/model_impl.h@@ -85,8 +85,21 @@ struct layer {/* qwen4exp's sparse attention (model_qsa.c): the indexer's query and keyprojections (bf16) and their norms */const struct janas_jns_tensor *ix_q, *ix_k, *ix_qn, *ix_kn;+ /* Nemotron-H (model_nh.c): what the layer is (JANAS_NH_*); a Mamba-2+ mixer's conv bias, D, and its projections' scales (the file's+ per-tensor ones of NVFP4); a mixture's experts (the router's rows),+ their choice's bias, the latent projections, its shared expert+ (up, down: relu^2, no gate) */+ int nh_kind;+ const float *conv_b, *ssm_dd;+ float in_scale, out_scale2;+ uint32_t n_exp;+ const float *exp_bias;+ const struct janas_jns_tensor *lat_down, *lat_up, *sh_up, *sh_down;};+enum { JANAS_NH_MAMBA, JANAS_NH_ATTN, JANAS_NH_MOE };+_Static_assert(JANAS_ATTN_MAX_BLOCK == JANAS_LLM_MAX_BLOCK, "block size");struct janas_llm_model {@@ -155,6 +168,19 @@ struct janas_llm_model {uint32_t head_swa, n_rot_swa, n_kv_swa, window, hd_max, kvd_max;float rope_base_swa, softcap;const float *rope_freqs;+ /* pictures in the sequence (model_media.c): where each begins, its+ grid of tokens and their embeddings; the multimodal RoPE's sections+ (rope.dimension_sections, Qwen3.5: 11, 11, 10; 0: none), which give+ a picture's tokens their row and column as positions; both: its+ tokens attend to one another both ways; att_last, a pass's last+ positions seen (blk) when one is */+ struct janas_media {+ uint32_t at, nx, ny;+ int both;+ float *embd;+ } *media;+ uint32_t n_media, mrope[3];+ uint32_t *att_last;/* Gemma 4's per-layer embeddings (0: none): ple_dim numbers per layerand token, from a table of their own read a row at a time from thefile (ple_tok, never in memory) and a projection of the embedding@@ -185,6 +211,23 @@ struct janas_llm_model {heads, n_vh value heads (n_vh / n_kh per key head), conv over theq|k|v channels */uint32_t d_conv, ds, n_kh, n_vh, d_inner, conv_ch, qkvz_dim;+ /* the recurrent state of a layer, in uint16 units (each buffer of+ ssm_buf holds n_rec of them): DeltaNet's n_vh heads of ds x ds at+ sixteen bits; Mamba-2's (JANAS_REC_MAMBA2) n_vh heads of ssm_hd rows+ of ds, in float, two units each */+ size_t ssm_per;+ uint32_t ssm_hd;+ /* Nemotron-H's mixtures (model_nh.c, model_ffn.c): the latent width+ (0: none), the weights' scale, and per call the experts' input and+ where their weighted outputs go - d_model, xq and x, or the latent+ ones; lat the latent input, lat_acc the sum, latq either quantized;+ sh_h and sh_hq the shared expert's rows */+ uint32_t moe_lat, moe_w;+ float route_scale;+ const struct janas_block_q8k *moe_xq;+ float *moe_out, *lat, *lat_acc, *sh_h;+ struct janas_block_q8k *latq, *sh_hq;+ uint32_t sh_ff;int avx2; /* AVX2 and FMA: the vector kernels of this file */int f16c;int trace; /* JANAS_TRACE=1: print the state after every layer */@@ -554,6 +597,19 @@ struct tok_job {};void janas_m_init_tuner(struct janas_llm_model *m);+/* model_media.c: the embedding of a picture's token at sequence position+ pos (NULL: not a picture's), the three RoPE positions of any (time, row,+ column), and the pictures forgotten */+const float *janas_m_media_row(const struct janas_llm_model *m, uint32_t pos);+void janas_m_media_pos(const struct janas_llm_model *m, uint32_t pos,+ float p3[3]);+void janas_m_media_free(struct janas_llm_model *m);+/* 1 when a picture whose tokens attend both ways is among the n tokens+ from pos0; then janas_m_media_last fills the last position each sees */+int janas_m_media_both(const struct janas_llm_model *m, uint32_t pos0,+ uint32_t n);+void janas_m_media_last(const struct janas_llm_model *m, uint32_t pos0,+ uint32_t n, uint32_t *last);/* Memory the GPU will import collapsed into 2 MB pages (Linux; seemodel_load.c), before the import pins it. *//* The Ana-Net probe (model_ana.c, JANAS_ANA_*): set up at load (0, or -1@@ -736,7 +792,7 @@ int janas_m_assist_draft(struct janas_llm_model *m, int32_t token,const float *h, uint32_t p, float *hnext, int32_t *out,float *conf);int janas_m_ple_inputs(struct janas_llm_model *m, const int32_t *tokens,- uint32_t n);+ uint32_t n, uint32_t pos0);/* y (n rows of rows) = scale w x (n rows of cols): w bf16, rows of cols,x already at bf16 (janas_m_round_bf16), as llama.cpp meets a bf16 matrix;each row and token one thread's, the same bits whatever the threads@@ -817,6 +873,17 @@ void janas_m_qsa_shift(struct janas_llm_model *m, uint32_t keep, uint32_t drop,uint32_t n_kv);/* which experts a moe_block call runs */enum { MOE_ALL, MOE_SHARED, MOE_ROUTED };++/* Nemotron-H (model_nh.c, model_mamba.c) */+int janas_m_nh_kind(struct janas_llm_model *m, uint32_t l);+int janas_m_nh_params(struct janas_llm_model *m);+int janas_m_nh_layer_load(struct janas_llm_model *m, uint32_t l,+ struct layer *ly, char *err, size_t err_len);+int janas_m_nh_alloc(struct janas_llm_model *m);+void janas_m_nh_free(struct janas_llm_model *m);+int janas_m_nh_layers(struct janas_llm_model *m, uint32_t n, uint32_t pos0);+void janas_m_mamba_layer(struct janas_llm_model *m, const struct layer *ly,+ uint32_t n);/* The router of layer l applied ahead to the state x (for the predictorprobe, janas_llm_model_route_predictor): 0 or -1. */int janas_m_route_predict(struct janas_llm_model *m, uint32_t l, uint32_t n);diff --git a/src/llm/model_load.c b/src/llm/model_load.cindex de36736..1a567c9 100644--- a/src/llm/model_load.c+++ b/src/llm/model_load.c@@ -7,6 +7,8 @@#define _GNU_SOURCE#include "model.h"+#include "janas/llm.h" /* JANAS_LLM_QUALITY_* */+#include <errno.h>#include <math.h>#include <pthread.h>@@ -63,8 +65,10 @@ static const struct janas_jns_tensor *need(const struct janas_jns *j,ok = ok && t->type == 0;else if (kind == T_F16)ok = ok && t->type == 1;- else- ok = ok && janas_qtype_block_size((int)t->type) != 0;+ else /* the bit planes are the experts' alone: a resident tensor in+ them could be neither unpacked (janas_dequantize) nor read */+ ok = ok && janas_qtype_block_size((int)t->type) != 0 &&+ t->type != JANAS_Q6_K_P && t->type != JANAS_Q6_K_PN;if (!ok) {snprintf(err, err_len, "tensor %s: unexpected type or shape", name);return NULL;@@ -87,6 +91,7 @@ static int load_ssm_params(struct janas_llm_model *m)return -1;m->conv_ch = 2 * m->n_kh * m->ds + m->d_inner;m->qkvz_dim = 2 * m->n_kh * m->ds + 2 * m->d_inner;+ m->ssm_per = (size_t)m->n_vh * m->ds * m->ds;return 0;}@@ -746,6 +751,11 @@ struct janas_llm_model *janas_llm_model_load(const char *path,snprintf(err, err_len, "architecture %.32s not supported", m->j.h.arch);goto fail;}+ if (!m->j.h.n_layer) { /* resident tensors only: a vision encoder */+ snprintf(err, err_len, "%s holds no layers: not a language model",+ path);+ goto fail;+ }m->n_layer = janas_llm_main_layers(&m->j);m->d_model = m->j.h.d_model;m->n_expert = m->j.h.n_expert;@@ -753,21 +763,40 @@ struct janas_llm_model *janas_llm_model_load(const char *path,m->d_ff = m->j.layers[0].m[JANAS_JNS_GATE].rows;uint32_t interval = 4;const uint8_t *swa_pat = NULL;- if (meta_u32(m, "attention.head_count", &m->n_head) ||- (m->a->swa ? load_swa_params(m, &swa_pat)- : meta_u32(m, "attention.head_count_kv", &m->n_head_kv)) ||- meta_u32(m, "attention.key_length", &m->head_dim) ||- meta_f32(m, "attention.layer_norm_rms_epsilon", &m->eps) ||- meta_f32(m, "rope.freq_base", &m->rope_base) || m->k > MAX_K ||- m->n_head_kv == 0 || m->n_head % m->n_head_kv || m->head_dim % 8 ||- m->d_model % JANAS_QK || (m->n_head * m->head_dim) % JANAS_QK ||- (m->d_ff % JANAS_QK && !m->a->hc) ||- (m->a->rec && load_ssm_params(m) != 0)) {+ if (m->a->nh+ ? janas_m_nh_params(m) != 0+ : meta_u32(m, "attention.head_count", &m->n_head) ||+ (m->a->swa ? load_swa_params(m, &swa_pat)+ : meta_u32(m, "attention.head_count_kv",+ &m->n_head_kv)) ||+ meta_u32(m, "attention.key_length", &m->head_dim) ||+ meta_f32(m, "attention.layer_norm_rms_epsilon", &m->eps) ||+ meta_f32(m, "rope.freq_base", &m->rope_base) ||+ m->k > MAX_K || m->n_head_kv == 0 ||+ m->n_head % m->n_head_kv || m->head_dim % 8 ||+ m->d_model % JANAS_QK ||+ (m->n_head * m->head_dim) % JANAS_QK ||+ (m->d_ff % JANAS_QK && !m->a->hc) ||+ (m->a->rec && load_ssm_params(m) != 0)) {snprintf(err, err_len, "missing or inconsistent hyperparameters");goto fail;}if (meta_u32(m, "rope.dimension_count", &m->n_rot) != 0)m->n_rot = m->head_dim;+ /* the multimodal RoPE's sections, where the model has them */+ {+ char key[128];+ uint64_t ns = 0;+ const int32_t *sec = NULL;+ snprintf(key, sizeof(key), "%.32s.rope.dimension_sections",+ m->j.h.arch);+ if (janas_jns_meta_ints(&m->j, key, &ns, &sec) == 0 && ns >= 3)+ for (int i = 0; i < 3; i++) {+ int32_t v;+ memcpy(&v, sec + i, sizeof v);+ m->mrope[i] = v > 0 ? (uint32_t)v : 0;+ }+ }if (m->a->rec)meta_u32(m, "full_attention_interval", &interval);if (m->n_rot == 0 || m->n_rot % 2 || m->n_rot > m->head_dim ||@@ -784,8 +813,10 @@ struct janas_llm_model *janas_llm_model_load(const char *path,/* the experts of a mixture are alike; a dense model's feed-forwardmay widen from one layer to the next (Gemma 4's E2B: 6144, then12288): the buffers take the widest */- for (uint32_t l = 0; l < m->n_layer; l++) {+ for (uint32_t l = 0; l < m->n_layer && !m->a->nh; l++) {uint32_t r = m->j.layers[l].m[JANAS_JNS_GATE].rows;+ const struct janas_jns_matrix *gt = &m->j.layers[l].m[JANAS_JNS_GATE];+ const struct janas_jns_matrix *up = &m->j.layers[l].m[JANAS_JNS_UP];const struct janas_jns_matrix *dn = &m->j.layers[l].m[JANAS_JNS_DOWN];/* by neuron (tools/jns_neurons.c), its rows are the gate's */int by_neuron = dn->type == JANAS_Q6_K_PN;@@ -793,11 +824,14 @@ struct janas_llm_model *janas_llm_model_load(const char *path,matrix's rows end inside a group, which the types in blocks of32 or 64 can do (janas_dot_rows_n) */int narrow = r % JANAS_QK != 0;- if ((r != m->d_ff && m->n_expert > 1) ||+ /* every matrix the shape the buffers are made for: the file says+ the shapes, and only the gate's rows were asked before */+ if (gt->type == 0 || r == 0 || (r != m->d_ff && m->n_expert > 1) ||+ gt->cols != m->d_model || up->rows != r || up->cols != m->d_model ||(narrow &&(!m->a->hc || janas_qtype_row_bytes((int)dn->type, r) == 0)) ||(by_neuron ? dn->rows != r || dn->cols != m->d_model- : dn->rows != m->d_model)) {+ : dn->rows != m->d_model || dn->cols != r)) {snprintf(err, err_len, "layer %u: expert shapes differ", l);goto fail;}@@ -821,6 +855,8 @@ struct janas_llm_model *janas_llm_model_load(const char *path,janas_jns_tensor(&m->j, "per_layer_token_embd.weight")) ||m->ple_tok->dims[0] != (uint64_t)m->n_layer * m->ple_dim ||janas_qtype_block_size((int)m->ple_tok->type) == 0 ||+ m->ple_tok->type == JANAS_Q6_K_P ||+ m->ple_tok->type == JANAS_Q6_K_PN ||!(m->ple_model =janas_jns_tensor(&m->j, "per_layer_model_proj.weight")) ||m->ple_model->type != 30 || m->ple_model->dims[0] != m->d_model ||@@ -927,6 +963,21 @@ struct janas_llm_model *janas_llm_model_load(const char *path,for (uint32_t l = 0; l < m->n_layer; l++) {struct layer *ly = &m->layers[l];ly->rec = m->a->rec && (l + 1) % interval != 0;+ if (m->a->nh) {+ /* one part a layer: only the Mamba-2 ones keep a state, only+ the attentions keys and values */+ ly->nh_kind = janas_m_nh_kind(m, l);+ ly->rec = ly->nh_kind == JANAS_NH_MAMBA;+ ly->hd = m->head_dim;+ ly->n_kv = (uint32_t)m->n_head_kv;+ ly->n_rot = 0; /* no RoPE */+ ly->slot = ly->rec ? m->n_rec+++ : ly->nh_kind == JANAS_NH_ATTN ? m->n_attn+++ : UINT32_MAX;+ if (janas_m_nh_layer_load(m, l, ly, err, err_len) != 0)+ goto fail;+ continue;+ }if (swa_pat) {ly->swa = swa_pat[l] != 0;ly->hd = ly->swa ? m->head_swa : m->head_dim;@@ -988,7 +1039,7 @@ struct janas_llm_model *janas_llm_model_load(const char *path,const char *kv = getenv("JANAS_KV");m->kv16 = kv ? atoi(kv) == 16 : m->a->kv16;}- m->blk = janas_llm_prefill_block(&m->j);+ m->blk = janas_llm_prefill_block(&m->j, o ? o->min_block : 0);/* the sliding-window layers keep a ring of positions, not the wholecontext: the window and a prefill block (JANAS_SWA_RING=0 keepsevery position, as the full layers do) */@@ -1014,7 +1065,8 @@ struct janas_llm_model *janas_llm_model_load(const char *path,}for (uint32_t l = 0; l <= m->n_layer; l++) {const struct layer *ly = l < m->n_layer ? &m->layers[l] : &m->mtp_layer;- if (ly->rec || (l == m->n_layer && !m->mtp))+ if (ly->rec || (l == m->n_layer && !m->mtp) ||+ (m->a->nh && l < m->n_layer && ly->nh_kind == JANAS_NH_MOE))continue;m->kv_hd[ly->slot] = ly->hd;m->kv_heads[ly->slot] = ly->n_kv;@@ -1143,7 +1195,10 @@ struct janas_llm_model *janas_llm_model_load(const char *path,memory, since then nothing is read while generating anyway;otherwise four bits, which read a third to a half fewer bytesand cost five to seven positions in four hundred */- bits = o->cache_bytes >= m->j.h.experts_bytes ? 6 : 4;+ bits = o->cache_bytes >= m->j.h.experts_bytes ||+ o->min_quality >= JANAS_LLM_QUALITY_HIGH+ ? 6+ : 4;m->exp_auto = 1;}m->exp_level = bits == 2 ? 1 : bits == 4 ? 2 : 3;@@ -1170,7 +1225,8 @@ struct janas_llm_model *janas_llm_model_load(const char *path,*/if (o->cache_bytes && m->j.h.experts_bytes) {double cover = (double)o->cache_bytes / (double)m->j.h.experts_bytes;- if (cover < 0.20) {+ /* below MEDIUM only: the caller's floor (min_quality) */+ if (cover < 0.20 && o->min_quality < JANAS_LLM_QUALITY_MEDIUM) {m->miss_skip = (cover < 0.10 ? 0.95f : 0.8f) / (float)m->k;m->k_auto = 1;}@@ -1203,13 +1259,21 @@ struct janas_llm_model *janas_llm_model_load(const char *path,goto fail;}}+ /* the experts' input and output: the model's width, the block, x+ (Nemotron-H's mixtures switch to their latent ones for a call) */+ m->moe_w = dm;+ m->moe_xq = m->xq;+ m->moe_out = m->x;+ if (m->a->nh && janas_m_nh_alloc(m) != 0) {+ snprintf(err, err_len, "out of memory");+ goto fail;+ }if (m->a->hc && (janas_m_hc_alloc(m, err, err_len) != 0 ||janas_m_qsa_load(m, err, err_len) != 0))goto fail;if (m->a->rec) {/* the GPU reads and writes them too (gpu_token.c): imported */- size_t sb = 2 * (size_t)m->n_rec * m->n_vh * m->ds * m->ds *- sizeof(uint16_t),+ size_t sb = 2 * (size_t)m->n_rec * m->ssm_per * sizeof(uint16_t),cb = 2 * (size_t)m->n_rec * m->conv_ch * (m->d_conv - 1) *sizeof(float);m->ssm_buf = (uint16_t *)out_alloc(m, sb);@@ -1444,6 +1508,8 @@ fail:void janas_llm_model_free(struct janas_llm_model *m){+ if (m)+ janas_m_media_free(m);if (janas_m_exp_tracing())janas_m_exp_report();if (m && getenv("JANAS_GPU_VERBOSE") &&@@ -1496,6 +1562,7 @@ void janas_llm_model_free(struct janas_llm_model *m)if (!m->resident_gpu)janas_aligned_free(m->resident);janas_m_hc_free(m);+ janas_m_nh_free(m);janas_m_qsa_free(m);if (m->layers)for (uint32_t l = 0; l < m->n_layer; l++) {@@ -1564,6 +1631,10 @@ void janas_llm_model_free(struct janas_llm_model *m)free(m->vs_ids);free(m->vs_y);free(m->mtp_layer.sh_gate);+ free(m->mtp_layer.router_own);+ free(m->mtp_layer.conv_t);+ free(m->mtp_layer.alpha_own);+ free(m->mtp_layer.beta_own);janas_jns_close(&m->j);free(m);}@@ -1816,7 +1887,7 @@ int janas_m_gpu_import(struct janas_llm_model *m, const uint8_t *p,* others. Checked after each pass until then; one change of a slot later* (a new level of bits) and the GPU's copy is no longer used.*/-uint32_t janas_llm_prefill_block(const struct janas_jns *j)+uint32_t janas_llm_prefill_block(const struct janas_jns *j, uint32_t at_least){/* 512 where the GPU takes the feed-forwards whole: a mixture's experts(gpu.c experts_id) get some 32 tokens each, not 16, and fill its@@ -1843,6 +1914,8 @@ uint32_t janas_llm_prefill_block(const struct janas_jns *j)}const char *pb = getenv("JANAS_PREFILL_BLOCK");long v = pb ? atol(pb) : fast ? 512 : 256;+ if (v < (long)at_least)+ v = (long)at_least;return v < JANAS_LLM_MAX_BLOCK ? JANAS_LLM_MAX_BLOCK: v > 4096 ? 4096: (uint32_t)v;diff --git a/src/llm/model_mamba.c b/src/llm/model_mamba.cnew file mode 100644index 0000000..abd1c36--- /dev/null+++ b/src/llm/model_mamba.c@@ -0,0 +1,223 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * model_mamba.c - the Mamba-2 layers of Nemotron-H (model_nh.c), as+ * llama.cpp's build_mamba2_layer computes them.+ *+ * ssm_in brings the normalized input to z (d_inner), xBC (the conv's+ * channels: x, d_inner, then B and C, n_group x d_state each) and dt (a+ * number a head), each times the matrix's own scale. A causal conv of+ * d_conv taps with a bias, then SiLU, runs over xBC. Head h (of n_vh, each+ * ssm_hd numbers of x, in group g = h / (n_vh / n_group)) keeps a state of+ * ssm_hd rows of d_state:+ *+ * dt' = softplus(dt + dt_bias), dA = exp(dt' A)+ * s[r][i] = s[r][i] dA + B[i] x[r] dt', y[r] = sum_i s[r][i] C[i]+ *+ * then y + D x, times SiLU(z), an RMS norm (with weights) over each group+ * of d_inner / n_group numbers, and ssm_out, times its scale.+ *+ * The state is in float, in ssm_buf (two uint16 units a number), with the+ * two buffers, the replay of logged tokens and the log of DeltaNet's+ * layers (model_rec.c): log_mixed holds xBC before the conv, log_beta dt'+ * and log_decay dA.+ */+#define _GNU_SOURCE+#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "matvec.h"+#include "vmath.h"++#include "model_impl.h"++struct mb_job {+ struct janas_llm_model *m;+ const struct layer *ly;+ uint32_t n;+};++/* ggml's softplus: x past 20 as it is */+static inline float softplus(float x)+{+ return x > 20.0f ? x : log1pf(expf(x));+}++/* 1. The conv over xBC, item = 8 channels across all the rows (the+ replayed ones, then the call's), as DeltaNet's: the base buffer moves+ past the replayed rows, the call's state goes to the other. */+static void conv_worker(void *arg, int tid, int n_threads)+{+ struct mb_job *a = arg;+ struct janas_llm_model *m = a->m;+ uint32_t cc = m->conv_ch, dc = m->d_conv, rp = m->replay;+ size_t sz = (size_t)m->n_rec * (dc - 1) * cc;+ float *src = m->conv_buf ++ (size_t)(m->rec_cont ? 1 - m->src : m->src) * sz ++ (size_t)a->ly->slot * (dc - 1) * cc;+ float *dst = m->conv_buf + (size_t)(1 - m->src) * sz ++ (size_t)a->ly->slot * (dc - 1) * cc;+ const float *wt = a->ly->conv_t, *bias = a->ly->conv_b;+ for (uint32_t c0 = (uint32_t)tid * 8; c0 < cc;+ c0 += (uint32_t)n_threads * 8)+ for (uint32_t c = c0; c < c0 + 8 && c < cc; c++) {+ float win[MAX_CONV];+ for (uint32_t i = 0; i + 1 < dc; i++)+ win[i] = src[(size_t)i * cc + c];+ for (uint32_t t = 0; t < rp + a->n; t++) {+ if (t == rp && rp > 0)+ for (uint32_t i = 0; i + 1 < dc; i++)+ src[(size_t)i * cc + c] = win[i];+ win[dc - 1] = m->mixed[(size_t)t * cc + c];+ float s = 0.0f;+ for (uint32_t i = 0; i < dc; i++)+ s += win[i] * wt[(size_t)i * cc + c];+ s += bias[c];+ m->conv_out[(size_t)t * cc + c] = s / (1.0f + expf(-s));+ for (uint32_t i = 0; i + 1 < dc; i++)+ win[i] = win[i + 1];+ }+ for (uint32_t i = 0; i + 1 < dc; i++)+ dst[(size_t)i * cc + c] = win[i];+ }+}++/* 2. The scan, item = a head: its state rows stay in one core's cache for+ all the rows of the call. y goes to att (with D x added). */+static void scan_worker(void *arg, int tid, int n_threads)+{+ struct mb_job *a = arg;+ struct janas_llm_model *m = a->m;+ uint32_t nh = m->n_vh, hd = m->ssm_hd, ds = m->ds, cc = m->conv_ch;+ uint32_t hpg = nh / m->n_kh, di = m->d_inner;+ size_t per = (size_t)nh * hd * ds; /* floats a layer */+ size_t sz = (size_t)m->n_rec * per;+ float *base = (float *)(void *)m->ssm_buf;+ const float *dd = a->ly->ssm_dd;+ for (uint32_t h = (uint32_t)tid; h < nh; h += (uint32_t)n_threads) {+ uint32_t g = h / hpg;+ size_t off = (size_t)a->ly->slot * per + (size_t)h * hd * ds;+ float *M =+ base + (size_t)(m->rec_cont ? 1 - m->src : m->src) * sz + off;+ float *D = base + (size_t)(1 - m->src) * sz + off;+ for (uint32_t t = 0; t < m->replay + a->n; t++) {+ if (t == m->replay) {+ if (M != D)+ memcpy(D, M, (size_t)hd * ds * sizeof(float));+ M = D;+ }+ const float *co = m->conv_out + (size_t)t * cc;+ const float *x = co + (size_t)h * hd;+ const float *B = co + di + (size_t)g * ds;+ const float *C = co + di + (size_t)m->n_kh * ds + (size_t)g * ds;+ float dt = m->beta[(size_t)t * nh + h];+ float dA = m->decay[(size_t)t * nh + h];+ float *y = t < m->replay ? NULL+ : m->att + (size_t)(t - m->replay) * di ++ (size_t)h * hd;+ for (uint32_t r = 0; r < hd; r++) {+ float *s = M + (size_t)r * ds;+ float xdt = x[r] * dt, sum = 0.0f;+ for (uint32_t i = 0; i < ds; i++) {+ float st = s[i] * dA + B[i] * xdt;+ sum += st * C[i];+ s[i] = st;+ }+ if (y)+ y[r] = sum + x[r] * dd[h];+ }+ }+ }+}++/* 3. y times SiLU(z), then the RMS norm of each group, item = (token,+ group). */+static void gate_worker(void *arg, int tid, int n_threads)+{+ struct mb_job *a = arg;+ struct janas_llm_model *m = a->m;+ uint32_t ng = m->n_kh, gw = m->d_inner / ng;+ const float *w = f32(m, a->ly->ssm_norm);+ for (uint32_t it = (uint32_t)tid; it < a->n * ng;+ it += (uint32_t)n_threads) {+ uint32_t j = it / ng, g = it % ng;+ float *o = m->att + (size_t)j * m->d_inner + (size_t)g * gw;+ const float *z = m->big + (size_t)j * m->qkvz_dim + (size_t)g * gw;+ for (uint32_t i = 0; i < gw; i++)+ o[i] *= z[i] / (1.0f + expf(-z[i]));+ rms_norm(o, o, w + (size_t)g * gw, gw, m->eps);+ }+}++void janas_m_mamba_layer(struct janas_llm_model *m, const struct layer *ly,+ uint32_t n)+{+ uint32_t dm = m->d_model, nh = m->n_vh, cc = m->conv_ch;+ uint32_t di = m->d_inner, qz = m->qkvz_dim;+ size_t nbi = di / JANAS_QK;+ double ta = now();+ struct janas_matvec_task t = {.type = (int)ly->ssm_in->type,+ .w = data(m, ly->ssm_in),+ .x = m->xq,+ .y = m->big,+ .rows = qz,+ .cols = dm,+ .n_vec = n};+ janas_m_products(m, janas_m_gpu(m, 0), &t, 1);+ if (ly->in_scale != 1.0f)+ for (size_t i = 0; i < (size_t)n * qz; i++)+ m->big[i] *= ly->in_scale;+ uint32_t rp = m->replay;+ size_t L = JANAS_LLM_MAX_BLOCK;+ float *lm = m->log_mixed + (size_t)ly->slot * L * cc;+ float *lb = m->log_beta + (size_t)ly->slot * L * nh;+ float *ld = m->log_decay + (size_t)ly->slot * L * nh;+ /* rows 0 .. rp - 1: the logged tokens to replay, then this call's */+ memcpy(m->mixed, lm, (size_t)rp * cc * sizeof(float));+ memcpy(m->beta, lb, (size_t)rp * nh * sizeof(float));+ memcpy(m->decay, ld, (size_t)rp * nh * sizeof(float));+ const float *dtb = f32(m, ly->dt_bias), *A = f32(m, ly->ssm_a);+ for (uint32_t j = 0; j < n; j++) {+ const float *src = m->big + (size_t)j * qz;+ memcpy(m->mixed + (size_t)(rp + j) * cc, src + di, cc * sizeof(float));+ for (uint32_t h = 0; h < nh; h++) {+ float dt = softplus(src[di + cc + h] + dtb[h]);+ m->beta[(size_t)(rp + j) * nh + h] = dt;+ m->decay[(size_t)(rp + j) * nh + h] = expf(dt * A[h]);+ }+ }+ double tb = now();+ m->phase[JANAS_PH_QKV] += tb - ta;+ trace_row(m, "xBC", m->mixed + (size_t)(rp + n - 1) * cc, cc);+ struct mb_job mj = {m, ly, n};+ janas_pool_run(m->compute, conv_worker, &mj);+ trace_row(m, "conv_out", m->conv_out + (size_t)(rp + n - 1) * cc, cc);+ janas_pool_run(m->compute, scan_worker, &mj);+ trace_row(m, "ssm_y", m->att + (size_t)(n - 1) * di, di);+ janas_pool_run(m->compute, gate_worker, &mj);+ trace_row(m, "ssm_gated", m->att + (size_t)(n - 1) * di, di);+ /* logged for a call that may be taken back; a longer one is not */+ if (n <= JANAS_LLM_MAX_BLOCK) {+ memcpy(lm, m->mixed + (size_t)rp * cc, (size_t)n * cc * sizeof(float));+ memcpy(lb, m->beta + (size_t)rp * nh, (size_t)n * nh * sizeof(float));+ memcpy(ld, m->decay + (size_t)rp * nh, (size_t)n * nh * sizeof(float));+ }+ double tc = now();+ m->phase[JANAS_PH_SSM] += tc - tb;+ for (uint32_t j = 0; j < n; j++)+ janas_q8k_quantize(m->att + (size_t)j * di, m->attq + j * nbi, di);+ struct janas_matvec_task to = {.type = (int)ly->ssm_out->type,+ .w = data(m, ly->ssm_out),+ .x = m->attq,+ .y = m->xn,+ .rows = dm,+ .cols = di,+ .n_vec = n};+ janas_m_products(m, janas_m_gpu(m, 0), &to, 1);+ float os = ly->out_scale2;+ for (size_t i = 0; i < (size_t)n * dm; i++)+ m->x[i] += m->xn[i] * os;+ trace_row(m, "residual", m->x + (size_t)(n - 1) * dm, dm);+ m->phase[JANAS_PH_WO] += now() - tc;+}diff --git a/src/llm/model_media.c b/src/llm/model_media.cnew file mode 100644index 0000000..3dc3563--- /dev/null+++ b/src/llm/model_media.c@@ -0,0 +1,175 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * model_media.c - pictures in a model's sequence (model.h): their+ * embeddings in place of their tokens', and the positions their RoPE+ * angles come from. A picture of nx x ny tokens starting at sequence+ * position `at` has, for its token k, the time position s, the row s ++ * k / nx and the column s + k % nx, s being the position its first token+ * would have as text; the text after it goes on from s + max(nx, ny),+ * so every later position is shifted by max(nx, ny) - nx * ny. The+ * interleaved multimodal RoPE of Qwen3-VL and Qwen3.5 takes, for frequency+ * i of n_rot / 2, the row when i % 3 == 1 and i < 3 * sections[1], the+ * column when i % 3 == 2 and i < 3 * sections[2], the time otherwise:+ * for text the three are equal and nothing changes. A model without the+ * multimodal sections (Gemma 4) numbers a picture's tokens as text.+ */+#include "model_impl.h"++#include <stdlib.h>+#include <string.h>++int janas_llm_model_media_add(struct janas_llm_model *m, uint32_t at,+ const float *embd, uint32_t nx, uint32_t ny,+ int both_ways)+{+ uint64_t n = (uint64_t)nx * ny;+ if (!n || at + n > m->n_ctx || (both_ways && n > m->blk))+ return -1;+ if (both_ways && !m->att_last &&+ !(m->att_last = malloc(sizeof(uint32_t) * m->blk)))+ return -1;+ for (uint32_t i = 0; i < m->n_media; i++) {+ const struct janas_media *p = &m->media[i];+ if (at < p->at + p->nx * p->ny && p->at < at + n)+ return -1; /* overlapping */+ }+ float *e = malloc(sizeof(float) * (size_t)n * m->d_model);+ struct janas_media *t =+ e ? realloc(m->media, sizeof(*t) * (m->n_media + 1)) : NULL;+ if (!t) {+ free(e);+ return -1;+ }+ memcpy(e, embd, sizeof(float) * (size_t)n * m->d_model);+ m->media = t;+ /* in order of their place in the sequence */+ uint32_t k = m->n_media;+ while (k > 0 && m->media[k - 1].at > at) {+ m->media[k] = m->media[k - 1];+ k--;+ }+ m->media[k] = (struct janas_media){+ .at = at, .nx = nx, .ny = ny, .both = both_ways != 0, .embd = e};+ m->n_media++;+ return 0;+}++void janas_llm_model_media_drop(struct janas_llm_model *m, uint32_t from)+{+ uint32_t keep = 0;+ for (uint32_t i = 0; i < m->n_media; i++) {+ struct janas_media *p = &m->media[i];+ if (p->at + p->nx * p->ny <= from)+ m->media[keep++] = *p;+ else+ free(p->embd);+ }+ m->n_media = keep;+ if (!keep) {+ free(m->media);+ m->media = NULL;+ }+}++uint32_t janas_llm_model_media_cut(const struct janas_llm_model *m,+ uint32_t pos0, uint32_t n)+{+ for (uint32_t i = 0; i < m->n_media; i++) {+ const struct janas_media *p = &m->media[i];+ uint32_t end = p->at + p->nx * p->ny;+ if (!p->both || p->at >= pos0 + n || end <= pos0 + n || end <= pos0)+ continue;+ /* cut by the pass: end before it, or (beginning with it) take it+ whole, which media_add made sure fits a pass */+ if (p->at > pos0)+ return p->at - pos0;+ if (end - pos0 <= m->blk)+ return end - pos0;+ }+ return n;+}++uint32_t janas_llm_model_media_count(const struct janas_llm_model *m)+{+ return m->n_media;+}++struct janas_pool *janas_llm_model_pool(const struct janas_llm_model *m)+{+ return m->compute;+}++const float *janas_m_media_row(const struct janas_llm_model *m, uint32_t pos)+{+ for (uint32_t i = 0; i < m->n_media; i++) {+ const struct janas_media *p = &m->media[i];+ if (pos >= p->at && pos < p->at + p->nx * p->ny)+ return p->embd + (size_t)(pos - p->at) * m->d_model;+ }+ return NULL;+}++void janas_m_media_pos(const struct janas_llm_model *m, uint32_t pos,+ float p3[3])+{+ if (!m->mrope[0] && !m->mrope[1] && !m->mrope[2]) {+ p3[0] = p3[1] = p3[2] = (float)pos;+ return;+ }+ int64_t shift = 0;+ for (uint32_t i = 0; i < m->n_media; i++) {+ const struct janas_media *p = &m->media[i];+ uint32_t n = p->nx * p->ny;+ if (pos >= p->at && pos < p->at + n) {+ uint32_t k = pos - p->at;+ float s = (float)((int64_t)p->at + shift);+ p3[0] = s;+ p3[1] = s + (float)(k / p->nx);+ p3[2] = s + (float)(k % p->nx);+ return;+ }+ if (pos >= p->at + n)+ shift += (int64_t)(p->nx > p->ny ? p->nx : p->ny) - (int64_t)n;+ }+ p3[0] = p3[1] = p3[2] = (float)((int64_t)pos + shift);+}++int janas_m_media_both(const struct janas_llm_model *m, uint32_t pos0,+ uint32_t n)+{+ for (uint32_t i = 0; i < m->n_media; i++) {+ const struct janas_media *p = &m->media[i];+ if (p->both && p->at < pos0 + n && pos0 < p->at + p->nx * p->ny)+ return 1;+ }+ return 0;+}++void janas_m_media_last(const struct janas_llm_model *m, uint32_t pos0,+ uint32_t n, uint32_t *last)+{+ for (uint32_t j = 0; j < n; j++)+ last[j] = pos0 + j;+ for (uint32_t i = 0; i < m->n_media; i++) {+ const struct janas_media *p = &m->media[i];+ uint32_t end = p->at + p->nx * p->ny; /* one past its last token */+ if (!p->both || p->at >= pos0 + n || end <= pos0)+ continue;+ /* a picture cut by the pass's end sees as far as the pass goes */+ uint32_t hi = (end < pos0 + n ? end : pos0 + n) - 1;+ for (uint32_t q = p->at > pos0 ? p->at : pos0; q <= hi; q++)+ last[q - pos0] = hi;+ }+}++void janas_m_media_free(struct janas_llm_model *m)+{+ for (uint32_t i = 0; i < m->n_media; i++)+ free(m->media[i].embd);+ free(m->media);+ m->media = NULL;+ m->n_media = 0;+ free(m->att_last);+ m->att_last = NULL;+}diff --git a/src/llm/model_nh.c b/src/llm/model_nh.cnew file mode 100644index 0000000..d8519fa--- /dev/null+++ b/src/llm/model_nh.c@@ -0,0 +1,324 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * model_nh.c - NVIDIA's Nemotron-H with LatentMoE (nemotron_h_moe, the+ * Nemotron 3.5 Bolt): its hyperparameters, its layers and their loop.+ *+ * Every layer is one part with its own RMS norm (attn_norm) and its own+ * residual: a Mamba-2 mixer (model_mamba.c), an attention or a mixture of+ * experts. The file says which by layer: KV heads, an attention; a+ * feed-forward width, a mixture; neither, a Mamba-2 layer.+ *+ * The attention has no RoPE and no norm on q and k: 32 heads of 128 over 2+ * KV heads, scores scaled by 1 / sqrt(128).+ *+ * The mixture: a router of the layer's own number of experts on the+ * normalized input, sigmoid scores; the experts chosen by score plus a+ * bias (exp_probs_b), their weights the scores normalized to one, times+ * expert_weights_scale. The experts work in a latent space: the input+ * brought down to moe_latent_size (ffn_latent_down) before them, their+ * weighted sum brought up (ffn_latent_up) after; each is up and down with+ * relu^2 between them, no gate. A shared expert of the same kind works on+ * the whole input and is added.+ *+ * What is computed is llama.cpp's src/models/nemotron-h.cpp and+ * build_mamba2_layer, the reference the logits are compared against.+ */+#define _GNU_SOURCE+#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "matvec.h"+#include "vmath.h"++#include "model_impl.h"++static int meta_u32(struct janas_llm_model *m, const char *suffix, uint32_t *v)+{+ char key[128];+ snprintf(key, sizeof(key), "%.32s.%s", m->j.h.arch, suffix);+ return janas_jns_meta_u32(&m->j, key, v);+}++static int meta_f32(struct janas_llm_model *m, const char *suffix, float *v)+{+ char key[128];+ snprintf(key, sizeof(key), "%.32s.%s", m->j.h.arch, suffix);+ return janas_jns_meta_f32(&m->j, key, v);+}++/* Element i of an integer array of the architecture's metadata, 0 if+ there is none. */+static uint64_t meta_at(struct janas_llm_model *m, const char *suffix,+ uint64_t i)+{+ char key[128];+ uint64_t v = 0, count = 0;+ snprintf(key, sizeof(key), "%.32s.%s", m->j.h.arch, suffix);+ if (janas_jns_meta_uint_at(&m->j, key, i, &v, &count) != 0)+ return 0;+ return v;+}++int janas_m_nh_kind(struct janas_llm_model *m, uint32_t l)+{+ if (meta_at(m, "attention.head_count_kv", l))+ return JANAS_NH_ATTN;+ if (meta_at(m, "feed_forward_length", l))+ return JANAS_NH_MOE;+ return JANAS_NH_MAMBA;+}++int janas_m_nh_params(struct janas_llm_model *m)+{+ uint32_t heads_kv = 0;+ for (uint32_t l = 0; l < m->n_layer; l++) {+ uint32_t h = (uint32_t)meta_at(m, "attention.head_count_kv", l);+ if (h > heads_kv)+ heads_kv = h;+ }+ m->n_head_kv = heads_kv;+ if (meta_u32(m, "attention.head_count", &m->n_head) ||+ meta_u32(m, "attention.key_length", &m->head_dim) ||+ meta_f32(m, "attention.layer_norm_rms_epsilon", &m->eps) ||+ meta_u32(m, "ssm.conv_kernel", &m->d_conv) ||+ meta_u32(m, "ssm.state_size", &m->ds) ||+ meta_u32(m, "ssm.group_count", &m->n_kh) ||+ meta_u32(m, "ssm.time_step_rank", &m->n_vh) ||+ meta_u32(m, "ssm.inner_size", &m->d_inner) ||+ meta_u32(m, "moe_latent_size", &m->moe_lat) ||+ meta_f32(m, "expert_weights_scale", &m->route_scale))+ return -1;+ /* no RoPE: the rotation tables are made for the head, never used */+ m->n_rot = m->head_dim;+ m->rope_base = 10000.0f;+ if (m->n_head_kv == 0 || m->n_head % m->n_head_kv || m->head_dim % 8 ||+ m->head_dim > 512 || m->d_model % JANAS_QK || m->k > MAX_K ||+ (m->n_head * m->head_dim) % JANAS_QK || m->d_conv < 2 ||+ m->d_conv > MAX_CONV || m->n_vh == 0 || m->n_kh == 0 ||+ m->n_vh % m->n_kh || m->d_inner % m->n_vh || m->d_inner % m->n_kh ||+ m->d_inner % JANAS_QK || m->ds % 8 || m->moe_lat == 0 ||+ m->moe_lat % JANAS_QK)+ return -1;+ m->ssm_hd = m->d_inner / m->n_vh;+ m->conv_ch = m->d_inner + 2 * m->n_kh * m->ds;+ /* z | x B C | dt */+ m->qkvz_dim = m->conv_ch + m->d_inner + m->n_vh;+ /* float states, two uint16 units a number */+ m->ssm_per = (size_t)m->n_vh * m->ssm_hd * m->ds * 2;+ /* the widest expert, as the experts' buffers take it */+ m->d_ff = 0;+ for (uint32_t l = 0; l < m->n_layer; l++) {+ const struct janas_jns_layer *jl = &m->j.layers[l];+ if (!jl->slot_bytes)+ continue;+ const struct janas_jns_matrix *up = &jl->m[JANAS_JNS_UP],+ *dn = &jl->m[JANAS_JNS_DOWN];+ if (up->cols != m->moe_lat || dn->rows != m->moe_lat ||+ dn->cols != up->rows || up->rows % JANAS_QK)+ return -1;+ if (up->rows > m->d_ff)+ m->d_ff = up->rows;+ }+ return m->d_ff ? 0 : -1;+}++/* A tensor of the layer, of type type (0 f32, -1 any the products read)+ and shape d0 x d1 (d1 0: one dimension). */+static const struct janas_jns_tensor *need(struct janas_llm_model *m,+ uint32_t l, const char *suffix,+ int type, uint64_t d0, uint64_t d1,+ char *err, size_t err_len)+{+ char name[96];+ snprintf(name, sizeof(name), "blk.%u.%s", l, suffix);+ const struct janas_jns_tensor *t = janas_jns_tensor(&m->j, name);+ int ok = t && t->dims[0] == d0 && (d1 ? t->dims[1] == d1 : 1);+ if (ok)+ ok = type == 0 ? t->type == 0+ : janas_qtype_block_size((int)t->type) != 0;+ if (!ok)+ snprintf(err, err_len, "tensor %s missing or unexpected", name);+ return ok ? t : NULL;+}++/* The scale of an NVFP4 matrix (the file's name.scale), 1 if none. */+static float scale_of(struct janas_llm_model *m, uint32_t l, const char *suffix)+{+ char name[96];+ snprintf(name, sizeof(name), "blk.%u.%s", l, suffix);+ const struct janas_jns_tensor *t = janas_jns_tensor(&m->j, name);+ return t && t->type == 0 && t->dims[0] == 1 ? f32(m, t)[0] : 1.0f;+}++int janas_m_nh_layer_load(struct janas_llm_model *m, uint32_t l,+ struct layer *ly, char *err, size_t err_len)+{+ uint32_t dm = m->d_model;+#define NEED(var, suffix, type, d0, d1) \+ if (!(var = need(m, l, suffix, type, d0, d1, err, err_len))) \+ return -1;+ NEED(ly->attn_norm, "attn_norm.weight", 0, dm, 0)+ if (ly->nh_kind == JANAS_NH_MAMBA) {+ const struct janas_jns_tensor *cb, *dd;+ NEED(ly->ssm_in, "ssm_in.weight", -1, dm, m->qkvz_dim)+ NEED(ly->conv, "ssm_conv1d.weight", 0, m->d_conv, m->conv_ch)+ NEED(cb, "ssm_conv1d.bias", 0, m->conv_ch, 0)+ NEED(ly->dt_bias, "ssm_dt.bias", 0, m->n_vh, 0)+ NEED(ly->ssm_a, "ssm_a", 0, 1, m->n_vh)+ NEED(dd, "ssm_d", 0, 1, m->n_vh)+ NEED(ly->ssm_norm, "ssm_norm.weight", 0, m->d_inner / m->n_kh, m->n_kh)+ NEED(ly->ssm_out, "ssm_out.weight", -1, m->d_inner, dm)+ ly->conv_b = f32(m, cb);+ ly->ssm_dd = f32(m, dd);+ ly->in_scale = scale_of(m, l, "ssm_in.scale");+ ly->out_scale2 = scale_of(m, l, "ssm_out.scale");+ /* the conv's weights transposed: tap after tap, channels together */+ ly->conv_t = malloc((size_t)m->d_conv * m->conv_ch * sizeof(float));+ if (!ly->conv_t)+ return -1;+ for (uint32_t c = 0; c < m->conv_ch; c++)+ for (uint32_t i = 0; i < m->d_conv; i++)+ ly->conv_t[(size_t)i * m->conv_ch + c] =+ f32(m, ly->conv)[(size_t)c * m->d_conv + i];+ } else if (ly->nh_kind == JANAS_NH_ATTN) {+ uint32_t qd = m->n_head * ly->hd, kvd = ly->n_kv * ly->hd;+ NEED(ly->wq, "attn_q.weight", -1, dm, qd)+ NEED(ly->wk, "attn_k.weight", -1, dm, kvd)+ NEED(ly->wv, "attn_v.weight", -1, dm, kvd)+ NEED(ly->wo, "attn_output.weight", -1, qd, dm)+ } else {+ const struct janas_jns_layer *jl = &m->j.layers[l];+ const struct janas_jns_tensor *eb;+ NEED(ly->gate_inp, "ffn_gate_inp.weight", 0, dm, 0)+ ly->n_exp = (uint32_t)ly->gate_inp->dims[1];+ /* the router's every expert present in the file (an absent one+ would be read from offset 0), and at least the k it chooses */+ int absent = 0;+ for (uint32_t e = 0; e < ly->n_exp && e < m->n_expert; e++)+ absent |= janas_jns_expert_offset(&m->j, l, e) == 0;+ if (!jl->slot_bytes || ly->n_exp == 0 || ly->n_exp > m->n_expert ||+ ly->n_exp < m->k || absent || jl->m[JANAS_JNS_GATE].type != 0) {+ snprintf(err, err_len, "layer %u: unexpected experts", l);+ return -1;+ }+ NEED(eb, "exp_probs_b.bias", 0, ly->n_exp, 0)+ ly->exp_bias = f32(m, eb);+ NEED(ly->lat_down, "ffn_latent_down.weight", -1, dm, m->moe_lat)+ NEED(ly->lat_up, "ffn_latent_up.weight", -1, m->moe_lat, dm)+ NEED(ly->sh_up, "ffn_up_shexp.weight", -1, dm, 0)+ uint32_t sf = (uint32_t)ly->sh_up->dims[1];+ NEED(ly->sh_down, "ffn_down_shexp.weight", -1, sf, dm)+ if (sf % JANAS_QK) {+ snprintf(err, err_len, "layer %u: shared expert's width", l);+ return -1;+ }+ if (sf > m->sh_ff)+ m->sh_ff = sf;+ /* the router in half precision, converted in place, as the other+ mixtures' (model_load.c) */+ uint16_t *h16 = (uint16_t *)data(m, ly->gate_inp);+ const float *w = f32(m, ly->gate_inp);+ for (size_t i = 0; i < (size_t)dm * ly->n_exp; i++)+ h16[i] = janas_fp32_to_fp16(w[i]);+ ly->router16 = h16;+ }+#undef NEED+ return 0;+}++int janas_m_nh_alloc(struct janas_llm_model *m)+{+ size_t B = m->blk, L = m->moe_lat, S = m->sh_ff;+ m->lat = malloc(B * L * sizeof(float));+ m->lat_acc = malloc(B * L * sizeof(float));+ m->latq = malloc(B * L / JANAS_QK * sizeof(*m->latq));+ m->sh_h = malloc(B * S * sizeof(float));+ m->sh_hq = malloc(B * S / JANAS_QK * sizeof(*m->sh_hq));+ return m->lat && m->lat_acc && m->latq && m->sh_h && m->sh_hq ? 0 : -1;+}++void janas_m_nh_free(struct janas_llm_model *m)+{+ free(m->lat);+ free(m->lat_acc);+ free(m->latq);+ free(m->sh_h);+ free(m->sh_hq);+}++static void relu2_worker(void *arg, int tid, int n_threads)+{+ struct tok_job *t = arg;+ struct janas_llm_model *m = t->m;+ uint32_t sf = (uint32_t)t->ly->sh_up->dims[1];+ for (uint32_t j = (uint32_t)tid; j < t->n; j += (uint32_t)n_threads) {+ float *h = m->sh_h + (size_t)j * sf;+ for (uint32_t i = 0; i < sf; i++)+ h[i] = h[i] > 0.0f ? h[i] * h[i] : 0.0f;+ janas_q8k_quantize(h, m->sh_hq + (size_t)j * (sf / JANAS_QK), sf);+ }+}++/* The shared expert on the normalized block (xq), added to x. */+static void shared_expert(struct janas_llm_model *m, const struct layer *ly,+ uint32_t n)+{+ uint32_t dm = m->d_model, sf = (uint32_t)ly->sh_up->dims[1];+ struct janas_matvec_task up = {.type = (int)ly->sh_up->type,+ .w = data(m, ly->sh_up),+ .x = m->xq,+ .y = m->sh_h,+ .rows = sf,+ .cols = dm,+ .n_vec = n};+ janas_m_products(m, janas_m_gpu(m, 1), &up, 1);+ struct tok_job t = {.m = m, .ly = ly, .n = n};+ if (n < 2)+ relu2_worker(&t, 0, 1);+ else+ janas_pool_run(m->compute, relu2_worker, &t);+ struct janas_matvec_task down = {.type = (int)ly->sh_down->type,+ .w = data(m, ly->sh_down),+ .x = m->sh_hq,+ .y = m->xn,+ .rows = dm,+ .cols = sf,+ .n_vec = n};+ janas_m_products(m, janas_m_gpu(m, 1), &down, 1);+ for (size_t i = 0; i < (size_t)n * dm; i++)+ m->x[i] += m->xn[i];+}++int janas_m_nh_layers(struct janas_llm_model *m, uint32_t n, uint32_t pos0)+{+ uint32_t dm = m->d_model;+ for (uint32_t l = 0; l < m->n_layer; l++) {+ const struct layer *ly = &m->layers[l];+ janas_m_norm_quant(m, f32(m, ly->attn_norm), 0, n);+ trace_row(m, "attn_norm", m->xn + (size_t)(n - 1) * dm, dm);+ if (ly->nh_kind == JANAS_NH_MAMBA) {+ janas_m_mamba_layer(m, ly, n);+ } else if (ly->nh_kind == JANAS_NH_ATTN) {+ janas_m_attn_layer(m, ly, n, pos0);+ } else {+ /* the routed experts first, then the shared one: each reads+ the normalized block, which the shared expert's down product+ overwrites */+ if (janas_m_moe_block(m, ly, m->cache, &m->j.layers[l], l, n,+ MOE_ROUTED) != 0)+ return -1;+ shared_expert(m, ly, n);+ }+ if (m->trace) {+ const float *v = m->x + (size_t)(n - 1) * dm;+ double nn = 0;+ for (uint32_t i = 0; i < dm; i++)+ nn += (double)v[i] * v[i];+ fprintf(stderr, "l_out-%-3u norm %10.4f %9.4f %9.4f %9.4f %9.4f\n",+ l, sqrt(nn), v[0], v[1], v[2], v[3]);+ }+ }+ return 0;+}diff --git a/src/llm/model_npu.c b/src/llm/model_npu.cindex 17cffc1..a57bb2f 100644--- a/src/llm/model_npu.c+++ b/src/llm/model_npu.c@@ -14,9 +14,9 @@* experts of a mixture whose experts do not all fit, a few tokens each,* stay on the CPU, which computes them while the NPU works. A matrix is* compiled the first time a block uses it (INT8, a scale a row, from its- * rows dequantized), for blocks of the model's largest;- * JANAS_NPU_CACHE=<dir> keeps the compiled matrices there for the next- * load. Only in the passes whose tuner candidate has the NPU (npu_use):+ * rows dequantized), for blocks of the model's largest, and kept in+ * ~/.cache/janas/npu (JANAS_NPU_CACHE=<dir> elsewhere, empty for none) for+ * the next load: prepared once. Only in the passes whose tuner candidate has the NPU (npu_use):* the tuner and janas-bench weigh it against the CPU and the GPU.** x reaches the NPU as the CPU's products see it (its Q8_K blocks expanded@@ -231,7 +231,16 @@ void janas_m_npu_open(struct janas_llm_model *m)free(n);return;}+ /* the compiled matrices kept across sessions: their preparation is+ paid once, until the cache is deleted - in JANAS_NPU_CACHE, or+ ~/.cache/janas/npu (XDG_CACHE_HOME honoured); JANAS_NPU_CACHE= (empty)+ keeps none */const char *c = getenv("JANAS_NPU_CACHE");+ char dir[1024];+ if (!c && janas_cache_dir(dir, sizeof dir, 1) == 0) {+ strcat(dir, "/npu");+ c = dir;+ }if (c && *c && janas_npu_set_cache(n->u, c))fprintf(stderr, "janas: NPU cache %s not usable\n", c);const char *mn = getenv("JANAS_NPU_MIN_N");@@ -1910,6 +1919,9 @@ int janas_m_npu_layers(struct janas_llm_model *m, uint32_t nt, uint32_t pos0)struct janas_m_npu *n = m->npu;if (!n || !janas_m_npu_takes(m, nt) || (int)nt > n->block || n->lmode < 0)return -1;+ /* a picture whose tokens attend both ways: the CPU's attention */+ if (m->n_media && janas_m_media_both(m, pos0, nt))+ return -1;if (n->lmode == 0 && !n->lg) {const char *e = getenv("JANAS_NPU_LAYERS");if ((e && atoi(e) <= 0) || !layers_fit(m)) {diff --git a/src/llm/model_ple.c b/src/llm/model_ple.cindex dfd9d86..9059150 100644--- a/src/llm/model_ple.c+++ b/src/llm/model_ple.c@@ -356,7 +356,7 @@ static void product(struct janas_llm_model *m, const struct janas_jns_tensor *t,* normalized, plus the table's row times sqrt(ple_dim), all over sqrt(2).*/int janas_m_ple_inputs(struct janas_llm_model *m, const int32_t *tokens,- uint32_t n)+ uint32_t n, uint32_t pos0){uint32_t dm = m->d_model, P = m->ple_dim, W = m->n_layer * P;float *xb = m->xn; /* the embeddings at bf16 */@@ -381,12 +381,15 @@ int janas_m_ple_inputs(struct janas_llm_model *m, const int32_t *tokens,uint8_t *raw = (uint8_t *)(m->ple_row + W); /* the row as read */float es = sqrtf((float)P), is2 = 1.0f / sqrtf(2.0f);for (uint32_t j = 0; j < n; j++) {- off_t at =- (off_t)(m->ple_tok->offset + (uint64_t)tokens[j] * row_bytes);+ /* a picture's token takes the padding's row (id 0), as both the+ reference and llama.cpp do */+ int32_t tok =+ m->n_media && janas_m_media_row(m, pos0 + j) ? 0 : tokens[j];+ off_t at = (off_t)(m->ple_tok->offset + (uint64_t)tok * row_bytes);const uint8_t *src = raw;if (m->ple_ram) /* the table in memory (janas_llm_options) */src = (const uint8_t *)data(m, m->ple_tok) +- (uint64_t)tokens[j] * row_bytes;+ (uint64_t)tok * row_bytes;for (size_t done = 0; !m->ple_ram && done < row_bytes;) {ssize_t r =pread(m->j.fd, raw + done, row_bytes - done, at + (off_t)done);diff --git a/src/llm/model_state.c b/src/llm/model_state.cindex 806edf3..1c2c52a 100644--- a/src/llm/model_state.c+++ b/src/llm/model_state.c@@ -115,7 +115,7 @@ static int copy_hc(struct janas_llm_model *m, struct janas_llm_state *st,static size_t ssm_len(const struct janas_llm_model *m){- return (size_t)m->n_rec * m->n_vh * m->ds * m->ds;+ return (size_t)m->n_rec * m->ssm_per;}static size_t conv_len(const struct janas_llm_model *m)@@ -462,8 +462,12 @@ struct janas_llm_state *janas_llm_state_read(const struct janas_llm_model *m,for (size_t i = 0; i < sizeof(same) / sizeof(same[0]); i++)if (h[same[i]] != want[same[i]])return NULL;+ /* the recurrent log within its buffers, the base within the text */if (h[H_N] > m->n_ctx || h[H_NBUF] > 2 ||- (h[H_NBUF] != 0) != (m->a->rec != 0))+ (h[H_NBUF] != 0) != (m->a->rec != 0) ||+ (h[H_NBUF] == 2 &&+ (h[H_LOG_N] > JANAS_LLM_MAX_BLOCK || h[H_BASE_POS] > h[H_N] ||+ h[H_N] > (uint64_t)h[H_BASE_POS] + h[H_LOG_N])))return NULL;struct janas_llm_state *st = calloc(1, sizeof(*st));if (!st)diff --git a/src/llm/quant.c b/src/llm/quant.cindex cf08c9e..0152e87 100644--- a/src/llm/quant.c+++ b/src/llm/quant.c@@ -14,6 +14,7 @@#include "iq3xxs_grid.h"#include <math.h>+#include <pthread.h>#include <stddef.h>#include <stdlib.h>#include <string.h>@@ -92,6 +93,31 @@ float janas_e8m0_half(uint8_t e)return f;}+static float ue4m3_value(uint8_t x)+{+ if (x == 0 || x == 0x7F)+ return 0.0f;+ int e = (x >> 3) & 15, man = x & 7;+ float raw = e == 0 ? ldexpf((float)man, -9)+ : ldexpf(1.0f + (float)man / 8.0f, e - 7);+ return raw * 0.5f;+}++/* all 256 bytes' values, made once */+static float ue4m3_tab[256];+static pthread_once_t ue4m3_once = PTHREAD_ONCE_INIT;+static void ue4m3_init(void)+{+ for (int i = 0; i < 256; i++)+ ue4m3_tab[i] = ue4m3_value((uint8_t)i);+}++float janas_ue4m3_half(uint8_t x)+{+ pthread_once(&ue4m3_once, ue4m3_init);+ return ue4m3_tab[x];+}+int janas_mxfp4_to_q8_0(const uint8_t *in, struct janas_block_q8_0 *out,size_t n, size_t *tiny){@@ -842,6 +868,44 @@ float janas_mxfp4_dot_ref(const struct janas_block_mxfp4 *w,((acc[1] + acc[5]) + (acc[3] + acc[7]));}+void janas_nvfp4_dequantize(const struct janas_block_nvfp4 *x, float *y,+ size_t n)+{+ for (size_t b = 0; b < n / 64; b++)+ for (int k = 0; k < 4; k++, y += 16) {+ float d = janas_ue4m3_half(x[b].d[k]);+ for (int j = 0; j < 8; j++) {+ y[j] = d * janas_mxfp4_values[x[b].qs[8 * k + j] & 15];+ y[j + 8] = d * janas_mxfp4_values[x[b].qs[8 * k + j] >> 4];+ }+ }+}++/* The lane of a block of 32 activations takes its two sub-blocks in turn,+ each its exact sum times the two scales. */+float janas_nvfp4_dot_ref(const struct janas_block_nvfp4 *w,+ const struct janas_block_q8k *x, size_t nb)+{+ float acc[8] = {0};+ for (size_t b = 0; b < nb; b++)+ for (int k = 0; k < 8; k++) {+ const struct janas_block_nvfp4 *wk = w + 4 * b + k / 2;+ const int8_t *a = x[b].qs32 + 32 * k;+ for (int h = 0; h < 2; h++) {+ int s = 2 * (k % 2) + h;+ const uint8_t *q = wk->qs + 8 * s;+ int32_t dot = 0;+ for (int j = 0; j < 8; j++)+ dot += janas_mxfp4_values[q[j] & 15] * a[16 * h + j] ++ janas_mxfp4_values[q[j] >> 4] * a[16 * h + j + 8];+ acc[k] = fmaf(x[b].d32[k] * janas_ue4m3_half(wk->d[s]),+ (float)dot, acc[k]);+ }+ }+ return ((acc[0] + acc[4]) + (acc[2] + acc[6])) ++ ((acc[1] + acc[5]) + (acc[3] + acc[7]));+}+_Static_assert(sizeof(struct janas_block_q4_0) ==sizeof(struct janas_block_iq4nl) &&offsetof(struct janas_block_q4_0, qs) ==@@ -1630,6 +1694,8 @@ static int use_tiles = 1;/* Resolved before main: the kernels are first called from pool threads. */__attribute__((constructor)) static void dot_kernels_init(void){+ /* NVFP4's scales, before any kernel reads them (not once a row) */+ pthread_once(&ue4m3_once, ue4m3_init);for (int e = 0; e < 256; e++)e8m0_tab[e] = janas_e8m0_half((uint8_t)e);for (uint32_t i = 0; i < 256; i++) {@@ -1731,6 +1797,139 @@ float janas_iq4xs_dot(const struct janas_block_iq4xs *w,return dotxs_impl(w, x, nb);}+#if defined(__x86_64__)+/* NVFP4 with AVX2: a block of 32 activations at a time, its two+ sub-blocks' codes in their order (low nibbles then high, each sub-block's+ eight bytes) through the table, sign and abs into maddubs; each+ sub-block's exact sum, then the lane's two fmas of the reference, in its+ order: the same bits. */+__attribute__((target("avx2,fma"))) static float+nvfp4_dot_avx2(const struct janas_block_nvfp4 *w,+ const struct janas_block_q8k *x, size_t nb)+{+ const __m128i tbl = _mm_loadu_si128((const __m128i *)janas_mxfp4_values);+ const __m128i m4 = _mm_set1_epi8(15);+ const __m256i one = _mm256_set1_epi16(1);+ __m256 acc = _mm256_setzero_ps(); /* lane k: the reference's acc[k] */+ for (size_t b = 0; b < nb; b++) {+ const struct janas_block_nvfp4 *wb = w + 4 * b;+ __m256i p[8];+ for (int k = 0; k < 8; k++) {+ __m128i q =+ _mm_loadu_si128((const __m128i *)(wb[k / 2].qs + 16 * (k % 2)));+ __m128i lo = _mm_shuffle_epi8(tbl, _mm_and_si128(q, m4));+ __m128i hi =+ _mm_shuffle_epi8(tbl, _mm_and_si128(_mm_srli_epi16(q, 4), m4));+ __m256i wv = _mm256_set_m128i(_mm_unpackhi_epi64(lo, hi),+ _mm_unpacklo_epi64(lo, hi));+ __m256i av =+ _mm256_loadu_si256((const __m256i *)(x[b].qs32 + 32 * k));+ p[k] = _mm256_madd_epi16(+ _mm256_maddubs_epi16(_mm256_sign_epi8(wv, wv),+ _mm256_sign_epi8(av, wv)),+ one);+ }+ /* each vector's two halves summed: [A0..A3 | B0..B3] for k 0-3,+ A the first sub-block of block k, B the second */+ __m256i h03 = _mm256_hadd_epi32(_mm256_hadd_epi32(p[0], p[1]),+ _mm256_hadd_epi32(p[2], p[3]));+ __m256i h47 = _mm256_hadd_epi32(_mm256_hadd_epi32(p[4], p[5]),+ _mm256_hadd_epi32(p[6], p[7]));+ __m256 ia =+ _mm256_cvtepi32_ps(_mm256_permute2x128_si256(h03, h47, 0x20));+ __m256 ib =+ _mm256_cvtepi32_ps(_mm256_permute2x128_si256(h03, h47, 0x31));+ __m256 d8 = _mm256_loadu_ps(x[b].d32);+ __m256 sa = _mm256_set_ps(ue4m3_tab[wb[3].d[2]], ue4m3_tab[wb[3].d[0]],+ ue4m3_tab[wb[2].d[2]], ue4m3_tab[wb[2].d[0]],+ ue4m3_tab[wb[1].d[2]], ue4m3_tab[wb[1].d[0]],+ ue4m3_tab[wb[0].d[2]], ue4m3_tab[wb[0].d[0]]);+ __m256 sb = _mm256_set_ps(ue4m3_tab[wb[3].d[3]], ue4m3_tab[wb[3].d[1]],+ ue4m3_tab[wb[2].d[3]], ue4m3_tab[wb[2].d[1]],+ ue4m3_tab[wb[1].d[3]], ue4m3_tab[wb[1].d[1]],+ ue4m3_tab[wb[0].d[3]], ue4m3_tab[wb[0].d[1]]);+ acc = _mm256_fmadd_ps(_mm256_mul_ps(d8, sa), ia, acc);+ acc = _mm256_fmadd_ps(_mm256_mul_ps(d8, sb), ib, acc);+ }+ return janas_hsum8(acc);+}+#endif++#if defined(__x86_64__)+/* The same on g vectors (up to 8) xs blocks apart: the weights decoded+ once a block for all of them, each vector's arithmetic that of+ nvfp4_dot_avx2: the same bits. */+__attribute__((target("avx2,fma"))) static void+nvfp4_dotn_avx2(const struct janas_block_nvfp4 *w,+ const struct janas_block_q8k *x, size_t xs, int g, size_t nb,+ float *r)+{+ const __m128i tbl = _mm_loadu_si128((const __m128i *)janas_mxfp4_values);+ const __m128i m4 = _mm_set1_epi8(15);+ const __m256i one = _mm256_set1_epi16(1);+ __m256 acc[8];+ for (int v = 0; v < g; v++)+ acc[v] = _mm256_setzero_ps();+ for (size_t b = 0; b < nb; b++) {+ const struct janas_block_nvfp4 *wb = w + 4 * b;+ __m256i wv[8], aw[8];+ for (int k = 0; k < 8; k++) {+ __m128i q =+ _mm_loadu_si128((const __m128i *)(wb[k / 2].qs + 16 * (k % 2)));+ __m128i lo = _mm_shuffle_epi8(tbl, _mm_and_si128(q, m4));+ __m128i hi =+ _mm_shuffle_epi8(tbl, _mm_and_si128(_mm_srli_epi16(q, 4), m4));+ wv[k] = _mm256_set_m128i(_mm_unpackhi_epi64(lo, hi),+ _mm_unpacklo_epi64(lo, hi));+ aw[k] = _mm256_sign_epi8(wv[k], wv[k]);+ }+ __m256 sa = _mm256_set_ps(ue4m3_tab[wb[3].d[2]], ue4m3_tab[wb[3].d[0]],+ ue4m3_tab[wb[2].d[2]], ue4m3_tab[wb[2].d[0]],+ ue4m3_tab[wb[1].d[2]], ue4m3_tab[wb[1].d[0]],+ ue4m3_tab[wb[0].d[2]], ue4m3_tab[wb[0].d[0]]);+ __m256 sb = _mm256_set_ps(ue4m3_tab[wb[3].d[3]], ue4m3_tab[wb[3].d[1]],+ ue4m3_tab[wb[2].d[3]], ue4m3_tab[wb[2].d[1]],+ ue4m3_tab[wb[1].d[3]], ue4m3_tab[wb[1].d[1]],+ ue4m3_tab[wb[0].d[3]], ue4m3_tab[wb[0].d[1]]);+ for (int v = 0; v < g; v++) {+ const struct janas_block_q8k *xb = x + (size_t)v * xs + b;+ __m256i p[8];+ for (int k = 0; k < 8; k++)+ p[k] = _mm256_madd_epi16(+ _mm256_maddubs_epi16(+ aw[k], _mm256_sign_epi8(+ _mm256_loadu_si256(+ (const __m256i *)(xb->qs32 + 32 * k)),+ wv[k])),+ one);+ __m256i h03 = _mm256_hadd_epi32(_mm256_hadd_epi32(p[0], p[1]),+ _mm256_hadd_epi32(p[2], p[3]));+ __m256i h47 = _mm256_hadd_epi32(_mm256_hadd_epi32(p[4], p[5]),+ _mm256_hadd_epi32(p[6], p[7]));+ __m256 ia =+ _mm256_cvtepi32_ps(_mm256_permute2x128_si256(h03, h47, 0x20));+ __m256 ib =+ _mm256_cvtepi32_ps(_mm256_permute2x128_si256(h03, h47, 0x31));+ __m256 d8 = _mm256_loadu_ps(xb->d32);+ acc[v] = _mm256_fmadd_ps(_mm256_mul_ps(d8, sa), ia, acc[v]);+ acc[v] = _mm256_fmadd_ps(_mm256_mul_ps(d8, sb), ib, acc[v]);+ }+ }+ for (int v = 0; v < g; v++)+ r[v] = janas_hsum8(acc[v]);+}+#endif++float janas_nvfp4_dot(const struct janas_block_nvfp4 *w,+ const struct janas_block_q8k *x, size_t nb)+{+#if defined(__x86_64__)+ if (use_avx2_fma)+ return nvfp4_dot_avx2(w, x, nb);+#endif+ return janas_nvfp4_dot_ref(w, x, nb);+}+float janas_mxfp4_dot(const struct janas_block_mxfp4 *w,const struct janas_block_q8k *x, size_t nb){@@ -1901,6 +2100,8 @@ size_t janas_qtype_block_size(int type)return 8 * sizeof(struct janas_block_iq4nl);case JANAS_MXFP4:return 8 * sizeof(struct janas_block_mxfp4);+ case JANAS_NVFP4:+ return 4 * sizeof(struct janas_block_nvfp4);case JANAS_Q4_0:return 8 * sizeof(struct janas_block_q4_0);case JANAS_Q5_1:@@ -1955,6 +2156,9 @@ void janas_dequantize(int type, const void *w, float *y, size_t n)case JANAS_MXFP4:janas_mxfp4_dequantize(w, y, n);return;+ case JANAS_NVFP4:+ janas_nvfp4_dequantize(w, y, n);+ return;case JANAS_IQ4_XS:janas_iq4xs_dequantize(w, y, n);return;@@ -2018,6 +2222,8 @@ float janas_dot(int type, const void *w, const struct janas_block_q8k *x,return janas_iq4nl_dot(w, x, nb);case JANAS_MXFP4:return janas_mxfp4_dot(w, x, nb);+ case JANAS_NVFP4:+ return janas_nvfp4_dot(w, x, nb);case JANAS_IQ4_XS:return janas_iq4xs_dot(w, x, nb);case JANAS_Q4_0:@@ -2305,6 +2511,16 @@ void janas_dot_multi(int type, const void *w, const struct janas_block_q8k *x,{size_t v = 0;#if defined(__x86_64__)+ if (type == JANAS_NVFP4 && use_avx2_fma) {+ float r[8];+ for (; v < nv; v += 8) {+ int g = nv - v >= 8 ? 8 : (int)(nv - v);+ nvfp4_dotn_avx2(w, x + v * x_stride, x_stride, g, nb, r);+ for (int i = 0; i < g; i++)+ out[(v + (size_t)i) * out_stride] = r[i];+ }+ return;+ }if (use_avx2_fma && __builtin_cpu_supports("f16c") &&(type == JANAS_Q4_K || type == JANAS_Q6_K || type == JANAS_Q5_K ||type == JANAS_Q8_0 || type == JANAS_IQ4_NL || type == JANAS_IQ4_XS ||@@ -2421,6 +2637,9 @@ size_t janas_qtype_row_bytes(int type, size_t n)case JANAS_MXFP4:sub = 32, bytes = sizeof(struct janas_block_mxfp4);break;+ case JANAS_NVFP4:+ sub = 64, bytes = sizeof(struct janas_block_nvfp4);+ break;case JANAS_Q4_0:sub = 32, bytes = sizeof(struct janas_block_q4_0);break;@@ -2450,6 +2669,14 @@ size_t janas_qtype_row_bytes(int type, size_t n)static void tail_group(int type, const void *row, size_t full, size_t row_bytes,uint8_t *pad){+ /* a shape the type cannot end inside a group with (row_bytes 0, or+ not past the whole groups): the loaders refuse it; a pad of zeros+ here rather than a copy past the row */+ size_t bs = janas_qtype_block_size(type);+ if (row_bytes <= full || row_bytes - full > bs || bs > TAIL_GROUP) {+ memset(pad, 0, TAIL_GROUP);+ return;+ }size_t tb = row_bytes - full;memcpy(pad, (const uint8_t *)row + full, tb);memset(pad + tb, 0, janas_qtype_block_size(type) - tb);diff --git a/src/llm/quant.h b/src/llm/quant.hindex f2002f5..b47f4d9 100644--- a/src/llm/quant.h+++ b/src/llm/quant.h@@ -100,6 +100,22 @@ extern const int8_t janas_mxfp4_values[16];e = 2): an MXFP4 block's scale */float janas_e8m0_half(uint8_t e);+/*+ * NVFP4 (ggml's type 40, NVIDIA's 4-bit format): 64 weights in four+ * sub-blocks of 16, each a 4-bit E2M1 code into janas_mxfp4_values (twice+ * the format's values) times its sub-block's UE4M3 scale (four exponent+ * bits, bias 7, three of mantissa; 0 and 0x7F read as zero). Sub-block s+ * keeps its codes in qs[8 s .. 8 s + 7]: byte j holds weight 16 s + j low+ * and 16 s + j + 8 high. Four blocks cover a Q8_K block.+ */+struct janas_block_nvfp4 {+ uint8_t d[4]; /* UE4M3 */+ uint8_t qs[32];+};+/* A UE4M3 scale times one half, as ggml's ggml_ue4m3_to_fp32 (the half+ pairs with janas_mxfp4_values' doubled values) */+float janas_ue4m3_half(uint8_t x);+/** Q5_1: 32 weights d * q + m, q of five bits: the low four as in Q4_0,* the fifth of weight l in bit l of qh (little-endian).@@ -296,6 +312,7 @@ _Static_assert(sizeof(struct janas_block_q5_1) == 24, "q5_1 block size");_Static_assert(sizeof(struct janas_block_q4_1) == 20, "q4_1 block size");_Static_assert(sizeof(struct janas_block_q5_0) == 22, "q5_0 block size");_Static_assert(sizeof(struct janas_block_q2_0) == 18, "q2_0 block size");+_Static_assert(sizeof(struct janas_block_nvfp4) == 36, "nvfp4 block size");_Static_assert(sizeof(struct janas_block_iq3xxs) == 98, "iq3_xxs block size");_Static_assert(sizeof(struct janas_block_iq2xxs) == 66, "iq2_xxs block size");_Static_assert(sizeof(struct janas_block_iq2xs) == 74, "iq2_xs block size");@@ -329,6 +346,7 @@ enum janas_qtype {JANAS_IQ1_M = 29, /* 56 */JANAS_IQ4_XS = 23,JANAS_MXFP4 = 39, /* sizes per 256 weights: 8 blocks, 136 bytes */+ JANAS_NVFP4 = 40, /* 4 blocks of 64, 144 bytes */JANAS_Q2_0 = 42, /* 4 blocks of 64, 72 bytes *//* Janas' own, outside the GGUF numbering: the high plane of a Q6_K rowcut into bit planes. Its block size counts that plane alone; the other@@ -384,8 +402,9 @@ float janas_dot(int type, const void *w, const struct janas_block_q8k *x,/** One row of n weights (n a multiple of JANAS_QK) of a supported type into- * floats. Aborts on an unsupported type, bit planes among them: check- * janas_qtype_block_size() first. Every caller goes through here, so a type+ * floats. Aborts on an unsupported type, bit planes among them (which+ * janas_qtype_block_size() still sizes: the loaders refuse them for any+ * resident tensor). Every caller goes through here, so a type* the engine reads in its products is read the same way when a row of it has* to be unpacked whole - a token embedding, say.*/@@ -589,6 +608,15 @@ float janas_mxfp4_dot(const struct janas_block_mxfp4 *w,const struct janas_block_q8k *x, size_t nb);float janas_mxfp4_dot_ref(const struct janas_block_mxfp4 *w,const struct janas_block_q8k *x, size_t nb);+/* NVFP4: as llama.cpp's product against Q8_0, sub-block by sub-block: a+ lane per block of 32 activations, each sub-block's exact sum times its+ scale and the block's */+void janas_nvfp4_dequantize(const struct janas_block_nvfp4 *x, float *y,+ size_t n);+float janas_nvfp4_dot(const struct janas_block_nvfp4 *w,+ const struct janas_block_q8k *x, size_t nb);+float janas_nvfp4_dot_ref(const struct janas_block_nvfp4 *w,+ const struct janas_block_q8k *x, size_t nb);void janas_iq4xs_dequantize(const struct janas_block_iq4xs *x, float *y,size_t n);float janas_iq4xs_dot(const struct janas_block_iq4xs *w,diff --git a/src/llm/quant_dotn.h b/src/llm/quant_dotn.hindex 067e80f..a24d229 100644--- a/src/llm/quant_dotn.h+++ b/src/llm/quant_dotn.h@@ -2545,6 +2545,8 @@ DOTN(b32g_fill)(const int TY, const uint8_t *const *w, size_t b0, int n,kb2;DOTN(q2_0_unpack)(blk->qs, &lo[j], &hi[j]);k->d[2 * kb2][j] = k->d[2 * kb2 + 1][j] = _cvtsh_ss(blk->d);+ /* no minimum, but b32g_tile loads the row of them */+ k->m[2 * kb2][j] = k->m[2 * kb2 + 1][j] = 0.0f;}DOTN(gemm_t8)(lo, k->qs[2 * kb2]);DOTN(gemm_t8)(hi, k->qs[2 * kb2 + 1]);@@ -2559,6 +2561,7 @@ DOTN(b32g_fill)(const int TY, const uint8_t *const *w, size_t b0, int n,r[j] = DOTN(b32g_unpack)(TY, blk);if (TY == JANAS_MXFP4) {k->d[kb][j] = e8m0_tab[blk[0]];+ k->m[kb][j] = 0.0f;} else {uint16_t d, m = 0;memcpy(&d, blk, 2);diff --git a/src/llm/schema.c b/src/llm/schema.cindex ede8e11..31597d8 100644--- a/src/llm/schema.c+++ b/src/llm/schema.c@@ -8,9 +8,11 @@#include <stdlib.h>#include <string.h>+#include "schema_impl.h"+#define WS_MAX 20 /* white space bytes in a row */#define DIGITS_MAX 20 /* digits of a number's parts */-#define LEN_MAX 64 /* string lengths enforced up to this */+#define LEN_MAX 4096 /* string lengths enforced up to this */#define ITEMS_MAX 32 /* array bounds enforced up to this */#define DEPTH_MAX 64 /* schemas inside schemas */#define NONE UINT32_MAX@@ -511,14 +513,117 @@ static uint32_t object_rule(struct janas_jsg *j, const struct janas_json *s,return r;}+/* The formats a grammar says, as expressions over the string's inside */+static const struct {+ const char *name, *re;+} FORMATS[] = {+#define DATE "[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])"+#define TIME \+ "([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]{1,9})?" \+ "(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])"+#define OCTET "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"+#define LABEL "[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?"+#define ATOM "[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+"+ {"date", DATE},+ {"time", TIME},+ {"date-time", DATE "T" TIME},+ {"uuid", "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-"+ "[0-9a-fA-F]{12}"},+ {"email", ATOM "(\\." ATOM ")*@" LABEL "(\\." LABEL ")+"},+ {"ipv4", OCTET "(\\." OCTET "){3}"},+#undef DATE+#undef TIME+#undef OCTET+#undef LABEL+#undef ATOM+};++/* A string of format f, quotes included; NONE when it is not one of them. */+static uint32_t format_rule(struct janas_jsg *j, const char *f)+{+ for (size_t i = 0; i < sizeof(FORMATS) / sizeof(FORMATS[0]); i++)+ if (strcmp(f, FORMATS[i].name) == 0) {+ uint32_t close = lit_rule(j, "\"", 1);+ uint32_t in = janas_jsg_regex(j->b, FORMATS[i].re,+ strlen(FORMATS[i].re), close);+ if (in == NONE)+ return NONE;+ uint32_t r = janas_gb_rule(j->b);+ uint32_t a = janas_gb_alt(j->b, r);+ janas_gb_byte(j->b, a, '"');+ janas_gb_ref(j->b, a, in);+ return r;+ }+ return NONE;+}++/* The bounds of a number, NULL-free; 1 when there are any. */+static int number_bounds(const struct janas_json *s,+ struct janas_jsg_bounds *bd)+{+ memset(bd, 0, sizeof(*bd));+ const struct janas_json *v;+ if ((v = janas_json_get(s, "minimum")) && v->type == JANAS_JSON_NUMBER)+ bd->has_lo = 1, bd->lo = janas_json_num(v, 0);+ if ((v = janas_json_get(s, "maximum")) && v->type == JANAS_JSON_NUMBER)+ bd->has_hi = 1, bd->hi = janas_json_num(v, 0);+ /* exclusive ones as numbers (draft 6 on); the stricter bound wins */+ if ((v = janas_json_get(s, "exclusiveMinimum")) &&+ v->type == JANAS_JSON_NUMBER) {+ double x = janas_json_num(v, 0);+ if (!bd->has_lo || x >= bd->lo)+ bd->has_lo = 1, bd->lo = x, bd->lo_x = 1;+ } else if (v && v->type == JANAS_JSON_TRUE && bd->has_lo) {+ bd->lo_x = 1; /* draft 4: a flag on minimum */+ }+ if ((v = janas_json_get(s, "exclusiveMaximum")) &&+ v->type == JANAS_JSON_NUMBER) {+ double x = janas_json_num(v, 0);+ if (!bd->has_hi || x <= bd->hi)+ bd->has_hi = 1, bd->hi = x, bd->hi_x = 1;+ } else if (v && v->type == JANAS_JSON_TRUE && bd->has_hi) {+ bd->hi_x = 1;+ }+ if ((v = janas_json_get(s, "multipleOf")) && v->type == JANAS_JSON_NUMBER &&+ janas_json_num(v, 0) > 0)+ bd->has_mult = 1, bd->mult = janas_json_num(v, 0);+ return bd->has_lo || bd->has_hi || bd->has_mult;+}+/* The schema with one type, t, of those it lists. */static uint32_t typed(struct janas_jsg *j, const struct janas_json *s,const struct janas_json *root, const char *t){if (strcmp(t, "string") == 0) {+ const struct janas_json *f = janas_json_get(s, "format");+ if (f && f->type == JANAS_JSON_STRING) {+ uint32_t fr = format_rule(j, f->s);+ if (fr != NONE) { /* its own length: the bounds not added */+ if (janas_json_get(s, "minLength") ||+ janas_json_get(s, "maxLength") ||+ janas_json_get(s, "pattern"))+ j->relaxed++;+ return fr;+ }+ }+ const struct janas_json *pat = janas_json_get(s, "pattern");+ if (pat && pat->type == JANAS_JSON_STRING) {+ uint32_t close = lit_rule(j, "\"", 1);+ uint32_t in = janas_jsg_pattern(j->b, pat->s, pat->n, close);+ if (in != NONE) { /* the lengths not added to it */+ if (f || janas_json_get(s, "minLength") ||+ janas_json_get(s, "maxLength"))+ j->relaxed++;+ uint32_t r = janas_gb_rule(j->b);+ uint32_t a = janas_gb_alt(j->b, r);+ janas_gb_byte(j->b, a, '"');+ janas_gb_ref(j->b, a, in);+ return r;+ }+ }int lo = bound(s, "minLength", LEN_MAX, 0);int hi = bound(s, "maxLength", LEN_MAX, -1);- if (janas_json_get(s, "pattern") || janas_json_get(s, "format"))+ if (pat || f)j->relaxed++;if (lo == -2 || hi == -2) {j->relaxed++;@@ -533,12 +638,15 @@ static uint32_t typed(struct janas_jsg *j, const struct janas_json *s,return string_rule(j, lo, hi < 0 ? -1 : hi < lo ? lo : hi);}if (strcmp(t, "integer") == 0 || strcmp(t, "number") == 0) {- static const char *const lim[] = {"minimum", "maximum",- "exclusiveMinimum",- "exclusiveMaximum", "multipleOf"};- for (int i = 0; i < 5; i++)- if (janas_json_get(s, lim[i]))- j->relaxed++;+ struct janas_jsg_bounds bd;+ if (number_bounds(s, &bd)) {+ uint32_t r = t[1] == 'n'+ ? janas_jsg_int_bounded(j->b, &bd, &j->relaxed)+ : janas_jsg_real_bounded(j->b, &bd, &j->relaxed);+ if (r != NONE)+ return r;+ j->relaxed++; /* memory: the number unbounded */+ }if (t[1] == 'n') {if (j->integer == NONE)j->integer = number_rule(j, 0);diff --git a/src/llm/schema.h b/src/llm/schema.hindex adaef8e..062c4d0 100644--- a/src/llm/schema.h+++ b/src/llm/schema.h@@ -6,11 +6,14 @@* What a schema says is enforced where a grammar can say it: types, the* properties of an object (in the order the schema lists them, the required* ones always there, no others), array items and small bounds on their- * count, string lengths up to 64, enum and const, anyOf and oneOf, $ref to- * the same document (recursion included), nullable. What it cannot say- * (pattern, format, minimum and maximum, allOf of more than one schema) is- * left out: the value is still valid JSON of the right type, and- * janas_jsg_relaxed counts the keywords so treated.+ * count, string lengths up to 4096, enum and const, anyOf and oneOf, $ref+ * to the same document (recursion included), nullable; minimum, maximum,+ * their exclusive forms and multipleOf (up to 50) on integers, the bounds+ * on numbers (schema_num.c); the formats date, time, date-time, uuid,+ * email and ipv4 (schema_re.c). What it cannot say (pattern, other+ * formats, allOf of more than one schema) is left out: the value is still+ * valid JSON of the right type, and janas_jsg_relaxed counts the keywords+ * so treated.*/#ifndef JANAS_LLM_SCHEMA_H#define JANAS_LLM_SCHEMA_Hdiff --git a/src/llm/schema_impl.h b/src/llm/schema_impl.hnew file mode 100644index 0000000..e05eead--- /dev/null+++ b/src/llm/schema_impl.h@@ -0,0 +1,51 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * schema_impl.h - the parts of schema.c in files of their own: numbers with+ * bounds (schema_num.c) and regular expressions (schema_re.c), as grammar+ * rules.+ */+#ifndef JANAS_LLM_SCHEMA_IMPL_H+#define JANAS_LLM_SCHEMA_IMPL_H++#include <stddef.h>+#include <stdint.h>++#include "grammar.h"++/* minimum / maximum (exclusive: exclusiveMinimum, exclusiveMaximum as+ numbers) and multipleOf, as a schema gives them */+struct janas_jsg_bounds {+ int has_lo, has_hi, lo_x, hi_x, has_mult;+ double lo, hi, mult;+};++/* An integer inside the bounds and a multiple (up to 50; larger, or a+ fraction other than 1/n, counted in *relaxed and not enforced). A rule+ with no alternatives when no integer fits; NONE (UINT32_MAX) on memory. */+uint32_t janas_jsg_int_bounded(struct janas_gbuild *b,+ const struct janas_jsg_bounds *bd, int *relaxed);+/* A number inside the bounds, without exponent; a fraction left out where+ it could pass a bound that is not an integer (>= 1.5 writes 2 and more).+ An integer multipleOf makes it an integer; a fractional one is counted in+ *relaxed. */+uint32_t janas_jsg_real_bounded(struct janas_gbuild *b,+ const struct janas_jsg_bounds *bd,+ int *relaxed);++/*+ * The n bytes of expression expr (character classes, \d \w \s, groups,+ * alternatives, * + ? {m,n}; no anchors, back references or look-arounds)+ * as a rule matching the inside of a JSON string, then going on with rule+ * then. NONE when the expression is not one this reads.+ */+uint32_t janas_jsg_regex(struct janas_gbuild *b, const char *expr, size_t n,+ uint32_t then);+/* The same for a schema's pattern: written from the string's start+ whether anchored with ^ or not (the string then contains it), followed+ by any text unless anchored with $ at the end (anchors elsewhere are not+ read). */+uint32_t janas_jsg_pattern(struct janas_gbuild *b, const char *expr, size_t n,+ uint32_t then);++#endifdiff --git a/src/llm/schema_num.c b/src/llm/schema_num.cnew file mode 100644index 0000000..c28ac81--- /dev/null+++ b/src/llm/schema_num.c@@ -0,0 +1,358 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * schema_num.c - numbers with bounds as grammar rules (see schema_impl.h).+ *+ * An integer between two bounds, and a multiple of m, is a regular language+ * of its digits: read left to right, what matters is whether the digits so+ * far are still those of the lower bound, still those of the upper, and the+ * remainder by m. Each such state is a rule whose alternatives are the+ * digits allowed there (those leading to the same state in one set), so the+ * grammar is as exact as a check after the fact would be, and it never+ * lets a number start that cannot end inside the bounds.+ *+ * A real number with bounds is its integer part with such a rule and a+ * fraction where any fraction keeps it inside: below an integer upper+ * bound any fraction, at it none (5 for <= 5, never 5.2); above an+ * exclusive integer lower bound a fraction that is not all zeros. Where a+ * bound is not an integer the part between it and the next integer is left+ * out (>= 1.5 writes 2 and more): what is written is always valid, though+ * not every valid number can be written. No exponent.+ */+#include "schema_impl.h"++#include <math.h>+#include <stdlib.h>+#include <string.h>++#define NONE UINT32_MAX+#define MAG_DIGITS 18 /* magnitudes below 10^18: an int64 holds them */+#define MAG_MAX 999999999999999999ull+#define MULT_MAX 50 /* multipleOf enforced up to this (states: m each) */+#define FRAC_MAX 19 /* a fraction's digits at most */++/* The digit-by-digit rules of one length, made as they are asked. */+struct dp {+ struct janas_gbuild *b;+ int len;+ char lo[MAG_DIGITS + 1], hi[MAG_DIGITS + 1];+ uint32_t m;+ uint32_t *edge; /* [pos][tl][th][r], tl or th set */+ uint32_t *free_; /* shared: [remaining][r], neither bound tight */+};++static uint32_t state(struct dp *d, int pos, int tl, int th, uint32_t r);++/* Digits left, neither bound in the way: any digits, the remainder r so+ far. Shared by every length (the caller keeps the table). */+static uint32_t free_state(struct dp *d, int left, uint32_t r)+{+ uint32_t *slot = &d->free_[(size_t)left * d->m + r];+ if (*slot != NONE)+ return *slot;+ uint32_t rule = *slot = janas_gb_rule(d->b);+ if (left == 0) {+ if (r == 0)+ janas_gb_alt(d->b, rule); /* the end */+ return rule;+ }+ /* the digits by the remainder they lead to, one set each */+ int done[10] = {0};+ for (int dg = 0; dg <= 9; dg++) {+ if (done[dg])+ continue;+ uint32_t nr = (r * 10 + (uint32_t)dg) % d->m;+ uint8_t set[32];+ janas_set_clear(set);+ for (int e = dg; e <= 9; e++)+ if (!done[e] && (r * 10 + (uint32_t)e) % d->m == nr) {+ janas_set_add(set, (uint8_t)('0' + e), (uint8_t)('0' + e));+ done[e] = 1;+ }+ uint32_t a = janas_gb_alt(d->b, rule);+ janas_gb_set(d->b, a, set);+ janas_gb_ref(d->b, a, free_state(d, left - 1, nr));+ }+ return rule;+}++static uint32_t state(struct dp *d, int pos, int tl, int th, uint32_t r)+{+ if (!tl && !th)+ return free_state(d, d->len - pos, r);+ size_t at = (((size_t)pos * 2 + (size_t)tl) * 2 + (size_t)th) * d->m + r;+ if (d->edge[at] != NONE)+ return d->edge[at];+ uint32_t rule = d->edge[at] = janas_gb_rule(d->b);+ if (pos == d->len) {+ if (r == 0)+ janas_gb_alt(d->b, rule);+ return rule;+ }+ int from = tl ? d->lo[pos] - '0' : 0, to = th ? d->hi[pos] - '0' : 9;+ /* runs of digits with the same next state, one alternative each */+ for (int dg = from; dg <= to;) {+ int ntl = tl && dg == d->lo[pos] - '0';+ int nth = th && dg == d->hi[pos] - '0';+ uint32_t nr = (r * 10 + (uint32_t)dg) % d->m;+ int e = dg;+ if (d->m == 1)+ while (e + 1 <= to && (tl && e + 1 == d->lo[pos] - '0') == ntl &&+ (th && e + 1 == d->hi[pos] - '0') == nth)+ e++;+ uint8_t set[32];+ janas_set_clear(set);+ janas_set_add(set, (uint8_t)('0' + dg), (uint8_t)('0' + e));+ uint32_t a = janas_gb_alt(d->b, rule);+ janas_gb_set(d->b, a, set);+ janas_gb_ref(d->b, a, state(d, pos + 1, ntl, nth, nr));+ dg = e + 1;+ }+ return rule;+}++static int ndigits(uint64_t x)+{+ int n = 1;+ while (x >= 10) {+ x /= 10;+ n++;+ }+ return n;+}++/* Alternatives of rule `into`: the magnitudes in [a, b] (b <= MAG_MAX)+ that are multiples of m, as their digits without leading zeros. */+static int magnitudes(struct janas_gbuild *b, uint32_t into, uint64_t a,+ uint64_t hi, uint32_t m)+{+ if (a > hi)+ return 0;+ struct dp d = {.b = b, .m = m};+ size_t nfree = (size_t)(MAG_DIGITS + 1) * m;+ size_t nedge = (size_t)(MAG_DIGITS + 1) * 4 * m;+ d.free_ = malloc(nfree * sizeof(uint32_t));+ d.edge = malloc(nedge * sizeof(uint32_t));+ if (!d.free_ || !d.edge) {+ free(d.free_);+ free(d.edge);+ return -1;+ }+ memset(d.free_, 0xff, nfree * sizeof(uint32_t));+ uint64_t p10 = 1;+ for (int len = 1; len <= MAG_DIGITS; len++, p10 *= 10) {+ uint64_t top = len == MAG_DIGITS ? MAG_MAX : p10 * 10 - 1;+ uint64_t lo = len == 1 ? 0 : p10, up = top;+ if (lo < a)+ lo = a;+ if (up > hi)+ up = hi;+ if (lo > up)+ continue;+ d.len = len;+ int nl = ndigits(lo), nh = ndigits(up);+ if (nl != len || nh != len)+ continue; /* cannot happen: lo, up of this length */+ uint64_t x = lo, y = up;+ for (int k = len - 1; k >= 0; k--, x /= 10, y /= 10) {+ d.lo[k] = (char)('0' + x % 10);+ d.hi[k] = (char)('0' + y % 10);+ }+ memset(d.edge, 0xff, nedge * sizeof(uint32_t));+ janas_gb_ref(b, janas_gb_alt(b, into), state(&d, 0, 1, 1, 0));+ }+ free(d.free_);+ free(d.edge);+ return 0;+}++/* A bound as a magnitude: the integer, clamped to what the rules take. */+static uint64_t clamp_mag(double x)+{+ return x <= 0 ? 0 : x >= (double)MAG_MAX ? MAG_MAX : (uint64_t)x;+}++uint32_t janas_jsg_int_bounded(struct janas_gbuild *b,+ const struct janas_jsg_bounds *bd, int *relaxed)+{+ uint32_t m = 1;+ if (bd->has_mult) {+ double mv = bd->mult;+ if (mv >= 1 && mv <= MULT_MAX && mv == floor(mv))+ m = (uint32_t)mv;+ else if (!(mv > 0 && mv < 1 && floor(1 / mv) == 1 / mv))+ (*relaxed)++; /* 0.5 lets every integer through */+ }+ /* the integers in [L, H] */+ double L = bd->has_lo ? (bd->lo_x ? floor(bd->lo) + 1 : ceil(bd->lo))+ : -(double)MAG_MAX;+ double H = bd->has_hi ? (bd->hi_x ? ceil(bd->hi) - 1 : floor(bd->hi))+ : (double)MAG_MAX;+ if (L < -(double)MAG_MAX)+ L = -(double)MAG_MAX;+ if (H > (double)MAG_MAX)+ H = (double)MAG_MAX;+ uint32_t r = janas_gb_rule(b);+ if (L > H)+ return r; /* nothing: the schema allows no integer */+ if (H >= 0 && magnitudes(b, r, clamp_mag(L), clamp_mag(H), m) != 0)+ return NONE;+ if (L < 0) { /* -x: x in [max(1, -H), -L] */+ uint32_t neg = janas_gb_rule(b);+ uint64_t a = H < 0 ? clamp_mag(-H) : 1;+ if (magnitudes(b, neg, a ? a : 1, clamp_mag(-L), m) != 0)+ return NONE;+ uint32_t alt = janas_gb_alt(b, r);+ janas_gb_byte(b, alt, '-');+ janas_gb_ref(b, alt, neg);+ }+ return r;+}++/* "." and up to FRAC_MAX digits, nz: not all zeros. */+static uint32_t fraction(struct janas_gbuild *b, int nz)+{+ uint8_t dg[32], d19[32];+ janas_set_clear(dg);+ janas_set_add(dg, '0', '9');+ janas_set_clear(d19);+ janas_set_add(d19, '1', '9');+ /* D_k: up to k more digits; Z_k: up to k digits, one of them not 0 */+ uint32_t d = janas_gb_empty(b), z = NONE;+ uint32_t dk[FRAC_MAX + 1];+ dk[0] = d;+ for (int k = 1; k <= FRAC_MAX; k++) {+ uint32_t r = janas_gb_rule(b);+ janas_gb_alt(b, r);+ uint32_t a = janas_gb_alt(b, r);+ janas_gb_set(b, a, dg);+ janas_gb_ref(b, a, dk[k - 1]);+ dk[k] = r;+ }+ if (nz) {+ for (int k = 1; k <= FRAC_MAX; k++) {+ uint32_t r = janas_gb_rule(b);+ uint32_t a = janas_gb_alt(b, r);+ janas_gb_set(b, a, d19);+ janas_gb_ref(b, a, dk[k - 1]);+ if (z != NONE) {+ a = janas_gb_alt(b, r);+ janas_gb_byte(b, a, '0');+ janas_gb_ref(b, a, z);+ }+ z = r;+ }+ }+ uint32_t r = janas_gb_rule(b);+ uint32_t a = janas_gb_alt(b, r);+ janas_gb_byte(b, a, '.');+ if (nz) {+ janas_gb_ref(b, a, z);+ } else {+ janas_gb_set(b, a, dg);+ janas_gb_ref(b, a, dk[FRAC_MAX - 1]);+ }+ return r;+}++/* An integer part from the magnitudes [a, b] followed by `tail` (NONE:+ nothing), as one alternative of rule into. */+static int part(struct janas_gbuild *b, uint32_t into, uint64_t a, uint64_t hi,+ uint32_t tail)+{+ if (a > hi)+ return 0;+ uint32_t ip = janas_gb_rule(b);+ if (magnitudes(b, ip, a, hi, 1) != 0)+ return -1;+ uint32_t alt = janas_gb_alt(b, into);+ janas_gb_ref(b, alt, ip);+ if (tail != NONE)+ janas_gb_ref(b, alt, tail);+ return 0;+}++/*+ * Magnitudes v in a range: A (excl: A < v), B (excl: v < B; inf: none), the+ * alternatives of `into`. positive: v > 0 (the negative side: never -0).+ */+static int real_side(struct janas_gbuild *b, uint32_t into, double A, int ax,+ double B, int bx, int inf, int positive, uint32_t any_f,+ uint32_t nz_f)+{+ int64_t i1, q = -1, p = -1, i2;+ if (A == floor(A)) {+ i1 = (int64_t)A + (ax ? 1 : 0);+ if (ax)+ q = (int64_t)A; /* A itself only with a fraction not zero */+ } else {+ i1 = (int64_t)ceil(A);+ }+ if (positive && i1 == 0 && q < 0) {+ q = 0; /* 0.x, never 0 */+ i1 = 1;+ }+ if (inf) {+ i2 = (int64_t)MAG_MAX;+ } else if (B == floor(B)) {+ i2 = (int64_t)B - 1;+ if (!bx)+ p = (int64_t)B;+ } else {+ i2 = (int64_t)floor(B) - 1;+ p = (int64_t)floor(B);+ }+ if (p >= 0 && (p < i1 || (positive && p == 0)))+ p = -1; /* not inside */+ if (q >= 0 && !inf && (double)q + 1 > B)+ q = -1; /* q.9 could pass the upper bound */+ /* any fraction or none, then the exact point, then the point above the+ exclusive bound */+ if (i1 <= i2) {+ uint32_t opt = janas_gb_rule(b);+ janas_gb_alt(b, opt);+ janas_gb_ref(b, janas_gb_alt(b, opt), any_f);+ if (part(b, into, (uint64_t)i1, clamp_mag((double)i2), opt) != 0)+ return -1;+ }+ if (p >= 0 && part(b, into, (uint64_t)p, (uint64_t)p, NONE) != 0)+ return -1;+ if (q >= 0 && part(b, into, (uint64_t)q, (uint64_t)q, nz_f) != 0)+ return -1;+ return 0;+}++uint32_t janas_jsg_real_bounded(struct janas_gbuild *b,+ const struct janas_jsg_bounds *bd, int *relaxed)+{+ if (bd->has_mult) {+ double mv = bd->mult;+ if (mv >= 1 && mv == floor(mv)) /* integers only: a valid subset */+ return janas_jsg_int_bounded(b, bd, relaxed);+ (*relaxed)++; /* a fractional step: not enforced */+ }+ uint32_t any_f = fraction(b, 0), nz_f = fraction(b, 1);+ uint32_t r = janas_gb_rule(b);+ double lo = bd->lo, hi = bd->hi;+ /* v >= 0 */+ if (!bd->has_hi || hi > 0 || (hi == 0 && !bd->hi_x)) {+ double A = bd->has_lo && lo > 0 ? lo : 0;+ int ax = bd->has_lo && lo >= 0 && bd->lo_x;+ if (real_side(b, r, A, ax, bd->has_hi ? hi : 0, bd->hi_x, !bd->has_hi,+ 0, any_f, nz_f) != 0)+ return NONE;+ }+ /* v < 0: -m, m in (max(0, -hi), -lo) */+ if (!bd->has_lo || lo < 0) {+ double A = bd->has_hi && hi < 0 ? -hi : 0;+ int ax = bd->has_hi && hi < 0 ? bd->hi_x : 1;+ uint32_t neg = janas_gb_rule(b);+ if (real_side(b, neg, A, ax, bd->has_lo ? -lo : 0, bd->lo_x,+ !bd->has_lo, 1, any_f, nz_f) != 0)+ return NONE;+ uint32_t alt = janas_gb_alt(b, r);+ janas_gb_byte(b, alt, '-');+ janas_gb_ref(b, alt, neg);+ }+ return r;+}diff --git a/src/llm/schema_re.c b/src/llm/schema_re.cnew file mode 100644index 0000000..bbc5fea--- /dev/null+++ b/src/llm/schema_re.c@@ -0,0 +1,496 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * schema_re.c - a regular expression as grammar rules (see schema_impl.h).+ *+ * The expression is read into a tree - characters and classes, sequences,+ * alternatives, repetitions - and each part is made a rule that matches it+ * and then goes on with the rule after it, so every rule recurses on its+ * right only, as the grammars want. A bounded repetition is written out+ * ({2,4}: two, then up to two more), an open one loops on itself.+ *+ * The text is the inside of a JSON string, so the bytes that would end it or+ * start an escape (the quote, the backslash, the control characters) are+ * matched as their escapes (\" \\ \n \u0001); a class that holds no byte+ * at all is not read. Bytes are matched as bytes: a class of non-ASCII+ * characters is not read.+ */+#include "schema_impl.h"++#include <stdlib.h>+#include <string.h>++#define NONE UINT32_MAX+#define REP_MAX 256 /* a bounded repetition written out up to this */++enum { RE_SET, RE_SEQ, RE_ALT, RE_REP };++struct re {+ int kind;+ uint8_t set[32];+ struct re **kids;+ int n;+ int lo, hi; /* RE_REP: hi < 0 open */+};++struct parser {+ const char *p, *end;+ int bad; /* not an expression this reads */+};++static struct re *node(int kind)+{+ struct re *r = calloc(1, sizeof(*r));+ if (r)+ r->kind = kind;+ return r;+}++static void re_free(struct re *r)+{+ if (!r)+ return;+ for (int i = 0; i < r->n; i++)+ re_free(r->kids[i]);+ free(r->kids);+ free(r);+}++static int add_kid(struct re *r, struct re *k)+{+ if (!k)+ return -1;+ struct re **t = realloc(r->kids, (size_t)(r->n + 1) * sizeof(*t));+ if (!t) {+ re_free(k);+ return -1;+ }+ r->kids = t;+ r->kids[r->n++] = k;+ return 0;+}++/* What a JSON string may hold as it is. */+static void json_plain(uint8_t set[32])+{+ uint8_t keep[32];+ janas_set_clear(keep);+ janas_set_add(keep, 0x20, 0xff);+ keep['"' >> 3] &= (uint8_t)~(1u << ('"' & 7));+ keep['\\' >> 3] &= (uint8_t)~(1u << ('\\' & 7));+ for (int i = 0; i < 32; i++)+ set[i] &= keep[i];+}++static int set_empty(const uint8_t set[32])+{+ for (int i = 0; i < 32; i++)+ if (set[i])+ return 0;+ return 1;+}++/* The escape a JSON string writes byte c with (c not plain). */+static size_t escape_of(uint8_t c, char out[8])+{+ static const char hex[] = "0123456789abcdef";+ const char *e = c == '"' ? "\\\""+ : c == '\\' ? "\\\\"+ : c == '\n' ? "\\n"+ : c == '\t' ? "\\t"+ : c == '\r' ? "\\r"+ : c == '\b' ? "\\b"+ : c == '\f' ? "\\f"+ : NULL;+ if (e) {+ memcpy(out, e, 2);+ return 2;+ }+ memcpy(out, "\\u00", 4);+ out[4] = hex[c >> 4];+ out[5] = hex[c & 15];+ return 6;+}++static void class_escape(char c, uint8_t set[32], int *ok)+{+ switch (c) {+ case 'd':+ janas_set_add(set, '0', '9');+ break;+ case 'w':+ janas_set_add(set, '0', '9');+ janas_set_add(set, 'a', 'z');+ janas_set_add(set, 'A', 'Z');+ janas_set_add(set, '_', '_');+ break;+ case 's':+ janas_set_add(set, ' ', ' ');+ janas_set_add(set, '\t', '\r');+ break;+ case 'D':+ case 'W':+ case 'S': {+ uint8_t s[32];+ janas_set_clear(s);+ class_escape((char)(c + 32), s, ok);+ janas_set_invert(s);+ for (int i = 0; i < 32; i++)+ set[i] |= s[i];+ break;+ }+ case 'n':+ janas_set_add(set, '\n', '\n');+ break;+ case 't':+ janas_set_add(set, '\t', '\t');+ break;+ case 'r':+ janas_set_add(set, '\r', '\r');+ break;+ default:+ /* an escaped punctuation character: itself; letters and digits+ with another meaning (\b, \p{...}, back references) are not read */+ if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||+ (c >= '0' && c <= '9'))+ *ok = 0;+ else+ janas_set_add(set, (uint8_t)c, (uint8_t)c);+ }+}++/* [...] after its '[' */+static struct re *parse_class(struct parser *ps)+{+ struct re *r = node(RE_SET);+ if (!r)+ return NULL;+ int neg = ps->p < ps->end && *ps->p == '^';+ if (neg)+ ps->p++;+ int ok = 1, first = 1;+ while (ps->p < ps->end && (*ps->p != ']' || first)) {+ first = 0;+ uint8_t c = (uint8_t)*ps->p++;+ if (c == '\\' && ps->p < ps->end) {+ char e = *ps->p++;+ if (strchr("dwsDWS", e)) { /* a class of its own: no range */+ class_escape(e, r->set, &ok);+ continue;+ }+ c = e == 'n' ? '\n'+ : e == 't' ? '\t'+ : e == 'r' ? '\r'+ : (uint8_t)e;+ if (!strchr("ntr", e) &&+ ((e >= 'a' && e <= 'z') || (e >= 'A' && e <= 'Z') ||+ (e >= '0' && e <= '9')))+ ok = 0;+ }+ uint8_t hi = c;+ if (ps->p + 1 < ps->end && *ps->p == '-' && ps->p[1] != ']') {+ ps->p++;+ hi = (uint8_t)*ps->p++;+ if (hi == '\\' && ps->p < ps->end)+ hi = (uint8_t)*ps->p++;+ if (hi < c)+ ok = 0;+ }+ if (ok)+ janas_set_add(r->set, c, hi);+ }+ if (ps->p >= ps->end)+ ok = 0;+ else+ ps->p++; /* ']' */+ if (!ok) {+ ps->bad = 1;+ re_free(r);+ return NULL;+ }+ if (neg)+ janas_set_invert(r->set);+ if (set_empty(r->set)) {+ ps->bad = 1;+ re_free(r);+ return NULL;+ }+ return r;+}++static struct re *parse_alt(struct parser *ps);++static struct re *parse_atom(struct parser *ps)+{+ char c = *ps->p++;+ if (c == '(') {+ if (ps->p + 1 < ps->end && ps->p[0] == '?' && ps->p[1] == ':')+ ps->p += 2;+ struct re *r = parse_alt(ps);+ if (!r || ps->p >= ps->end || *ps->p != ')') {+ ps->bad = 1;+ re_free(r);+ return NULL;+ }+ ps->p++;+ return r;+ }+ if (c == '[')+ return parse_class(ps);+ struct re *r = node(RE_SET);+ if (!r)+ return NULL;+ int ok = 1;+ if (c == '.') {+ janas_set_add(r->set, 0x20, 0xff);+ } else if (c == '\\' && ps->p < ps->end) {+ class_escape(*ps->p++, r->set, &ok);+ } else if (strchr(")|*+?{}^$", c)) {+ ok = 0; /* an anchor or a misplaced operator */+ } else {+ janas_set_add(r->set, (uint8_t)c, (uint8_t)c);+ }+ if (!ok) {+ ps->bad = 1;+ re_free(r);+ return NULL;+ }+ return r;+}++static int number(struct parser *ps, int *v)+{+ if (ps->p >= ps->end || *ps->p < '0' || *ps->p > '9')+ return -1;+ long x = 0;+ while (ps->p < ps->end && *ps->p >= '0' && *ps->p <= '9' && x < 100000)+ x = x * 10 + (*ps->p++ - '0');+ *v = (int)x;+ return 0;+}++static struct re *parse_rep(struct parser *ps)+{+ struct re *a = parse_atom(ps);+ while (a && ps->p < ps->end && strchr("*+?{", *ps->p)) {+ int lo, hi;+ char q = *ps->p++;+ if (q == '*')+ lo = 0, hi = -1;+ else if (q == '+')+ lo = 1, hi = -1;+ else if (q == '?')+ lo = 0, hi = 1;+ else {+ if (number(ps, &lo) != 0) {+ ps->bad = 1;+ re_free(a);+ return NULL;+ }+ hi = lo;+ if (ps->p < ps->end && *ps->p == ',') {+ ps->p++;+ hi = -1;+ if (ps->p < ps->end && *ps->p != '}' && number(ps, &hi) != 0)+ hi = -2;+ }+ if (ps->p >= ps->end || *ps->p != '}' || hi == -2 ||+ (hi >= 0 && hi < lo) || lo > REP_MAX || hi > REP_MAX) {+ ps->bad = 1;+ re_free(a);+ return NULL;+ }+ ps->p++;+ }+ if (ps->p < ps->end && *ps->p == '?')+ ps->p++; /* lazy: the same language */+ struct re *r = node(RE_REP);+ if (!r) {+ re_free(a);+ return NULL;+ }+ r->lo = lo;+ r->hi = hi;+ if (add_kid(r, a) != 0) {+ re_free(r);+ return NULL;+ }+ a = r;+ }+ return a;+}++static struct re *parse_seq(struct parser *ps)+{+ struct re *s = node(RE_SEQ);+ if (!s)+ return NULL;+ while (ps->p < ps->end && *ps->p != '|' && *ps->p != ')') {+ if (add_kid(s, parse_rep(ps)) != 0) {+ ps->bad = 1;+ re_free(s);+ return NULL;+ }+ }+ return s;+}++static struct re *parse_alt(struct parser *ps)+{+ struct re *a = node(RE_ALT);+ if (!a)+ return NULL;+ for (;;) {+ if (add_kid(a, parse_seq(ps)) != 0) {+ ps->bad = 1;+ re_free(a);+ return NULL;+ }+ if (ps->p < ps->end && *ps->p == '|') {+ ps->p++;+ continue;+ }+ return a;+ }+}++/* Whether r can match nothing. */+static int nullable(const struct re *r)+{+ switch (r->kind) {+ case RE_SET:+ return 0;+ case RE_SEQ:+ for (int i = 0; i < r->n; i++)+ if (!nullable(r->kids[i]))+ return 0;+ return 1;+ case RE_ALT:+ for (int i = 0; i < r->n; i++)+ if (nullable(r->kids[i]))+ return 1;+ return 0;+ default:+ return r->lo == 0 || nullable(r->kids[0]);+ }+}++/* An open repetition of what can be empty would be a rule that reaches+ itself without reading (left recursion, which the grammars refuse). */+static int loops_empty(const struct re *r)+{+ if (r->kind == RE_REP && r->hi < 0 && nullable(r->kids[0]))+ return 1;+ for (int i = 0; i < r->n; i++)+ if (loops_empty(r->kids[i]))+ return 1;+ return 0;+}++/* A rule matching r and then going on with k. */+static uint32_t make(struct janas_gbuild *b, const struct re *r, uint32_t k)+{+ switch (r->kind) {+ case RE_SET: {+ uint32_t x = janas_gb_rule(b);+ uint8_t plain[32];+ memcpy(plain, r->set, sizeof plain);+ json_plain(plain);+ if (!set_empty(plain)) {+ uint32_t a = janas_gb_alt(b, x);+ janas_gb_set(b, a, plain);+ janas_gb_ref(b, a, k);+ }+ for (int c = 0; c < 256; c++)+ if ((r->set[c >> 3] >> (c & 7) & 1) &&+ !(plain[c >> 3] >> (c & 7) & 1)) {+ char e[8];+ size_t n = escape_of((uint8_t)c, e);+ uint32_t a = janas_gb_alt(b, x);+ janas_gb_lit(b, a, e, n);+ janas_gb_ref(b, a, k);+ }+ return x;+ }+ case RE_SEQ:+ for (int i = r->n - 1; i >= 0; i--)+ k = make(b, r->kids[i], k);+ return k;+ case RE_ALT: {+ if (r->n == 1)+ return make(b, r->kids[0], k);+ uint32_t x = janas_gb_rule(b);+ for (int i = 0; i < r->n; i++)+ janas_gb_ref(b, janas_gb_alt(b, x), make(b, r->kids[i], k));+ return x;+ }+ default: { /* RE_REP */+ const struct re *in = r->kids[0];+ uint32_t c = k;+ if (r->hi < 0) { /* L = in L | k */+ c = janas_gb_rule(b);+ janas_gb_ref(b, janas_gb_alt(b, c), make(b, in, c));+ janas_gb_ref(b, janas_gb_alt(b, c), k);+ } else {+ for (int i = r->lo; i < r->hi; i++) { /* O = in O' | k */+ uint32_t o = janas_gb_rule(b);+ janas_gb_ref(b, janas_gb_alt(b, o), make(b, in, c));+ janas_gb_ref(b, janas_gb_alt(b, o), k);+ c = o;+ }+ }+ for (int i = 0; i < r->lo; i++)+ c = make(b, in, c);+ return c;+ }+ }+}++uint32_t janas_jsg_regex(struct janas_gbuild *b, const char *expr, size_t n,+ uint32_t then)+{+ struct parser ps = {.p = expr, .end = expr + n};+ struct re *r = parse_alt(&ps);+ if (!r || ps.bad || ps.p != ps.end || loops_empty(r)) {+ re_free(r);+ return NONE;+ }+ uint32_t x = make(b, r, then);+ re_free(r);+ return x;+}++/* Any bytes a JSON string holds as they are, as many as wanted, then then. */+static uint32_t any_then(struct janas_gbuild *b, uint32_t then)+{+ uint8_t any[32];+ janas_set_clear(any);+ janas_set_add(any, 0x20, 0xff);+ json_plain(any);+ uint32_t r = janas_gb_rule(b);+ uint32_t a = janas_gb_alt(b, r);+ janas_gb_set(b, a, any);+ janas_gb_ref(b, a, r);+ janas_gb_ref(b, janas_gb_alt(b, r), then);+ return r;+}++uint32_t janas_jsg_pattern(struct janas_gbuild *b, const char *expr, size_t n,+ uint32_t then)+{+ /* Written from the string's start even when it may be found later: a+ model let to write before the match may never come to it, and could+ not close the string meanwhile. A string that begins with the match+ contains it. */+ if (n && expr[0] == '^')+ expr++, n--;+ /* a '$' at the end, not escaped */+ size_t bs = 0;+ while (n >= 2 + bs && expr[n - 2 - bs] == '\\')+ bs++;+ int end = n && expr[n - 1] == '$' && bs % 2 == 0;+ if (end)+ n--;+ uint32_t tail = end ? then : any_then(b, then);+ return janas_jsg_regex(b, expr, n, tail);+}diff --git a/src/llm/shaders/b32_matvec.comp b/src/llm/shaders/b32_matvec.compindex 6ad8876..725fd7e 100644--- a/src/llm/shaders/b32_matvec.comp+++ b/src/llm/shaders/b32_matvec.comp@@ -9,7 +9,12 @@// 12 and their negatives), 5 Q5_0 (22 bytes: d, qh, the nibbles; q - 16),// 6 Q2_0 (18 bytes for 64 weights: d, then two bits a weight, weight i in// bits 2 (i % 4) of byte i / 4; q - 1: each half of a block one block of-// 32 here, with the block's d). As the CPU computes them (nib_dot_ref,+// 32 here, with the block's d), 7 NVFP4 (36 bytes for 64 weights: four+// UE4M3 scales, one a sub-block of 16, then sub-block s's codes in bytes+// 8s to 8s + 7, low nibbles its first eight weights, high ones the next+// eight, through MXFP4's table; each half of a block one block of 32+// here, its two sub-blocks' dots each in its own fma, the first first, as+// janas_nvfp4_dot_ref). As the CPU computes them (nib_dot_ref,// janas_mxfp4_dot_ref, dm_dot_ref, janas_q5_0_dot_ref and// janas_q2_0_dot_ref in quant.c): each block's integer dot in one fma into// the accumulator of its position in the group of eight, the minimum of@@ -47,7 +52,8 @@ const bool ID = (KID & 256u) != 0u;const uint NX = (20u + SG - 1u) / SG;/* bytes of a block of 32 weights (Q2_0: half a block), and of a group ofeight in words */-const uint S = KIND == 6u ? 9u+const uint S = KIND == 7u ? 18u+ : KIND == 6u ? 9u: KIND == 5u ? 22u: KIND == 4u ? 17u: KIND == 3u ? 24u@@ -55,7 +61,7 @@ const uint S = KIND == 6u ? 9u: 18u;const uint GW = 2u * S;const bool DM = KIND == 2u || KIND == 3u;-const bool TAB = KIND == 1u || KIND == 4u; /* levels through a table */+const bool TAB = KIND == 1u || KIND == 4u || KIND == 7u; /* a table */const bool FIVE = KIND == 3u || KIND == 5u; /* a fifth bit in qh *//* the constant folded out of the weights' integer dot */const int OFF = KIND == 0u ? 8 : KIND == 5u ? 16 : KIND == 6u ? 1 : 0;@@ -75,6 +81,16 @@ const int IQ4[16] = int[16](-127, -104, -83, -65, -49, -35, -22, -10,1, 13, 25, 38, 53, 69, 89, 113);const int MX[16] = int[16](0, 1, 2, 3, 4, 6, 8, 12, 0, -1, -2, -3, -4, -6, -8, -12);+/* a UE4M3 byte as NVFP4's scale, times one half (quant.c+ janas_ue4m3_half): exact */+float ue4m3_half(uint x)+{+ if (x == 0u || x == 0x7fu)+ return 0.0;+ int e = int((x >> 3) & 15u), man = int(x & 7u);+ return e == 0 ? ldexp(float(man), -10) : ldexp(float(8 + man), e - 11);+}+/* an E8M0 byte as MXFP4's scale, 2^(e - 128) (quant.c janas_e8m0_half) */float e8m0_half(uint e){@@ -127,7 +143,8 @@ void main(){if (gl_LocalInvocationID.x < 16u)T[gl_LocalInvocationID.x] =- uint(KIND == 4u ? MX[gl_LocalInvocationID.x] : IQ4[gl_LocalInvocationID.x]) & 0xffu;+ uint(KIND == 4u || KIND == 7u ? MX[gl_LocalInvocationID.x]+ : IQ4[gl_LocalInvocationID.x]) & 0xffu;barrier();uvec2 pw = p.w, px = p.x, py = p.y;uint nv = p.nv, xs = p.xs, ys = p.ys, v0 = gl_WorkGroupID.x * NV;@@ -179,9 +196,15 @@ void main()for (uint k = 0; k < 8u; k++) {/* the block's first byte, and that of its 16 bytes of quants(Q2_0: of the 8 of its half) */- uint B = KIND == 6u ? 18u * (k / 2u) : S * k;- uint qb = KIND == 6u ? B + 2u + 8u * (k % 2u) : B + S - 16u;- if (KIND == 4u) {+ uint B = KIND == 6u || KIND == 7u ? 2u * S * (k / 2u) : S * k;+ uint qb = KIND == 6u ? B + 2u + 8u * (k % 2u)+ : KIND == 7u ? B + 4u + 16u * (k % 2u)+ : B + S - 16u;+ if (KIND == 7u) { /* the half's two sub-blocks' scales */+ uint sb = B + 2u * (k % 2u);+ d[k] = ue4m3_half((wg[sb >> 2] >> (8u * (sb & 3u))) & 0xffu);+ m[k] = ue4m3_half((wg[sb >> 2] >> (8u * (sb & 3u) + 8u)) & 0xffu);+ } else if (KIND == 4u) {d[k] = e8m0_half((wg[B >> 2] >> (8u * (B & 3u))) & 0xffu);m[k] = 0.0;} else {@@ -213,6 +236,15 @@ void main()h |= spread5((qh >> (16u + 4u * i)) & 15u);}q[i] = l;+ if (KIND == 7u) {+ /* NVFP4: words 0-1 the first sub-block, low then high+ nibbles; words 2-3 the second */+ if (i == 0u) { lo[k].x = l; lo[k].z = h; }+ if (i == 1u) { lo[k].y = l; lo[k].w = h; }+ if (i == 2u) { hi[k].x = l; hi[k].z = h; }+ if (i == 3u) { hi[k].y = l; hi[k].w = h; }+ continue;+ }if (i == 0u) { lo[k].x = l; hi[k].x = h; }if (i == 1u) { lo[k].y = l; hi[k].y = h; }if (i == 2u) { lo[k].z = l; hi[k].z = h; }@@ -241,6 +273,15 @@ void main()uvec4 b0 = bc(xr, 16u), b1 = bc(xr, 17u);uint bw[8] = uint[8](b0.x, b0.y, b0.z, b0.w, b1.x, b1.y, b1.z, b1.w);for (uint k = 0; k < 8u; k++) {+ if (KIND == 7u) {+ if (k < nk) {+ acc[v][k] = fma(d[k] * d32[k], float(dp4(lo[k], bc(xr, 2u * k))),+ acc[v][k]);+ acc[v][k] = fma(m[k] * d32[k], float(dp4(hi[k], bc(xr, 2u * k + 1u))),+ acc[v][k]);+ }+ continue;+ }int dot = dp4(lo[k], bc(xr, 2u * k)) + dp4(hi[k], bc(xr, 2u * k + 1u));/* the block's activation sum: bsums32 of its two halves */int asum = bitfieldExtract(int(bw[k]), 0, 16) + bitfieldExtract(int(bw[k]), 16, 16);diff --git a/src/llm/shaders/gdn_rec.comp b/src/llm/shaders/gdn_rec.compindex 685c6dd..656b37e 100644--- a/src/llm/shaders/gdn_rec.comp+++ b/src/llm/shaders/gdn_rec.comp@@ -7,6 +7,9 @@// (key head g), v and the head's beta and decay -// x = M[r] decay, sk = x . k, d = (v[r] - sk) beta, y = x + d k,// out[r] = (y . q) scale, M[r] = y at sixteen bits.+// The same operations, not the same bits: the dot products are summed in+// another order (clusters of eight lanes) and packHalf2x16 rounds the state+// as the GPU does, so the results agree with the CPU's to rounding.// The row stays in registers for the whole block: eight threads a row,// sixteen numbers each (their q and k read four at a time), sixteen rows a// workgroup. The state is read from the half the position buffer's word 1diff --git a/src/llm/shaders/ix_pool.comp b/src/llm/shaders/ix_pool.compindex de0b2b1..9a223d5 100644--- a/src/llm/shaders/ix_pool.comp+++ b/src/llm/shaders/ix_pool.comp@@ -54,7 +54,7 @@ void main()barrier();uint half_ = p.n_rot / 2u, row = p.rb + w;if (t < d) {- float v = sv[t];+ precise float v = sv[t]; /* rounded as the CPU's */if (t < p.n_rot) {R RO = R(p.rope);uint i = t % half_;diff --git a/src/llm/shaders/ix_prep.comp b/src/llm/shaders/ix_prep.compindex 9457c4a..94076e4 100644--- a/src/llm/shaders/ix_prep.comp+++ b/src/llm/shaders/ix_prep.comp@@ -56,7 +56,7 @@ void main()barrier();uint half_ = p.n_rot / 2u;if (t < d) {- float v = sv[t];+ precise float v = sv[t]; /* rounded as the CPU's */if (t < p.n_rot) {R RO = R(p.rope);uint i = t % half_;diff --git a/src/llm/shaders/qk_rope.comp b/src/llm/shaders/qk_rope.compindex 8f79508..86813c9 100644--- a/src/llm/shaders/qk_rope.comp+++ b/src/llm/shaders/qk_rope.comp@@ -180,7 +180,9 @@ void main()barrier();uint half_ = p.n_rot / 2u;for (uint i = t; i < hd; i += 128u) {- float r = sv[i];+ /* precise: a * c - b * s rounded twice, as the CPU's (never one+ fma, which a driver may make of it) */+ precise float r = sv[i];if (i < half_) {float a = sv[i], b = sv[i + half_];r = a * R.v[ro + i] - b * R.v[ro + half_ + i];diff --git a/src/llm/speaker.c b/src/llm/speaker.cnew file mode 100644index 0000000..06c14ce--- /dev/null+++ b/src/llm/speaker.c@@ -0,0 +1,345 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * speaker.c - WeSpeaker's ResNet34 speaker embedding as its ONNX model+ * computes it: Kaldi's log mel filter banks (torchaudio's kaldi.fbank: 25+ * ms frames every 10 ms, DC removed, pre-emphasis 0.97, Hamming window,+ * 512-point power spectrum, 80 triangular bands on Kaldi's mel scale from+ * 20 Hz to Nyquist, the samples scaled to 16 bits), their mean over the+ * window taken off; a 3x3 convolution to 32 channels, then 3, 4, 6 and 3+ * residual blocks of 32, 64, 128 and 256 channels (batch norms folded in,+ * strides of 2 and 1x1 shortcuts where the size halves), the frequency and+ * channel axes kept, the mean and the standard deviation over time, their+ * projection to 256 less the embeddings' mean. The convolutions as tiled+ * products of patches (im2col) on the vision encoders' kernels.+ */+#include "speaker.h"++#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <unistd.h>++#include "common/audio_fft.h"+#include "common/pool.h"+#include "common/sysinfo.h"+#include "jns.h"+#include "vision_kernels.h"++#define MELS 80+#define NCONV 36+#define DIM 256++struct janas_speaker {+ struct janas_jns j;+ uint8_t *data;+ const uint16_t *w[NCONV];+ const float *b[NCONV];+ int kernel[NCONV], stride[NCONV], cin[NCONV], cout[NCONV];+ const uint16_t *proj;+ const float *proj_b, *mean;+ struct janas_pool *pool;+};++static const void *tensor(struct janas_speaker *s, const char *name,+ uint32_t type, uint64_t count)+{+ const struct janas_jns_tensor *t = janas_jns_tensor(&s->j, name);+ if (!t || t->type != type || t->bytes != count * (type ? 2 : 4))+ return NULL;+ return s->data + (t->offset - s->j.h.resident_offset);+}++void janas_speaker_close(struct janas_speaker *s)+{+ if (!s)+ return;+ if (s->pool)+ janas_pool_destroy(s->pool);+ free(s->data);+ janas_jns_close(&s->j);+ free(s);+}++struct janas_speaker *janas_speaker_open(const char *path, int n_threads,+ char *err, size_t err_len)+{+ static const int CH[NCONV] = {32, 32, 32, 32, 32, 32, 32, 64, 64,+ 64, 64, 64, 64, 64, 64, 64, 128, 128,+ 128, 128, 128, 128, 128, 128, 128, 128, 128,+ 128, 128, 256, 256, 256, 256, 256, 256, 256};+ struct janas_speaker *s = calloc(1, sizeof *s);+ if (!s) {+ snprintf(err, err_len, "out of memory");+ return NULL;+ }+ s->j.fd = -1;+ if (janas_jns_open(path, &s->j, err, err_len) != 0) {+ free(s);+ return NULL;+ }+ const char *why = NULL;+ s->data = malloc(s->j.h.resident_bytes ? s->j.h.resident_bytes : 1);+ size_t got = 0;+ while (s->data && got < s->j.h.resident_bytes) {+ ssize_t k = pread(s->j.fd, s->data + got, s->j.h.resident_bytes - got,+ (off_t)(s->j.h.resident_offset + got));+ if (k <= 0)+ break;+ got += (size_t)k;+ }+ if (!s->data || got != s->j.h.resident_bytes)+ why = "cannot read the weights";+ const int32_t *ks = NULL, *ss = NULL;+ uint64_t nk = 0, ns = 0;+ if (!why && (janas_jns_meta_ints(&s->j, "spk.kernels", &nk, &ks) ||+ janas_jns_meta_ints(&s->j, "spk.strides", &ns, &ss) ||+ nk != NCONV || ns != NCONV))+ why = "not a WeSpeaker ResNet34 model (made by wespeaker2jns)";+ for (int i = 0; i < NCONV && !why; i++) {+ s->kernel[i] = ks[i];+ s->stride[i] = ss[i];+ s->cout[i] = CH[i];+ /* the input: the block's, or the layer before's for its first */+ s->cin[i] = i == 0 ? 1+ : (i == 7 || i == 9) ? 32+ : (i == 16 || i == 18) ? 64+ : (i == 29 || i == 31) ? 128+ : CH[i];+ if ((ks[i] != 1 && ks[i] != 3) || (ss[i] != 1 && ss[i] != 2)) {+ why = "a convolution of a kernel or stride not read";+ break;+ }+ char name[32];+ snprintf(name, sizeof name, "spk.conv%d.w", i);+ s->w[i] = tensor(s, name, 1,+ (uint64_t)s->cout[i] * s->cin[i] * ks[i] * ks[i]);+ snprintf(name, sizeof name, "spk.conv%d.b", i);+ s->b[i] = tensor(s, name, 0, (uint64_t)s->cout[i]);+ if (!s->w[i] || !s->b[i])+ why = "a convolution's weights are missing or of another size";+ }+ if (!why) {+ s->proj = tensor(s, "spk.proj.w", 1, 256 * 5120);+ s->proj_b = tensor(s, "spk.proj.b", 0, 256);+ s->mean = tensor(s, "spk.mean", 0, 256);+ if (!s->proj || !s->proj_b || !s->mean)+ why = "the projection is missing";+ }+ if (!why) {+ int n = n_threads;+ struct janas_cpu_layout lay;+ if (n <= 0)+ n = janas_cpu_layout(NULL, 0, &lay) == 0 ? lay.n_p_cores : 1;+ s->pool = janas_pool_create(n > 0 ? n : 1, NULL);+ if (!s->pool)+ why = "cannot start the threads";+ }+ if (why) {+ snprintf(err, err_len, "%s: %s", path, why);+ janas_speaker_close(s);+ return NULL;+ }+ return s;+}++/* ---- the filter banks ---- */++static double mel(double f)+{+ return 1127.0 * log(1.0 + f / 700.0);+}++float *janas_speaker_fbank(const float *pcm, size_t n, uint32_t *frames)+{+ const int len = 400, shift = 160, nfft = 512, bins = 256;+ *frames = 0;+ if (n < (size_t)len)+ return calloc(1, sizeof(float));+ size_t nf = 1 + (n - (size_t)len) / (size_t)shift;+ if (nf > UINT32_MAX)+ return NULL;+ float *out = malloc(sizeof(float) * nf * MELS);+ struct afft *f = afft_new(nfft);+ float *bank = calloc((size_t)MELS * bins, sizeof(float));+ float win[400], in[1024], spec[1024], frame[400];+ if (!out || !f || !bank) {+ free(out);+ afft_free(f);+ free(bank);+ return NULL;+ }+ for (int i = 0; i < len; i++)+ win[i] = (float)(0.54 - 0.46 * cos(2 * M_PI * i / (len - 1)));+ /* the triangles, on Kaldi's mel scale from 20 Hz to 8 kHz */+ double lo = mel(20), hi = mel(8000), delta = (hi - lo) / (MELS + 1);+ for (int m = 0; m < MELS; m++) {+ double l = lo + m * delta, c = lo + (m + 1) * delta,+ r = lo + (m + 2) * delta;+ for (int k = 0; k < bins; k++) {+ double x = mel(16000.0 / nfft * k);+ double up = (x - l) / (c - l), down = (r - x) / (r - c);+ double v = up < down ? up : down;+ bank[m * bins + k] = v > 0 ? (float)v : 0;+ }+ }+ for (size_t t = 0; t < nf; t++) {+ const float *x = pcm + t * (size_t)shift;+ float mean = 0;+ for (int i = 0; i < len; i++) {+ frame[i] = x[i] * 32768.0f;+ mean += frame[i];+ }+ mean /= (float)len;+ for (int i = 0; i < len; i++)+ frame[i] -= mean;+ for (int i = len - 1; i > 0; i--)+ frame[i] -= 0.97f * frame[i - 1];+ frame[0] -= 0.97f * frame[0];+ memset(in, 0, sizeof in);+ for (int i = 0; i < len; i++)+ in[2 * i] = frame[i] * win[i]; /* (re, im) pairs */+ afft_run(f, in, spec);+ float pw[256];+ for (int k = 0; k < bins; k++)+ pw[k] =+ spec[2 * k] * spec[2 * k] + spec[2 * k + 1] * spec[2 * k + 1];+ for (int m = 0; m < MELS; m++) {+ float e = 0;+ const float *bk = bank + m * bins;+ for (int k = 0; k < bins; k++)+ e += bk[k] * pw[k];+ out[t * MELS + m] = logf(e > 1.1920929e-07f ? e : 1.1920929e-07f);+ }+ }+ afft_free(f);+ free(bank);+ *frames = (uint32_t)nf;+ return out;+}++/* ---- the network ---- */++/* a convolution of in (h x w x cin, channels last) into out (h' x w' x+ cout); col: the patches' room */+static void conv(struct janas_speaker *s, int i, const float *in, int h, int w,+ float *out, int *oh, int *ow, float *col)+{+ int k = s->kernel[i], st = s->stride[i], pad = (k - 1) / 2, cin = s->cin[i];+ int H = (h + 2 * pad - k) / st + 1, Wd = (w + 2 * pad - k) / st + 1;+ size_t cols = (size_t)k * k * cin;+ for (int y = 0; y < H; y++)+ for (int x = 0; x < Wd; x++) {+ float *c = col + ((size_t)y * Wd + x) * cols;+ for (int ky = 0; ky < k; ky++)+ for (int kx = 0; kx < k; kx++) {+ int sy = y * st + ky - pad, sx = x * st + kx - pad;+ float *d = c + ((size_t)ky * k + kx) * cin;+ if (sy < 0 || sy >= h || sx < 0 || sx >= w)+ memset(d, 0, sizeof(float) * (size_t)cin);+ else+ memcpy(d, in + ((size_t)sy * w + sx) * cin,+ sizeof(float) * (size_t)cin);+ }+ }+ vk_matmul_f16(s->pool, s->w[i], s->b[i], col, out, (uint32_t)s->cout[i],+ (uint32_t)cols, (uint32_t)(H * Wd), 0);+ *oh = H;+ *ow = Wd;+}++static void relu(float *x, size_t n)+{+ for (size_t i = 0; i < n; i++)+ x[i] = x[i] > 0 ? x[i] : 0;+}++int janas_speaker_embed(struct janas_speaker *s, const float *fbank,+ uint32_t f0, uint32_t nf, float *emb)+{+ if (nf < 8)+ return -1;+ /* the input, frequency down, time across: h 80 x w nf x 1 */+ size_t big = (size_t)MELS * nf * 32;+ float *a = malloc(sizeof(float) * big), *b = malloc(sizeof(float) * big);+ float *c = malloc(sizeof(float) * big), *t = malloc(sizeof(float) * big);+ float *col = malloc(sizeof(float) * (size_t)MELS * nf * 9 * 32);+ int rc = -1;+ if (!a || !b || !c || !t || !col)+ goto end;+ for (int m = 0; m < MELS; m++) {+ double mean = 0;+ for (uint32_t i = 0; i < nf; i++)+ mean += fbank[(size_t)(f0 + i) * MELS + m];+ mean /= nf;+ for (uint32_t i = 0; i < nf; i++)+ a[(size_t)m * nf + i] =+ (float)(fbank[(size_t)(f0 + i) * MELS + m] - mean);+ }+ int h = MELS, w = (int)nf, oh, ow;+ conv(s, 0, a, h, w, b, &oh, &ow, col);+ relu(b, (size_t)oh * ow * 32);+ h = oh;+ w = ow;+ /* the residual blocks: x in b */+ static const int BLOCKS[4] = {3, 4, 6, 3};+ int ci = 1;+ float *x = b, *y = a, *z = c;+ for (int l = 0; l < 4; l++)+ for (int k = 0; k < BLOCKS[l]; k++) {+ int down = l > 0 && k == 0;+ int ia = ci, ib = ci + 1, is = down ? ci + 2 : -1;+ ci += down ? 3 : 2;+ int h1, w1, h2, w2;+ conv(s, ia, x, h, w, y, &h1, &w1, col);+ relu(y, (size_t)h1 * w1 * s->cout[ia]);+ conv(s, ib, y, h1, w1, z, &h2, &w2, col);+ const float *sc = x;+ if (is >= 0) {+ int h3, w3;+ conv(s, is, x, h, w, t, &h3, &w3, col);+ sc = t;+ }+ size_t nn = (size_t)h2 * w2 * s->cout[ib];+ for (size_t q = 0; q < nn; q++) {+ float v = z[q] + sc[q];+ z[q] = v > 0 ? v : 0;+ }+ float *tmp = x;+ x = z;+ z = tmp;+ h = h2;+ w = w2;+ }+ /* the statistics over time of each channel and frequency: c f order */+ float st[5120];+ int ch = 256;+ if (h * ch != 2560) /* 80 / 8 frequencies */+ goto end;+ for (int cc = 0; cc < ch; cc++)+ for (int f = 0; f < h; f++) {+ double sum = 0;+ for (int q = 0; q < w; q++)+ sum += x[((size_t)f * w + q) * ch + cc];+ double mean = sum / w, var = 0;+ for (int q = 0; q < w; q++) {+ double d = x[((size_t)f * w + q) * ch + cc] - mean;+ var += d * d;+ }+ var = w > 1 ? var / (w - 1) : 0;+ st[cc * h + f] = (float)mean;+ st[2560 + cc * h + f] = (float)sqrt(var + 1e-8);+ }+ vk_matmul_f16(s->pool, s->proj, s->proj_b, st, emb, DIM, 5120, 1, 0);+ for (int i = 0; i < DIM; i++)+ emb[i] -= s->mean[i];+ rc = 0;+end:+ free(a);+ free(b);+ free(c);+ free(t);+ free(col);+ return rc;+}diff --git a/src/llm/speaker.h b/src/llm/speaker.hnew file mode 100644index 0000000..a73f660--- /dev/null+++ b/src/llm/speaker.h@@ -0,0 +1,51 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * speaker.h - who is speaking: WeSpeaker's ResNet34 speaker embeddings+ * (speaker.c) over Kaldi's filter banks, and the diarization that groups+ * the stretches of a recording by their speaker (diarize.c).+ */+#ifndef JANAS_LLM_SPEAKER_H+#define JANAS_LLM_SPEAKER_H++#include <stddef.h>+#include <stdint.h>++struct janas_speaker;++/* a model made by tools/wespeaker2jns, its products on n_threads (0: the+ performance cores), or NULL with the reason in err */+struct janas_speaker *janas_speaker_open(const char *path, int n_threads,+ char *err, size_t err_len);+void janas_speaker_close(struct janas_speaker *s);++/* Kaldi's log mel filter banks (80, 25 ms every 10 ms, Hamming, as+ torchaudio's kaldi.fbank) of n samples at 16 kHz in [-1, 1]: *frames x+ 80 (malloc'd), or NULL when out of memory */+float *janas_speaker_fbank(const float *pcm, size_t n, uint32_t *frames);++/* the embedding (256) of frames f0 .. f0 + nf of the filter banks, their+ mean taken off first: 0, or -1 when out of memory or nf too short */+int janas_speaker_embed(struct janas_speaker *s, const float *fbank,+ uint32_t f0, uint32_t nf, float *emb);++/* diarize.c: who speaks when, from stretches of speech (pairs of samples+ at 16 kHz): windows of 1.5 s every 0.75 s embedded, grouped by spectral+ clustering into at most max_speakers (0: 20), each window's group in+ speaker; known speakers (embeddings ref, n_ref, 256 each) take the+ groups nearest them. 0, or -1 when out of memory */+struct janas_diar {+ size_t *win; /* 2 x n_win: start, end in samples */+ int *speaker; /* n_win: 0, 1... in order of first appearance */+ uint32_t n_win;+ uint32_t n_speakers;+ int *known; /* n_speakers: the known speaker it is, or -1 */+};++int janas_diarize(struct janas_speaker *s, const float *pcm, size_t n,+ const size_t *regions, size_t n_regions, const float *ref,+ uint32_t n_ref, uint32_t max_speakers,+ struct janas_diar *out);+void janas_diar_free(struct janas_diar *d);++#endifdiff --git a/src/llm/tokenizer.c b/src/llm/tokenizer.cindex 45c0368..b44438d 100644--- a/src/llm/tokenizer.c+++ b/src/llm/tokenizer.c@@ -32,6 +32,9 @@ struct janas_tokenizer {int digits; /* a number's digits taken together, up to: 1, Llama 3's 3 */int spm; /* SentencePiece style (gemma4): raw UTF-8 characters, spaceswritten as U+2581, unknown characters as <0xXX> bytes */+ int tekken; /* Mistral's Tekken expression (pre "pixtral": Nemotron-H),+ and a pre-token in the vocabulary taken whole, unmerged */+ int gpt2; /* GPT-2's expression (pre "whisper") */};static uint64_t hash_bytes(const char *s, size_t n)@@ -212,6 +215,8 @@ struct janas_tokenizer *janas_tokenizer_create(const struct janas_jns *j,t->marks = strcmp(pre, "qwen35") == 0;t->digits = strcmp(pre, "llama-bpe") == 0 ? 3 : 1;t->spm = strcmp(model, "gemma4") == 0;+ t->tekken = strcmp(pre, "pixtral") == 0 || strcmp(pre, "tekken") == 0;+ t->gpt2 = strcmp(pre, "whisper") == 0 || strcmp(pre, "gpt-2") == 0;uint64_t total = 0;const uint8_t *p = toks;for (uint64_t i = 0; i < n; i++) {@@ -426,6 +431,117 @@ static size_t pretoken_end(const uint32_t *cp, size_t n, size_t i, int marks,return i + 1;}+/*+ * The same for GPT-2's expression (Whisper's):+ * 's|'t|'re|'ve|'m|'ll|'d | ' '?\p{L}+ | ' '?\p{N}++ * | ' '?[^\s\p{L}\p{N}]+ | \s+(?!\S) | \s++ */+static size_t gpt2_end(const uint32_t *cp, size_t n, size_t i)+{+ uint32_t c = cp[i];+ if (c == '\'' && i + 1 < n) {+ uint32_t a = cp[i + 1], b = i + 2 < n ? cp[i + 2] : 0;+ if (a == 's' || a == 't' || a == 'm' || a == 'd')+ return i + 2;+ if ((a == 'r' && b == 'e') || (a == 'v' && b == 'e') ||+ (a == 'l' && b == 'l'))+ return i + 3;+ }+ size_t k = i + (c == ' ' && i + 1 < n && !is_space(cp[i + 1]));+ if (k < n && is_letter(cp[k])) {+ while (k < n && is_letter(cp[k]))+ k++;+ return k;+ }+ if (k < n && is_number(cp[k])) {+ while (k < n && is_number(cp[k]))+ k++;+ return k;+ }+ if (k < n && !is_space(cp[k])) {+ while (k < n && !is_space(cp[k]) && !is_letter(cp[k]) &&+ !is_number(cp[k]))+ k++;+ return k;+ }+ size_t e = i;+ while (e < n && is_space(cp[e]))+ e++;+ if (e < n && e - i >= 2)+ return e - 1; /* leave the last space to the next word */+ return e;+}++/*+ * The same for Tekken's expression (llama.cpp's form of it):+ * [^\r\n\p{L}\p{N}]?U*W+ | [^\r\n\p{L}\p{N}]?U+W* | \p{N}+ * | ' '?[^\s\p{L}\p{N}]+[\r\n/]* | \s*[\r\n]+ | \s+(?!\S) | \s++ * U a letter or mark that is not a-z, W one that is not A-Z: a word is its+ * capitals then its small letters (HelloWorld is two), letters beyond ASCII+ * either. The first alternative backtracks as a regex does: U* gives back+ * up to the last W it took.+ */+static size_t tekken_end(const uint32_t *cp, size_t n, size_t i)+{+#define LM(c) (is_letter(c) || is_mark(c))+#define UP(c) (LM(c) && !((c) >= 'a' && (c) <= 'z'))+#define LO(c) (LM(c) && !((c) >= 'A' && (c) <= 'Z'))+ uint32_t c = cp[i];+ size_t k = i;+ if (!is_newline(c) && !is_letter(c) && !is_number(c) && i + 1 < n &&+ LM(cp[i + 1]))+ k = i + 1;+ if (k < n && LM(cp[k])) {+ size_t u = k;+ while (u < n && UP(cp[u]))+ u++;+ if (u < n && LO(cp[u])) { /* U* then W+ */+ while (u < n && LO(cp[u]))+ u++;+ return u;+ }+ for (size_t p = u; p > k; p--) /* U* gives back to a W */+ if (LO(cp[p - 1])) {+ size_t e = p - 1;+ while (e < n && LO(cp[e]))+ e++;+ return e;+ }+ if (u > k) /* U+ then W*, W* empty here */+ return u;+ }+ if (is_number(c))+ return i + 1;+ k = i + (c == ' ');+ if (k < n && !is_space(cp[k]) && !is_letter(cp[k]) && !is_number(cp[k])) {+ while (k < n && !is_space(cp[k]) && !is_letter(cp[k]) &&+ !is_number(cp[k]))+ k++;+ while (k < n && (is_newline(cp[k]) || cp[k] == '/'))+ k++;+ return k;+ }+ if (is_space(c)) {+ size_t e = i, last_nl = 0;+ while (e < n && is_space(cp[e])) {+ if (is_newline(cp[e]))+ last_nl = e + 1;+ e++;+ }+ if (last_nl)+ return last_nl;+ if (e == n)+ return e;+ if (e - i >= 2)+ return e - 1;+ return e;+ }+ return i + 1;+#undef LM+#undef UP+#undef LO+}+struct cand {uint32_t rank, pos; /* a mergeable pair, by its left symbol */};@@ -591,6 +707,32 @@ static int encode_spm(const struct janas_tokenizer *t, const char *text,}/* Plain text (no special tokens): pre-tokens, then BPE on each. */+/* Tekken's ignore_merges: a pre-token that is a token of the vocabulary+ (its bytes as byte-level characters) is that token, unmerged. */+static int whole(const struct janas_tokenizer *t, const unsigned char *s,+ size_t len, int32_t *out, size_t max, long *count)+{+ char buf[256];+ size_t k = 0;+ for (size_t b = 0; b < len; b++) {+ int32_t id = t->byte_id[s[b]];+ if (id < 0)+ return 0;+ size_t l = t->off[id + 1] - t->off[id];+ if (k + l > sizeof(buf))+ return 0;+ memcpy(buf + k, t->pool + t->off[id], l);+ k += l;+ }+ int32_t id = lookup(t, buf, k);+ if (id < 0)+ return 0;+ if ((size_t)*count < max)+ out[*count] = id;+ (*count)++;+ return 1;+}+static int encode_plain(const struct janas_tokenizer *t, const char *text,size_t len, int32_t *out, size_t max, long *count){@@ -612,7 +754,14 @@ static int encode_plain(const struct janas_tokenizer *t, const char *text,}at[n] = len;for (size_t i = 0; i < n;) {- size_t e = pretoken_end(cp, n, i, t->marks, t->digits);+ size_t e = t->tekken ? tekken_end(cp, n, i)+ : t->gpt2 ? gpt2_end(cp, n, i)+ : pretoken_end(cp, n, i, t->marks, t->digits);+ if (t->tekken && whole(t, (const unsigned char *)text + at[i],+ at[e] - at[i], out, max, count)) {+ i = e;+ continue;+ }bpe(t, (const unsigned char *)text + at[i], at[e] - at[i], out, max,count);i = e;diff --git a/src/llm/vad.c b/src/llm/vad.cnew file mode 100644index 0000000..d840459--- /dev/null+++ b/src/llm/vad.c@@ -0,0 +1,318 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vad.c - Silero VAD v5, as its 16 kHz model computes it: each 512+ * samples with the 64 before them, reflected 64 to the right, through the+ * STFT's basis (258 filters of 256 taps every 128 samples: 129 bins'+ * magnitudes over 4 frames), four convolutions with ReLU (strides 1, 2,+ * 2, 1: one frame of 128), an LSTM cell of 128 carried from window to+ * window, ReLU and the output convolution's sigmoid: the probability of+ * speech. The stretches of speech from those probabilities as Silero's+ * get_speech_timestamps finds them (a threshold, its hysteresis 0.15+ * below, the shortest speech and silence, padding split between close+ * stretches).+ */+#include "vad.h"++#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <unistd.h>++#include "jns.h"++#define WIN 512+#define CTX 64+#define HID 128++struct janas_vad {+ struct janas_jns j;+ uint8_t *data;+ const float *stft, *w[4], *b[4], *lw, *lr, *lb, *ow, *ob;+};++static const int CIN[4] = {129, 128, 64, 64}, COUT[4] = {128, 64, 64, 128};+static const int STRIDE[4] = {1, 2, 2, 1};++static const float *tensor(struct janas_vad *v, const char *name,+ uint64_t count)+{+ const struct janas_jns_tensor *t = janas_jns_tensor(&v->j, name);+ if (!t || t->type != 0 || t->bytes != 4 * count)+ return NULL;+ return (const float *)(v->data + (t->offset - v->j.h.resident_offset));+}++void janas_vad_close(struct janas_vad *v)+{+ if (!v)+ return;+ free(v->data);+ janas_jns_close(&v->j);+ free(v);+}++struct janas_vad *janas_vad_open(const char *path, char *err, size_t err_len)+{+ struct janas_vad *v = calloc(1, sizeof *v);+ if (!v) {+ snprintf(err, err_len, "out of memory");+ return NULL;+ }+ v->j.fd = -1;+ if (janas_jns_open(path, &v->j, err, err_len) != 0) {+ free(v);+ return NULL;+ }+ const char *why = NULL;+ v->data = malloc(v->j.h.resident_bytes ? v->j.h.resident_bytes : 1);+ size_t got = 0;+ while (v->data && got < v->j.h.resident_bytes) {+ ssize_t k = pread(v->j.fd, v->data + got, v->j.h.resident_bytes - got,+ (off_t)(v->j.h.resident_offset + got));+ if (k <= 0)+ break;+ got += (size_t)k;+ }+ if (!v->data || got != v->j.h.resident_bytes)+ why = "cannot read the weights";+ char name[32];+ if (!why) {+ v->stft = tensor(v, "vad.stft", 258 * 256);+ for (int i = 0; i < 4 && !why; i++) {+ snprintf(name, sizeof name, "vad.enc%d.w", i);+ v->w[i] = tensor(v, name, (uint64_t)COUT[i] * CIN[i] * 3);+ snprintf(name, sizeof name, "vad.enc%d.b", i);+ v->b[i] = tensor(v, name, (uint64_t)COUT[i]);+ if (!v->w[i] || !v->b[i])+ why = "not a Silero VAD model (a convolution is missing)";+ }+ v->lw = tensor(v, "vad.lstm.w", 4 * HID * HID);+ v->lr = tensor(v, "vad.lstm.r", 4 * HID * HID);+ v->lb = tensor(v, "vad.lstm.b", 8 * HID);+ v->ow = tensor(v, "vad.out.w", HID);+ v->ob = tensor(v, "vad.out.b", 1);+ if (!why &&+ (!v->stft || !v->lw || !v->lr || !v->lb || !v->ow || !v->ob))+ why = "not a Silero VAD model (made by silero2jns)";+ }+ if (why) {+ snprintf(err, err_len, "%s: %s", path, why);+ janas_vad_close(v);+ return NULL;+ }+ return v;+}++uint32_t janas_vad_window(const struct janas_vad *v)+{+ (void)v;+ return WIN;+}++uint32_t janas_vad_rate(const struct janas_vad *v)+{+ (void)v;+ return 16000;+}++static float sigmoid(float x)+{+ return 1.0f / (1.0f + expf(-x));+}++/* one window: x the 576 samples (context and window), h and c the LSTM's+ state; the probability of speech */+static float step(const struct janas_vad *v, const float *x, float *h, float *c)+{+ float pad[CTX + WIN + 64];+ memcpy(pad, x, sizeof(float) * (CTX + WIN));+ for (int i = 0; i < 64; i++) /* reflected: the last sample not repeated */+ pad[CTX + WIN + i] = x[CTX + WIN - 2 - i];+ /* the STFT: 4 frames of 129 magnitudes */+ float a[129 * 4], b[128 * 4];+ for (int t = 0; t < 4; t++) {+ float re[258];+ for (int f = 0; f < 258; f++) {+ const float *basis = v->stft + (size_t)f * 256;+ const float *s = pad + t * 128;+ float sum = 0;+ for (int k = 0; k < 256; k++)+ sum += basis[k] * s[k];+ re[f] = sum;+ }+ for (int f = 0; f < 129; f++)+ a[f * 4 + t] = sqrtf(re[f] * re[f] + re[129 + f] * re[129 + f]);+ }+ /* the encoder: [channels][frames] */+ float *in = a, *out = b, tmp[128 * 4];+ int len = 4;+ for (int l = 0; l < 4; l++) {+ int ol = (len + 2 - 3) / STRIDE[l] + 1;+ for (int o = 0; o < COUT[l]; o++)+ for (int t = 0; t < ol; t++) {+ float sum = v->b[l][o];+ for (int i = 0; i < CIN[l]; i++) {+ const float *w = v->w[l] + ((size_t)o * CIN[l] + i) * 3;+ for (int k = 0; k < 3; k++) {+ int p = t * STRIDE[l] + k - 1;+ if (p >= 0 && p < len)+ sum += w[k] * in[i * len + p];+ }+ }+ out[o * ol + t] = sum > 0 ? sum : 0;+ }+ len = ol;+ in = out;+ out = in == b ? tmp : b;+ }+ /* the LSTM (ONNX's gates: input, output, forget, cell) */+ float g[4 * HID];+ for (int r = 0; r < 4 * HID; r++) {+ float sum = v->lb[r] + v->lb[4 * HID + r];+ const float *wr = v->lw + (size_t)r * HID,+ *rr = v->lr + (size_t)r * HID;+ for (int k = 0; k < HID; k++)+ sum += wr[k] * in[k * len] + rr[k] * h[k];+ g[r] = sum;+ }+ float y = v->ob[0];+ for (int k = 0; k < HID; k++) {+ float ig = sigmoid(g[k]), og = sigmoid(g[HID + k]);+ float fg = sigmoid(g[2 * HID + k]), cg = tanhf(g[3 * HID + k]);+ c[k] = fg * c[k] + ig * cg;+ h[k] = og * tanhf(c[k]);+ y += v->ow[k] * (h[k] > 0 ? h[k] : 0);+ }+ return sigmoid(y);+}++long janas_vad_probs(struct janas_vad *v, const float *pcm, size_t n,+ float **probs)+{+ size_t nw = (n + WIN - 1) / WIN;+ float *p = malloc(sizeof(float) * (nw ? nw : 1));+ if (!p)+ return -1;+ float x[CTX + WIN], h[HID] = {0}, c[HID] = {0};+ memset(x, 0, sizeof(float) * CTX);+ for (size_t w = 0; w < nw; w++) {+ size_t at = w * WIN, k = n - at < WIN ? n - at : WIN;+ memcpy(x + CTX, pcm + at, sizeof(float) * k);+ memset(x + CTX + k, 0, sizeof(float) * (WIN - k));+ p[w] = step(v, x, h, c);+ memmove(x, x + WIN, sizeof(float) * CTX); /* the next one's context */+ }+ *probs = p;+ return (long)nw;+}++long janas_vad_segments(const struct janas_vad *v, const float *probs,+ long n_probs, size_t n,+ const struct janas_vad_params *p, size_t **seg)+{+ (void)v;+ const double sr = 16000;+ double threshold = p->threshold;+ double neg = threshold - 0.15 > 0.01 ? threshold - 0.15 : 0.01;+ double min_speech = sr * p->min_speech_ms / 1000;+ double pad = sr * p->speech_pad_ms / 1000;+ double max_speech =+ p->max_speech_s > 0 ? sr * p->max_speech_s - WIN - 2 * pad : 1e300;+ double min_silence = sr * p->min_silence_ms / 1000;+ double min_silence_at_max = sr * 98 / 1000;+ size_t cap = 16, cnt = 0;+ size_t *s = malloc(sizeof(size_t) * 2 * cap);+ if (!s)+ return -1;+ int triggered = 0, has = 0;+ size_t start = 0, temp_end = 0, prev_end = 0, next_start = 0;+#define PUSH(a, b) \+ do { \+ if (cnt == cap) { \+ size_t *t2 = realloc(s, sizeof(size_t) * 4 * cap); \+ if (!t2) { \+ free(s); \+ return -1; \+ } \+ s = t2; \+ cap *= 2; \+ } \+ s[2 * cnt] = (a); \+ s[2 * cnt + 1] = (b); \+ cnt++; \+ } while (0)+ for (long i = 0; i < n_probs; i++) {+ double pr = probs[i];+ size_t cur = (size_t)i * WIN;+ if (pr >= threshold && temp_end) {+ temp_end = 0;+ if (next_start < prev_end)+ next_start = cur;+ }+ if (pr >= threshold && !triggered) {+ triggered = 1;+ has = 1;+ start = cur;+ continue;+ }+ if (triggered && (double)(cur - start) > max_speech) {+ if (prev_end) {+ PUSH(start, prev_end);+ has = 0;+ if (next_start < prev_end) {+ triggered = 0;+ } else {+ start = next_start;+ has = 1;+ }+ prev_end = next_start = temp_end = 0;+ } else {+ PUSH(start, cur);+ has = 0;+ prev_end = next_start = temp_end = 0;+ triggered = 0;+ continue;+ }+ }+ if (pr < neg && triggered) {+ if (!temp_end)+ temp_end = cur;+ if ((double)(cur - temp_end) > min_silence_at_max)+ prev_end = temp_end;+ if ((double)(cur - temp_end) < min_silence)+ continue;+ if ((double)(temp_end - start) > min_speech)+ PUSH(start, temp_end);+ has = 0;+ prev_end = next_start = temp_end = 0;+ triggered = 0;+ continue;+ }+ }+ if (has && triggered && (double)(n - start) > min_speech)+ PUSH(start, n);+#undef PUSH+ for (size_t i = 0; i < cnt; i++) {+ size_t *a = &s[2 * i];+ size_t ipad = (size_t)pad;+ if (i == 0)+ a[0] = a[0] > ipad ? a[0] - ipad : 0;+ if (i + 1 < cnt) {+ size_t *b = &s[2 * i + 2];+ size_t silence = b[0] > a[1] ? b[0] - a[1] : 0;+ if (silence < 2 * ipad) {+ a[1] += silence / 2;+ b[0] = b[0] > silence / 2 ? b[0] - silence / 2 : 0;+ } else {+ a[1] = a[1] + ipad < n ? a[1] + ipad : n;+ b[0] = b[0] > ipad ? b[0] - ipad : 0;+ }+ } else {+ a[1] = a[1] + ipad < n ? a[1] + ipad : n;+ }+ }+ *seg = s;+ return (long)cnt;+}diff --git a/src/llm/vad.h b/src/llm/vad.hnew file mode 100644index 0000000..2bdc30d--- /dev/null+++ b/src/llm/vad.h@@ -0,0 +1,43 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vad.h - voice activity detection with Silero VAD (vad.c): the+ * probability of speech in every 32 ms of 16 kHz sound, and the stretches+ * of speech found from it as Silero's get_speech_timestamps finds them.+ */+#ifndef JANAS_LLM_VAD_H+#define JANAS_LLM_VAD_H++#include <stddef.h>+#include <stdint.h>++struct janas_vad;++/* a model made by tools/silero2jns, or NULL with the reason in err */+struct janas_vad *janas_vad_open(const char *path, char *err, size_t err_len);+void janas_vad_close(struct janas_vad *v);+/* the samples a probability covers (512) and their rate (16000) */+uint32_t janas_vad_window(const struct janas_vad *v);+uint32_t janas_vad_rate(const struct janas_vad *v);++/* the probability of speech in each window of n samples (the last one+ padded with silence), into probs (n / window rounded up, malloc'd):+ their count, or -1 when out of memory */+long janas_vad_probs(struct janas_vad *v, const float *pcm, size_t n,+ float **probs);++struct janas_vad_params {+ float threshold; /* 0.5 */+ uint32_t min_speech_ms; /* 250 */+ uint32_t min_silence_ms; /* 100 */+ float max_speech_s; /* 0: no limit */+ uint32_t speech_pad_ms; /* 30 */+};++/* the stretches of speech of n samples from their probabilities: pairs+ (start, end) in samples into *seg (malloc'd), their count, or -1 */+long janas_vad_segments(const struct janas_vad *v, const float *probs,+ long n_probs, size_t n,+ const struct janas_vad_params *p, size_t **seg);++#endifdiff --git a/src/llm/vision.c b/src/llm/vision.cnew file mode 100644index 0000000..0d7a6ca--- /dev/null+++ b/src/llm/vision.c@@ -0,0 +1,461 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vision.c - the vision encoder of Qwen3.5 and Qwen3-VL (vision.h), as their+ * Hugging Face processor and model compute it:+ * - the picture resized (Pillow's bicubic, image_resize.c) to the size+ * smart_resize gives: multiples of patch x merge (32), its pixels within+ * the model's bounds, the aspect kept; scaled to [0, 1], normalized;+ * - cut into 16x16 patches taken 2x2 block by 2x2 block (the order the+ * merger joins them in), each patch given as two equal frames;+ * - the patch embedding (a 3D convolution: one matrix), plus the learned+ * positions of a 48x48 grid resampled bilinearly (corners aligned) to+ * the picture's grid;+ * - blocks of LayerNorm, attention over every patch with a rotary+ * embedding of the patch's row (first quarter of each half of a head)+ * and column, LayerNorm and a tanh-GELU MLP, each with its residual;+ * - the merger: LayerNorm of each patch, four neighbours joined, a linear+ * layer, exact GELU, a linear layer to the language model's width.+ * Weights are bfloat16 as the checkpoint has them, everything else float.+ */+#include "vision_impl.h"++#include <math.h>+#include <unistd.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "bf16.h"+#include "vmath.h"+#include "common/image.h"+#include "common/pool.h"+#include "jns.h"++const void *janas_vision_tensor(struct janas_vision *v, const char *name,+ uint32_t type, uint64_t rows, uint64_t cols)+{+ for (uint64_t i = 0; i < v->j.h.n_tensor; i++) {+ const struct janas_jns_tensor *t = &v->j.tensors[i];+ if (strncmp(t->name, name, sizeof t->name))+ continue;+ uint64_t n = t->n_dims == 2 ? t->dims[0] * t->dims[1] : t->dims[0];+ uint64_t esz = type == 30 ? 2 : 4;+ if (t->type != type || n != rows * cols || t->bytes != n * esz ||+ (t->n_dims == 2 && t->dims[0] != cols))+ return NULL;+ return v->data + (t->offset - v->j.h.resident_offset);+ }+ return NULL;+}++void janas_vision_close(struct janas_vision *v)+{+ if (!v)+ return;+ for (uint32_t l = 0; l < MAX_BLOCKS; l++) {+ free(v->l[l].t_qkv);+ free(v->l[l].t_out);+ free(v->l[l].t_up);+ free(v->l[l].t_down);+ }+ free(v->t_patch);+ free(v->t_fc1);+ free(v->t_fc2);+ janas_vg_close(v);+ free(v->data);+ janas_jns_close(&v->j);+ free(v);+}++/* the resident tensors read into memory: NULL, or why not */+static const char *read_data(struct janas_vision *v)+{+ struct janas_jns *j = &v->j;+ v->data = malloc(j->h.resident_bytes ? j->h.resident_bytes : 1);+ if (!v->data)+ return "out of memory";+ size_t got = 0;+ while (got < j->h.resident_bytes) {+ ssize_t k = pread(j->fd, v->data + got, j->h.resident_bytes - got,+ (off_t)(j->h.resident_offset + got));+ if (k <= 0)+ break;+ got += (size_t)k;+ }+ return got == j->h.resident_bytes ? NULL+ : "cannot read the encoder's weights";+}++struct janas_vision *janas_vision_open(const char *path, char *err,+ size_t err_len)+{+ struct janas_vision *v = calloc(1, sizeof(*v));+ if (!v) {+ snprintf(err, err_len, "out of memory");+ return NULL;+ }+ if (janas_jns_open(path, &v->j, err, err_len) != 0) {+ free(v);+ return NULL;+ }+ const char *why = NULL;+ v->kind = !strncmp(v->j.h.arch, "qwen3vl_vision", sizeof v->j.h.arch)+ ? VISION_QWEN+ : !strncmp(v->j.h.arch, "gemma4_vision", sizeof v->j.h.arch)+ ? VISION_GEMMA+ : 0;+ if (!v->kind)+ why = "not a vision encoder this runs";+ struct janas_jns *j = &v->j;+ if (!why && v->kind == VISION_GEMMA) { /* vision_gemma.c */+ why = read_data(v);+ if (!why)+ why = janas_vg_open(v);+ goto opened;+ }+ if (!why &&+ (janas_jns_meta_u32(j, "vision.block_count", &v->n_block) ||+ janas_jns_meta_u32(j, "vision.embedding_length", &v->dim) ||+ janas_jns_meta_u32(j, "vision.feed_forward_length", &v->ff) ||+ janas_jns_meta_u32(j, "vision.attention.head_count", &v->heads) ||+ janas_jns_meta_u32(j, "vision.patch_size", &v->patch) ||+ janas_jns_meta_u32(j, "vision.temporal_patch_size", &v->tpatch) ||+ janas_jns_meta_u32(j, "vision.spatial_merge_size", &v->merge) ||+ janas_jns_meta_u32(j, "vision.position_count", &v->npos) ||+ janas_jns_meta_u32(j, "vision.projection_dim", &v->proj) ||+ janas_jns_meta_u32(j, "vision.min_pixels", &v->min_pixels) ||+ janas_jns_meta_u32(j, "vision.max_pixels", &v->max_pixels) ||+ janas_jns_meta_f32(j, "vision.attention.layer_norm_epsilon",+ &v->eps) ||+ janas_jns_meta_f32(j, "vision.rope.freq_base", &v->theta)))+ why = "the encoder's sizes are missing";+ uint32_t ti = 0, ts = 0, te = 0;+ if (!why && (janas_jns_meta_u32(j, "vision.token.image", &ti) ||+ janas_jns_meta_u32(j, "vision.token.start", &ts) ||+ janas_jns_meta_u32(j, "vision.token.end", &te)))+ why = "the image tokens are missing";+ v->tok_image = (int32_t)ti;+ v->tok_start = (int32_t)ts;+ v->tok_end = (int32_t)te;+ /* the processor's mean and deviation, per channel */+ for (int k = 0; !why && k < 3; k++) {+ char key[40];+ snprintf(key, sizeof key, "vision.image_mean.%d", k);+ if (janas_jns_meta_f32(j, key, &v->mean[k]))+ why = "the processor's mean is missing";+ snprintf(key, sizeof key, "vision.image_std.%d", k);+ if (janas_jns_meta_f32(j, key, &v->stdv[k]) || v->stdv[k] == 0)+ why = "the processor's deviation is missing";+ }+ uint32_t side = 0;+ while ((side + 1) * (side + 1) <= v->npos)+ side++;+ if (!why && (v->n_block < 1 || v->n_block > MAX_BLOCKS || !v->heads ||+ v->dim % v->heads || (v->dim / v->heads) % 4 ||+ v->dim / v->heads > 512 || v->merge != 2 ||+ side * side != v->npos || v->patch < 2))+ why = "the encoder's sizes are out of range";+ if (!why)+ why = read_data(v);+ uint64_t pd = 3ull * v->tpatch * v->patch * v->patch;+ uint64_t md = (uint64_t)v->dim * v->merge * v->merge;+ if (!why) {+ v->patch_w = janas_vision_tensor(v, "v.patch_embd", 30, v->dim, pd);+ v->patch_b = janas_vision_tensor(v, "v.patch_embd.bias", 0, 1, v->dim);+ v->pos = janas_vision_tensor(v, "v.pos_embd", 30, v->npos, v->dim);+ v->m_ln_w = janas_vision_tensor(v, "v.merger.ln.weight", 0, 1, v->dim);+ v->m_ln_b = janas_vision_tensor(v, "v.merger.ln.bias", 0, 1, v->dim);+ v->m_fc1 = janas_vision_tensor(v, "v.merger.fc1", 30, md, md);+ v->m_fc1_b = janas_vision_tensor(v, "v.merger.fc1.bias", 0, 1, md);+ v->m_fc2 = janas_vision_tensor(v, "v.merger.fc2", 30, v->proj, md);+ v->m_fc2_b = janas_vision_tensor(v, "v.merger.fc2.bias", 0, 1, v->proj);+ int ok = v->patch_w && v->patch_b && v->pos && v->m_ln_w && v->m_ln_b &&+ v->m_fc1 && v->m_fc1_b && v->m_fc2 && v->m_fc2_b;+ for (uint32_t l = 0; ok && l < v->n_block; l++) {+ struct vlayer *L = &v->l[l];+ char n[64];+#define T(field, part, type, r, c) \+ snprintf(n, sizeof n, "v.blk.%u." part, l); \+ L->field = janas_vision_tensor(v, n, type, r, c); \+ ok = ok && L->field+ T(ln1_w, "ln1.weight", 0, 1, v->dim);+ T(ln1_b, "ln1.bias", 0, 1, v->dim);+ T(ln2_w, "ln2.weight", 0, 1, v->dim);+ T(ln2_b, "ln2.bias", 0, 1, v->dim);+ T(qkv, "attn_qkv", 30, 3 * v->dim, v->dim);+ T(qkv_b, "attn_qkv.bias", 0, 1, 3 * v->dim);+ T(out, "attn_out", 30, v->dim, v->dim);+ T(out_b, "attn_out.bias", 0, 1, v->dim);+ T(up, "ffn_up", 30, v->ff, v->dim);+ T(up_b, "ffn_up.bias", 0, 1, v->ff);+ T(down, "ffn_down", 30, v->dim, v->ff);+ T(down_b, "ffn_down.bias", 0, 1, v->dim);+#undef T+ }+ if (!ok)+ why = "a tensor of the encoder is missing or of another shape";+ }+ if (!why) { /* the matrices transposed once, for the products */+ v->t_patch = vk_transposed(v->patch_w, v->dim, pd);+ v->t_fc1 = vk_transposed(v->m_fc1, md, md);+ v->t_fc2 = vk_transposed(v->m_fc2, v->proj, md);+ int ok = v->t_patch && v->t_fc1 && v->t_fc2;+ for (uint32_t l = 0; ok && l < v->n_block; l++) {+ struct vlayer *L = &v->l[l];+ L->t_qkv = vk_transposed(L->qkv, 3 * v->dim, v->dim);+ L->t_out = vk_transposed(L->out, v->dim, v->dim);+ L->t_up = vk_transposed(L->up, v->ff, v->dim);+ L->t_down = vk_transposed(L->down, v->dim, v->ff);+ ok = L->t_qkv && L->t_out && L->t_up && L->t_down;+ }+ if (!ok)+ why = "out of memory";+ }+opened:+ if (why) {+ snprintf(err, err_len, "%s: %s", path, why);+ janas_vision_close(v);+ return NULL;+ }+ return v;+}++uint32_t janas_vision_dim(const struct janas_vision *v)+{+ return v->proj;+}++int janas_vision_both_ways(const struct janas_vision *v)+{+ return v->both_ways;+}++void janas_vision_tokens(const struct janas_vision *v, int32_t *image,+ int32_t *start, int32_t *end)+{+ *image = v->tok_image;+ *start = v->tok_start;+ *end = v->tok_end;+}++/* Python's round(): halves to the even neighbour */+static double round_even(double x)+{+ double r = floor(x + 0.5);+ if (r - x == 0.5 && fmod(r, 2) != 0)+ r -= 1;+ return r;+}++void janas_vision_size(const struct janas_vision *v, int w, int h,+ int max_tokens, int *rw, int *rh, int *nx, int *ny)+{+ if (v->kind == VISION_GEMMA) {+ janas_vg_size(v, w, h, max_tokens, rw, rh, nx, ny);+ return;+ }+ double f = (double)v->patch * v->merge;+ double maxp = v->max_pixels, minp = v->min_pixels;+ if (max_tokens > 0 && maxp > (double)max_tokens * f * f)+ maxp = (double)max_tokens * f * f;+ double hb = round_even(h / f) * f, wb = round_even(w / f) * f;+ if (hb * wb > maxp) {+ double beta = sqrt((double)h * w / maxp);+ hb = fmax(f, floor(h / beta / f) * f);+ wb = fmax(f, floor(w / beta / f) * f);+ } else if (hb * wb < minp) {+ double beta = sqrt(minp / ((double)h * w));+ hb = ceil(h * beta / f) * f;+ wb = ceil(w * beta / f) * f;+ }+ if (hb < f)+ hb = f;+ if (wb < f)+ wb = f;+ *rw = (int)wb;+ *rh = (int)hb;+ *nx = (int)(wb / f);+ *ny = (int)(hb / f);+}++/* ---- the computation ---- */++int janas_vision_encode(struct janas_vision *v, const struct janas_image *im,+ int max_tokens, struct janas_pool *pool, float **out,+ int *nx, int *ny, char *err, size_t err_len)+{+ if (v->kind == VISION_GEMMA)+ return janas_vg_encode(v, im, max_tokens, pool, out, nx, ny, err,+ err_len);+ *out = NULL;+ int rw, rh;+ janas_vision_size(v, im->w, im->h, max_tokens, &rw, &rh, nx, ny);+ struct janas_image rs;+ const struct janas_image *src = im;+ if (rw != im->w || rh != im->h) {+ if (janas_image_resize(im, rw, rh, &rs) != 0) {+ snprintf(err, err_len, "out of memory");+ return -1;+ }+ src = &rs;+ }+ uint32_t P = v->patch, M = v->merge, D = v->dim;+ uint32_t gw = (uint32_t)rw / P, gh = (uint32_t)rh / P;+ uint32_t n = gw * gh, pd = 3 * v->tpatch * P * P;+ uint32_t hd = D / v->heads;+ float *pix = malloc(sizeof(float) * (size_t)n * pd);+ float *x = malloc(sizeof(float) * (size_t)n * D);+ float *t = malloc(sizeof(float) * (size_t)n * D);+ float *qkv = malloc(sizeof(float) * (size_t)n * 3 * D);+ float *ffb =+ malloc(sizeof(float) * (size_t)n * (v->ff > 3 * D ? v->ff : 3 * D));+ int *prow = malloc(sizeof(int) * n), *pcol = malloc(sizeof(int) * n);+ float *res = NULL, *cs = NULL, *kt = NULL;+ struct janas_pool *own = NULL; /* one thread, when none is given */+ int rc = -1;+ if (!pool && !(pool = own = janas_pool_create(1, NULL))) {+ snprintf(err, err_len, "out of memory");+ return -1;+ }+ kt = malloc(sizeof(float) * (size_t)n * D * 2); /* keys, then values */+ if (!pix || !x || !t || !qkv || !ffb || !prow || !pcol || !kt) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ /* patches, 2x2 block by block; each patch [channel][frame][y][x] */+ uint32_t idx = 0;+ for (uint32_t by = 0; by < gh / M; by++)+ for (uint32_t bx = 0; bx < gw / M; bx++)+ for (uint32_t my = 0; my < M; my++)+ for (uint32_t mx = 0; mx < M; mx++, idx++) {+ uint32_t r = by * M + my, c = bx * M + mx;+ prow[idx] = (int)r;+ pcol[idx] = (int)c;+ float *p = pix + (size_t)idx * pd;+ for (uint32_t ch = 0; ch < 3; ch++)+ for (uint32_t f = 0; f < v->tpatch; f++)+ for (uint32_t yy = 0; yy < P; yy++)+ for (uint32_t xx = 0; xx < P; xx++) {+ uint8_t s = src->rgb[((size_t)(r * P + yy) *+ (size_t)rw ++ (c * P + xx)) *+ 3 ++ ch];+ *p++ = (s / 255.0f - v->mean[ch]) /+ v->stdv[ch];+ }+ }+ vk_matmul(pool, v->t_patch, v->patch_b, pix, x, D, pd, n, 0);+ /* the learned positions, resampled to the grid (corners aligned) */+ uint32_t side = 0;+ while ((side + 1) * (side + 1) <= v->npos)+ side++;+ for (uint32_t i = 0; i < n; i++) {+ double sr = gh > 1 ? (double)prow[i] * (side - 1) / (gh - 1) : 0;+ double sc = gw > 1 ? (double)pcol[i] * (side - 1) / (gw - 1) : 0;+ int r0 = (int)floor(sr), c0 = (int)floor(sc);+ int r1 = r0 + 1 < (int)side ? r0 + 1 : (int)side - 1;+ int c1 = c0 + 1 < (int)side ? c0 + 1 : (int)side - 1;+ float wr = (float)(sr - r0), wc = (float)(sc - c0);+ float w00 = (1 - wr) * (1 - wc), w01 = (1 - wr) * wc;+ float w10 = wr * (1 - wc), w11 = wr * wc;+ const uint16_t *e00 = v->pos + ((size_t)r0 * side + (size_t)c0) * D;+ const uint16_t *e01 = v->pos + ((size_t)r0 * side + (size_t)c1) * D;+ const uint16_t *e10 = v->pos + ((size_t)r1 * side + (size_t)c0) * D;+ const uint16_t *e11 = v->pos + ((size_t)r1 * side + (size_t)c1) * D;+ float *xi = x + (size_t)i * D;+ for (uint32_t k = 0; k < D; k++)+ xi[k] += w00 * vk_bf(e00[k]) + w01 * vk_bf(e01[k]) ++ w10 * vk_bf(e10[k]) + w11 * vk_bf(e11[k]);+ }+ /* the rotary angles: a quarter of a head for the row, a quarter for the+ column, the half repeated */+ uint32_t q4 = hd / 4;+ cs = malloc(sizeof(float) * (size_t)n * hd * 2);+ if (!cs) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ for (uint32_t i = 0; i < n; i++) {+ float *c = cs + (size_t)i * hd * 2, *s = c + hd;+ for (uint32_t k = 0; k < q4; k++) {+ float inv = 1.0f / powf(v->theta, (float)(2 * k) / (float)(hd / 2));+ float ar = (float)prow[i] * inv, ac = (float)pcol[i] * inv;+ c[k] = c[k + hd / 2] = cosf(ar);+ s[k] = s[k + hd / 2] = sinf(ar);+ c[q4 + k] = c[q4 + k + hd / 2] = cosf(ac);+ s[q4 + k] = s[q4 + k + hd / 2] = sinf(ac);+ }+ }+ for (uint32_t l = 0; l < v->n_block; l++) {+ const struct vlayer *L = &v->l[l];+ for (uint32_t i = 0; i < n; i++)+ vk_layer_norm(t + (size_t)i * D, x + (size_t)i * D, L->ln1_w,+ L->ln1_b, D, v->eps);+ vk_matmul(pool, L->t_qkv, L->qkv_b, t, qkv, 3 * D, D, n, 0);+ /* rotate q and k: x cos + rotate_half(x) sin, per head */+ for (uint32_t i = 0; i < n; i++) {+ const float *c = cs + (size_t)i * hd * 2, *s = c + hd;+ for (uint32_t part = 0; part < 2; part++)+ for (uint32_t h = 0; h < v->heads; h++) {+ float *q = qkv + (size_t)i * 3 * D + part * D + h * hd;+ float tmp[512];+ for (uint32_t k = 0; k < hd; k++) {+ float rot = k < hd / 2 ? -q[k + hd / 2] : q[k - hd / 2];+ tmp[k] = q[k] * c[k] + rot * s[k];+ }+ memcpy(q, tmp, sizeof(float) * hd);+ }+ }+ vk_attention(pool, qkv, kt, t, n, D, v->heads, 1.0f / sqrtf((float)hd));+ vk_matmul(pool, L->t_out, L->out_b, t, ffb, D, D, n, 0);+ for (size_t k = 0; k < (size_t)n * D; k++)+ x[k] += ffb[k];+ for (uint32_t i = 0; i < n; i++)+ vk_layer_norm(t + (size_t)i * D, x + (size_t)i * D, L->ln2_w,+ L->ln2_b, D, v->eps);+ vk_matmul(pool, L->t_up, L->up_b, t, ffb, v->ff, D, n, 0);+ for (size_t k = 0; k < (size_t)n * v->ff; k++)+ ffb[k] = vk_gelu_tanh(ffb[k]);+ vk_matmul(pool, L->t_down, L->down_b, ffb, t, D, v->ff, n, 0);+ for (size_t k = 0; k < (size_t)n * D; k++)+ x[k] += t[k];+ }+ /* the merger: each patch normalized, four consecutive ones joined */+ uint32_t nm = n / (M * M), md = D * M * M;+ for (uint32_t i = 0; i < n; i++)+ vk_layer_norm(t + (size_t)i * D, x + (size_t)i * D, v->m_ln_w,+ v->m_ln_b, D, v->eps);+ float *h1 = malloc(sizeof(float) * (size_t)nm * md);+ res = malloc(sizeof(float) * (size_t)nm * v->proj);+ if (!h1 || !res) {+ free(h1);+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ vk_matmul(pool, v->t_fc1, v->m_fc1_b, t, h1, md, md, nm, 0);+ for (size_t k = 0; k < (size_t)nm * md; k++)+ h1[k] = vk_gelu_erf(h1[k]);+ vk_matmul(pool, v->t_fc2, v->m_fc2_b, h1, res, v->proj, md, nm, 0);+ free(h1);+ *out = res;+ res = NULL;+ rc = 0;+done:+ free(res);+ free(cs);+ free(kt);+ if (own)+ janas_pool_destroy(own);+ free(pix);+ free(x);+ free(t);+ free(qkv);+ free(ffb);+ free(prow);+ free(pcol);+ if (src == &rs)+ janas_image_free(&rs);+ return rc;+}diff --git a/src/llm/vision.h b/src/llm/vision.hnew file mode 100644index 0000000..f090fa6--- /dev/null+++ b/src/llm/vision.h@@ -0,0 +1,44 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vision.h - a model's vision encoder (vision.c), from the JNS file+ * tools/hf2jns_vision.c writes: a picture in, the embeddings of its tokens+ * out, as wide as the language model's.+ */+#ifndef JANAS_LLM_VISION_H+#define JANAS_LLM_VISION_H++#include <stddef.h>+#include <stdint.h>++struct janas_image;+struct janas_pool;+struct janas_vision;++/* NULL with the reason in err. */+struct janas_vision *janas_vision_open(const char *path, char *err,+ size_t err_len);+void janas_vision_close(struct janas_vision *v);++/* The width of an embedding, and the tokens that frame a picture. */+uint32_t janas_vision_dim(const struct janas_vision *v);+/* 1 when a picture's tokens attend to one another both ways in the+ language model (Gemma 4 12B and 26B) */+int janas_vision_both_ways(const struct janas_vision *v);+void janas_vision_tokens(const struct janas_vision *v, int32_t *image,+ int32_t *start, int32_t *end);++/* The size a picture of w x h is resized to (multiples of the patch and the+ merge, its pixels within the model's bounds and max_tokens tokens when+ that is not 0), and the grid of tokens it gives. */+void janas_vision_size(const struct janas_vision *v, int w, int h,+ int max_tokens, int *rw, int *rh, int *nx, int *ny);++/* The picture's embeddings: *out (malloc'd) nx * ny rows of+ janas_vision_dim floats, row-major over the token grid. 0, or -1 with+ the reason. pool may be NULL (one thread). */+int janas_vision_encode(struct janas_vision *v, const struct janas_image *im,+ int max_tokens, struct janas_pool *pool, float **out,+ int *nx, int *ny, char *err, size_t err_len);++#endifdiff --git a/src/llm/vision_gemma.c b/src/llm/vision_gemma.cnew file mode 100644index 0000000..148f30d--- /dev/null+++ b/src/llm/vision_gemma.c@@ -0,0 +1,363 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vision_gemma.c - Gemma 4's vision encoder (vision_impl.h), as its Hugging+ * Face processor and model compute it:+ * - the picture resized (Pillow's bicubic) to the largest size, aspect+ * kept, whose 16x16 patches are at most the soft tokens times 3 x 3, a+ * multiple of 48 a side; pixels scaled to [-1, 1];+ * - each patch (row, column, channel) through one matrix, plus the+ * learned embeddings of its column and of its row;+ * - blocks of RMS norm, attention (queries and keys RMS-normed with a+ * scale, values without, a rotary embedding of the column on the first+ * half of each head and of the row on the second, no scaling of the+ * scores), RMS norm, residual; RMS norm, a gated tanh-GELU MLP, RMS+ * norm, residual - every linear clamping its input and its output to+ * bounds of its own;+ * - the patches averaged 3 x 3, times sqrt(width), standardized where+ * the model says, RMS-normed and projected to the language model's+ * width.+ */+#include "vision_impl.h"++#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "common/image.h"+#include "common/pool.h"++/* the soft tokens a picture may have, as the processor allows them */+static const uint32_t SOFT[] = {70, 140, 280, 560, 1120};++const char *janas_vg_open(struct janas_vision *v)+{+ struct janas_jns *j = &v->j;+ uint32_t ti = 0, ts = 0, te = 0, stdz = 0, unified = 0;+ janas_jns_meta_u32(j, "vision.unified", &unified);+ if (unified) /* the 12B's, without blocks */+ return janas_vgu_open(v);+ if (janas_jns_meta_u32(j, "vision.block_count", &v->n_block) ||+ janas_jns_meta_u32(j, "vision.embedding_length", &v->dim) ||+ janas_jns_meta_u32(j, "vision.feed_forward_length", &v->ff) ||+ janas_jns_meta_u32(j, "vision.attention.head_count", &v->heads) ||+ janas_jns_meta_u32(j, "vision.patch_size", &v->patch) ||+ janas_jns_meta_u32(j, "vision.pooling_kernel_size", &v->pool_k) ||+ janas_jns_meta_u32(j, "vision.position_size", &v->pos_size) ||+ janas_jns_meta_u32(j, "vision.max_soft_tokens", &v->max_soft) ||+ janas_jns_meta_u32(j, "vision.projection_dim", &v->proj) ||+ janas_jns_meta_u32(j, "vision.standardize", &stdz) ||+ janas_jns_meta_f32(j, "vision.attention.layer_norm_epsilon", &v->eps) ||+ janas_jns_meta_f32(j, "vision.rope.freq_base", &v->theta))+ return "the encoder's sizes are missing";+ if (janas_jns_meta_u32(j, "vision.token.image", &ti) ||+ janas_jns_meta_u32(j, "vision.token.start", &ts) ||+ janas_jns_meta_u32(j, "vision.token.end", &te))+ return "the image tokens are missing";+ uint32_t both = 0; /* absent in the files of before */+ janas_jns_meta_u32(j, "vision.bidirectional", &both);+ v->both_ways = both != 0;+ v->tok_image = (int32_t)ti;+ v->tok_start = (int32_t)ts;+ v->tok_end = (int32_t)te;+ if (v->n_block < 1 || v->n_block > MAX_BLOCKS || !v->heads ||+ v->dim % v->heads || (v->dim / v->heads) % 4 ||+ v->dim / v->heads > 512 || v->pool_k < 1 || v->pool_k > 8 ||+ v->patch < 2 || !v->pos_size || !v->max_soft || !v->proj)+ return "the encoder's sizes are out of range";+ uint64_t pd = 3ull * v->patch * v->patch;+ const uint16_t *pw = janas_vision_tensor(v, "v.patch_embd", 30, v->dim, pd);+ v->g_pos =+ janas_vision_tensor(v, "v.pos_embd", 30, 2ull * v->pos_size, v->dim);+ const uint16_t *proj =+ janas_vision_tensor(v, "v.proj", 30, v->proj, v->dim);+ if (stdz) {+ v->std_bias = janas_vision_tensor(v, "v.std_bias", 0, 1, v->dim);+ v->std_scale = janas_vision_tensor(v, "v.std_scale", 0, 1, v->dim);+ if (!v->std_bias || !v->std_scale)+ return "the standardization is missing";+ }+ if (!pw || !v->g_pos || !proj)+ return "a tensor of the encoder is missing or of another shape";+ v->t_g_patch = vk_transposed(pw, v->dim, pd);+ v->t_g_proj = vk_transposed(proj, v->proj, v->dim);+ if (!v->t_g_patch || !v->t_g_proj)+ return "out of memory";+ static const char *const LIN[GL_N] = {"q", "k", "v", "o",+ "gate", "up", "down"};+ uint32_t hd = v->dim / v->heads;+ for (uint32_t l = 0; l < v->n_block; l++) {+ struct glayer *L = &v->gl[l];+ char n[64];+#define NORM(field, part, len) \+ snprintf(n, sizeof n, "v.blk.%u." part, l); \+ if (!(L->field = janas_vision_tensor(v, n, 0, 1, len))) \+ return "a norm of the encoder is missing";+ NORM(n_in, "in_norm", v->dim);+ NORM(n_post_att, "post_att_norm", v->dim);+ NORM(n_pre_ff, "pre_ff_norm", v->dim);+ NORM(n_post_ff, "post_ff_norm", v->dim);+ NORM(q_norm, "q_norm", hd);+ NORM(k_norm, "k_norm", hd);+#undef NORM+ snprintf(n, sizeof n, "v.blk.%u.clip", l);+ if (!(L->clip = janas_vision_tensor(v, n, 0, 1, GL_N * 4)))+ return "the clamps of the encoder are missing";+ for (int k = 0; k < GL_N; k++) {+ uint64_t rows = k == GL_GATE || k == GL_UP ? v->ff : v->dim;+ uint64_t cols = k == GL_DOWN ? v->ff : v->dim;+ snprintf(n, sizeof n, "v.blk.%u.%s", l, LIN[k]);+ const uint16_t *w = janas_vision_tensor(v, n, 30, rows, cols);+ if (!w)+ return "a matrix of the encoder is missing";+ if (!(L->t[k] = vk_transposed(w, rows, cols)))+ return "out of memory";+ }+ }+ return NULL;+}++void janas_vg_close(struct janas_vision *v)+{+ for (uint32_t l = 0; l < MAX_BLOCKS; l++)+ for (int k = 0; k < GL_N; k++)+ free(v->gl[l].t[k]);+ free(v->t_g_patch);+ free(v->t_g_proj);+ free(v->t_u_dense);+}++void janas_vg_size(const struct janas_vision *v, int w, int h, int max_tokens,+ int *rw, int *rh, int *nx, int *ny)+{+ uint32_t soft = v->max_soft;+ if (max_tokens > 0 && (uint32_t)max_tokens < soft) {+ soft = SOFT[0];+ for (size_t i = 0; i < sizeof SOFT / sizeof SOFT[0]; i++)+ if (SOFT[i] <= (uint32_t)max_tokens)+ soft = SOFT[i];+ }+ double k = v->pool_k, P = v->patch, side = k * P;+ double max_patches = (double)soft * k * k;+ double target = max_patches * P * P;+ double f = sqrt(target / ((double)w * h));+ int th = (int)(floor(f * h / side) * side);+ int tw = (int)(floor(f * w / side) * side);+ int longest = (int)((uint32_t)max_patches / (uint32_t)(k * k) * side);+ if (th == 0 && tw == 0)+ th = tw = (int)side;+ else if (th == 0) {+ th = (int)side;+ tw = (int)(floor((double)w / h) * side);+ if (tw > longest)+ tw = longest;+ } else if (tw == 0) {+ tw = (int)side;+ th = (int)(floor((double)h / w) * side);+ if (th > longest)+ th = longest;+ }+ *rw = tw;+ *rh = th;+ *nx = (int)(tw / side);+ *ny = (int)(th / side);+}++/* x clamped into y */+static void clamp_into(float *y, const float *x, size_t n, float lo, float hi)+{+ for (size_t i = 0; i < n; i++)+ y[i] = x[i] < lo ? lo : x[i] > hi ? hi : x[i];+}++/* y[n] (ldy apart) = clamp(W clamp(x)) with linear k's bounds; tmp holds+ n x cols floats */+static void clipped(struct janas_pool *pool, const struct glayer *L, int k,+ const float *x, float *tmp, float *y, uint32_t rows,+ uint32_t cols, uint32_t n, size_t ldy)+{+ const float *c = L->clip + 4 * k;+ clamp_into(tmp, x, (size_t)n * cols, c[0], c[1]);+ vk_matmul(pool, L->t[k], NULL, tmp, y, rows, cols, n, ldy);+ size_t ld = ldy ? ldy : rows;+ for (uint32_t t = 0; t < n; t++)+ clamp_into(y + t * ld, y + t * ld, rows, c[2], c[3]);+}++/* the rotary embedding of position p on half a head (len channels) */+static void rope_half(float *x, uint32_t len, float p, float theta)+{+ uint32_t q = len / 2;+ for (uint32_t i = 0; i < q; i++) {+ float inv = 1.0f / powf(theta, (float)(2 * i) / (float)len);+ float a = p * inv, cs = cosf(a), sn = sinf(a);+ float x0 = x[i], x1 = x[i + q];+ x[i] = x0 * cs - x1 * sn;+ x[i + q] = x1 * cs + x0 * sn;+ }+}++int janas_vg_encode(struct janas_vision *v, const struct janas_image *im,+ int max_tokens, struct janas_pool *pool, float **out,+ int *nx, int *ny, char *err, size_t err_len)+{+ if (v->unified)+ return janas_vgu_encode(v, im, max_tokens, pool, out, nx, ny, err,+ err_len);+ *out = NULL;+ int rw, rh;+ janas_vg_size(v, im->w, im->h, max_tokens, &rw, &rh, nx, ny);+ struct janas_image rs;+ const struct janas_image *src = im;+ if (rw != im->w || rh != im->h) {+ if (janas_image_resize(im, rw, rh, &rs) != 0) {+ snprintf(err, err_len, "out of memory");+ return -1;+ }+ src = &rs;+ }+ uint32_t P = v->patch, D = v->dim, K = v->pool_k, hd = D / v->heads;+ uint32_t gw = (uint32_t)rw / P, gh = (uint32_t)rh / P, n = gw * gh;+ uint32_t pd = 3 * P * P, wide = v->ff > 3 * D ? v->ff : 3 * D;+ if (pd > wide)+ wide = pd;+ float *pix = malloc(sizeof(float) * (size_t)n * pd);+ float *x = malloc(sizeof(float) * (size_t)n * D);+ float *h = malloc(sizeof(float) * (size_t)n * D);+ float *tmp = malloc(sizeof(float) * (size_t)n * wide);+ float *qkv = malloc(sizeof(float) * (size_t)n * 3 * D);+ float *g = malloc(sizeof(float) * (size_t)n * v->ff);+ float *u = malloc(sizeof(float) * (size_t)n * v->ff);+ float *buf = malloc(sizeof(float) * (size_t)n * D * 2);+ float *res = NULL;+ struct janas_pool *own = NULL;+ int rc = -1;+ if (!pool && !(pool = own = janas_pool_create(1, NULL))) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ if (!pix || !x || !h || !tmp || !qkv || !g || !u || !buf) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ /* patches in rows, each (row, column, channel), pixels in [-1, 1] */+ for (uint32_t r = 0; r < gh; r++)+ for (uint32_t c = 0; c < gw; c++) {+ float *p = pix + ((size_t)r * gw + c) * pd;+ for (uint32_t yy = 0; yy < P; yy++)+ for (uint32_t xx = 0; xx < P; xx++)+ for (uint32_t ch = 0; ch < 3; ch++)+ *p++ = src->rgb[((size_t)(r * P + yy) * (size_t)rw ++ (c * P + xx)) *+ 3 ++ ch] *+ (2.0f / 255.0f) -+ 1.0f;+ }+ vk_matmul(pool, v->t_g_patch, NULL, pix, x, D, pd, n, 0);+ for (uint32_t r = 0; r < gh; r++)+ for (uint32_t c = 0; c < gw; c++) {+ float *xi = x + ((size_t)r * gw + c) * D;+ const uint16_t *ex =+ v->g_pos + (size_t)(c < v->pos_size ? c : 0) * D;+ const uint16_t *ey =+ v->g_pos ++ ((size_t)v->pos_size + (r < v->pos_size ? r : 0)) * D;+ for (uint32_t k = 0; k < D; k++)+ xi[k] += vk_bf(ex[k]) + vk_bf(ey[k]);+ }+ for (uint32_t l = 0; l < v->n_block; l++) {+ const struct glayer *L = &v->gl[l];+ for (uint32_t i = 0; i < n; i++)+ vk_rms_norm(h + (size_t)i * D, x + (size_t)i * D, L->n_in, D,+ v->eps);+ clipped(pool, L, GL_Q, h, tmp, qkv, D, D, n, 3 * (size_t)D);+ clipped(pool, L, GL_K, h, tmp, qkv + D, D, D, n, 3 * (size_t)D);+ clipped(pool, L, GL_V, h, tmp, qkv + 2 * D, D, D, n, 3 * (size_t)D);+ /* per head: queries and keys normed and rotated, values normed */+ for (uint32_t i = 0; i < n; i++) {+ float col = (float)(i % gw), row = (float)(i / gw);+ float *q = qkv + (size_t)i * 3 * D;+ for (uint32_t hh = 0; hh < v->heads; hh++) {+ float *qh = q + hh * hd, *kh = q + D + hh * hd;+ float *vh = q + 2 * D + hh * hd;+ vk_rms_norm(qh, qh, L->q_norm, hd, v->eps);+ vk_rms_norm(kh, kh, L->k_norm, hd, v->eps);+ vk_rms_norm(vh, vh, NULL, hd, v->eps);+ rope_half(qh, hd / 2, col, v->theta);+ rope_half(qh + hd / 2, hd / 2, row, v->theta);+ rope_half(kh, hd / 2, col, v->theta);+ rope_half(kh + hd / 2, hd / 2, row, v->theta);+ }+ }+ vk_attention(pool, qkv, buf, h, n, D, v->heads, 1.0f);+ clipped(pool, L, GL_O, h, tmp, g, D, D, n, 0);+ for (uint32_t i = 0; i < n; i++) {+ float *o = g + (size_t)i * D;+ vk_rms_norm(o, o, L->n_post_att, D, v->eps);+ for (uint32_t k = 0; k < D; k++)+ x[(size_t)i * D + k] += o[k];+ }+ for (uint32_t i = 0; i < n; i++)+ vk_rms_norm(h + (size_t)i * D, x + (size_t)i * D, L->n_pre_ff, D,+ v->eps);+ clipped(pool, L, GL_GATE, h, tmp, g, v->ff, D, n, 0);+ clipped(pool, L, GL_UP, h, tmp, u, v->ff, D, n, 0);+ for (size_t k = 0; k < (size_t)n * v->ff; k++)+ g[k] = vk_gelu_tanh(g[k]) * u[k];+ clipped(pool, L, GL_DOWN, g, tmp, h, D, v->ff, n, 0);+ for (uint32_t i = 0; i < n; i++) {+ float *o = h + (size_t)i * D;+ vk_rms_norm(o, o, L->n_post_ff, D, v->eps);+ for (uint32_t k = 0; k < D; k++)+ x[(size_t)i * D + k] += o[k];+ }+ }+ /* the patches averaged K x K, row-major over the pooled grid */+ uint32_t pw2 = gw / K, ph2 = gh / K, m = pw2 * ph2;+ memset(h, 0, sizeof(float) * (size_t)m * D);+ for (uint32_t r = 0; r < gh; r++)+ for (uint32_t c = 0; c < gw; c++) {+ float *o = h + ((size_t)(r / K) * pw2 + c / K) * D;+ const float *xi = x + ((size_t)r * gw + c) * D;+ for (uint32_t k = 0; k < D; k++)+ o[k] += xi[k];+ }+ float scale = sqrtf((float)D) / (float)(K * K);+ for (uint32_t i = 0; i < m; i++) {+ float *o = h + (size_t)i * D;+ for (uint32_t k = 0; k < D; k++) {+ o[k] *= scale;+ if (v->std_bias)+ o[k] = (o[k] - v->std_bias[k]) * v->std_scale[k];+ }+ vk_rms_norm(o, o, NULL, D, v->eps);+ }+ res = malloc(sizeof(float) * (size_t)m * v->proj);+ if (!res) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ vk_matmul(pool, v->t_g_proj, NULL, h, res, v->proj, D, m, 0);+ *nx = (int)pw2;+ *ny = (int)ph2;+ *out = res;+ res = NULL;+ rc = 0;+done:+ free(res);+ free(pix);+ free(x);+ free(h);+ free(tmp);+ free(qkv);+ free(g);+ free(u);+ free(buf);+ if (own)+ janas_pool_destroy(own);+ if (src == &rs)+ janas_image_free(&rs);+ return rc;+}diff --git a/src/llm/vision_gemma_u.c b/src/llm/vision_gemma_u.cnew file mode 100644index 0000000..7241438--- /dev/null+++ b/src/llm/vision_gemma_u.c@@ -0,0 +1,152 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vision_gemma_u.c - Gemma 4 12B's vision, an embedder without blocks+ * (vision_impl.h): the picture sized as the other Gemma 4 encoders do it+ * (vision_gemma.c, janas_vg_size), cut into patches of 48x48 pixels (row,+ * column, channel; pixels in [0, 1]), each layer-normed, through a dense+ * layer with a bias, layer-normed, plus the learned embeddings of its+ * column and its row, layer-normed again; then RMS-normed without a scale+ * and projected to the language model's width.+ */+#include "vision_impl.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "common/image.h"+#include "common/pool.h"++const char *janas_vgu_open(struct janas_vision *v)+{+ struct janas_jns *j = &v->j;+ uint32_t ti = 0, ts = 0, te = 0, both = 0;+ if (janas_jns_meta_u32(j, "vision.embedding_length", &v->dim) ||+ janas_jns_meta_u32(j, "vision.patch_size", &v->patch) ||+ janas_jns_meta_u32(j, "vision.pooling_kernel_size", &v->pool_k) ||+ janas_jns_meta_u32(j, "vision.position_size", &v->pos_size) ||+ janas_jns_meta_u32(j, "vision.max_soft_tokens", &v->max_soft) ||+ janas_jns_meta_u32(j, "vision.projection_dim", &v->proj) ||+ janas_jns_meta_f32(j, "vision.attention.layer_norm_epsilon", &v->eps) ||+ janas_jns_meta_f32(j, "vision.patch_norm_epsilon", &v->u_eps))+ return "the encoder's sizes are missing";+ if (janas_jns_meta_u32(j, "vision.token.image", &ti) ||+ janas_jns_meta_u32(j, "vision.token.start", &ts) ||+ janas_jns_meta_u32(j, "vision.token.end", &te))+ return "the image tokens are missing";+ janas_jns_meta_u32(j, "vision.bidirectional", &both);+ v->unified = 1;+ v->both_ways = both != 0;+ v->tok_image = (int32_t)ti;+ v->tok_start = (int32_t)ts;+ v->tok_end = (int32_t)te;+ uint32_t side = v->patch * v->pool_k;+ if (v->patch < 2 || v->pool_k < 1 || side > 256 || !v->dim ||+ !v->pos_size || !v->max_soft || !v->proj)+ return "the encoder's sizes are out of range";+ uint64_t pd = 3ull * side * side;+ static const char *const LN[3] = {"v.u.ln1", "v.u.ln2", "v.u.ln3"};+ for (int k = 0; k < 3; k++) {+ char n[64];+ uint64_t len = k ? v->dim : pd;+ snprintf(n, sizeof n, "%s.weight", LN[k]);+ v->u_ln[k][0] = janas_vision_tensor(v, n, 0, 1, len);+ snprintf(n, sizeof n, "%s.bias", LN[k]);+ v->u_ln[k][1] = janas_vision_tensor(v, n, 0, 1, len);+ if (!v->u_ln[k][0] || !v->u_ln[k][1])+ return "a norm of the encoder is missing";+ }+ const uint16_t *dense = janas_vision_tensor(v, "v.u.dense", 30, v->dim, pd);+ v->u_dense_b = janas_vision_tensor(v, "v.u.dense.bias", 0, 1, v->dim);+ /* rows of (position, axis): x then y for each position */+ v->g_pos =+ janas_vision_tensor(v, "v.pos_embd", 30, 2ull * v->pos_size, v->dim);+ const uint16_t *proj =+ janas_vision_tensor(v, "v.proj", 30, v->proj, v->dim);+ if (!dense || !v->u_dense_b || !v->g_pos || !proj)+ return "a tensor of the encoder is missing or of another shape";+ v->t_u_dense = vk_transposed(dense, v->dim, pd);+ v->t_g_proj = vk_transposed(proj, v->proj, v->dim);+ if (!v->t_u_dense || !v->t_g_proj)+ return "out of memory";+ return NULL;+}++int janas_vgu_encode(struct janas_vision *v, const struct janas_image *im,+ int max_tokens, struct janas_pool *pool, float **out,+ int *nx, int *ny, char *err, size_t err_len)+{+ *out = NULL;+ int rw, rh;+ janas_vg_size(v, im->w, im->h, max_tokens, &rw, &rh, nx, ny);+ struct janas_image rs;+ const struct janas_image *src = im;+ if (rw != im->w || rh != im->h) {+ if (janas_image_resize(im, rw, rh, &rs) != 0) {+ snprintf(err, err_len, "out of memory");+ return -1;+ }+ src = &rs;+ }+ uint32_t S = v->patch * v->pool_k, D = v->dim;+ uint32_t gw = (uint32_t)*nx, gh = (uint32_t)*ny, n = gw * gh;+ uint32_t pd = 3 * S * S;+ float *pix = malloc(sizeof(float) * (size_t)n * pd);+ float *x = malloc(sizeof(float) * (size_t)n * D);+ float *res = NULL;+ struct janas_pool *own = NULL;+ int rc = -1;+ if (!pool && !(pool = own = janas_pool_create(1, NULL))) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ if (!pix || !x) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ /* patches in rows, each (row, column, channel) in [0, 1], normed */+ for (uint32_t r = 0; r < gh; r++)+ for (uint32_t c = 0; c < gw; c++) {+ float *p = pix + ((size_t)r * gw + c) * pd, *p0 = p;+ for (uint32_t yy = 0; yy < S; yy++) {+ const uint8_t *row =+ src->rgb + ((size_t)(r * S + yy) * (size_t)rw + c * S) * 3;+ for (uint32_t k = 0; k < 3 * S; k++)+ *p++ = row[k] * (1.0f / 255.0f);+ }+ vk_layer_norm(p0, p0, v->u_ln[0][0], v->u_ln[0][1], pd, v->u_eps);+ }+ vk_matmul(pool, v->t_u_dense, v->u_dense_b, pix, x, D, pd, n, 0);+ for (uint32_t r = 0; r < gh; r++)+ for (uint32_t c = 0; c < gw; c++) {+ float *xi = x + ((size_t)r * gw + c) * D;+ vk_layer_norm(xi, xi, v->u_ln[1][0], v->u_ln[1][1], D, v->u_eps);+ uint32_t px = c < v->pos_size ? c : v->pos_size - 1;+ uint32_t py = r < v->pos_size ? r : v->pos_size - 1;+ const uint16_t *ex = v->g_pos + (size_t)px * 2 * D;+ const uint16_t *ey = v->g_pos + ((size_t)py * 2 + 1) * D;+ for (uint32_t k = 0; k < D; k++)+ xi[k] += vk_bf(ex[k]) + vk_bf(ey[k]);+ vk_layer_norm(xi, xi, v->u_ln[2][0], v->u_ln[2][1], D, v->u_eps);+ vk_rms_norm(xi, xi, NULL, D, v->eps);+ }+ res = malloc(sizeof(float) * (size_t)n * v->proj);+ if (!res) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ vk_matmul(pool, v->t_g_proj, NULL, x, res, v->proj, D, n, 0);+ *out = res;+ res = NULL;+ rc = 0;+done:+ free(res);+ free(pix);+ free(x);+ if (own)+ janas_pool_destroy(own);+ if (src == &rs)+ janas_image_free(&rs);+ return rc;+}diff --git a/src/llm/vision_impl.h b/src/llm/vision_impl.hnew file mode 100644index 0000000..874eb66--- /dev/null+++ b/src/llm/vision_impl.h@@ -0,0 +1,92 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vision_impl.h - the inside of the vision encoders: the encoder as loaded+ * (vision.c: Qwen3.5 and Qwen3-VL; vision_gemma.c: Gemma 4) and the+ * kernels they share (vision_kernels.c).+ */+#ifndef JANAS_LLM_VISION_IMPL_H+#define JANAS_LLM_VISION_IMPL_H++#include <stddef.h>+#include <stdint.h>++#include "jns.h"+#include "vision.h"+#include "vision_kernels.h"++#define MAX_BLOCKS 64++enum { VISION_QWEN = 1, VISION_GEMMA = 2 };++/* a block of Qwen's ViT */+struct vlayer {+ const float *ln1_w, *ln1_b, *ln2_w, *ln2_b;+ const uint16_t *qkv, *out, *up, *down;+ const float *qkv_b, *out_b, *up_b, *down_b;+ /* the same matrices transposed, for the products (owned) */+ uint16_t *t_qkv, *t_out, *t_up, *t_down;+};++/* a block of Gemma 4's: its norms, its linears transposed (owned) and the+ bounds each clamps its input and output to (q k v o gate up down, each+ in_min in_max out_min out_max) */+enum { GL_Q, GL_K, GL_V, GL_O, GL_GATE, GL_UP, GL_DOWN, GL_N };+struct glayer {+ const float *n_in, *n_post_att, *n_pre_ff, *n_post_ff, *q_norm, *k_norm;+ const float *clip; /* GL_N x 4 */+ uint16_t *t[GL_N];+};++struct janas_vision {+ struct janas_jns j;+ uint8_t *data; /* the resident tensors */+ int kind; /* VISION_QWEN, VISION_GEMMA */+ int both_ways; /* the pictures' tokens attend both ways */+ uint32_t n_block, dim, ff, heads, patch, tpatch, merge, npos, proj;+ uint32_t min_pixels, max_pixels;+ float eps, theta, mean[3], stdv[3];+ int32_t tok_image, tok_start, tok_end;+ /* Qwen */+ const uint16_t *patch_w, *pos, *m_fc1, *m_fc2;+ const float *patch_b, *m_ln_w, *m_ln_b, *m_fc1_b, *m_fc2_b;+ struct vlayer l[MAX_BLOCKS];+ uint16_t *t_patch, *t_fc1, *t_fc2; /* transposed, owned */+ /* Gemma 4: the pooling, the positions' table (2 x pos_size rows: x,+ then y), the soft tokens a picture at most, the standardization+ (NULL: none), the projection to the language model (transposed) */+ uint32_t pool_k, pos_size, max_soft;+ const uint16_t *g_pos;+ const float *std_bias, *std_scale;+ struct glayer gl[MAX_BLOCKS];+ uint16_t *t_g_patch, *t_g_proj;+ /* Gemma 4 12B's, without blocks (vision_gemma_u.c): a 48x48 patch+ layer-normed, through a dense layer, normed, plus the positions,+ normed (u_ln: weight and bias of each norm, u_eps theirs) */+ int unified;+ float u_eps;+ const float *u_ln[3][2], *u_dense_b;+ uint16_t *t_u_dense;+};++/* a resident tensor of that name, type (0 float, 30 bfloat16) and shape,+ or NULL (vision.c) */+const void *janas_vision_tensor(struct janas_vision *v, const char *name,+ uint32_t type, uint64_t rows, uint64_t cols);++/* vision_gemma.c: the rest of the open (NULL, or why not), the size of a+ picture, its encoding, what it owns freed */+const char *janas_vg_open(struct janas_vision *v);+void janas_vg_size(const struct janas_vision *v, int w, int h, int max_tokens,+ int *rw, int *rh, int *nx, int *ny);+int janas_vg_encode(struct janas_vision *v, const struct janas_image *im,+ int max_tokens, struct janas_pool *pool, float **out,+ int *nx, int *ny, char *err, size_t err_len);+void janas_vg_close(struct janas_vision *v);+/* vision_gemma_u.c: the same for Gemma 4 12B's encoder without blocks */+const char *janas_vgu_open(struct janas_vision *v);+int janas_vgu_encode(struct janas_vision *v, const struct janas_image *im,+ int max_tokens, struct janas_pool *pool, float **out,+ int *nx, int *ny, char *err, size_t err_len);++#endifdiff --git a/src/llm/vision_kernels.c b/src/llm/vision_kernels.cnew file mode 100644index 0000000..5d16427--- /dev/null+++ b/src/llm/vision_kernels.c@@ -0,0 +1,447 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vision_kernels.c - what the vision encoders share (vision_kernels.h):+ * the products, the attention over every patch, the norms and activations;+ * Whisper's encoder (whisper.c) too.+ */+#include "vision_kernels.h"++#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "common/pool.h"+#include "quant.h"+#include "vmath.h"++/* ---- the products: C[j][i] = sum_k B[j][k] A[k][i] (+ bias[i]) ----++ B's rows are tokens or queries (float, k contiguous); A's rows run over+ the outputs (i contiguous): the weights transposed once at open+ (bfloat16), or keys transposed and values as they are (float). A tile of+ four rows of B and twenty-four outputs keeps twelve sums in registers+ over k, a weight read once for four tokens and a token's number once for+ twenty-four outputs; the threads take blocks of outputs, whose part of A+ stays in the cache while every token goes through it. Float throughout:+ the activations of a ViT have outliers that eight bits lose. The weights+ are bfloat16, or half precision where the model was trained in it+ (Whisper's are its originals). */++struct gemm {+ int kind; /* A: 0 float, 1 bfloat16, 2 half */+ const void *a; /* rows of A (k), lda apart */+ size_t lda;+ const float *b; /* rows of B (j), ldb apart */+ size_t ldb;+ float *c; /* C[j * ldc + i] */+ size_t ldc;+ const float *bias;+ uint32_t ni, nj, k;+};++static float a_at(const struct gemm *g, uint32_t k, uint32_t i)+{+ if (!g->kind)+ return ((const float *)g->a)[(size_t)k * g->lda + i];+ if (g->kind == 2)+ return janas_fp16_to_fp32(+ ((const uint16_t *)g->a)[(size_t)k * g->lda + i]);+ uint32_t u = (uint32_t)((const uint16_t *)g->a)[(size_t)k * g->lda + i]+ << 16;+ float f;+ memcpy(&f, &u, 4);+ return f;+}++static void gemm_plain(const struct gemm *g, uint32_t i0, uint32_t i1)+{+ for (uint32_t j = 0; j < g->nj; j++) {+ const float *bj = g->b + (size_t)j * g->ldb;+ float *cj = g->c + (size_t)j * g->ldc;+ for (uint32_t i = i0; i < i1; i++)+ cj[i] = g->bias ? g->bias[i] : 0;+ for (uint32_t k = 0; k < g->k; k++)+ for (uint32_t i = i0; i < i1; i++)+ cj[i] += bj[k] * a_at(g, k, i);+ }+}++#if defined(__x86_64__)+#define AVX2 __attribute__((target("avx2,fma,f16c")))++AVX2 static inline __m256 load_a(const struct gemm *g, uint32_t k, uint32_t i)+{+ if (!g->kind)+ return _mm256_loadu_ps((const float *)g->a + (size_t)k * g->lda + i);+ __m128i h = _mm_loadu_si128(+ (const __m128i *)((const uint16_t *)g->a + (size_t)k * g->lda + i));+ if (g->kind == 2)+ return _mm256_cvtph_ps(h);+ return _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_cvtepu16_epi32(h), 16));+}++AVX2 static inline float hsum(__m256 v)+{+ __m128 s =+ _mm_add_ps(_mm256_castps256_ps128(v), _mm256_extractf128_ps(v, 1));+ s = _mm_add_ps(s, _mm_movehl_ps(s, s));+ s = _mm_add_ss(s, _mm_movehdup_ps(s));+ return _mm_cvtss_f32(s);+}++/* outputs i .. i + 8 * nv of rows j .. j + nb of B */+AVX2 static inline void tile(const struct gemm *g, uint32_t j, uint32_t nb,+ uint32_t i, uint32_t nv)+{+ __m256 acc[4][3];+ for (uint32_t r = 0; r < 4; r++)+ for (uint32_t v = 0; v < 3; v++)+ acc[r][v] = g->bias && v < nv ? _mm256_loadu_ps(g->bias + i + 8 * v)+ : _mm256_setzero_ps();+ const float *b0 = g->b + (size_t)j * g->ldb;+ for (uint32_t k = 0; k < g->k; k++) {+ __m256 a[3];+ for (uint32_t v = 0; v < nv; v++)+ a[v] = load_a(g, k, i + 8 * v);+ for (uint32_t r = 0; r < nb; r++) {+ __m256 x = _mm256_set1_ps(b0[(size_t)r * g->ldb + k]);+ for (uint32_t v = 0; v < nv; v++)+ acc[r][v] = _mm256_fmadd_ps(x, a[v], acc[r][v]);+ }+ }+ for (uint32_t r = 0; r < nb; r++)+ for (uint32_t v = 0; v < nv; v++)+ _mm256_storeu_ps(g->c + (size_t)(j + r) * g->ldc + i + 8 * v,+ acc[r][v]);+}++/* the whole tile, four rows of B and twenty-four outputs, its twelve sums+ named so they stay in registers */+AVX2 static inline void tile_full(const struct gemm *g, uint32_t j, uint32_t i)+{+ __m256 c00, c01, c02, c10, c11, c12, c20, c21, c22, c30, c31, c32;+ if (g->bias) {+ c00 = c10 = c20 = c30 = _mm256_loadu_ps(g->bias + i);+ c01 = c11 = c21 = c31 = _mm256_loadu_ps(g->bias + i + 8);+ c02 = c12 = c22 = c32 = _mm256_loadu_ps(g->bias + i + 16);+ } else {+ c00 = c01 = c02 = c10 = c11 = c12 = _mm256_setzero_ps();+ c20 = c21 = c22 = c30 = c31 = c32 = _mm256_setzero_ps();+ }+ const float *b0 = g->b + (size_t)j * g->ldb, *b1 = b0 + g->ldb;+ const float *b2 = b1 + g->ldb, *b3 = b2 + g->ldb;+ for (uint32_t k = 0; k < g->k; k++) {+ __m256 a0 = load_a(g, k, i), a1 = load_a(g, k, i + 8);+ __m256 a2 = load_a(g, k, i + 16);+ __m256 x = _mm256_broadcast_ss(b0 + k);+ c00 = _mm256_fmadd_ps(x, a0, c00);+ c01 = _mm256_fmadd_ps(x, a1, c01);+ c02 = _mm256_fmadd_ps(x, a2, c02);+ x = _mm256_broadcast_ss(b1 + k);+ c10 = _mm256_fmadd_ps(x, a0, c10);+ c11 = _mm256_fmadd_ps(x, a1, c11);+ c12 = _mm256_fmadd_ps(x, a2, c12);+ x = _mm256_broadcast_ss(b2 + k);+ c20 = _mm256_fmadd_ps(x, a0, c20);+ c21 = _mm256_fmadd_ps(x, a1, c21);+ c22 = _mm256_fmadd_ps(x, a2, c22);+ x = _mm256_broadcast_ss(b3 + k);+ c30 = _mm256_fmadd_ps(x, a0, c30);+ c31 = _mm256_fmadd_ps(x, a1, c31);+ c32 = _mm256_fmadd_ps(x, a2, c32);+ }+ float *o = g->c + (size_t)j * g->ldc + i;+ _mm256_storeu_ps(o, c00);+ _mm256_storeu_ps(o + 8, c01);+ _mm256_storeu_ps(o + 16, c02);+ o += g->ldc;+ _mm256_storeu_ps(o, c10);+ _mm256_storeu_ps(o + 8, c11);+ _mm256_storeu_ps(o + 16, c12);+ o += g->ldc;+ _mm256_storeu_ps(o, c20);+ _mm256_storeu_ps(o + 8, c21);+ _mm256_storeu_ps(o + 16, c22);+ o += g->ldc;+ _mm256_storeu_ps(o, c30);+ _mm256_storeu_ps(o + 8, c31);+ _mm256_storeu_ps(o + 16, c32);+}++/* outputs [i0, i1): i0 and i1 multiples of 8, or i1 the end */+AVX2 static void gemm_avx2(const struct gemm *g, uint32_t i0, uint32_t i1)+{+ uint32_t i8 = i0 + (i1 - i0) / 8 * 8;+ /* a block of outputs at a time, every token through it */+ for (uint32_t ib = i0; ib < i8; ib += 192) {+ uint32_t ie = ib + 192 < i8 ? ib + 192 : i8;+ for (uint32_t j = 0; j < g->nj; j += 4) {+ uint32_t nb = g->nj - j < 4 ? g->nj - j : 4;+ for (uint32_t i = ib; i < ie; i += 24) {+ uint32_t nv = (ie - i) / 8 < 3 ? (ie - i) / 8 : 3;+ if (nb == 4 && nv == 3)+ tile_full(g, j, i);+ else+ tile(g, j, nb, i, nv);+ }+ }+ }+ if (i8 < i1) { /* the last outputs, fewer than eight */+ struct gemm t = *g;+ gemm_plain(&t, i8, i1);+ }+}++/* x[k] = e^(x[k] - mx) for the first n / 8 * 8, their sum added to *sum;+ returns how many it did */+AVX2 static uint32_t janas_exp_sub_avx2(float *x, uint32_t n, float mx,+ double *sum)+{+ uint32_t n8 = n / 8 * 8;+ __m256 vm = _mm256_set1_ps(mx), acc = _mm256_setzero_ps();+ for (uint32_t k = 0; k < n8; k += 8) {+ __m256 e = janas_exp8(_mm256_sub_ps(_mm256_loadu_ps(x + k), vm));+ _mm256_storeu_ps(x + k, e);+ acc = _mm256_add_ps(acc, e);+ }+ *sum += hsum(acc);+ return n8;+}+#endif++static int fast_cpu(void)+{+#if defined(__x86_64__)+ return __builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma") &&+ __builtin_cpu_supports("f16c");+#else+ return 0;+#endif+}++static void gemm_cols(const struct gemm *g, uint32_t i0, uint32_t i1)+{+#if defined(__x86_64__)+ if (fast_cpu()) {+ gemm_avx2(g, i0, i1);+ return;+ }+#endif+ gemm_plain(g, i0, i1);+}++static void gemm_worker(void *arg, int tid, int nt)+{+ const struct gemm *g = arg;+ /* outputs in multiples of 24, the tiles whole */+ uint32_t tiles = (g->ni + 23) / 24;+ uint32_t per = (tiles + (uint32_t)nt - 1) / (uint32_t)nt;+ uint32_t i0 = (uint32_t)tid * per * 24, i1 = i0 + per * 24;+ if (i1 > g->ni)+ i1 = g->ni;+ if (i0 < i1)+ gemm_cols(g, i0, i1);+}++static void matmul(struct janas_pool *pool, int kind, const uint16_t *wt,+ const float *b, const float *x, float *y, uint32_t rows,+ uint32_t cols, uint32_t n, size_t ldy)+{+ struct gemm g = {.kind = kind,+ .a = wt,+ .lda = rows,+ .b = x,+ .ldb = cols,+ .c = y,+ .ldc = ldy ? ldy : rows,+ .bias = b,+ .ni = rows,+ .nj = n,+ .k = cols};+ janas_pool_run(pool, gemm_worker, &g);+}++/* y[n][rows] = w x[n][cols] + b, wt the weights transposed (cols x rows,+ bfloat16) */+void vk_matmul(struct janas_pool *pool, const uint16_t *wt, const float *b,+ const float *x, float *y, uint32_t rows, uint32_t cols,+ uint32_t n, size_t ldy)+{+ matmul(pool, 1, wt, b, x, y, rows, cols, n, ldy);+}++/* the same with the weights in half precision */+void vk_matmul_f16(struct janas_pool *pool, const uint16_t *wt, const float *b,+ const float *x, float *y, uint32_t rows, uint32_t cols,+ uint32_t n, size_t ldy)+{+ matmul(pool, 2, wt, b, x, y, rows, cols, n, ldy);+}++/* ---- attention over every patch, a block of queries at a time ---- */++#define QB 128 /* queries a block: keys and values read once for them */++struct att {+ const float *qkv; /* n x 3 dim: q, k, v of every patch */+ float *kt; /* heads x hd x n: the keys transposed */+ float *vh; /* heads x n x hd: the values, a head together */+ float *out; /* n x dim */+ uint32_t n, dim, heads, hd;+ float scale;+};++static void kt_worker(void *arg, int tid, int nt)+{+ struct att *a = arg;+ for (uint32_t h = (uint32_t)tid; h < a->heads; h += (uint32_t)nt)+ for (uint32_t k = 0; k < a->n; k++) {+ const float *key =+ a->qkv + (size_t)k * 3 * a->dim + a->dim + (size_t)h * a->hd;+ float *col = a->kt + (size_t)h * a->hd * a->n + k;+ for (uint32_t d = 0; d < a->hd; d++)+ col[(size_t)d * a->n] = key[d];+ memcpy(a->vh + ((size_t)h * a->n + k) * a->hd, key + a->dim,+ sizeof(float) * a->hd);+ }+}++/* each row of s (len n) scaled, exponentiated and normalized in place */+static void softmax_rows(float *s, uint32_t rows, uint32_t n, float scale)+{+ for (uint32_t r = 0; r < rows; r++) {+ float *x = s + (size_t)r * n, mx = -INFINITY;+ for (uint32_t k = 0; k < n; k++) {+ x[k] *= scale;+ if (x[k] > mx)+ mx = x[k];+ }+ double sum = 0;+ uint32_t k = 0;+#if defined(__x86_64__)+ if (fast_cpu())+ k = janas_exp_sub_avx2(x, n, mx, &sum);+#endif+ for (; k < n; k++) {+ x[k] = janas_exp_det(x[k] - mx);+ sum += x[k];+ }+ float inv = (float)(1.0 / sum);+ for (k = 0; k < n; k++)+ x[k] *= inv;+ }+}++static void att_worker(void *arg, int tid, int nt)+{+ struct att *a = arg;+ float *s = malloc(sizeof(float) * QB * a->n);+ if (!s)+ return;+ float scale = a->scale;+ size_t stride = 3 * (size_t)a->dim;+ uint32_t blocks = (a->n + QB - 1) / QB;+ size_t total = (size_t)blocks * a->heads;+ for (size_t t = (size_t)tid; t < total; t += (size_t)nt) {+ uint32_t h = (uint32_t)(t / blocks), q0 = (uint32_t)(t % blocks) * QB;+ uint32_t nq = a->n - q0 < QB ? a->n - q0 : QB;+ /* scores: the block's queries as B, the keys transposed as A */+ struct gemm sc = {.kind = 0,+ .a = a->kt + (size_t)h * a->hd * a->n,+ .lda = a->n,+ .b = a->qkv + (size_t)q0 * stride + (size_t)h * a->hd,+ .ldb = stride,+ .c = s,+ .ldc = a->n,+ .ni = a->n,+ .nj = nq,+ .k = a->hd};+ gemm_cols(&sc, 0, a->n);+ softmax_rows(s, nq, a->n, scale);+ /* the output: the weights as B, the values as A */+ struct gemm ov = {.kind = 0,+ .a = a->vh + (size_t)h * a->n * a->hd,+ .lda = a->hd,+ .b = s,+ .ldb = a->n,+ .c = a->out + (size_t)q0 * a->dim + (size_t)h * a->hd,+ .ldc = a->dim,+ .ni = a->hd,+ .nj = nq,+ .k = a->n};+ gemm_cols(&ov, 0, a->hd);+ }+ free(s);+}++void vk_layer_norm(float *y, const float *x, const float *w, const float *b,+ uint32_t n, float eps)+{+ double mean = 0, var = 0;+ for (uint32_t i = 0; i < n; i++)+ mean += x[i];+ mean /= n;+ for (uint32_t i = 0; i < n; i++)+ var += (x[i] - mean) * (x[i] - mean);+ var /= n;+ float inv = (float)(1.0 / sqrt(var + eps));+ for (uint32_t i = 0; i < n; i++)+ y[i] = ((float)(x[i] - mean)) * inv * w[i] + b[i];+}++float vk_gelu_tanh(float x)+{+ return 0.5f * x *+ (1.0f + tanhf(0.7978845608028654f * (x + 0.044715f * x * x * x)));+}++float vk_gelu_erf(float x)+{+ return 0.5f * x * (1.0f + erff(x * 0.7071067811865476f));+}++float vk_bf(uint16_t b)+{+ uint32_t u = (uint32_t)b << 16;+ float f;+ memcpy(&f, &u, 4);+ return f;+}++void vk_attention(struct janas_pool *pool, const float *qkv, float *buf,+ float *out, uint32_t n, uint32_t dim, uint32_t heads,+ float scale)+{+ struct att a = {.qkv = qkv,+ .kt = buf,+ .vh = buf + (size_t)n * dim,+ .out = out,+ .n = n,+ .dim = dim,+ .heads = heads,+ .hd = dim / heads,+ .scale = scale};+ janas_pool_run(pool, kt_worker, &a);+ janas_pool_run(pool, att_worker, &a);+}++uint16_t *vk_transposed(const uint16_t *w, uint64_t rows, uint64_t cols)+{+ uint16_t *t = malloc(2 * (size_t)(rows * cols));+ for (uint64_t r = 0; t && r < rows; r++)+ for (uint64_t c = 0; c < cols; c++)+ t[c * rows + r] = w[r * cols + c];+ return t;+}++void vk_rms_norm(float *y, const float *x, const float *w, uint32_t n,+ float eps)+{+ double ss = 0;+ for (uint32_t i = 0; i < n; i++)+ ss += (double)x[i] * x[i];+ float inv = (float)pow(ss / n + eps, -0.5);+ for (uint32_t i = 0; i < n; i++)+ y[i] = x[i] * inv * (w ? w[i] : 1.0f);+}diff --git a/src/llm/vision_kernels.h b/src/llm/vision_kernels.hnew file mode 100644index 0000000..af683f3--- /dev/null+++ b/src/llm/vision_kernels.h@@ -0,0 +1,39 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vision_kernels.h - the float kernels of the encoders (vision_kernels.c):+ * the tiled products, the attention of every token over every other, the+ * norms and activations. The vision encoders and Whisper's share them.+ */+#ifndef JANAS_LLM_VISION_KERNELS_H+#define JANAS_LLM_VISION_KERNELS_H++#include <stddef.h>+#include <stdint.h>++struct janas_pool;+/* y[n] (ldy apart, 0: rows) = w x[n] + b, wt the weights transposed (cols+ x rows, bfloat16), every product on the pool */+void vk_matmul(struct janas_pool *pool, const uint16_t *wt, const float *b,+ const float *x, float *y, uint32_t rows, uint32_t cols,+ uint32_t n, size_t ldy);+/* the same, wt in half precision */+void vk_matmul_f16(struct janas_pool *pool, const uint16_t *wt, const float *b,+ const float *x, float *y, uint32_t rows, uint32_t cols,+ uint32_t n, size_t ldy);+/* attention of every patch over every other, qkv n x 3 dim (q, k, v), the+ output n x dim; buf 2 n dim floats of room */+void vk_attention(struct janas_pool *pool, const float *qkv, float *buf,+ float *out, uint32_t n, uint32_t dim, uint32_t heads,+ float scale);+uint16_t *vk_transposed(const uint16_t *w, uint64_t rows, uint64_t cols);+void vk_layer_norm(float *y, const float *x, const float *w, const float *b,+ uint32_t n, float eps);+/* x / sqrt(mean(x^2) + eps), times w (NULL: none) */+void vk_rms_norm(float *y, const float *x, const float *w, uint32_t n,+ float eps);+float vk_gelu_tanh(float x);+float vk_gelu_erf(float x);+float vk_bf(uint16_t b);++#endifdiff --git a/src/llm/whisper.c b/src/llm/whisper.cnew file mode 100644index 0000000..f9065f9--- /dev/null+++ b/src/llm/whisper.c@@ -0,0 +1,382 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * whisper.c - a Whisper model opened (whisper.h): its sizes, special tokens+ * and languages from the metadata, its tensors read into memory as the+ * converter laid them out, the tables of the spectrogram, the state.+ */+#define _GNU_SOURCE+#include "whisper.h"++#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <unistd.h>++#include "common/pool.h"+#include "common/sysinfo.h"+#include "tokenizer.h"++#define T_F32 0+#define T_F16 1++/* a resident tensor of that name, type and shape (rows x cols, cols the+ length of a stored row), or NULL */+static const void *tensor(struct janas_whisper *w, const char *name,+ uint32_t type, uint64_t rows, uint64_t cols)+{+ for (uint64_t i = 0; i < w->j.h.n_tensor; i++) {+ const struct janas_jns_tensor *t = &w->j.tensors[i];+ if (strncmp(t->name, name, sizeof t->name))+ continue;+ uint64_t n = t->n_dims == 2 ? t->dims[0] * t->dims[1] : t->dims[0];+ uint64_t esz = type == T_F16 ? 2 : 4;+ if (t->type != type || n != rows * cols || t->bytes != n * esz ||+ (t->n_dims == 2 && t->dims[0] != cols))+ return NULL;+ return w->data + (t->offset - w->j.h.resident_offset);+ }+ return NULL;+}++static const char *read_data(struct janas_whisper *w)+{+ struct janas_jns *j = &w->j;+ w->data = malloc(j->h.resident_bytes ? j->h.resident_bytes : 1);+ if (!w->data)+ return "out of memory";+ size_t got = 0;+ while (got < j->h.resident_bytes) {+ ssize_t k = pread(j->fd, w->data + got, j->h.resident_bytes - got,+ (off_t)(j->h.resident_offset + got));+ if (k <= 0)+ break;+ got += (size_t)k;+ }+ return got == j->h.resident_bytes ? NULL : "cannot read the weights";+}++static const char *sizes(struct janas_whisper *w)+{+ struct janas_jns *j = &w->j;+ static const struct {+ const char *key;+ size_t at;+ } U[] = {+#define F(k, f) {k, offsetof(struct janas_whisper, f)}+ F("whisper.dim", dim),+ F("whisper.audio.layers", a_layers),+ F("whisper.audio.heads", a_heads),+ F("whisper.audio.ffn", a_ffn),+ F("whisper.audio.ctx", a_ctx),+ F("whisper.text.layers", t_layers),+ F("whisper.text.heads", t_heads),+ F("whisper.text.ffn", t_ffn),+ F("whisper.text.ctx", t_ctx),+ F("whisper.vocab", vocab),+ F("whisper.n_mels", n_mels),+ F("whisper.n_fft", n_fft),+ F("whisper.hop", hop),+ F("whisper.sample_rate", rate),+ F("whisper.chunk_seconds", chunk),+ F("whisper.max_initial_timestamp", max_initial_ts),+#undef F+ };+ for (size_t i = 0; i < sizeof U / sizeof *U; i++)+ if (janas_jns_meta_u32(j, U[i].key, (uint32_t *)((char *)w + U[i].at)))+ return "the model's sizes are missing";+ static const struct {+ const char *key;+ size_t at;+ } S[] = {+#define F(k, f) {"whisper.token." k, offsetof(struct janas_whisper, f)}+ F("eot", eot),+ F("sot", sot),+ F("translate", translate),+ F("transcribe", transcribe),+ F("sot_lm", sot_lm),+ F("sot_prev", sot_prev),+ F("no_speech", no_speech),+ F("no_timestamps", no_timestamps),+ F("timestamp_begin", ts_begin),+#undef F+ };+ for (size_t i = 0; i < sizeof S / sizeof *S; i++) {+ uint32_t v;+ if (janas_jns_meta_u32(j, S[i].key, &v) || v >= w->vocab)+ return "the special tokens are missing";+ *(int32_t *)((char *)w + S[i].at) = (int32_t)v;+ }+ uint32_t multi = 0;+ janas_jns_meta_u32(j, "whisper.multilingual", &multi);+ w->multilingual = multi != 0;+ uint64_t nl;+ if (janas_jns_meta_strings(j, "whisper.languages", &nl, &w->langs) || !nl ||+ nl > 200 || w->sot + nl >= w->vocab)+ return "the languages are missing";+ w->n_lang = (uint32_t)nl;+ if (janas_jns_meta_ints(j, "whisper.suppress", &w->n_suppress,+ &w->suppress))+ return "the tokens to suppress are missing";+ if (janas_jns_meta_ints(j, "whisper.begin_suppress", &w->n_begin_suppress,+ &w->begin_suppress))+ w->n_begin_suppress = 0;+ /* the alignment heads; without them OpenAI's default: every head of+ the decoder's second half */+ const int32_t *ah = NULL;+ uint64_t nah = 0;+ if (janas_jns_meta_ints(j, "whisper.alignment_heads", &nah, &ah))+ nah = 0;+ uint64_t pairs = nah / 2;+ if (!pairs)+ pairs = (uint64_t)(w->t_layers - w->t_layers / 2) * w->t_heads;+ w->aheads = malloc(sizeof(int32_t) * 2 * (pairs ? pairs : 1));+ if (!w->aheads)+ return "out of memory";+ w->n_aheads = 0;+ if (nah >= 2) {+ for (uint64_t i = 0; i + 1 < nah; i += 2)+ if (ah[i] >= 0 && (uint32_t)ah[i] < w->t_layers && ah[i + 1] >= 0 &&+ (uint32_t)ah[i + 1] < w->t_heads) {+ w->aheads[2 * w->n_aheads] = ah[i];+ w->aheads[2 * w->n_aheads + 1] = ah[i + 1];+ w->n_aheads++;+ }+ } else {+ for (uint32_t l = w->t_layers / 2; l < w->t_layers; l++)+ for (uint32_t hh = 0; hh < w->t_heads; hh++) {+ w->aheads[2 * w->n_aheads] = (int32_t)l;+ w->aheads[2 * w->n_aheads + 1] = (int32_t)hh;+ w->n_aheads++;+ }+ }+ if (!w->dim || !w->a_heads || !w->t_heads || w->dim % w->a_heads ||+ w->dim % w->t_heads || !w->a_layers || !w->t_layers ||+ w->a_layers > 64 || w->t_layers > 64 || w->t_heads > 64 || !w->a_ctx ||+ !w->t_ctx || !w->n_mels || w->n_fft < 2 || !w->hop || !w->rate ||+ w->a_ctx * 2 * w->hop != w->chunk * w->rate ||+ w->ts_begin >= (int32_t)w->vocab)+ return "the model's sizes are out of range";+ return NULL;+}++static const char *block(struct janas_whisper *w, struct wlayer *L,+ const char *pre, int cross, uint32_t ffn)+{+ uint32_t d = w->dim;+ char n[64];+ int ok = 1;+#define V(field, part) \+ snprintf(n, sizeof n, "%s." part, pre); \+ L->field = tensor(w, n, T_F32, 1, d); \+ ok = ok && L->field+#define M(field, part, r, c) \+ snprintf(n, sizeof n, "%s." part, pre); \+ L->field = tensor(w, n, T_F16, r, c); \+ ok = ok && L->field+ V(ln_w, "ln.w");+ V(ln_b, "ln.b");+ M(q, "q", d, d);+ M(k, "k", d, d);+ M(v, "v", d, d);+ M(o, "o", d, d);+ V(q_b, "q.b");+ V(v_b, "v.b");+ V(o_b, "o.b");+ if (cross) {+ V(xln_w, "xln.w");+ V(xln_b, "xln.b");+ M(xq, "xq", d, d);+ M(xk, "xk", d, d);+ M(xv, "xv", d, d);+ M(xo, "xo", d, d);+ V(xq_b, "xq.b");+ V(xv_b, "xv.b");+ V(xo_b, "xo.b");+ }+ V(mln_w, "mln.w");+ V(mln_b, "mln.b");+ V(fc2_b, "fc2.b");+ M(fc1, "fc1", d, ffn);+ M(fc2, "fc2", ffn, d);+ snprintf(n, sizeof n, "%s.fc1.b", pre);+ L->fc1_b = tensor(w, n, T_F32, 1, ffn);+ ok = ok && L->fc1_b;+#undef V+#undef M+ return ok ? NULL : "a tensor of a block is missing or of another shape";+}++static const char *tensors(struct janas_whisper *w)+{+ uint32_t d = w->dim, bins = w->n_fft / 2 + 1;+ w->mel_filters = tensor(w, "mel_filters", T_F32, w->n_mels, bins);+ w->conv1 = tensor(w, "e.conv1", T_F16, 3ull * w->n_mels, d);+ w->conv1_b = tensor(w, "e.conv1.b", T_F32, 1, d);+ w->conv2 = tensor(w, "e.conv2", T_F16, 3ull * d, d);+ w->conv2_b = tensor(w, "e.conv2.b", T_F32, 1, d);+ w->e_pos = tensor(w, "e.pos", T_F32, 1, (uint64_t)w->a_ctx * d);+ w->e_ln_w = tensor(w, "e.ln.w", T_F32, 1, d);+ w->e_ln_b = tensor(w, "e.ln.b", T_F32, 1, d);+ w->tok = tensor(w, "d.tok", T_F16, d, w->vocab);+ w->d_pos = tensor(w, "d.pos", T_F32, 1, (uint64_t)w->t_ctx * d);+ w->d_ln_w = tensor(w, "d.ln.w", T_F32, 1, d);+ w->d_ln_b = tensor(w, "d.ln.b", T_F32, 1, d);+ if (!w->mel_filters || !w->conv1 || !w->conv1_b || !w->conv2 ||+ !w->conv2_b || !w->e_pos || !w->e_ln_w || !w->e_ln_b || !w->tok ||+ !w->d_pos || !w->d_ln_w || !w->d_ln_b)+ return "a tensor is missing or of another shape";+ w->enc = calloc(w->a_layers, sizeof *w->enc);+ w->dec = calloc(w->t_layers, sizeof *w->dec);+ if (!w->enc || !w->dec)+ return "out of memory";+ const char *why = NULL;+ char pre[16];+ for (uint32_t l = 0; !why && l < w->a_layers; l++) {+ snprintf(pre, sizeof pre, "e.%u", l);+ why = block(w, &w->enc[l], pre, 0, w->a_ffn);+ }+ for (uint32_t l = 0; !why && l < w->t_layers; l++) {+ snprintf(pre, sizeof pre, "d.%u", l);+ why = block(w, &w->dec[l], pre, 1, w->t_ffn);+ }+ return why;+}++/* the DFT's rows with the periodic Hann window folded in */+static const char *tables(struct janas_whisper *w)+{+ uint32_t N = w->n_fft, bins = N / 2 + 1;+ w->dft_cos = malloc(sizeof(float) * bins * N);+ w->dft_sin = malloc(sizeof(float) * bins * N);+ if (!w->dft_cos || !w->dft_sin)+ return "out of memory";+ for (uint32_t k = 0; k < bins; k++)+ for (uint32_t n = 0; n < N; n++) {+ double win = 0.5 - 0.5 * cos(2 * M_PI * n / N);+ /* k n mod N keeps the angle small and exact */+ double a = 2 * M_PI * (double)((uint64_t)k * n % N) / N;+ w->dft_cos[k * N + n] = (float)(win * cos(a));+ w->dft_sin[k * N + n] = (float)(win * sin(a));+ }+ return NULL;+}++static const char *state(struct janas_whisper *w)+{+ size_t d = w->dim, cross = (size_t)w->t_layers * w->a_ctx * d;+ size_t self = (size_t)w->t_layers * w->t_ctx * d;+ w->xk = malloc(sizeof(float) * cross);+ w->xv = malloc(sizeof(float) * cross);+ w->kc = malloc(sizeof(float) * self);+ w->vc = malloc(sizeof(float) * self);+ w->x = malloc(sizeof(float) * w->t_ctx * d);+ w->h = malloc(sizeof(float) * w->t_ctx * d);+ w->q = malloc(sizeof(float) * w->t_ctx * d);+ w->att = malloc(sizeof(float) * w->t_ctx * d);+ w->ff = malloc(sizeof(float) * w->t_ctx * (size_t)w->t_ffn);+ if (!w->xk || !w->xv || !w->kc || !w->vc || !w->x || !w->h || !w->q ||+ !w->att || !w->ff)+ return "out of memory";+ return NULL;+}++struct janas_whisper *janas_whisper_open(const char *path, int n_threads,+ char *err, size_t err_len)+{+ struct janas_whisper *w = calloc(1, sizeof(*w));+ if (!w) {+ snprintf(err, err_len, "out of memory");+ return NULL;+ }+ if (janas_jns_open(path, &w->j, err, err_len) != 0) {+ free(w);+ return NULL;+ }+ const char *why = NULL;+ if (strncmp(w->j.h.arch, "whisper", sizeof w->j.h.arch))+ why = "not a Whisper model";+ if (!why)+ why = sizes(w);+ if (!why)+ why = read_data(w);+ if (!why)+ why = tensors(w);+ if (!why)+ why = tables(w);+ if (!why)+ why = state(w);+ if (!why) {+ w->tk = janas_tokenizer_create(&w->j, err, err_len);+ if (!w->tk) {+ janas_whisper_close(w);+ return NULL;+ }+ }+ if (!why) {+ /* one thread a performance core of those the caller may use (its+ threads inherit the caller's CPUs: a caller a chat model pinned+ to one gets one) */+ int n = n_threads;+ struct janas_cpu_layout lay;+ if (n <= 0)+ n = janas_cpu_layout(NULL, 0, &lay) == 0 ? lay.n_p_cores : 1;+ w->pool = janas_pool_create(n > 0 ? n : 1, NULL);+ if (!w->pool)+ why = "cannot start the threads";+ }+ if (why) {+ snprintf(err, err_len, "%s: %s", path, why);+ janas_whisper_close(w);+ return NULL;+ }+ return w;+}++void janas_whisper_close(struct janas_whisper *w)+{+ if (!w)+ return;+ if (w->pool)+ janas_pool_destroy(w->pool);+ if (w->tk)+ janas_tokenizer_destroy(w->tk);+ free(w->enc);+ free(w->dec);+ free(w->dft_cos);+ free(w->dft_sin);+ free(w->xk);+ free(w->xv);+ free(w->kc);+ free(w->vc);+ free(w->x);+ free(w->h);+ free(w->q);+ free(w->att);+ free(w->ff);+ free(w->aheads);+ free(w->data);+ janas_jns_close(&w->j);+ free(w);+}++void janas_whisper_lang(const struct janas_whisper *w, uint32_t i, char *buf,+ size_t cap)+{+ const uint8_t *p = w->langs;+ if (!cap)+ return;+ buf[0] = 0;+ for (uint32_t k = 0; k < w->n_lang; k++) {+ uint64_t len;+ memcpy(&len, p, 8);+ if (k == i) {+ size_t n = len < cap - 1 ? (size_t)len : cap - 1;+ memcpy(buf, p + 8, n);+ buf[n] = 0;+ return;+ }+ p += 8 + len;+ }+}diff --git a/src/llm/whisper.h b/src/llm/whisper.hnew file mode 100644index 0000000..aa23ad6--- /dev/null+++ b/src/llm/whisper.h@@ -0,0 +1,170 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * whisper.h - Whisper, speech to text (OpenAI's model, from a JNS file made+ * by tools/hf2jns_whisper): the log-mel spectrogram of the sound+ * (whisper_mel.c), the encoder over thirty seconds of it and the decoder a+ * token at a time (whisper_net.c), the transcription of a whole recording+ * window by window, with its times (whisper_run.c). Inside the library.+ */+#ifndef JANAS_LLM_WHISPER_H+#define JANAS_LLM_WHISPER_H++#include <stddef.h>+#include <stdint.h>++#include "jns.h"++struct janas_pool;+struct janas_tokenizer;++/* a block's tensors: matrices in half precision, transposed (a row per+ input), the rest float; x* the attention over the encoder's output */+struct wlayer {+ const float *ln_w, *ln_b, *q_b, *v_b, *o_b;+ const uint16_t *q, *k, *v, *o;+ const float *xln_w, *xln_b, *xq_b, *xv_b, *xo_b;+ const uint16_t *xq, *xk, *xv, *xo;+ const float *mln_w, *mln_b, *fc1_b, *fc2_b;+ const uint16_t *fc1, *fc2;+};++struct janas_whisper {+ struct janas_jns j;+ uint8_t *data; /* the resident tensors */+ uint32_t dim, a_layers, a_heads, a_ffn, a_ctx;+ uint32_t t_layers, t_heads, t_ffn, t_ctx, vocab;+ uint32_t n_mels, n_fft, hop, rate, chunk; /* chunk: seconds a window */+ /* the special tokens */+ int32_t eot, sot, translate, transcribe, sot_lm, sot_prev, no_speech;+ int32_t no_timestamps, ts_begin;+ uint32_t n_lang; /* languages, their tokens after sot */+ const uint8_t *langs; /* their codes (GGUF strings) */+ int multilingual;+ const int32_t *suppress, *begin_suppress;+ uint64_t n_suppress, n_begin_suppress;+ uint32_t max_initial_ts; /* the first timestamp at most, its index */+ /* the decoder's heads whose attention over the sound follows the+ words (layer, head), from the model or OpenAI's default */+ int32_t *aheads;+ uint32_t n_aheads;+ /* the spectrogram */+ const float *mel_filters; /* n_mels x (n_fft / 2 + 1) */+ float *dft_cos, *dft_sin; /* (n_fft / 2 + 1) x n_fft, the window in */+ /* the encoder */+ const uint16_t *conv1, *conv2;+ const float *conv1_b, *conv2_b, *e_pos, *e_ln_w, *e_ln_b;+ struct wlayer *enc, *dec;+ /* the decoder */+ const uint16_t *tok; /* dim x vocab: the token embeddings transposed */+ const float *d_pos, *d_ln_w, *d_ln_b;+ struct janas_tokenizer *tk;+ struct janas_pool *pool;+ /* the state: the encoder's keys and values for each decoder block+ (t_layers x a_ctx x dim), the decoder's own (t_layers x t_ctx x+ dim), the buffers of a pass */+ float *xk, *xv, *kc, *vc;+ float *x, *h, *q, *att, *ff; /* t_ctx rows each (ff: t_ctx x t_ffn) */+ /* when set, a decoder pass leaves the attention scores (before the+ softmax) of the alignment heads over the sound: n_aheads x n x a_ctx */+ float *cap;+};++/* whisper.c: the model opened with n_threads (0: the machine's cores), or+ NULL with the reason in err */+struct janas_whisper *janas_whisper_open(const char *path, int n_threads,+ char *err, size_t err_len);+void janas_whisper_close(struct janas_whisper *w);+/* the code of language i ("en"), into buf */+void janas_whisper_lang(const struct janas_whisper *w, uint32_t i, char *buf,+ size_t cap);++/* whisper_mel.c: the normalized log-mel spectrogram of n samples (mono, at+ w->rate) followed by pad zeros: *frames = (n + pad) / hop columns,+ out[m * *frames + f] (malloc'd), or NULL when out of memory */+float *janas_whisper_mel(struct janas_whisper *w, const float *pcm, size_t n,+ size_t pad, uint32_t *frames);++/* whisper_net.c: the encoder over a window of the spectrogram (rows of+ stride columns): 2 a_ctx columns from off, those from off + len on taken+ as zeros; it leaves the keys and values of every decoder block over it.+ 0 or -1 (out of memory) */+int janas_whisper_encode(struct janas_whisper *w, const float *mel,+ uint32_t stride, uint32_t off, uint32_t len);+/* the decoder over n tokens at positions past .. past + n: the logits of+ the last into logits (vocab), or of each (n x vocab) when all; 0 or -1+ (past + n beyond the context) */+int janas_whisper_decode(struct janas_whisper *w, const int32_t *tok,+ uint32_t n, uint32_t past, float *logits, int all);++/* whisper_run.c: a whole recording transcribed */+struct janas_whisper_opts {+ const char *language; /* its code ("it"); NULL or "": found out */+ int translate; /* into English */+ const char *prompt; /* text that comes before (names, a style) */+ float temperature; /* the first tried; 0: the most likely tokens,+ higher ones only where those repeat themselves+ or are unlikely */+ uint64_t seed; /* of the sampling at temperatures above 0 */+ volatile int *stop; /* set to stop between windows (may be NULL) */+ int words; /* the words' times, as OpenAI's finds them */+ /* the stretches of the recording to read (pairs of samples, start and+ end, in order; a voice activity detector's), or none: all of it */+ const size_t *regions;+ size_t n_regions;+ /* called with each segment once it is final (its text, times, words+ readable in the result being written), in order */+ void (*on_segment)(void *user, uint32_t index);+ void *user;+};++struct janas_whisper_word {+ double t0, t1; /* seconds */+ char *text; /* with its leading space, punctuation joined */+ float probability;+};++struct janas_whisper_seg {+ double t0, t1; /* seconds */+ char *text;+ int32_t *tokens; /* the text's */+ float *logprobs; /* each token's log-probability */+ uint32_t n_tokens;+ float avg_logprob, no_speech_prob, temperature, compression_ratio;+ struct janas_whisper_word *words; /* when asked for */+ uint32_t n_words;+};++struct janas_whisper_result {+ char language[16]; /* its code */+ double duration; /* seconds of sound */+ struct janas_whisper_seg *segs;+ uint32_t n_segs;+ char *text; /* the segments' text together, trimmed */+};++/* n samples (mono, at w->rate): 0, or -1 with the reason in err (also+ when stopped) */+int janas_whisper_transcribe(struct janas_whisper *w, const float *pcm,+ size_t n, const struct janas_whisper_opts *o,+ struct janas_whisper_result *r, char *err,+ size_t err_len);+void janas_whisper_result_free(struct janas_whisper_result *r);++/* whisper_align.c: the times of the words of segments segs[first..n) of+ the window encoded last (from offset seconds, frames of the sound in+ it), as OpenAI's timing.py finds them (cross-attention of the alignment+ heads, dynamic time warping), their segments' times adjusted to them;+ *last_speech carried from window to window. 0, or -1 when out of+ memory or the decoder fails */+int janas_whisper_align(struct janas_whisper *w, int lang, int translate,+ struct janas_whisper_seg *segs, uint32_t first,+ uint32_t n, double offset, uint32_t frames,+ double *last_speech);+/* the English name of a language ("italian") from its code, or NULL */+const char *janas_whisper_lang_name(const char *code);+/* the model's language of that code or English name ("it", "Italian"),+ its index, or -1 */+int janas_whisper_lang_index(const struct janas_whisper *w, const char *s);++#endifdiff --git a/src/llm/whisper_align.c b/src/llm/whisper_align.cnew file mode 100644index 0000000..bee8575--- /dev/null+++ b/src/llm/whisper_align.c@@ -0,0 +1,589 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * whisper_align.c - the times of the words, as OpenAI's timing.py finds+ * them: the window's text decoded again after the start of transcript,+ * its language, its task and "no timestamps", keeping the scores of the+ * alignment heads' attention over the sound; their softmax over the+ * sound's frames, standardized across the tokens, a median filter of 7+ * frames, their mean; the path of least cost through it (dynamic time+ * warping) gives each token the frame where it starts; tokens joined into+ * words as OpenAI's tokenizer joins them, punctuation into its word, and+ * OpenAI's corrections of words too long at pauses and at the segments'+ * ends.+ */+#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "tokenizer.h"+#include "whisper.h"++#define TOKENS_PER_SECOND 50.0+#define MEDFILT 7++static const char PUNCTUATION[] = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";+static const char PREPEND[] = "\"'\xe2\x80\x9c\xc2\xbf([{-";+static const char APPEND[] = "\"'.\xe3\x80\x82,\xef\xbc\x8c!\xef\xbc\x81?"+ "\xef\xbc\x9f:\xef\xbc\x9a\xe2\x80\x9d)]}"+ "\xe3\x80\x81";+static const char SENTENCE_END[] = ".\xe3\x80\x82!\xef\xbc\x81?\xef\xbc\x9f";++/* Python's "a in b" for strings: a substring (the empty one too) */+static int in(const char *a, size_t n, const char *b)+{+ if (!n)+ return 1;+ size_t bn = strlen(b);+ for (size_t i = 0; i + n <= bn; i++)+ if (!memcmp(b + i, a, n))+ return 1;+ return 0;+}++static int is_space(char c)+{+ return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' ||+ c == '\f';+}++/* str.strip(): the part between the spaces */+static const char *strip(const char *s, size_t *n)+{+ size_t a = 0, b = strlen(s);+ while (a < b && is_space(s[a]))+ a++;+ while (b > a && is_space(s[b - 1]))+ b--;+ *n = b - a;+ return s + a;+}++/* bytes that end a whole UTF-8 sequence, or that no more bytes could make+ one of (what Python decodes with a replacement character for good) */+static int utf8_done(const unsigned char *s, size_t n)+{+ size_t i = 0;+ while (i < n) {+ unsigned c = s[i];+ size_t need = c < 0x80 ? 0+ : c < 0xC2 ? (size_t)-1+ : c < 0xE0 ? 1+ : c < 0xF0 ? 2+ : c < 0xF5 ? 3+ : (size_t)-1;+ if (need == (size_t)-1) {+ i++;+ continue;+ }+ size_t k = 1;+ while (k <= need && i + k < n && (s[i + k] & 0xC0) == 0x80)+ k++;+ if (k <= need && i + k == n)+ return 0; /* a sequence the next token may end */+ i += k;+ }+ return 1;+}++struct word {+ char *text;+ int32_t *tok;+ uint32_t n_tok;+ double t0, t1;+ float prob;+};++static void words_free(struct word *w, uint32_t n)+{+ for (uint32_t i = 0; i < n; i++) {+ free(w[i].text);+ free(w[i].tok);+ }+ free(w);+}++static int append(struct word *w, const char *s, size_t sn, const int32_t *t,+ uint32_t tn)+{+ size_t old = w->text ? strlen(w->text) : 0;+ char *x = realloc(w->text, old + sn + 1);+ if (!x)+ return -1;+ memcpy(x + old, s, sn);+ x[old + sn] = 0;+ w->text = x;+ int32_t *y = realloc(w->tok, sizeof(int32_t) * (w->n_tok + tn + 1));+ if (!y)+ return -1;+ memcpy(y + w->n_tok, t, sizeof(int32_t) * tn);+ w->tok = y;+ w->n_tok += tn;+ return 0;+}++/* tokenizer.split_to_word_tokens: the words of tokens (the end of text+ last), into *out; their count, or -1 */+static long split_words(struct janas_whisper *w, const int32_t *tok, uint32_t n,+ int no_spaces, struct word **out)+{+ struct word *ws = calloc(n + 1, sizeof *ws);+ long nw = 0;+ char *buf = NULL;+ size_t cap = 0, len = 0;+ uint32_t start = 0;+ if (!ws)+ return -1;+ for (uint32_t i = 0; i < n; i++) {+ char none[1];+ size_t k = janas_tokenizer_decode(w->tk, tok[i], none, 0);+ if (len + k + 1 > cap) {+ size_t nc = (len + k + 1) * 2;+ char *b = realloc(buf, nc);+ if (!b)+ goto fail;+ buf = b;+ cap = nc;+ }+ janas_tokenizer_decode(w->tk, tok[i], buf + len, k);+ len += k;+ buf[len] = 0;+ int special = tok[i] >= w->eot;+ if (!special && !utf8_done((const unsigned char *)buf, len) &&+ i + 1 < n)+ continue;+ /* a subword: tokens start .. i */+ size_t sn;+ const char *st = strip(buf, &sn);+ int new_word = no_spaces || special || buf[0] == ' ' ||+ in(st, sn, PUNCTUATION) || nw == 0;+ if (new_word)+ nw++;+ if (append(&ws[nw - 1], buf, len, tok + start, i + 1 - start) != 0)+ goto fail;+ len = 0;+ start = i + 1;+ }+ free(buf);+ *out = ws;+ return nw;+fail:+ free(buf);+ words_free(ws, n + 1);+ return -1;+}++static int cmp_float(const void *a, const void *b)+{+ float x = *(const float *)a, y = *(const float *)b;+ return (x > y) - (x < y);+}++static int cmp_double(const void *a, const void *b)+{+ double x = *(const double *)a, y = *(const double *)b;+ return (x > y) - (x < y);+}++/* dtw(-matrix) of n x m: the path's (row, column) pairs, from the start;+ their count, or -1 */+static long dtw(const float *mat, uint32_t n, uint32_t m, uint32_t **ti,+ uint32_t **fi)+{+ size_t W = (size_t)m + 1;+ float *cost = malloc(sizeof(float) * (n + 1) * W);+ signed char *trace = malloc((size_t)(n + 1) * W);+ *ti = malloc(sizeof(uint32_t) * (n + m + 2));+ *fi = malloc(sizeof(uint32_t) * (n + m + 2));+ long cnt = -1;+ if (!cost || !trace || !*ti || !*fi)+ goto end;+ for (size_t i = 0; i < (n + 1) * W; i++) {+ cost[i] = INFINITY;+ trace[i] = -1;+ }+ cost[0] = 0;+ for (uint32_t j = 1; j <= m; j++)+ for (uint32_t i = 1; i <= n; i++) {+ float c0 = cost[(i - 1) * W + j - 1], c1 = cost[(i - 1) * W + j];+ float c2 = cost[i * W + j - 1], c;+ int t;+ if (c0 < c1 && c0 < c2) {+ c = c0;+ t = 0;+ } else if (c1 < c0 && c1 < c2) {+ c = c1;+ t = 1;+ } else {+ c = c2;+ t = 2;+ }+ cost[i * W + j] =+ (float)(-(double)mat[(size_t)(i - 1) * m + j - 1] + c);+ trace[i * W + j] = (signed char)t;+ }+ for (uint32_t j = 0; j <= m; j++)+ trace[j] = 2;+ for (uint32_t i = 0; i <= n; i++)+ trace[i * W] = 1;+ uint32_t i = n, j = m;+ long k = 0;+ while (i > 0 || j > 0) {+ (*ti)[k] = i - 1;+ (*fi)[k] = j - 1;+ k++;+ int t = trace[i * W + j];+ if (t == 0) {+ i--;+ j--;+ } else if (t == 1) {+ i--;+ } else {+ j--;+ }+ }+ for (long a = 0, b = k - 1; a < b; a++, b--) {+ uint32_t x = (*ti)[a];+ (*ti)[a] = (*ti)[b];+ (*ti)[b] = x;+ x = (*fi)[a];+ (*fi)[a] = (*fi)[b];+ (*fi)[b] = x;+ }+ cnt = k;+end:+ free(cost);+ free(trace);+ if (cnt < 0) {+ free(*ti);+ free(*fi);+ *ti = *fi = NULL;+ }+ return cnt;+}++static void merge_punctuation(struct word *al, uint32_t n)+{+ if (n < 2)+ return;+ /* prepended: into the word after */+ long i = (long)n - 2, j = (long)n - 1;+ while (i >= 0) {+ struct word *p = &al[i], *f = &al[j];+ size_t sn;+ const char *st = strip(p->text, &sn);+ if (p->text[0] == ' ' && in(st, sn, PREPEND)) {+ size_t a = strlen(p->text), b = strlen(f->text);+ char *x = malloc(a + b + 1);+ int32_t *t = malloc(sizeof(int32_t) * (p->n_tok + f->n_tok + 1));+ if (x && t) {+ memcpy(x, p->text, a);+ memcpy(x + a, f->text, b + 1);+ memcpy(t, p->tok, sizeof(int32_t) * p->n_tok);+ memcpy(t + p->n_tok, f->tok, sizeof(int32_t) * f->n_tok);+ free(f->text);+ free(f->tok);+ f->text = x;+ f->tok = t;+ f->n_tok += p->n_tok;+ p->text[0] = 0;+ p->n_tok = 0;+ } else {+ free(x);+ free(t);+ }+ } else {+ j = i;+ }+ i--;+ }+ /* appended: into the word before */+ i = 0;+ j = 1;+ while ((uint32_t)j < n) {+ struct word *p = &al[i], *f = &al[j];+ size_t pl = strlen(p->text);+ if (!(pl && p->text[pl - 1] == ' ') &&+ in(f->text, strlen(f->text), APPEND)) {+ if (append(p, f->text, strlen(f->text), f->tok, f->n_tok) == 0) {+ f->text[0] = 0;+ f->n_tok = 0;+ }+ } else {+ i = j;+ }+ j++;+ }+}++static double round2(double x)+{+ return round(x * 100.0) / 100.0;+}++int janas_whisper_align(struct janas_whisper *w, int lang, int translate,+ struct janas_whisper_seg *segs, uint32_t first,+ uint32_t n_segs, double offset, uint32_t frames,+ double *last_speech)+{+ if (first >= n_segs)+ return 0;+ /* the text tokens of the window's segments */+ uint32_t n_text = 0;+ for (uint32_t s = first; s < n_segs; s++)+ for (uint32_t i = 0; i < segs[s].n_tokens; i++)+ n_text += segs[s].tokens[i] < w->eot;+ int32_t sot_seq[3];+ uint32_t n_sot = 0;+ sot_seq[n_sot++] = w->sot;+ if (w->multilingual) {+ sot_seq[n_sot++] = w->sot + 1 + (lang >= 0 ? lang : 0);+ sot_seq[n_sot++] = translate ? w->translate : w->transcribe;+ }+ uint32_t n = n_sot + 1 + n_text + 1;+ if (!n_text || n > w->t_ctx)+ return 0;+ uint32_t nf = frames / 2;+ if (nf > w->a_ctx)+ nf = w->a_ctx;+ if (!nf)+ return 0;+ int32_t *tok = malloc(sizeof(int32_t) * n);+ float *logits = malloc(sizeof(float) * (size_t)n * w->vocab);+ float *cap = malloc(sizeof(float) * (size_t)w->n_aheads * n * w->a_ctx);+ float *mat = malloc(sizeof(float) * (size_t)n * nf);+ float *probs = malloc(sizeof(float) * (n_text + 1));+ struct word *ws = NULL;+ long nw = 0;+ uint32_t *ti = NULL, *fi = NULL;+ int rc = -1;+ if (!tok || !logits || !cap || !mat || !probs)+ goto end;+ uint32_t k = 0;+ for (uint32_t i = 0; i < n_sot; i++)+ tok[k++] = sot_seq[i];+ tok[k++] = w->no_timestamps;+ for (uint32_t s = first; s < n_segs; s++)+ for (uint32_t i = 0; i < segs[s].n_tokens; i++)+ if (segs[s].tokens[i] < w->eot)+ tok[k++] = segs[s].tokens[i];+ tok[k++] = w->eot;+ w->cap = cap;+ int dec = janas_whisper_decode(w, tok, n, 0, logits, 1);+ w->cap = NULL;+ if (dec != 0)+ goto end;+ /* each text token's probability among the text tokens */+ for (uint32_t i = 0; i < n_text; i++) {+ const float *l = logits + (size_t)(n_sot + i) * w->vocab;+ float mx = -INFINITY;+ for (int32_t v = 0; v < w->eot; v++)+ if (l[v] > mx)+ mx = l[v];+ double sum = 0;+ for (int32_t v = 0; v < w->eot; v++)+ sum += exp((double)l[v] - mx);+ probs[i] = (float)(exp((double)l[tok[n_sot + 1 + i]] - mx) / sum);+ }+ /* the heads' softmax over the frames, standardized over the tokens,+ median-filtered, averaged */+ memset(mat, 0, sizeof(float) * (size_t)n * nf);+ float *row = malloc(sizeof(float) * (nf + MEDFILT));+ float *col = malloc(sizeof(float) * n);+ if (!row || !col) {+ free(row);+ free(col);+ goto end;+ }+ for (uint32_t h = 0; h < w->n_aheads; h++) {+ float *c = cap + (size_t)h * n * w->a_ctx;+ for (uint32_t i = 0; i < n; i++) {+ float *r = c + (size_t)i * w->a_ctx, mx = -INFINITY;+ for (uint32_t f = 0; f < nf; f++)+ if (r[f] > mx)+ mx = r[f];+ float sum = 0;+ for (uint32_t f = 0; f < nf; f++) {+ r[f] = expf(r[f] - mx);+ sum += r[f];+ }+ for (uint32_t f = 0; f < nf; f++)+ r[f] /= sum;+ }+ for (uint32_t f = 0; f < nf; f++) {+ double mean = 0, var = 0;+ for (uint32_t i = 0; i < n; i++)+ mean += c[(size_t)i * w->a_ctx + f];+ mean /= n;+ for (uint32_t i = 0; i < n; i++) {+ double dv = c[(size_t)i * w->a_ctx + f] - mean;+ var += dv * dv;+ }+ float sd = (float)sqrt(var / n);+ for (uint32_t i = 0; i < n; i++) {+ float *v = &c[(size_t)i * w->a_ctx + f];+ *v = (float)((*v - (float)mean) / sd);+ }+ }+ int pad = MEDFILT / 2;+ for (uint32_t i = 0; i < n; i++) {+ const float *r = c + (size_t)i * w->a_ctx;+ float *out = mat + (size_t)i * nf;+ if (nf <= (uint32_t)pad) {+ for (uint32_t f = 0; f < nf; f++)+ out[f] += r[f];+ continue;+ }+ /* reflected at both ends */+ for (int f = -pad; f < (int)nf + pad; f++) {+ int g = f < 0 ? -f : f >= (int)nf ? 2 * ((int)nf - 1) - f : f;+ row[f + pad] = r[g];+ }+ for (uint32_t f = 0; f < nf; f++) {+ float win[MEDFILT];+ memcpy(win, row + f, sizeof win);+ qsort(win, MEDFILT, sizeof(float), cmp_float);+ out[f] += win[MEDFILT / 2];+ }+ }+ }+ free(row);+ free(col);+ for (size_t i = 0; i < (size_t)n * nf; i++)+ mat[i] /= (float)w->n_aheads;+ /* the rows of "no timestamps" and the text: the time path */+ long np = dtw(mat + (size_t)n_sot * nf, n_text + 1, nf, &ti, &fi);+ if (np < 0)+ goto end;+ double *jump = malloc(sizeof(double) * (n_text + 2));+ if (!jump)+ goto end;+ uint32_t nj = 0;+ for (long p = 0; p < np; p++)+ if (p == 0 || ti[p] != ti[p - 1])+ jump[nj++] = fi[p] / TOKENS_PER_SECOND;+ /* the words, the end of text last */+ const char *code = NULL;+ char lc[16] = "en";+ if (w->multilingual && lang >= 0)+ janas_whisper_lang(w, (uint32_t)lang, lc, sizeof lc);+ code = lc;+ int no_spaces = !strcmp(code, "zh") || !strcmp(code, "ja") ||+ !strcmp(code, "th") || !strcmp(code, "lo") ||+ !strcmp(code, "my") || !strcmp(code, "yue");+ nw = split_words(w, tok + n_sot + 1, n_text + 1, no_spaces, &ws);+ if (nw < 0) {+ free(jump);+ goto end;+ }+ if (nw <= 1) {+ free(jump);+ rc = 0;+ goto end;+ }+ uint32_t at = 0, nal = (uint32_t)nw - 1;+ for (uint32_t i = 0; i < nal; i++) {+ uint32_t a = at, b = at + ws[i].n_tok;+ ws[i].t0 = a < nj ? jump[a] : jump[nj - 1];+ ws[i].t1 = b < nj ? jump[b] : jump[nj - 1];+ double sp = 0;+ for (uint32_t t = a; t < b && t < n_text; t++)+ sp += probs[t];+ ws[i].prob = b > a ? (float)(sp / (b - a)) : 0;+ at = b;+ }+ free(jump);+ /* OpenAI's corrections */+ double *dur = malloc(sizeof(double) * nal);+ if (!dur)+ goto end;+ uint32_t nd = 0;+ for (uint32_t i = 0; i < nal; i++)+ if (ws[i].t1 - ws[i].t0 != 0)+ dur[nd++] = ws[i].t1 - ws[i].t0;+ double median = 0;+ if (nd) {+ qsort(dur, nd, sizeof(double), cmp_double);+ median = nd & 1 ? dur[nd / 2] : (dur[nd / 2 - 1] + dur[nd / 2]) / 2;+ }+ free(dur);+ if (median > 0.7)+ median = 0.7;+ double max_dur = median * 2;+ if (nd)+ for (uint32_t i = 1; i < nal; i++)+ if (ws[i].t1 - ws[i].t0 > max_dur) {+ if (in(ws[i].text, strlen(ws[i].text), SENTENCE_END))+ ws[i].t1 = ws[i].t0 + max_dur;+ else if (in(ws[i - 1].text, strlen(ws[i - 1].text),+ SENTENCE_END))+ ws[i].t0 = ws[i].t1 - max_dur;+ }+ merge_punctuation(ws, nal);+ /* into the segments */+ uint32_t wi = 0;+ for (uint32_t s = first; s < n_segs; s++) {+ struct janas_whisper_seg *sg = &segs[s];+ uint32_t seg_text = 0, saved = 0, cnt = 0;+ for (uint32_t i = 0; i < sg->n_tokens; i++)+ seg_text += sg->tokens[i] < w->eot;+ sg->words = calloc(nal ? nal : 1, sizeof *sg->words);+ if (!sg->words)+ goto end;+ while (wi < nal && saved < seg_text) {+ struct word *t = &ws[wi];+ if (t->text[0]) {+ struct janas_whisper_word *o = &sg->words[cnt];+ o->text = strdup(t->text);+ if (!o->text)+ goto end;+ o->t0 = round2(offset + t->t0);+ o->t1 = round2(offset + t->t1);+ o->probability = t->prob;+ cnt++;+ sg->n_words = cnt;+ }+ saved += t->n_tok;+ wi++;+ }+ if (!cnt)+ continue;+ struct janas_whisper_word *wd = sg->words;+ if (wd[0].t1 - *last_speech > median * 4 &&+ (wd[0].t1 - wd[0].t0 > max_dur ||+ (cnt > 1 && wd[1].t1 - wd[0].t0 > max_dur * 2))) {+ if (cnt > 1 && wd[1].t1 - wd[1].t0 > max_dur) {+ double boundary = wd[1].t1 / 2 > wd[1].t1 - max_dur+ ? wd[1].t1 / 2+ : wd[1].t1 - max_dur;+ wd[0].t1 = wd[1].t0 = boundary;+ }+ wd[0].t0 = wd[0].t1 - max_dur > 0 ? wd[0].t1 - max_dur : 0;+ }+ if (sg->t0 < wd[0].t1 && sg->t0 - 0.5 > wd[0].t0) {+ double v = wd[0].t1 - median < sg->t0 ? wd[0].t1 - median : sg->t0;+ wd[0].t0 = v > 0 ? v : 0;+ } else {+ sg->t0 = wd[0].t0;+ }+ if (sg->t1 > wd[cnt - 1].t0 && sg->t1 + 0.5 < wd[cnt - 1].t1) {+ double v = wd[cnt - 1].t0 + median;+ wd[cnt - 1].t1 = v > sg->t1 ? v : sg->t1;+ } else {+ sg->t1 = wd[cnt - 1].t1;+ }+ *last_speech = sg->t1;+ }+ rc = 0;+end:+ if (ws)+ words_free(ws, nw > 0 ? (uint32_t)nw : 0);+ free(ti);+ free(fi);+ free(tok);+ free(logits);+ free(cap);+ free(mat);+ free(probs);+ return rc;+}diff --git a/src/llm/whisper_mel.c b/src/llm/whisper_mel.cnew file mode 100644index 0000000..b0d168c--- /dev/null+++ b/src/llm/whisper_mel.c@@ -0,0 +1,127 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * whisper_mel.c - Whisper's log-mel spectrogram (whisper.h), as OpenAI's+ * log_mel_spectrogram computes it: the sound followed by zeros, reflected+ * at both ends by half a window (torch.stft's center), a periodic Hann+ * window of n_fft samples every hop, the power of each frequency, the last+ * frame dropped; the model's mel filters over the powers, log10 (at least+ * 1e-10), nothing below the largest less 8, then (x + 4) / 4.+ */+#include "whisper.h"++#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "common/pool.h"++struct mel_job {+ const struct janas_whisper *w;+ const float *pcm;+ size_t n, total; /* samples, and with the zeros after them */+ float *out;+ uint32_t frames;+ float *maxes; /* each thread's largest */+};++/* sample i of the sound padded with zeros, reflected past either end */+static float at(const struct mel_job *m, long i)+{+ long t = (long)m->total;+ if (i < 0)+ i = -i;+ if (i >= t)+ i = 2 * (t - 1) - i;+ return i >= 0 && (size_t)i < m->n ? m->pcm[i] : 0.0f;+}++static void mel_worker(void *arg, int tid, int nt)+{+ struct mel_job *m = arg;+ const struct janas_whisper *w = m->w;+ uint32_t N = w->n_fft, bins = N / 2 + 1, half = N / 2;+ float *frame = malloc(sizeof(float) * (N + bins));+ float mx = -INFINITY;+ if (!frame) {+ m->maxes[tid] = NAN; /* told to the caller */+ return;+ }+ float *pw = frame + N;+ uint32_t per = (m->frames + (uint32_t)nt - 1) / (uint32_t)nt;+ uint32_t f0 = (uint32_t)tid * per, f1 = f0 + per;+ if (f1 > m->frames)+ f1 = m->frames;+ for (uint32_t f = f0; f < f1; f++) {+ long s0 = (long)f * w->hop - (long)half;+ if (s0 >= 0 && (size_t)s0 + N <= m->n)+ memcpy(frame, m->pcm + s0, sizeof(float) * N);+ else+ for (uint32_t i = 0; i < N; i++)+ frame[i] = at(m, s0 + (long)i);+ for (uint32_t k = 0; k < bins; k++) {+ const float *c = w->dft_cos + (size_t)k * N;+ const float *s = w->dft_sin + (size_t)k * N;+ float re = 0, im = 0;+ for (uint32_t i = 0; i < N; i++) {+ re += frame[i] * c[i];+ im += frame[i] * s[i];+ }+ pw[k] = re * re + im * im;+ }+ for (uint32_t b = 0; b < w->n_mels; b++) {+ const float *fl = w->mel_filters + (size_t)b * bins;+ double e = 0;+ for (uint32_t k = 0; k < bins; k++)+ e += (double)fl[k] * pw[k];+ float v = (float)log10(e > 1e-10 ? e : 1e-10);+ m->out[(size_t)b * m->frames + f] = v;+ if (v > mx)+ mx = v;+ }+ }+ m->maxes[tid] = mx;+ free(frame);+}++float *janas_whisper_mel(struct janas_whisper *w, const float *pcm, size_t n,+ size_t pad, uint32_t *frames)+{+ size_t total = n + pad, nf = total / w->hop;+ *frames = 0;+ if (nf > UINT32_MAX / 2)+ return NULL;+ int nt = janas_pool_size(w->pool);+ float *out = malloc(sizeof(float) * (nf ? nf : 1) * w->n_mels);+ float *maxes = malloc(sizeof(float) * (size_t)nt);+ if (!out || !maxes) {+ free(out);+ free(maxes);+ return NULL;+ }+ struct mel_job m = {.w = w,+ .pcm = pcm,+ .n = n,+ .total = total,+ .out = out,+ .frames = (uint32_t)nf,+ .maxes = maxes};+ janas_pool_run(w->pool, mel_worker, &m);+ float mx = -INFINITY;+ for (int t = 0; t < nt; t++) {+ if (isnan(maxes[t])) {+ free(out);+ free(maxes);+ return NULL;+ }+ if (maxes[t] > mx)+ mx = maxes[t];+ }+ free(maxes);+ for (size_t i = 0; i < nf * w->n_mels; i++) {+ float v = out[i] < mx - 8.0f ? mx - 8.0f : out[i];+ out[i] = (v + 4.0f) / 4.0f;+ }+ *frames = (uint32_t)nf;+ return out;+}diff --git a/src/llm/whisper_net.c b/src/llm/whisper_net.cnew file mode 100644index 0000000..5460844--- /dev/null+++ b/src/llm/whisper_net.c@@ -0,0 +1,260 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * whisper_net.c - Whisper's encoder and decoder (whisper.h), as the+ * Hugging Face model computes them.+ *+ * Encoder: two convolutions of width 3 over the window of the spectrogram+ * (the second with stride 2, a_ctx positions out), each followed by GELU+ * (erf), plus sinusoidal positions; blocks of LayerNorm, attention (queries,+ * values and output with a bias, keys without), residual, LayerNorm, MLP+ * fc1 - GELU - fc2, residual; a final LayerNorm. Its output gives every+ * decoder block its keys and values once.+ *+ * Decoder: token embeddings plus learned positions; blocks of causal+ * attention over its own tokens (their keys and values kept), attention+ * over the encoder's output and an MLP, each LayerNorm first, a residual+ * after; a final LayerNorm, the logits against the token embeddings. The+ * products are the vision encoders' tiled kernels (vision_kernels.h) on the+ * weights in half precision.+ */+#include "whisper.h"++#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "common/pool.h"+#include "quant.h"+#include "vision_kernels.h"++static void gelu_rows(float *x, size_t n)+{+ for (size_t i = 0; i < n; i++)+ x[i] = vk_gelu_erf(x[i]);+}++static void ln_rows(float *y, const float *x, const float *w, const float *b,+ uint32_t rows, uint32_t d)+{+ for (uint32_t r = 0; r < rows; r++)+ vk_layer_norm(y + (size_t)r * d, x + (size_t)r * d, w, b, d, 1e-5f);+}++static void mm(struct janas_whisper *w, const uint16_t *wt, const float *b,+ const float *x, float *y, uint32_t rows, uint32_t cols,+ uint32_t n, size_t ldy)+{+ vk_matmul_f16(w->pool, wt, b, x, y, rows, cols, n, ldy);+}++/* ---- the encoder ---- */++int janas_whisper_encode(struct janas_whisper *w, const float *mel,+ uint32_t stride, uint32_t off, uint32_t len)+{+ uint32_t d = w->dim, n = w->a_ctx, nin = 2 * n, nm = w->n_mels;+ size_t big = (size_t)n * (3 * d > w->a_ffn ? 3 * d : w->a_ffn);+ float *col = malloc(sizeof(float) * (size_t)nin * 3 * nm);+ float *h1 = malloc(sizeof(float) * (size_t)nin * d);+ float *x = malloc(sizeof(float) * (size_t)n * d);+ float *h = malloc(sizeof(float) * (size_t)n * d);+ float *t = malloc(sizeof(float) * big);+ float *buf = malloc(sizeof(float) * 2 * (size_t)n * d);+ float *o = malloc(sizeof(float) * (size_t)n * d);+ int rc = -1;+ if (!col || !h1 || !x || !h || !t || !buf || !o)+ goto done;+ /* conv1: a row per output position, (channel, tap) its inputs */+ for (uint32_t p = 0; p < nin; p++)+ for (uint32_t c = 0; c < nm; c++)+ for (uint32_t k = 0; k < 3; k++) {+ long s = (long)p + (long)k - 1;+ float v = 0;+ if (s >= 0 && s < (long)nin && (uint32_t)s < len)+ v = mel[(size_t)c * stride + off + (uint32_t)s];+ col[((size_t)p * nm + c) * 3 + k] = v;+ }+ mm(w, w->conv1, w->conv1_b, col, h1, d, 3 * nm, nin, 0);+ gelu_rows(h1, (size_t)nin * d);+ /* conv2, stride 2: the inputs of position p are 2p - 1 .. 2p + 1 */+ float *col2 = t; /* n x 3 d */+ for (uint32_t p = 0; p < n; p++)+ for (uint32_t c = 0; c < d; c++)+ for (uint32_t k = 0; k < 3; k++) {+ long s = 2 * (long)p + (long)k - 1;+ col2[((size_t)p * d + c) * 3 + k] =+ s >= 0 && s < (long)nin ? h1[(size_t)s * d + c] : 0.0f;+ }+ mm(w, w->conv2, w->conv2_b, col2, x, d, 3 * d, n, 0);+ gelu_rows(x, (size_t)n * d);+ for (size_t i = 0; i < (size_t)n * d; i++)+ x[i] += w->e_pos[i];+ float scale = 1.0f / sqrtf((float)(d / w->a_heads));+ for (uint32_t l = 0; l < w->a_layers; l++) {+ const struct wlayer *L = &w->enc[l];+ ln_rows(h, x, L->ln_w, L->ln_b, n, d);+ mm(w, L->q, L->q_b, h, t, d, d, n, 3 * (size_t)d);+ mm(w, L->k, NULL, h, t + d, d, d, n, 3 * (size_t)d);+ mm(w, L->v, L->v_b, h, t + 2 * d, d, d, n, 3 * (size_t)d);+ vk_attention(w->pool, t, buf, o, n, d, w->a_heads, scale);+ mm(w, L->o, L->o_b, o, h, d, d, n, 0);+ for (size_t i = 0; i < (size_t)n * d; i++)+ x[i] += h[i];+ ln_rows(h, x, L->mln_w, L->mln_b, n, d);+ mm(w, L->fc1, L->fc1_b, h, t, w->a_ffn, d, n, 0);+ gelu_rows(t, (size_t)n * w->a_ffn);+ mm(w, L->fc2, L->fc2_b, t, h, d, w->a_ffn, n, 0);+ for (size_t i = 0; i < (size_t)n * d; i++)+ x[i] += h[i];+ }+ ln_rows(h, x, w->e_ln_w, w->e_ln_b, n, d);+ /* every decoder block's keys and values over it */+ for (uint32_t l = 0; l < w->t_layers; l++) {+ const struct wlayer *L = &w->dec[l];+ size_t at = (size_t)l * n * d;+ mm(w, L->xk, NULL, h, w->xk + at, d, d, n, 0);+ mm(w, L->xv, L->xv_b, h, w->xv + at, d, d, n, 0);+ }+ rc = 0;+done:+ free(col);+ free(h1);+ free(x);+ free(h);+ free(t);+ free(buf);+ free(o);+ return rc;+}++/* ---- the decoder ---- */++struct dec_att {+ const float *q; /* n x dim */+ const float *k, *v; /* positions x dim */+ float *out; /* n x dim */+ uint32_t n, dim, heads, hd;+ uint32_t keys; /* how many keys the last token sees */+ int causal; /* token i sees keys .. keys - n + i */+ float scale;+ float *const *cap; /* by head: n x keys scores kept, or NULL */+};++static void dec_att_worker(void *arg, int tid, int nt)+{+ const struct dec_att *a = arg;+ float *s = malloc(sizeof(float) * a->keys);+ if (!s)+ return;+ size_t total = (size_t)a->n * a->heads;+ for (size_t t = (size_t)tid; t < total; t += (size_t)nt) {+ uint32_t i = (uint32_t)(t / a->heads), hh = (uint32_t)(t % a->heads);+ uint32_t nk = a->causal ? a->keys - a->n + i + 1 : a->keys;+ const float *q = a->q + (size_t)i * a->dim + (size_t)hh * a->hd;+ float mx = -INFINITY;+ for (uint32_t k = 0; k < nk; k++) {+ const float *key = a->k + (size_t)k * a->dim + (size_t)hh * a->hd;+ float dot = 0;+ for (uint32_t e = 0; e < a->hd; e++)+ dot += q[e] * key[e];+ s[k] = dot * a->scale;+ if (s[k] > mx)+ mx = s[k];+ }+ if (a->cap && a->cap[hh])+ memcpy(a->cap[hh] + (size_t)i * a->keys, s, sizeof(float) * nk);+ double sum = 0;+ for (uint32_t k = 0; k < nk; k++) {+ s[k] = expf(s[k] - mx);+ sum += s[k];+ }+ float inv = (float)(1.0 / sum);+ float *o = a->out + (size_t)i * a->dim + (size_t)hh * a->hd;+ memset(o, 0, sizeof(float) * a->hd);+ for (uint32_t k = 0; k < nk; k++) {+ const float *v = a->v + (size_t)k * a->dim + (size_t)hh * a->hd;+ float p = s[k] * inv;+ for (uint32_t e = 0; e < a->hd; e++)+ o[e] += p * v[e];+ }+ }+ free(s);+}++static void attend(struct janas_whisper *w, const float *q, const float *k,+ const float *v, float *out, uint32_t n, uint32_t keys,+ int causal, float *const *cap)+{+ struct dec_att a = {.q = q,+ .k = k,+ .v = v,+ .out = out,+ .n = n,+ .dim = w->dim,+ .heads = w->t_heads,+ .hd = w->dim / w->t_heads,+ .keys = keys,+ .causal = causal,+ .scale = 1.0f / sqrtf((float)(w->dim / w->t_heads)),+ .cap = cap};+ janas_pool_run(w->pool, dec_att_worker, &a);+}++int janas_whisper_decode(struct janas_whisper *w, const int32_t *tok,+ uint32_t n, uint32_t past, float *logits, int all)+{+ uint32_t d = w->dim;+ if (!n || past + n > w->t_ctx)+ return -1;+ float *x = w->x, *h = w->h, *q = w->q, *att = w->att;+ for (uint32_t i = 0; i < n; i++) {+ if (tok[i] < 0 || (uint32_t)tok[i] >= w->vocab)+ return -1;+ const float *pos = w->d_pos + (size_t)(past + i) * d;+ for (uint32_t c = 0; c < d; c++)+ x[(size_t)i * d + c] =+ janas_fp16_to_fp32(w->tok[(size_t)c * w->vocab + tok[i]]) ++ pos[c];+ }+ for (uint32_t l = 0; l < w->t_layers; l++) {+ const struct wlayer *L = &w->dec[l];+ float *kc = w->kc + (size_t)l * w->t_ctx * d;+ float *vc = w->vc + (size_t)l * w->t_ctx * d;+ ln_rows(h, x, L->ln_w, L->ln_b, n, d);+ mm(w, L->q, L->q_b, h, q, d, d, n, 0);+ mm(w, L->k, NULL, h, kc + (size_t)past * d, d, d, n, 0);+ mm(w, L->v, L->v_b, h, vc + (size_t)past * d, d, d, n, 0);+ attend(w, q, kc, vc, att, n, past + n, 1, NULL);+ mm(w, L->o, L->o_b, att, h, d, d, n, 0);+ for (size_t i = 0; i < (size_t)n * d; i++)+ x[i] += h[i];+ ln_rows(h, x, L->xln_w, L->xln_b, n, d);+ mm(w, L->xq, L->xq_b, h, q, d, d, n, 0);+ size_t at = (size_t)l * w->a_ctx * d;+ /* the alignment heads' scores, when asked for */+ float *capv[64] = {0}, *const *cap = NULL;+ if (w->cap) {+ for (uint32_t i = 0; i < w->n_aheads; i++)+ if ((uint32_t)w->aheads[2 * i] == l &&+ (uint32_t)w->aheads[2 * i + 1] < 64)+ capv[w->aheads[2 * i + 1]] =+ w->cap + (size_t)i * n * w->a_ctx;+ cap = capv;+ }+ attend(w, q, w->xk + at, w->xv + at, att, n, w->a_ctx, 0, cap);+ mm(w, L->xo, L->xo_b, att, h, d, d, n, 0);+ for (size_t i = 0; i < (size_t)n * d; i++)+ x[i] += h[i];+ ln_rows(h, x, L->mln_w, L->mln_b, n, d);+ mm(w, L->fc1, L->fc1_b, h, w->ff, w->t_ffn, d, n, 0);+ gelu_rows(w->ff, (size_t)n * w->t_ffn);+ mm(w, L->fc2, L->fc2_b, w->ff, h, d, w->t_ffn, n, 0);+ for (size_t i = 0; i < (size_t)n * d; i++)+ x[i] += h[i];+ }+ uint32_t first = all ? 0 : n - 1;+ ln_rows(h, x + (size_t)first * d, w->d_ln_w, w->d_ln_b, n - first, d);+ mm(w, w->tok, NULL, h, logits, w->vocab, d, n - first, 0);+ return 0;+}diff --git a/src/llm/whisper_run.c b/src/llm/whisper_run.cnew file mode 100644index 0000000..8ef7b4e--- /dev/null+++ b/src/llm/whisper_run.c@@ -0,0 +1,654 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * whisper_run.c - a whole recording transcribed (whisper.h), as OpenAI's+ * transcribe() goes through it.+ *+ * The spectrogram of the sound and thirty seconds of silence after it; a+ * window of thirty seconds at a time, the language found on the first when+ * it is not given. In each window the decoder starts from the text before+ * it (<|startofprev|>, at most half the context), then <|startoftranscript|>,+ * the language and the task, and picks tokens - the likeliest, or sampled+ * at a temperature - under OpenAI's rules: no blank at the start, never the+ * suppressed tokens, timestamps in pairs and never going back, the first+ * within a second, a timestamp whenever all of them together are likelier+ * than any text. A window whose text repeats itself (its length over its+ * zlib-compressed length above 2.4, as OpenAI measures it, by+ * common/deflate.c) or is unlikely (average log-probability below -1) is+ * decoded again at a higher+ * temperature, up to 1; one the model calls silence (<|nospeech|> above 0.6+ * at the start, the text unlikely) is skipped. The timestamp pairs cut the+ * window into segments; the next window starts at the last timestamp, or+ * after the whole window when the speech ended in it.+ */+#include "whisper.h"++#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "common/deflate.h"+#include "tokenizer.h"++#define LOGPROB_MIN (-1.0f)+#define NO_SPEECH 0.6f+#define COMPRESSION_MAX 2.4f++static const char *const LANGS[][2] = {+ {"en", "english"}, {"zh", "chinese"}, {"de", "german"},+ {"es", "spanish"}, {"ru", "russian"}, {"ko", "korean"},+ {"fr", "french"}, {"ja", "japanese"}, {"pt", "portuguese"},+ {"tr", "turkish"}, {"pl", "polish"}, {"ca", "catalan"},+ {"nl", "dutch"}, {"ar", "arabic"}, {"sv", "swedish"},+ {"it", "italian"}, {"id", "indonesian"}, {"hi", "hindi"},+ {"fi", "finnish"}, {"vi", "vietnamese"}, {"he", "hebrew"},+ {"uk", "ukrainian"}, {"el", "greek"}, {"ms", "malay"},+ {"cs", "czech"}, {"ro", "romanian"}, {"da", "danish"},+ {"hu", "hungarian"}, {"ta", "tamil"}, {"no", "norwegian"},+ {"th", "thai"}, {"ur", "urdu"}, {"hr", "croatian"},+ {"bg", "bulgarian"}, {"lt", "lithuanian"}, {"la", "latin"},+ {"mi", "maori"}, {"ml", "malayalam"}, {"cy", "welsh"},+ {"sk", "slovak"}, {"te", "telugu"}, {"fa", "persian"},+ {"lv", "latvian"}, {"bn", "bengali"}, {"sr", "serbian"},+ {"az", "azerbaijani"}, {"sl", "slovenian"}, {"kn", "kannada"},+ {"et", "estonian"}, {"mk", "macedonian"}, {"br", "breton"},+ {"eu", "basque"}, {"is", "icelandic"}, {"hy", "armenian"},+ {"ne", "nepali"}, {"mn", "mongolian"}, {"bs", "bosnian"},+ {"kk", "kazakh"}, {"sq", "albanian"}, {"sw", "swahili"},+ {"gl", "galician"}, {"mr", "marathi"}, {"pa", "punjabi"},+ {"si", "sinhala"}, {"km", "khmer"}, {"sn", "shona"},+ {"yo", "yoruba"}, {"so", "somali"}, {"af", "afrikaans"},+ {"oc", "occitan"}, {"ka", "georgian"}, {"be", "belarusian"},+ {"tg", "tajik"}, {"sd", "sindhi"}, {"gu", "gujarati"},+ {"am", "amharic"}, {"yi", "yiddish"}, {"lo", "lao"},+ {"uz", "uzbek"}, {"fo", "faroese"}, {"ht", "haitian creole"},+ {"ps", "pashto"}, {"tk", "turkmen"}, {"nn", "nynorsk"},+ {"mt", "maltese"}, {"sa", "sanskrit"}, {"lb", "luxembourgish"},+ {"my", "myanmar"}, {"bo", "tibetan"}, {"tl", "tagalog"},+ {"mg", "malagasy"}, {"as", "assamese"}, {"tt", "tatar"},+ {"haw", "hawaiian"}, {"ln", "lingala"}, {"ha", "hausa"},+ {"ba", "bashkir"}, {"jw", "javanese"}, {"su", "sundanese"},+ {"yue", "cantonese"},+};++const char *janas_whisper_lang_name(const char *code)+{+ for (size_t i = 0; i < sizeof LANGS / sizeof *LANGS; i++)+ if (!strcmp(LANGS[i][0], code))+ return LANGS[i][1];+ return NULL;+}++static int same_ci(const char *a, const char *b)+{+ for (; *a && *b; a++, b++)+ if ((*a | 0x20) != (*b | 0x20))+ return 0;+ return !*a && !*b;+}++int janas_whisper_lang_index(const struct janas_whisper *w, const char *s)+{+ const char *code = s;+ for (size_t i = 0; i < sizeof LANGS / sizeof *LANGS; i++)+ if (same_ci(LANGS[i][1], s))+ code = LANGS[i][0];+ char c[16];+ for (uint32_t i = 0; i < w->n_lang; i++) {+ janas_whisper_lang(w, i, c, sizeof c);+ if (same_ci(c, code))+ return (int)i;+ }+ return -1;+}++/* ---- a window decoded ---- */++struct run {+ struct janas_whisper *w;+ const struct janas_whisper_opts *o;+ int lang; /* index, -1 for an English-only model */+ float *logits, *buf;+ uint8_t *suppress; /* by token: never sampled */+ uint64_t rng;+ int32_t *all; /* the tokens of the segments so far */+ size_t n_all, cap_all, prompt_from;+};++struct window {+ int32_t tok[512];+ float lp[512]; /* each token's log-probability */+ uint32_t n;+ double sum_logprob;+ float no_speech, temperature, avg_logprob, compression_ratio;+};++static double rnd(struct run *r)+{+ /* splitmix64 */+ uint64_t z = (r->rng += 0x9e3779b97f4a7c15ull);+ z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull;+ z = (z ^ (z >> 27)) * 0x94d049bb133111ebull;+ z ^= z >> 31;+ return (double)(z >> 11) / 9007199254740992.0;+}++/* OpenAI's logit filters on the logits of the next token, given those+ sampled in the window so far */+static void filter(struct run *r, float *l, const int32_t *seq, uint32_t n)+{+ struct janas_whisper *w = r->w;+ uint32_t V = w->vocab, tb = (uint32_t)w->ts_begin;+ if (n == 0)+ for (uint64_t i = 0; i < w->n_begin_suppress; i++)+ if ((uint32_t)w->begin_suppress[i] < V)+ l[w->begin_suppress[i]] = -INFINITY;+ for (uint32_t i = 0; i < V; i++)+ if (r->suppress[i])+ l[i] = -INFINITY;+ l[w->no_timestamps] = -INFINITY;+ int last = n >= 1 && seq[n - 1] >= (int32_t)tb;+ int penult = n < 2 || seq[n - 2] >= (int32_t)tb;+ if (last) {+ if (penult)+ for (uint32_t i = tb; i < V; i++)+ l[i] = -INFINITY;+ else+ for (uint32_t i = 0; i < (uint32_t)w->eot; i++)+ l[i] = -INFINITY;+ }+ int32_t ts_last = -1;+ for (uint32_t i = 0; i < n; i++)+ if (seq[i] >= (int32_t)tb)+ ts_last = seq[i];+ if (ts_last >= 0) {+ uint32_t upto = (uint32_t)ts_last + !(last && !penult);+ for (uint32_t i = tb; i < upto && i < V; i++)+ l[i] = -INFINITY;+ }+ if (n == 0) {+ for (uint32_t i = 0; i < tb; i++)+ l[i] = -INFINITY;+ for (uint32_t i = tb + w->max_initial_ts + 1; i < V; i++)+ l[i] = -INFINITY;+ }+ /* a timestamp when they are likelier together than any text */+ float mx = -INFINITY;+ for (uint32_t i = 0; i < V; i++)+ if (l[i] > mx)+ mx = l[i];+ double ts = 0, best_text = -INFINITY;+ for (uint32_t i = 0; i < V; i++) {+ if (l[i] == -INFINITY)+ continue;+ if (i >= tb)+ ts += exp((double)l[i] - mx);+ else if (l[i] > best_text)+ best_text = l[i];+ }+ if (ts > 0 && log(ts) + mx > best_text)+ for (uint32_t i = 0; i < tb; i++)+ l[i] = -INFINITY;+}++/* log of the sum of e^l, the largest mx */+static double log_sum(const float *l, uint32_t n, float mx)+{+ double s = 0;+ for (uint32_t i = 0; i < n; i++)+ if (l[i] != -INFINITY)+ s += exp((double)l[i] - mx);+ return log(s) + mx;+}++static int32_t pick(struct run *r, const float *l, float temp, double *lp)+{+ uint32_t V = r->w->vocab, best = 0;+ for (uint32_t i = 1; i < V; i++)+ if (l[i] > l[best])+ best = i;+ double lse = log_sum(l, V, l[best]);+ if (temp > 0) {+ double s = 0, *e = (double *)(void *)r->buf;+ for (uint32_t i = 0; i < V; i++) {+ e[i] = l[i] == -INFINITY ? 0 : exp((l[i] - l[best]) / temp);+ s += e[i];+ }+ double u = rnd(r) * s;+ for (uint32_t i = 0; i < V; i++) {+ u -= e[i];+ if (u <= 0 && e[i] > 0) {+ best = i;+ break;+ }+ }+ }+ *lp = l[best] - lse;+ return (int32_t)best;+}++/* the text's bytes (its text tokens, stripped) over its zlib-compressed+ bytes, as OpenAI's get_compression_ratio: high when the text goes round+ in a loop */+static float compression_ratio(struct janas_whisper *w, const int32_t *t,+ uint32_t n)+{+ size_t len = 0;+ char none[1];+ for (uint32_t i = 0; i < n; i++)+ if (t[i] < w->eot)+ len += janas_tokenizer_decode(w->tk, t[i], none, 0);+ char *s = malloc(len + 1), *p = s;+ if (!s)+ return 0;+ for (uint32_t i = 0; i < n; i++)+ if (t[i] < w->eot)+ p += janas_tokenizer_decode(w->tk, t[i], p, len - (size_t)(p - s));+ const char *b = s, *e = s + len;+ while (b < e && (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r' ||+ *b == '\v' || *b == '\f'))+ b++;+ while (e > b && (e[-1] == ' ' || e[-1] == '\t' || e[-1] == '\n' ||+ e[-1] == '\r' || e[-1] == '\v' || e[-1] == '\f'))+ e--;+ long z = janas_deflate_size((const uint8_t *)b, (size_t)(e - b));+ float ratio = z > 0 ? (float)((double)(e - b) / (double)z) : 0;+ free(s);+ return ratio;+}++static int decode_window(struct run *r, float temp, struct window *win)+{+ struct janas_whisper *w = r->w;+ uint32_t V = w->vocab, half = w->t_ctx / 2;+ int32_t init[512];+ uint32_t ni = 0;+ size_t np = r->n_all - r->prompt_from;+ if (np) {+ size_t keep = np < half - 1 ? np : half - 1;+ init[ni++] = w->sot_prev;+ memcpy(init + ni, r->all + r->n_all - keep, sizeof(int32_t) * keep);+ ni += (uint32_t)keep;+ }+ uint32_t at_sot = ni;+ init[ni++] = w->sot;+ if (r->lang >= 0) {+ init[ni++] = w->sot + 1 + r->lang;+ init[ni++] = r->o->translate ? w->translate : w->transcribe;+ }+ if (janas_whisper_decode(w, init, ni, 0, r->buf, 1) != 0)+ return -1;+ /* the probability of silence, at the start of transcript */+ const float *ls = r->buf + (size_t)(at_sot - 0) * V;+ float mx = -INFINITY;+ for (uint32_t i = 0; i < V; i++)+ if (ls[i] > mx)+ mx = ls[i];+ win->no_speech = (float)exp(ls[w->no_speech] - log_sum(ls, V, mx));+ memcpy(r->logits, r->buf + (size_t)(ni - 1) * V, sizeof(float) * V);+ win->n = 0;+ win->sum_logprob = 0;+ win->temperature = temp;+ uint32_t limit = half < sizeof win->tok / sizeof *win->tok+ ? half+ : sizeof win->tok / sizeof *win->tok;+ while (win->n < limit && ni + win->n < w->t_ctx) {+ filter(r, r->logits, win->tok, win->n);+ double lp;+ int32_t t = pick(r, r->logits, temp, &lp);+ win->sum_logprob += lp;+ if (t == w->eot)+ break;+ win->lp[win->n] = (float)lp;+ win->tok[win->n++] = t;+ if (ni + win->n >= w->t_ctx)+ break;+ if (janas_whisper_decode(w, &t, 1, ni + win->n - 1, r->logits, 0))+ return -1;+ }+ win->avg_logprob = (float)(win->sum_logprob / (win->n + 1));+ win->compression_ratio = compression_ratio(w, win->tok, win->n);+ return 0;+}++/* ---- the recording ---- */++static int add_segment(struct janas_whisper_result *res, uint32_t *cap,+ struct janas_whisper *w, const struct window *win,+ const int32_t *tok, uint32_t n, double t0, double t1)+{+ /* its text: the tokens before the end of text */+ size_t len = 0;+ char piece[256];+ for (uint32_t i = 0; i < n; i++)+ if (tok[i] < w->eot)+ len += janas_tokenizer_decode(w->tk, tok[i], piece, 0);+ char *text = malloc(len + 1), *p = text;+ if (!text)+ return -1;+ for (uint32_t i = 0; i < n; i++)+ if (tok[i] < w->eot)+ p += janas_tokenizer_decode(w->tk, tok[i], p,+ len + 1 - (size_t)(p - text));+ *p = 0;+ int blank = 1;+ for (const char *c = text; *c; c++)+ blank = blank && (*c == ' ' || *c == '\t' || *c == '\n');+ if (blank || t1 <= t0) {+ free(text);+ return 0;+ }+ if (res->n_segs == *cap) {+ uint32_t nc = *cap ? 2 * *cap : 16;+ struct janas_whisper_seg *s =+ realloc(res->segs, sizeof(*s) * (size_t)nc);+ if (!s) {+ free(text);+ return -1;+ }+ res->segs = s;+ *cap = nc;+ }+ struct janas_whisper_seg *s = &res->segs[res->n_segs];+ s->tokens = malloc(sizeof(int32_t) * (n ? n : 1));+ s->logprobs = malloc(sizeof(float) * (n ? n : 1));+ if (!s->tokens || !s->logprobs) {+ free(s->tokens);+ free(s->logprobs);+ free(text);+ return -1;+ }+ memcpy(s->tokens, tok, sizeof(int32_t) * n);+ memcpy(s->logprobs, win->lp + (tok - win->tok), sizeof(float) * n);+ s->n_tokens = n;+ s->t0 = t0;+ s->t1 = t1;+ s->text = text;+ s->avg_logprob = win->avg_logprob;+ s->no_speech_prob = win->no_speech;+ s->temperature = win->temperature;+ s->compression_ratio = win->compression_ratio;+ s->words = NULL;+ s->n_words = 0;+ res->n_segs++;+ return 0;+}++static int remember(struct run *r, const int32_t *t, uint32_t n)+{+ if (r->n_all + n > r->cap_all) {+ size_t nc = (r->n_all + n) * 2;+ int32_t *a = realloc(r->all, sizeof(int32_t) * nc);+ if (!a)+ return -1;+ r->all = a;+ r->cap_all = nc;+ }+ memcpy(r->all + r->n_all, t, sizeof(int32_t) * n);+ r->n_all += n;+ return 0;+}++static int detect_language(struct run *r)+{+ struct janas_whisper *w = r->w;+ if (janas_whisper_decode(w, &w->sot, 1, 0, r->logits, 0) != 0)+ return -1;+ int best = 0;+ for (uint32_t i = 1; i < w->n_lang; i++)+ if (r->logits[w->sot + 1 + i] > r->logits[w->sot + 1 + best])+ best = (int)i;+ return best;+}++int janas_whisper_transcribe(struct janas_whisper *w, const float *pcm,+ size_t n, const struct janas_whisper_opts *o,+ struct janas_whisper_result *res, char *err,+ size_t err_len)+{+ memset(res, 0, sizeof *res);+ struct run r = {.w = w, .o = o, .lang = -1, .rng = o->seed};+ struct window *win = malloc(sizeof *win);+ uint32_t frames = 0, cap = 0, win_frames = 2 * w->a_ctx;+ float *mel = NULL;+ int rc = -1;+ r.logits = malloc(sizeof(float) * w->vocab);+ /* the logits of a window's first tokens, at most half the context and+ three; doubles for the sampling */+ r.buf = malloc(sizeof(float) * (size_t)w->vocab * (w->t_ctx / 2 + 4));+ r.suppress = calloc(w->vocab, 1);+ if (!win || !r.logits || !r.buf || !r.suppress) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ for (uint64_t i = 0; i < w->n_suppress; i++)+ if (w->suppress[i] >= 0 && (uint32_t)w->suppress[i] < w->vocab)+ r.suppress[w->suppress[i]] = 1;+ int32_t never[] = {w->translate, w->transcribe, w->sot,+ w->sot_prev, w->sot_lm, w->no_speech};+ for (size_t i = 0; i < sizeof never / sizeof *never; i++)+ r.suppress[never[i]] = 1;+ if (w->multilingual) {+ if (o->language && o->language[0]) {+ r.lang = janas_whisper_lang_index(w, o->language);+ if (r.lang < 0) {+ snprintf(err, err_len,+ "a language this model does not know: %s",+ o->language);+ goto done;+ }+ }+ } else if (o->translate) {+ snprintf(err, err_len,+ "this model knows English only: it does not "+ "translate");+ goto done;+ }+ if (o->prompt && o->prompt[0]) {+ /* " " + the prompt, as OpenAI encodes it */+ size_t pl = strlen(o->prompt);+ char *sp = malloc(pl + 2);+ if (!sp) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ sp[0] = ' ';+ memcpy(sp + 1, o->prompt, pl + 1);+ long k = janas_tokenizer_encode(w->tk, sp, pl + 1, 0, NULL, 0);+ int32_t *pt = k > 0 ? malloc(sizeof(int32_t) * (size_t)k) : NULL;+ if (pt &&+ janas_tokenizer_encode(w->tk, sp, pl + 1, 0, pt, (size_t)k) == k &&+ remember(&r, pt, (uint32_t)k) != 0)+ k = -1;+ free(pt);+ free(sp);+ if (k < 0) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ }+ mel = janas_whisper_mel(w, pcm, n, (size_t)w->chunk * w->rate, &frames);+ if (!mel) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ res->duration = (double)n / w->rate;+ uint32_t content = (uint32_t)(n / w->hop), seek = 0;+ double last_speech = 0;+ double frame_s = (double)w->hop / w->rate; /* 10 ms */+ double ts_s = 2 * frame_s; /* a timestamp's step */+ /* the stretches of speech to read (a voice activity detector's), or+ the whole recording */+ size_t ri = 0;+ uint32_t rend = content;+ if (o->n_regions) {+ seek = (uint32_t)(o->regions[0] / w->hop);+ rend = (uint32_t)(o->regions[1] / w->hop);+ }+ for (;;) {+ if (rend > content)+ rend = content;+ if (seek >= rend) {+ if (!o->n_regions || ++ri >= o->n_regions)+ break;+ uint32_t rs = (uint32_t)(o->regions[2 * ri] / w->hop);+ if (seek < rs)+ seek = rs;+ rend = (uint32_t)(o->regions[2 * ri + 1] / w->hop);+ continue;+ }+ if (o->stop && *o->stop) {+ snprintf(err, err_len, "stopped");+ goto done;+ }+ uint32_t size = rend - seek < win_frames ? rend - seek : win_frames;+ if (janas_whisper_encode(w, mel, frames, seek, size) != 0) {+ snprintf(err, err_len, "out of memory");+ goto done;+ }+ if (w->multilingual && r.lang < 0 &&+ (r.lang = detect_language(&r)) < 0) {+ snprintf(err, err_len, "the decoder failed");+ goto done;+ }+ float temp = o->temperature;+ for (;;) {+ if (decode_window(&r, temp, win) != 0) {+ snprintf(err, err_len, "the decoder failed");+ goto done;+ }+ int again = win->compression_ratio > COMPRESSION_MAX ||+ win->avg_logprob < LOGPROB_MIN;+ if (win->no_speech > NO_SPEECH && win->avg_logprob < LOGPROB_MIN)+ again = 0; /* silence: skipped below */+ if (!again || temp >= 1.0f - 1e-6f)+ break;+ temp += 0.2f;+ }+ double offset = seek * frame_s;+ if (win->no_speech > NO_SPEECH && win->avg_logprob < LOGPROB_MIN) {+ seek += size;+ continue;+ }+ int32_t tb = w->ts_begin, *t = win->tok;+ uint32_t nt = win->n, before = res->n_segs;+ int single_end = nt >= 2 && t[nt - 1] >= tb && t[nt - 2] < tb;+ uint32_t last = 0, cuts = 0;+ for (uint32_t i = 1; i < nt; i++)+ if (t[i] >= tb && t[i - 1] >= tb) {+ double t0 = offset + (t[last] - tb) * ts_s;+ double t1 = offset + (t[i - 1] - tb) * ts_s;+ if (add_segment(res, &cap, w, win, t + last, i - last, t0, t1))+ goto oom;+ last = i;+ cuts++;+ }+ if (cuts) {+ if (single_end) {+ double t0 = offset + (t[last] - tb) * ts_s;+ double t1 = offset + (t[nt - 1] - tb) * ts_s;+ if (add_segment(res, &cap, w, win, t + last, nt - last, t0, t1))+ goto oom;+ seek += size;+ } else {+ seek += (uint32_t)(t[last - 1] - tb) * 2;+ }+ } else {+ double dur = size * frame_s;+ for (uint32_t i = nt; i-- > 0;)+ if (t[i] >= tb) {+ if (t[i] != tb)+ dur = (t[i] - tb) * ts_s;+ break;+ }+ if (add_segment(res, &cap, w, win, t, nt, offset, offset + dur))+ goto oom;+ seek += size;+ }+ if (o->words && res->n_segs > before) {+ /* the words' times; then, as OpenAI's transcribe does, the+ next window starts at the last word's end */+ double ls = last_speech;+ if (janas_whisper_align(w, r.lang, o->translate, res->segs, before,+ res->n_segs, offset, size, &ls) != 0)+ goto oom;+ double last_end = -1;+ for (uint32_t s = res->n_segs; s-- > before;)+ if (res->segs[s].n_words) {+ last_end = res->segs[s].words[res->segs[s].n_words - 1].t1;+ break;+ }+ if (!single_end && last_end > offset)+ seek = (uint32_t)lround(last_end / frame_s);+ if (last_end >= 0)+ last_speech = last_end;+ }+ for (uint32_t s = before; s < res->n_segs; s++) {+ if (res->segs[s].t1 > res->duration)+ res->segs[s].t1 = res->duration;+ if (remember(&r, res->segs[s].tokens, res->segs[s].n_tokens))+ goto oom;+ }+ if (win->temperature > 0.5f)+ r.prompt_from = r.n_all;+ if (o->on_segment)+ for (uint32_t s = before; s < res->n_segs; s++)+ o->on_segment(o->user, s);+ }+ if (r.lang >= 0)+ janas_whisper_lang(w, (uint32_t)r.lang, res->language,+ sizeof res->language);+ else+ snprintf(res->language, sizeof res->language, "en");+ size_t len = 0;+ for (uint32_t s = 0; s < res->n_segs; s++)+ len += strlen(res->segs[s].text);+ res->text = malloc(len + 1);+ if (!res->text)+ goto oom;+ char *p = res->text;+ for (uint32_t s = 0; s < res->n_segs; s++) {+ size_t k = strlen(res->segs[s].text);+ memcpy(p, res->segs[s].text, k);+ p += k;+ }+ *p = 0;+ /* trimmed */+ char *b = res->text;+ while (*b == ' ' || *b == '\n')+ b++;+ memmove(res->text, b, strlen(b) + 1);+ for (size_t k = strlen(res->text);+ k && (res->text[k - 1] == ' ' || res->text[k - 1] == '\n'); k--)+ res->text[k - 1] = 0;+ rc = 0;+ goto done;+oom:+ snprintf(err, err_len, "out of memory");+done:+ if (rc != 0)+ janas_whisper_result_free(res);+ free(mel);+ free(win);+ free(r.logits);+ free(r.buf);+ free(r.suppress);+ free(r.all);+ return rc;+}++void janas_whisper_result_free(struct janas_whisper_result *r)+{+ for (uint32_t i = 0; i < r->n_segs; i++) {+ free(r->segs[i].text);+ free(r->segs[i].tokens);+ free(r->segs[i].logprobs);+ for (uint32_t k = 0; k < r->segs[i].n_words; k++)+ free(r->segs[i].words[k].text);+ free(r->segs[i].words);+ }+ free(r->segs);+ free(r->text);+ memset(r, 0, sizeof *r);+}diff --git a/src/node/janas.d.ts b/src/node/janas.d.tsindex fe2fef7..3e56874 100644--- a/src/node/janas.d.ts+++ b/src/node/janas.d.ts@@ -68,6 +68,8 @@ export interface OpenOptions {headBits?: 4 | 6;/** How the attention scores are computed: "exact", "fast" (sixteen-bit integers), "auto". */attention?: "auto" | "exact" | "fast";+ /** The lowest quality the engine may choose to be faster where memory is short: "low" (default) the experts at four bits and the light ones not in memory left out, "medium" four bits only, "high" nothing lowered. expertBits and headBits win over it. */+ quality?: "low" | "medium" | "high";}/** An open model. */diff --git a/src/node/model.c b/src/node/model.cindex f5fac81..99be7d3 100644--- a/src/node/model.c+++ b/src/node/model.c@@ -39,8 +39,9 @@ static void open_drop(void *data)}static const char *const open_known[] = {- "context", "cacheGiB", "reserveGiB", "mtp", "draft", "mode",- "gpu", "preload", "expertBits", "headBits", "attention", NULL};+ "context", "cacheGiB", "reserveGiB", "mtp", "draft",+ "mode", "gpu", "preload", "expertBits", "headBits",+ "attention", "quality", NULL};/* one of the strings, as its index; -1 after throwing */static int choice(napi_env env, napi_value o, const char *name,@@ -66,6 +67,7 @@ static int open_options(napi_env env, napi_value o, struct open_job *oj)const char *w = "open";static const char *const modes[] = {"auto", "eco", "max", NULL};static const char *const attn[] = {"auto", "exact", "fast", NULL};+ static const char *const quality[] = {"low", "medium", "high", NULL};int64_t i;double d;int b, r, k;@@ -134,6 +136,10 @@ static int open_options(napi_env env, napi_value o, struct open_job *oj)oj->p.attn_scores = k == 1 ? JANAS_LLM_ATTN_EXACT: k == 2 ? JANAS_LLM_ATTN_FAST: JANAS_LLM_ATTN_AUTO;+ if ((r = choice(env, o, "quality", quality, &k)) < 0)+ return -1;+ if (r)+ oj->p.min_quality = k; /* JANAS_LLM_QUALITY_*: low 0, high 2 */return 0;}diff --git a/src/nsm/main.c b/src/nsm/main.cindex 9040253..a54adf7 100644--- a/src/nsm/main.c+++ b/src/nsm/main.c@@ -167,9 +167,9 @@ static const char **read_words(const char *path, char **text)fprintf(stderr, "janas-nsm: %s: %s\n", path, strerror(errno));exit(1);}- size_t n = 0;+ size_t n = 0; /* lines end in \n, \r or both: as many as either */for (size_t i = 0; i < len; i++)- n += (*text)[i] == '\n';+ n += (*text)[i] == '\n' || (*text)[i] == '\r';const char **w = calloc(n + 2, sizeof(*w));size_t k = 0;for (char *p = strtok(*text, "\r\n"); p && w; p = strtok(NULL, "\r\n"))diff --git a/src/server/assistants.c b/src/server/assistants.cnew file mode 100644index 0000000..0bfc473--- /dev/null+++ b/src/server/assistants.c@@ -0,0 +1,1069 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * assistants.c - OpenAI's Assistants API, the assistants, the threads and+ * their messages: kept in the store (assistants.h), the messages of a+ * thread as its aux text. An assistant's or a thread's file_search+ * resources are vector stores of this server (vectors_api.c); the files+ * attached to a message for file_search go into the thread's own store.+ * Runs: runs.c.+ */+#include "assistants.h"++#include <pthread.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <time.h>++#include "containers.h"+#include "llm/json.h"++static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;++void as_lock(void)+{+ pthread_mutex_lock(&mu);+}++void as_unlock(void)+{+ pthread_mutex_unlock(&mu);+}++static int bad(struct srv_req *r, const char *msg)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_value",+ "%s", msg);+}++static int not_found(struct srv_req *r, const char *what, const char *id)+{+ return srv_reply_error(r, 404, "invalid_request_error", "not_found",+ "No %s found with id '%s'.", what, id);+}++static int reply_val(struct srv_req *r, yyjson_mut_val *o)+{+ size_t n = 0;+ char *t = yyjson_mut_val_write(o, 0, &n);+ if (!t)+ return srv_reply_error(r, 500, "server_error", NULL, "out of memory");+ return srv_reply_json(r, 200, t, n);+}++char *as_get(struct srv *s, const char *kind, const char *id, char **aux)+{+ char *j = NULL;+ if (aux)+ *aux = NULL;+ if (srv_store_get(s->store, kind, id, &j, aux) != 0)+ return NULL;+ return j;+}++int as_save(struct srv *s, const char *kind, const char *id, yyjson_mut_val *o,+ const char *aux, int fresh)+{+ char *t = yyjson_mut_val_write(o, 0, NULL);+ int rc = -1;+ if (t)+ rc = fresh ? srv_store_put(s->store, kind, id, (int64_t)time(NULL), t,+ aux)+ : srv_store_update(s->store, kind, id, t, aux);+ free(t);+ return rc ? -1 : 0;+}++const char *as_metadata_error(yyjson_val *md)+{+ if (!md || yyjson_is_null(md))+ return NULL;+ if (!yyjson_is_obj(md) || yyjson_obj_size(md) > 16)+ return "'metadata' holds at most 16 keys.";+ size_t i, n;+ yyjson_val *k, *v;+ yyjson_obj_foreach(md, i, n, k, v)+ {+ if (!yyjson_is_str(v) || yyjson_get_len(v) > 512 ||+ yyjson_get_len(k) > 64)+ return "'metadata' values are strings of at most 512 "+ "characters, keys of at most 64.";+ }+ return NULL;+}++const char *as_tools_error(yyjson_val *tools)+{+ if (!tools || yyjson_is_null(tools))+ return NULL;+ if (!yyjson_is_arr(tools) || yyjson_arr_size(tools) > 128)+ return "'tools' is a list of at most 128 tools.";+ size_t i, n;+ yyjson_val *t;+ yyjson_arr_foreach(tools, i, n, t)+ {+ const char *type = yyjson_get_str(yyjson_obj_get(t, "type"));+ if (type && !strcmp(type, "function")) {+ yyjson_val *f = yyjson_obj_get(t, "function");+ if (!yyjson_get_str(yyjson_obj_get(f, "name")))+ return "A function tool has a function with a name.";+ } else if (type && !strcmp(type, "file_search")) {+ yyjson_val *m = yyjson_obj_get(yyjson_obj_get(t, "file_search"),+ "max_num_results");+ if (m && (!yyjson_is_int(m) || yyjson_get_sint(m) < 1 ||+ yyjson_get_sint(m) > 50))+ return "file_search's max_num_results is from 1 to 50.";+ } else if (type && !strcmp(type, "code_interpreter")) {+ if (srv_containers_off())+ return srv_containers_off();+ } else {+ return "A tool is of type function, file_search or "+ "code_interpreter.";+ }+ }+ return NULL;+}++/* A vector store made of file_ids through the Vector stores API: its id+ into id, or the reason. */+static const char *make_store(struct srv *s, yyjson_val *spec, char *id,+ size_t cap)+{+ srv_handler h = srv_route_find("POST", "/vector_stores");+ if (!h)+ return "vector stores are not available";+ char *body = spec ? yyjson_val_write(spec, 0, NULL) : strdup("{}");+ struct srv_capture c = {0};+ struct srv_req r = {.srv = s,+ .cap = &c,+ .method = "POST",+ .path = "/vector_stores",+ .body = body ? body : "{}",+ .n_body = body ? strlen(body) : 2};+ h(&r);+ free(body);+ yyjson_doc *d = c.body ? yyjson_read(c.body, c.n, 0) : NULL;+ const char *got =+ yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(d), "id"));+ const char *why = NULL;+ if (c.status != 200 || !got)+ why = "a vector store of the files could not be made (are they "+ "uploaded here?)";+ else+ snprintf(id, cap, "%s", got);+ yyjson_doc_free(d);+ free(c.body);+ return why;+}++const char *as_resources(struct srv *s, yyjson_mut_doc *d, yyjson_val *res,+ yyjson_mut_val **out)+{+ *out = yyjson_mut_obj(d);+ if (!res || yyjson_is_null(res))+ return NULL;+ if (!yyjson_is_obj(res))+ return "'tool_resources' is an object.";+ yyjson_val *ci = yyjson_obj_get(res, "code_interpreter");+ yyjson_val *cids = yyjson_obj_get(ci, "file_ids");+ if (cids) { /* kept: copied into the thread's container at its runs */+ if (!yyjson_is_arr(cids) || yyjson_arr_size(cids) > 20)+ return "code_interpreter takes at most 20 file_ids.";+ yyjson_mut_val *c = yyjson_mut_obj_add_obj(d, *out, "code_interpreter");+ yyjson_mut_obj_add_val(d, c, "file_ids", yyjson_val_mut_copy(d, cids));+ }+ yyjson_val *fs = yyjson_obj_get(res, "file_search");+ if (!fs)+ return NULL;+ yyjson_val *ids = yyjson_obj_get(fs, "vector_store_ids");+ yyjson_val *make = yyjson_obj_get(fs, "vector_stores");+ if (yyjson_arr_size(ids) + yyjson_arr_size(make) > 1)+ return "file_search takes one vector store.";+ yyjson_mut_val *o = yyjson_mut_obj_add_obj(d, *out, "file_search");+ yyjson_mut_val *arr = yyjson_mut_obj_add_arr(d, o, "vector_store_ids");+ size_t i, n;+ yyjson_val *v;+ yyjson_arr_foreach(ids, i, n, v)+ {+ if (!yyjson_is_str(v))+ return "vector_store_ids are ids.";+ yyjson_mut_arr_add_strcpy(d, arr, yyjson_get_str(v));+ }+ yyjson_arr_foreach(make, i, n, v)+ {+ char id[64];+ const char *why = make_store(s, v, id, sizeof id);+ if (why)+ return why;+ yyjson_mut_arr_add_strcpy(d, arr, id);+ }+ return NULL;+}++int as_list_reply(struct srv_req *r, yyjson_val *items)+{+ const char *l = srv_query(r, "limit"), *o = srv_query(r, "order");+ const char *after = srv_query(r, "after"), *before = srv_query(r, "before");+ long limit = 20;+ if (l) {+ char *end;+ limit = strtol(l, &end, 10);+ if (*end || limit < 1 || limit > 100)+ return bad(r, "'limit' is a number from 1 to 100.");+ }+ if (o && strcmp(o, "asc") && strcmp(o, "desc"))+ return bad(r, "'order' is asc or desc.");+ int desc = !o || !strcmp(o, "desc");+ size_t n = yyjson_arr_size(items);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *root = yyjson_mut_obj(d), *data = yyjson_mut_arr(d);+ /* after and before: the items strictly between them, in this order */+ long from = 0, to = (long)n;+ for (size_t k = 0; k < n; k++) {+ yyjson_val *v = yyjson_arr_get(items, desc ? n - 1 - k : k);+ yyjson_val *id = yyjson_obj_get(v, "id");+ if (after && yyjson_equals_str(id, after))+ from = (long)k + 1;+ if (before && yyjson_equals_str(id, before))+ to = (long)k;+ }+ long shown = 0;+ int more = 0;+ const char *first = NULL, *last = NULL;+ for (long k = from; k < to; k++) {+ if (shown == limit) {+ more = 1;+ break;+ }+ yyjson_val *v =+ yyjson_arr_get(items, desc ? n - 1 - (size_t)k : (size_t)k);+ yyjson_mut_arr_append(data, yyjson_val_mut_copy(d, v));+ const char *id = yyjson_get_str(yyjson_obj_get(v, "id"));+ if (!shown)+ first = id;+ last = id;+ shown++;+ }+ yyjson_mut_obj_add_str(d, root, "object", "list");+ yyjson_mut_obj_add_val(d, root, "data", data);+ if (first) {+ yyjson_mut_obj_add_strcpy(d, root, "first_id", first);+ yyjson_mut_obj_add_strcpy(d, root, "last_id", last);+ } else {+ yyjson_mut_obj_add_null(d, root, "first_id");+ yyjson_mut_obj_add_null(d, root, "last_id");+ }+ yyjson_mut_obj_add_bool(d, root, "has_more", more);+ int ret = reply_val(r, root);+ yyjson_mut_doc_free(d);+ return ret;+}++yyjson_doc *as_all(struct srv *s, const char *kind, const char *key,+ const char *value)+{+ char **ids;+ size_t n = srv_store_list(s->store, kind, &ids);+ struct janas_buf b = {0};+ janas_buf_put(&b, "[", 1);+ int first = 1;+ for (size_t i = 0; i < n; i++) {+ char *t = as_get(s, kind, ids[i], NULL);+ if (!t)+ continue;+ int keep = 1;+ if (key) {+ yyjson_doc *x = yyjson_read(t, strlen(t), 0);+ keep = yyjson_equals_str(+ yyjson_obj_get(yyjson_doc_get_root(x), key), value);+ yyjson_doc_free(x);+ }+ if (keep) {+ if (!first)+ janas_buf_put(&b, ",", 1);+ janas_buf_puts(&b, t);+ first = 0;+ }+ free(t);+ }+ srv_store_list_free(ids, n);+ janas_buf_put(&b, "]", 1);+ yyjson_doc *d = b.oom ? NULL : yyjson_read(b.p, b.n, 0);+ janas_buf_free(&b);+ return d;+}++int as_active_run(struct srv *s, const char *thread, char *id, size_t cap)+{+ yyjson_doc *d = as_all(s, AS_RUN, "thread_id", thread);+ size_t i, n;+ yyjson_val *v;+ int found = 0;+ yyjson_arr_foreach(yyjson_doc_get_root(d), i, n, v)+ {+ const char *st = yyjson_get_str(yyjson_obj_get(v, "status"));+ if (st &&+ (!strcmp(st, "queued") || !strcmp(st, "in_progress") ||+ !strcmp(st, "requires_action") || !strcmp(st, "cancelling"))) {+ snprintf(id, cap, "%s", yyjson_get_str(yyjson_obj_get(v, "id")));+ found = 1;+ }+ }+ yyjson_doc_free(d);+ return found;+}++/* ---- assistants ---- */++static const char *const SAMPLING[] = {"temperature", "top_p",+ "response_format", "reasoning_effort"};++/* The fields an assistant's body may set into o; NULL or the reason. */+static const char *assistant_fields(struct srv *s, yyjson_mut_doc *d,+ yyjson_mut_val *o, yyjson_val *root,+ int fresh)+{+ const char *why;+ if ((why = as_metadata_error(yyjson_obj_get(root, "metadata"))) ||+ (why = as_tools_error(yyjson_obj_get(root, "tools"))))+ return why;+ yyjson_val *t = yyjson_obj_get(root, "temperature");+ if (t && !yyjson_is_null(t) &&+ (!yyjson_is_num(t) || yyjson_get_num(t) < 0 || yyjson_get_num(t) > 2))+ return "'temperature' is from 0 to 2.";+ yyjson_val *p = yyjson_obj_get(root, "top_p");+ if (p && !yyjson_is_null(p) &&+ (!yyjson_is_num(p) || yyjson_get_num(p) < 0 || yyjson_get_num(p) > 1))+ return "'top_p' is from 0 to 1.";+ yyjson_mut_val *res = NULL;+ yyjson_val *rv = yyjson_obj_get(root, "tool_resources");+ if (rv && (why = as_resources(s, d, rv, &res)))+ return why;+ static const char *const TEXT[] = {"name", "description", "instructions",+ "model"};+ for (int i = 0; i < 4; i++) {+ yyjson_val *v = yyjson_obj_get(root, TEXT[i]);+ if (v || fresh) {+ yyjson_mut_obj_remove_key(o, TEXT[i]);+ if (yyjson_is_str(v))+ yyjson_mut_obj_add_strcpy(d, o, TEXT[i], yyjson_get_str(v));+ else if (i == 3)+ yyjson_mut_obj_add_strcpy(d, o, TEXT[i], s->cfg.model_id);+ else+ yyjson_mut_obj_add_null(d, o, TEXT[i]);+ }+ }+ yyjson_val *tools = yyjson_obj_get(root, "tools");+ if (tools || fresh) {+ yyjson_mut_obj_remove_key(o, "tools");+ yyjson_mut_obj_add_val(d, o, "tools",+ yyjson_is_arr(tools)+ ? yyjson_val_mut_copy(d, tools)+ : yyjson_mut_arr(d));+ }+ if (rv || fresh) {+ yyjson_mut_obj_remove_key(o, "tool_resources");+ yyjson_mut_obj_add_val(d, o, "tool_resources",+ res ? res : yyjson_mut_obj(d));+ }+ yyjson_val *md = yyjson_obj_get(root, "metadata");+ if (md || fresh) {+ yyjson_mut_obj_remove_key(o, "metadata");+ yyjson_mut_obj_add_val(d, o, "metadata",+ yyjson_is_obj(md) ? yyjson_val_mut_copy(d, md)+ : yyjson_mut_obj(d));+ }+ for (int i = 0; i < 4; i++) {+ yyjson_val *v = yyjson_obj_get(root, SAMPLING[i]);+ if (v || fresh) {+ yyjson_mut_obj_remove_key(o, SAMPLING[i]);+ if (v && !yyjson_is_null(v))+ yyjson_mut_obj_add_val(d, o, SAMPLING[i],+ yyjson_val_mut_copy(d, v));+ else if (i < 2)+ yyjson_mut_obj_add_real(d, o, SAMPLING[i], 1.0);+ else if (i == 2)+ yyjson_mut_obj_add_str(d, o, SAMPLING[i], "auto");+ else+ yyjson_mut_obj_add_null(d, o, SAMPLING[i]);+ }+ }+ return NULL;+}++int srv_assistants_create(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ if (!yyjson_is_obj(root)) {+ yyjson_doc_free(doc);+ return bad(r, "The body is a JSON object.");+ }+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ char id[48];+ srv_new_id(id, sizeof id, "asst_");+ yyjson_mut_obj_add_strcpy(d, o, "id", id);+ yyjson_mut_obj_add_str(d, o, "object", "assistant");+ yyjson_mut_obj_add_int(d, o, "created_at", (int64_t)time(NULL));+ const char *why = assistant_fields(r->srv, d, o, root, 1);+ int ret;+ if (why)+ ret = bad(r, why);+ else if (as_save(r->srv, AS_ASSISTANT, id, o, NULL, 1) != 0)+ ret = srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the assistant");+ else+ ret = reply_val(r, o);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(doc);+ return ret;+}++int srv_assistants_get(struct srv_req *r)+{+ char *t = as_get(r->srv, AS_ASSISTANT, r->param[0], NULL);+ if (!t)+ return not_found(r, "assistant", r->param[0]);+ return srv_reply_json(r, 200, t, strlen(t));+}++int srv_assistants_modify(struct srv_req *r)+{+ char *t = as_get(r->srv, AS_ASSISTANT, r->param[0], NULL);+ if (!t)+ return not_found(r, "assistant", r->param[0]);+ yyjson_doc *cur = yyjson_read(t, strlen(t), 0);+ free(t);+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_mut_doc *d = yyjson_doc_mut_copy(cur, NULL);+ yyjson_mut_val *o = yyjson_mut_doc_get_root(d);+ const char *why = !yyjson_is_obj(root)+ ? "The body is a JSON object."+ : assistant_fields(r->srv, d, o, root, 0);+ int ret;+ if (why)+ ret = bad(r, why);+ else if (as_save(r->srv, AS_ASSISTANT, r->param[0], o, NULL, 0) != 0)+ ret = srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the assistant");+ else+ ret = reply_val(r, o);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(doc);+ yyjson_doc_free(cur);+ return ret;+}++static int deleted(struct srv_req *r, const char *object)+{+ char out[400];+ snprintf(out, sizeof out,+ "{\"id\":\"%s\",\"object\":\"%s\",\"deleted\":true}",+ r->param[r->n_param - 1], object);+ char *t = strdup(out);+ return srv_reply_json(r, 200, t, t ? strlen(t) : 0);+}++int srv_assistants_delete(struct srv_req *r)+{+ if (srv_store_del(r->srv->store, AS_ASSISTANT, r->param[0]) != 0)+ return not_found(r, "assistant", r->param[0]);+ return deleted(r, "assistant.deleted");+}++int srv_assistants_list(struct srv_req *r)+{+ yyjson_doc *d = as_all(r->srv, AS_ASSISTANT, NULL, NULL);+ int ret = as_list_reply(r, yyjson_doc_get_root(d));+ yyjson_doc_free(d);+ return ret;+}++/* ---- messages ---- */++char *as_message_text(yyjson_val *content, const char **why)+{+ if (yyjson_is_str(content))+ return strdup(yyjson_get_str(content));+ if (!yyjson_is_arr(content) || !yyjson_arr_size(content)) {+ *why = "A message's content is a string or a list of text parts.";+ return NULL;+ }+ struct janas_buf b = {0};+ size_t i, n;+ yyjson_val *p;+ yyjson_arr_foreach(content, i, n, p)+ {+ const char *type = yyjson_get_str(yyjson_obj_get(p, "type"));+ if (type &&+ (!strcmp(type, "image_file") || !strcmp(type, "image_url"))) {+ *why = "Images in messages are not read on this server yet.";+ janas_buf_free(&b);+ return NULL;+ }+ const char *text = yyjson_get_str(yyjson_obj_get(p, "text"));+ if (!type || strcmp(type, "text") || !text) {+ *why = "A message's parts are {\"type\": \"text\", \"text\"}.";+ janas_buf_free(&b);+ return NULL;+ }+ if (b.n)+ janas_buf_put(&b, "\n", 1);+ janas_buf_puts(&b, text);+ }+ janas_buf_put(&b, "", 1);+ if (b.oom) {+ janas_buf_free(&b);+ *why = "out of memory";+ return NULL;+ }+ return b.p;+}++yyjson_mut_val *as_message(yyjson_mut_doc *d, const char *thread,+ const char *role, const char *text,+ yyjson_val *attachments, yyjson_val *metadata,+ const char *assistant, const char *run)+{+ yyjson_mut_val *m = yyjson_mut_obj(d);+ char id[48];+ srv_new_id(id, sizeof id, "msg_");+ int64_t now = (int64_t)time(NULL);+ yyjson_mut_obj_add_strcpy(d, m, "id", id);+ yyjson_mut_obj_add_str(d, m, "object", "thread.message");+ yyjson_mut_obj_add_int(d, m, "created_at", now);+ yyjson_mut_obj_add_strcpy(d, m, "thread_id", thread);+ yyjson_mut_obj_add_str(d, m, "status", "completed");+ yyjson_mut_obj_add_null(d, m, "incomplete_details");+ yyjson_mut_obj_add_int(d, m, "completed_at", now);+ yyjson_mut_obj_add_null(d, m, "incomplete_at");+ yyjson_mut_obj_add_strcpy(d, m, "role", role);+ yyjson_mut_val *c = yyjson_mut_obj_add_arr(d, m, "content");+ yyjson_mut_val *part = yyjson_mut_arr_add_obj(d, c);+ yyjson_mut_obj_add_str(d, part, "type", "text");+ yyjson_mut_val *tx = yyjson_mut_obj_add_obj(d, part, "text");+ yyjson_mut_obj_add_strcpy(d, tx, "value", text ? text : "");+ yyjson_mut_obj_add_arr(d, tx, "annotations");+ if (assistant)+ yyjson_mut_obj_add_strcpy(d, m, "assistant_id", assistant);+ else+ yyjson_mut_obj_add_null(d, m, "assistant_id");+ if (run)+ yyjson_mut_obj_add_strcpy(d, m, "run_id", run);+ else+ yyjson_mut_obj_add_null(d, m, "run_id");+ yyjson_mut_obj_add_val(d, m, "attachments",+ yyjson_is_arr(attachments)+ ? yyjson_val_mut_copy(d, attachments)+ : yyjson_mut_arr(d));+ yyjson_mut_obj_add_val(d, m, "metadata",+ yyjson_is_obj(metadata)+ ? yyjson_val_mut_copy(d, metadata)+ : yyjson_mut_obj(d));+ return m;+}++int as_message_append(struct srv *s, const char *thread, yyjson_mut_val *m)+{+ char *aux = NULL;+ char *t = as_get(s, AS_THREAD, thread, &aux);+ if (!t)+ return -1;+ free(t);+ yyjson_doc *x = aux ? yyjson_read(aux, strlen(aux), 0) : NULL;+ free(aux);+ yyjson_mut_doc *d =+ x ? yyjson_doc_mut_copy(x, NULL) : yyjson_mut_doc_new(NULL);+ yyjson_doc_free(x);+ yyjson_mut_val *arr = yyjson_mut_doc_get_root(d);+ if (!arr) {+ arr = yyjson_mut_arr(d);+ yyjson_mut_doc_set_root(d, arr);+ }+ yyjson_mut_arr_append(arr, yyjson_mut_val_mut_copy(d, m));+ char *out = yyjson_mut_write(d, 0, NULL);+ yyjson_mut_doc_free(d);+ int rc =+ out ? srv_store_update(s->store, AS_THREAD, thread, NULL, out) : -1;+ free(out);+ return rc ? -1 : 0;+}++/* Whether the attachment a names the tool. */+static int attach_for(yyjson_val *a, const char *tool)+{+ size_t k, m;+ yyjson_val *t;+ yyjson_arr_foreach(yyjson_obj_get(a, "tools"), k, m, t)+ {+ if (yyjson_equals_str(yyjson_obj_get(t, "type"), tool))+ return 1;+ }+ return 0;+}++/* The object o[key], made when missing. */+static yyjson_mut_val *obj_at(yyjson_mut_doc *d, yyjson_mut_val *o,+ const char *key)+{+ yyjson_mut_val *v = yyjson_mut_obj_get(o, key);+ if (!yyjson_mut_is_obj(v)) {+ yyjson_mut_obj_remove_key(o, key);+ v = yyjson_mut_obj_add_obj(d, o, key);+ }+ return v;+}++static yyjson_mut_val *arr_at(yyjson_mut_doc *d, yyjson_mut_val *o,+ const char *key)+{+ yyjson_mut_val *v = yyjson_mut_obj_get(o, key);+ if (!yyjson_mut_is_arr(v)) {+ yyjson_mut_obj_remove_key(o, key);+ v = yyjson_mut_obj_add_arr(d, o, key);+ }+ return v;+}++const char *as_attach(struct srv *s, const char *thread, yyjson_val *att)+{+ if (!att || yyjson_is_null(att))+ return NULL;+ if (!yyjson_is_arr(att) || yyjson_arr_size(att) > 10)+ return "'attachments' is a list of at most 10 files.";+ size_t i, n;+ yyjson_val *a;+ int fs = 0, ci = 0;+ yyjson_arr_foreach(att, i, n, a)+ {+ const char *fid = yyjson_get_str(yyjson_obj_get(a, "file_id"));+ if (!fid)+ return "An attachment has a file_id.";+ size_t k, m;+ yyjson_val *t;+ yyjson_arr_foreach(yyjson_obj_get(a, "tools"), k, m, t)+ {+ const char *type = yyjson_get_str(yyjson_obj_get(t, "type"));+ if (type && !strcmp(type, "code_interpreter") &&+ srv_containers_off())+ return srv_containers_off();+ if (!type || (strcmp(type, "file_search") &&+ strcmp(type, "code_interpreter")))+ return "An attachment's tools are file_search or "+ "code_interpreter.";+ }+ fs += attach_for(a, "file_search");+ ci += attach_for(a, "code_interpreter");+ }+ char *tt = as_get(s, AS_THREAD, thread, NULL);+ yyjson_doc *td = tt ? yyjson_read(tt, strlen(tt), 0) : NULL;+ free(tt);+ if (!td)+ return "The thread is gone.";+ yyjson_mut_doc *d = yyjson_doc_mut_copy(td, NULL);+ yyjson_doc_free(td);+ yyjson_mut_val *o = yyjson_mut_doc_get_root(d);+ yyjson_mut_val *res = obj_at(d, o, "tool_resources");+ const char *why = NULL;+ char vsid[64] = "";+ if (fs) { /* the thread's own store: made at the first file */+ yyjson_mut_val *ids =+ arr_at(d, obj_at(d, res, "file_search"), "vector_store_ids");+ const char *vs = yyjson_mut_get_str(yyjson_mut_arr_get_first(ids));+ if (vs) {+ snprintf(vsid, sizeof vsid, "%s", vs);+ } else {+ static const char spec[] = "{\"name\":\"thread files\"}";+ yyjson_doc *spd = yyjson_read(spec, sizeof spec - 1, 0);+ why = make_store(s, yyjson_doc_get_root(spd), vsid, sizeof vsid);+ yyjson_doc_free(spd);+ if (!why)+ yyjson_mut_arr_add_strcpy(d, ids, vsid);+ }+ }+ if (ci && !why) { /* copied into the thread's container at its runs */+ yyjson_mut_val *ids =+ arr_at(d, obj_at(d, res, "code_interpreter"), "file_ids");+ yyjson_arr_foreach(att, i, n, a)+ {+ const char *fid = yyjson_get_str(yyjson_obj_get(a, "file_id"));+ int have = 0;+ size_t k, m;+ yyjson_mut_val *v;+ yyjson_mut_arr_foreach(ids, k, m, v)+ {+ have |= yyjson_mut_equals_str(v, fid);+ }+ if (attach_for(a, "code_interpreter") && !have)+ yyjson_mut_arr_add_strcpy(d, ids, fid);+ }+ if (yyjson_mut_arr_size(ids) > 20)+ why = "A thread's code_interpreter takes at most 20 files.";+ }+ if (!why)+ as_save(s, AS_THREAD, thread, o, NULL, 0);+ yyjson_mut_doc_free(d);+ yyjson_arr_foreach(att, i, n, a)+ {+ if (why)+ break;+ const char *fid = yyjson_get_str(yyjson_obj_get(a, "file_id"));+ if (attach_for(a, "file_search") &&+ srv_vs_attach(s, vsid, fid, NULL, NULL, &why) != 0 && !why)+ why = "A file could not be attached.";+ }+ return why;+}++int srv_messages_create(struct srv_req *r)+{+ const char *thread = r->param[0];+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ const char *role = yyjson_get_str(yyjson_obj_get(root, "role"));+ const char *why = NULL;+ char run[64];+ char *text = NULL;+ as_lock();+ char *tt = as_get(r->srv, AS_THREAD, thread, NULL);+ free(tt);+ if (!tt) {+ as_unlock();+ yyjson_doc_free(doc);+ return not_found(r, "thread", thread);+ }+ if (!yyjson_is_obj(root))+ why = "The body is a JSON object.";+ else if (!role || (strcmp(role, "user") && strcmp(role, "assistant")))+ why = "'role' is user or assistant.";+ else if (as_active_run(r->srv, thread, run, sizeof run))+ why = "The thread has a run going: messages are added once it "+ "ends.";+ else if (!(why = as_metadata_error(yyjson_obj_get(root, "metadata"))))+ text = as_message_text(yyjson_obj_get(root, "content"), &why);+ yyjson_val *att = yyjson_obj_get(root, "attachments");+ if (!why)+ why = as_attach(r->srv, thread, att);+ int ret;+ if (why) {+ ret = bad(r, why);+ } else {+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *m =+ as_message(d, thread, role, text, att,+ yyjson_obj_get(root, "metadata"), NULL, NULL);+ ret = as_message_append(r->srv, thread, m) == 0+ ? reply_val(r, m)+ : srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the message");+ yyjson_mut_doc_free(d);+ }+ as_unlock();+ free(text);+ yyjson_doc_free(doc);+ return ret;+}++/* The thread's messages (as_lock held): NULL when there is no thread. */+static yyjson_doc *messages_of(struct srv *s, const char *thread)+{+ char *aux = NULL;+ char *t = as_get(s, AS_THREAD, thread, &aux);+ if (!t)+ return NULL;+ free(t);+ yyjson_doc *x = yyjson_read(aux ? aux : "[]", aux ? strlen(aux) : 2, 0);+ free(aux);+ return x;+}++int srv_messages_list(struct srv_req *r)+{+ as_lock();+ yyjson_doc *x = messages_of(r->srv, r->param[0]);+ as_unlock();+ if (!x)+ return not_found(r, "thread", r->param[0]);+ const char *run = srv_query(r, "run_id");+ int ret;+ if (run) { /* those the run wrote */+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *arr = yyjson_mut_arr(d);+ yyjson_mut_doc_set_root(d, arr);+ size_t i, n;+ yyjson_val *m;+ yyjson_arr_foreach(yyjson_doc_get_root(x), i, n, m)+ {+ if (yyjson_equals_str(yyjson_obj_get(m, "run_id"), run))+ yyjson_mut_arr_append(arr, yyjson_val_mut_copy(d, m));+ }+ yyjson_doc *f = yyjson_mut_doc_imut_copy(d, NULL);+ yyjson_mut_doc_free(d);+ ret = as_list_reply(r, yyjson_doc_get_root(f));+ yyjson_doc_free(f);+ } else {+ ret = as_list_reply(r, yyjson_doc_get_root(x));+ }+ yyjson_doc_free(x);+ return ret;+}++/* The message's index in the thread's list, or -1. */+static long message_at(yyjson_val *arr, const char *id)+{+ size_t i, n;+ yyjson_val *m;+ yyjson_arr_foreach(arr, i, n, m)+ {+ if (yyjson_equals_str(yyjson_obj_get(m, "id"), id))+ return (long)i;+ }+ return -1;+}++int srv_messages_get(struct srv_req *r)+{+ as_lock();+ yyjson_doc *x = messages_of(r->srv, r->param[0]);+ as_unlock();+ if (!x)+ return not_found(r, "thread", r->param[0]);+ long k = message_at(yyjson_doc_get_root(x), r->param[1]);+ int ret;+ if (k < 0) {+ ret = not_found(r, "message", r->param[1]);+ } else {+ char *t = yyjson_val_write(+ yyjson_arr_get(yyjson_doc_get_root(x), (size_t)k), 0, NULL);+ ret = srv_reply_json(r, 200, t, t ? strlen(t) : 0);+ }+ yyjson_doc_free(x);+ return ret;+}++/* The thread's messages with message k changed (metadata) or removed. */+static int message_change(struct srv_req *r, int remove)+{+ yyjson_doc *doc = remove ? NULL : yyjson_read(r->body, r->n_body, 0);+ yyjson_val *md = yyjson_obj_get(yyjson_doc_get_root(doc), "metadata");+ const char *why = remove ? NULL : as_metadata_error(md);+ if (!remove && !yyjson_is_obj(yyjson_doc_get_root(doc)))+ why = "The body is a JSON object.";+ if (why) {+ yyjson_doc_free(doc);+ return bad(r, why);+ }+ as_lock();+ yyjson_doc *x = messages_of(r->srv, r->param[0]);+ long k = x ? message_at(yyjson_doc_get_root(x), r->param[1]) : -1;+ int ret;+ if (!x || k < 0) {+ as_unlock();+ ret = !x ? not_found(r, "thread", r->param[0])+ : not_found(r, "message", r->param[1]);+ } else {+ yyjson_mut_doc *d = yyjson_doc_mut_copy(x, NULL);+ yyjson_mut_val *arr = yyjson_mut_doc_get_root(d);+ yyjson_mut_val *m = yyjson_mut_arr_get(arr, (size_t)k);+ if (remove) {+ yyjson_mut_arr_remove(arr, (size_t)k);+ } else if (md) {+ yyjson_mut_obj_remove_key(m, "metadata");+ yyjson_mut_obj_add_val(d, m, "metadata",+ yyjson_is_obj(md)+ ? yyjson_val_mut_copy(d, md)+ : yyjson_mut_obj(d));+ }+ char *out = yyjson_mut_write(d, 0, NULL);+ int rc = out ? srv_store_update(r->srv->store, AS_THREAD, r->param[0],+ NULL, out)+ : -1;+ free(out);+ as_unlock();+ if (rc)+ ret = srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the thread");+ else if (remove)+ ret = deleted(r, "thread.message.deleted");+ else+ ret = reply_val(r, m);+ yyjson_mut_doc_free(d);+ }+ yyjson_doc_free(x);+ yyjson_doc_free(doc);+ return ret;+}++int srv_messages_modify(struct srv_req *r)+{+ return message_change(r, 0);+}++int srv_messages_delete(struct srv_req *r)+{+ return message_change(r, 1);+}++/* ---- threads ---- */++/* A thread made of a body {messages, tool_resources, metadata}: 0 with its+ id, or the reason. */+const char *as_thread_make(struct srv *s, yyjson_val *root, char *id,+ size_t cap)+{+ yyjson_val *msgs = yyjson_obj_get(root, "messages");+ yyjson_val *md = yyjson_obj_get(root, "metadata");+ const char *why;+ if (root && !yyjson_is_obj(root))+ return "The thread is a JSON object.";+ if ((why = as_metadata_error(md)))+ return why;+ if (msgs && (!yyjson_is_arr(msgs) || yyjson_arr_size(msgs) > 32))+ return "'messages' is a list of at most 32 messages.";+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *res = NULL;+ if ((why = as_resources(s, d, yyjson_obj_get(root, "tool_resources"),+ &res))) {+ yyjson_mut_doc_free(d);+ return why;+ }+ /* the messages checked before the thread is kept */+ size_t i, n;+ yyjson_val *m;+ yyjson_arr_foreach(msgs, i, n, m)+ {+ const char *role = yyjson_get_str(yyjson_obj_get(m, "role"));+ if (!role || (strcmp(role, "user") && strcmp(role, "assistant")))+ why = "A message's role is user or assistant.";+ char *t =+ why ? NULL : as_message_text(yyjson_obj_get(m, "content"), &why);+ free(t);+ if (!why)+ why = as_metadata_error(yyjson_obj_get(m, "metadata"));+ if (why)+ break;+ }+ if (why) {+ yyjson_mut_doc_free(d);+ return why;+ }+ yyjson_mut_val *o = yyjson_mut_obj(d);+ srv_new_id(id, cap, "thread_");+ yyjson_mut_obj_add_strcpy(d, o, "id", id);+ yyjson_mut_obj_add_str(d, o, "object", "thread");+ yyjson_mut_obj_add_int(d, o, "created_at", (int64_t)time(NULL));+ yyjson_mut_obj_add_val(d, o, "tool_resources", res);+ yyjson_mut_obj_add_val(d, o, "metadata",+ yyjson_is_obj(md) ? yyjson_val_mut_copy(d, md)+ : yyjson_mut_obj(d));+ as_lock();+ if (as_save(s, AS_THREAD, id, o, "[]", 1) != 0)+ why = "cannot keep the thread";+ yyjson_arr_foreach(msgs, i, n, m)+ {+ if (why)+ break;+ const char *role = yyjson_get_str(yyjson_obj_get(m, "role"));+ char *t = as_message_text(yyjson_obj_get(m, "content"), &why);+ yyjson_val *att = yyjson_obj_get(m, "attachments");+ if (t && !(why = as_attach(s, id, att))) {+ yyjson_mut_val *mm = as_message(+ d, id, role, t, att, yyjson_obj_get(m, "metadata"), NULL, NULL);+ if (as_message_append(s, id, mm) != 0)+ why = "cannot keep the message";+ }+ free(t);+ }+ if (why)+ srv_store_del(s->store, AS_THREAD, id);+ as_unlock();+ yyjson_mut_doc_free(d);+ return why;+}++int srv_threads_create(struct srv_req *r)+{+ yyjson_doc *doc = r->n_body ? yyjson_read(r->body, r->n_body, 0) : NULL;+ if (r->n_body && !doc)+ return bad(r, "The body is a JSON object.");+ char id[48];+ const char *why =+ as_thread_make(r->srv, yyjson_doc_get_root(doc), id, sizeof id);+ yyjson_doc_free(doc);+ if (why)+ return bad(r, why);+ char *t = as_get(r->srv, AS_THREAD, id, NULL);+ return srv_reply_json(r, 200, t, t ? strlen(t) : 0);+}++int srv_threads_get(struct srv_req *r)+{+ char *t = as_get(r->srv, AS_THREAD, r->param[0], NULL);+ if (!t)+ return not_found(r, "thread", r->param[0]);+ return srv_reply_json(r, 200, t, strlen(t));+}++int srv_threads_modify(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_val *md = yyjson_obj_get(root, "metadata");+ const char *why = !yyjson_is_obj(root) ? "The body is a JSON object."+ : as_metadata_error(md);+ as_lock();+ char *t = as_get(r->srv, AS_THREAD, r->param[0], NULL);+ yyjson_doc *cur = t ? yyjson_read(t, strlen(t), 0) : NULL;+ free(t);+ int ret;+ if (!cur) {+ ret = not_found(r, "thread", r->param[0]);+ } else {+ yyjson_mut_doc *d = yyjson_doc_mut_copy(cur, NULL);+ yyjson_mut_val *o = yyjson_mut_doc_get_root(d);+ yyjson_mut_val *res = NULL;+ yyjson_val *rv = yyjson_obj_get(root, "tool_resources");+ if (!why && rv)+ why = as_resources(r->srv, d, rv, &res);+ if (!why && res) {+ yyjson_mut_obj_remove_key(o, "tool_resources");+ yyjson_mut_obj_add_val(d, o, "tool_resources", res);+ }+ if (!why && md) {+ yyjson_mut_obj_remove_key(o, "metadata");+ yyjson_mut_obj_add_val(d, o, "metadata",+ yyjson_is_obj(md)+ ? yyjson_val_mut_copy(d, md)+ : yyjson_mut_obj(d));+ }+ if (why)+ ret = bad(r, why);+ else if (as_save(r->srv, AS_THREAD, r->param[0], o, NULL, 0) != 0)+ ret = srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the thread");+ else+ ret = reply_val(r, o);+ yyjson_mut_doc_free(d);+ }+ as_unlock();+ yyjson_doc_free(cur);+ yyjson_doc_free(doc);+ return ret;+}++int srv_threads_delete(struct srv_req *r)+{+ char run[64];+ as_lock();+ int active = as_active_run(r->srv, r->param[0], run, sizeof run);+ int rc = active ? 0 : srv_store_del(r->srv->store, AS_THREAD, r->param[0]);+ as_unlock();+ if (active)+ return bad(r, "The thread has a run going: cancel it first.");+ if (rc != 0)+ return not_found(r, "thread", r->param[0]);+ return deleted(r, "thread.deleted");+}diff --git a/src/server/assistants.h b/src/server/assistants.hnew file mode 100644index 0000000..9f1fd19--- /dev/null+++ b/src/server/assistants.h@@ -0,0 +1,87 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * assistants.h - what assistants.c (assistants, threads, messages) and+ * runs.c (runs and their steps) share: OpenAI's Assistants API, kept in the+ * store; a thread's messages are the aux text of the thread, a run's steps+ * the aux text of the run.+ */+#ifndef JANAS_SERVER_ASSISTANTS_H+#define JANAS_SERVER_ASSISTANTS_H++#include "server.h"++#include <yyjson.h>++#define AS_ASSISTANT "assistant"+#define AS_THREAD "thread"+#define AS_RUN "thread.run"++/* Every read-change-write of a thread's messages or of a run. */+void as_lock(void);+void as_unlock(void);++/* A document and its aux (either NULL), malloc'd; NULL when not there. */+char *as_get(struct srv *s, const char *kind, const char *id, char **aux);+/* o written as the document of kind and id (aux NULL: unchanged, unless+ fresh): 0 or -1. */+int as_save(struct srv *s, const char *kind, const char *id, yyjson_mut_val *o,+ const char *aux, int fresh);++/* NULL when metadata is absent or at most 16 strings, else the reason. */+const char *as_metadata_error(yyjson_val *md);+/* NULL when tools are ones a run here can use, else the reason. */+const char *as_tools_error(yyjson_val *tools);+/* NULL when tool_resources is one these can keep (vector_stores made into+ vector_store_ids in out, a copy made in d), else the reason. */+const char *as_resources(struct srv *s, yyjson_mut_doc *d, yyjson_val *res,+ yyjson_mut_val **out);++/* A page of items (an array, oldest first, each with an id) as OpenAI's+ lists answer: limit, order (desc by default), after, before. */+int as_list_reply(struct srv_req *r, yyjson_val *items);++/* A new message of the thread (role, text, attachments as given; the+ assistant and the run that wrote it, or NULL). */+yyjson_mut_val *as_message(yyjson_mut_doc *d, const char *thread,+ const char *role, const char *text,+ yyjson_val *attachments, yyjson_val *metadata,+ const char *assistant, const char *run);+/* m appended to the thread's messages (as_lock held): 0, -1 none. */+int as_message_append(struct srv *s, const char *thread, yyjson_mut_val *m);+/* The text of a message given by a client (a string, or text parts), or+ NULL with the reason; malloc'd. */+char *as_message_text(yyjson_val *content, const char **why);+/* The files of attachments with file_search added to the thread's vector+ store (made when the thread has none): NULL, or the reason. */+const char *as_attach(struct srv *s, const char *thread, yyjson_val *att);++/* The documents of a kind as an array, oldest first (those whose key has+ value when key is given). */+yyjson_doc *as_all(struct srv *s, const char *kind, const char *key,+ const char *value);+/* A thread made of a body {messages, tool_resources, metadata} (root may+ be NULL): NULL with its id, or the reason. */+const char *as_thread_make(struct srv *s, yyjson_val *root, char *id,+ size_t cap);++/* The run of the thread still going (queued, in_progress, requires_action,+ cancelling): 1 with its id, 0 none. */+int as_active_run(struct srv *s, const char *thread, char *id, size_t cap);++/* runs_stream.c: the events of runs for the clients streaming them. A+ feed opened for a run gets every event emitted for it until done. */+struct as_feed;+struct as_feed *as_feed_open(const char *run);+void as_feed_close(void *f); /* a feed not handed to as_feed_reply */+int as_feed_reply(struct srv_req *r, struct as_feed *f);+void as_emit(const char *run, const char *event, yyjson_mut_val *v);+void as_emit_done(const char *run);+void as_feeds_end(void); /* at stop: every feed ended */++/* runs.c: resumes the runs a stopped server left going, and waits at stop+ for those running. */+void srv_runs_start(struct srv *s);+void srv_runs_stop(void);++#endifdiff --git a/src/server/audio.c b/src/server/audio.cnew file mode 100644index 0000000..75edfb5--- /dev/null+++ b/src/server/audio.c@@ -0,0 +1,751 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * audio.c - POST /audio/transcriptions and /audio/translations: a recording+ * (multipart/form-data, its file a WAV) turned into text by the speech+ * model the server was started with (--speech-model, a Whisper model), as+ * OpenAI's whisper-1 answers: json, text, srt, vtt or verbose_json with+ * the segments and their times. The model runs in the request's thread,+ * one recording at a time, beside the chat model's engine.+ */+#include "llm/json.h"+#include "multipart.h"+#include "server.h"++#include <math.h>+#include <stdlib.h>+#include <string.h>+#include <time.h>++#include <microhttpd.h>++int srv_speech_start(struct srv *s, char *err, size_t err_len)+{+ pthread_mutex_init(&s->speech_mu, NULL);+ if (!s->cfg.speech_path)+ return 0;+ struct janas_llm_speech_params p;+ janas_llm_speech_params_default(&p);+ p.vad_model = s->cfg.vad_path;+ p.speaker_model = s->cfg.speaker_path;+ if (janas_llm_speech_open(s->cfg.speech_path, &p, &s->speech) !=+ JANAS_LLM_OK) {+ snprintf(err, err_len, "%s", janas_llm_last_error());+ return -1;+ }+ return 0;+}++void srv_speech_stop(struct srv *s)+{+ janas_llm_speech_close(s->speech);+ s->speech = NULL;+ pthread_mutex_destroy(&s->speech_mu);+}++static int bad(struct srv_req *r, const char *msg)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_value",+ "%s", msg);+}++/* The text of a form field, NUL-terminated into buf: 1, 0 when absent, -1+ when it does not fit or is a file. */+static int field(const struct srv_part *parts, int n, const char *name,+ char *buf, size_t cap)+{+ const struct srv_part *p = srv_multipart_get(parts, n, name);+ if (!p)+ return 0;+ if (p->n >= cap || p->has_filename)+ return -1;+ memcpy(buf, p->data, p->n);+ buf[p->n] = 0;+ return 1;+}++/* seconds as 00:00:00,000 (srt) or 00:00:00.000 (vtt) */+static void stamp(struct janas_buf *b, double t, char sep)+{+ long ms = lround(t * 1000);+ janas_buf_printf(b, "%02ld:%02ld:%02ld%c%03ld", ms / 3600000,+ ms / 60000 % 60, ms / 1000 % 60, sep, ms % 1000);+}++/* segment i's text, malloc'd */+static char *seg_text(const janas_llm_speech *sp, int32_t i, double *t0,+ double *t1)+{+ int32_t len = 0;+ janas_llm_speech_segment(sp, i, t0, t1, NULL, 0, &len);+ char *t = malloc((size_t)len + 1);+ if (t && janas_llm_speech_segment(sp, i, NULL, NULL, t, len + 1, &len) !=+ JANAS_LLM_OK) {+ free(t);+ t = NULL;+ }+ return t;+}++static char *whole_text(const janas_llm_speech *sp)+{+ int32_t len = 0;+ janas_llm_speech_text(sp, NULL, 0, &len);+ char *t = malloc((size_t)len + 1);+ if (t && janas_llm_speech_text(sp, t, len + 1, &len) != JANAS_LLM_OK) {+ free(t);+ t = NULL;+ }+ return t;+}++/* srt and vtt: a cue a segment */+static int reply_cues(struct srv_req *r, const janas_llm_speech *sp, int vtt)+{+ struct janas_buf b = {0};+ if (vtt)+ janas_buf_puts(&b, "WEBVTT\n\n");+ for (int32_t i = 0; i < janas_llm_speech_segments(sp); i++) {+ double t0, t1;+ char *t = seg_text(sp, i, &t0, &t1);+ if (!t) {+ janas_buf_free(&b);+ return srv_reply_error(r, 500, "server_error", NULL,+ "out of memory");+ }+ if (!vtt)+ janas_buf_printf(&b, "%d\n", i + 1);+ stamp(&b, t0, vtt ? '.' : ',');+ janas_buf_puts(&b, " --> ");+ stamp(&b, t1, vtt ? '.' : ',');+ janas_buf_printf(&b, "\n%s\n\n", t[0] == ' ' ? t + 1 : t);+ free(t);+ }+ if (b.oom) {+ janas_buf_free(&b);+ return srv_reply_error(r, 500, "server_error", NULL, "out of memory");+ }+ return srv_reply_bytes(r, 200, b.p ? b.p : calloc(1, 1), b.n,+ vtt ? "text/vtt; charset=utf-8"+ : "application/x-subrip; charset=utf-8");+}++/* OpenAI's diarized_json: the turns as transcript.text.segment items */+static void add_turns(yyjson_mut_doc *d, yyjson_mut_val *arr,+ const janas_llm_speech *sp)+{+ for (int32_t i = 0; i < janas_llm_speech_turns(sp); i++) {+ double t0, t1;+ char who[128];+ int32_t wl = 0, tl = 0;+ janas_llm_speech_turn(sp, i, &t0, &t1, who, sizeof who, &wl, NULL, 0,+ &tl);+ char *t = malloc((size_t)tl + 1);+ if (!t)+ return;+ janas_llm_speech_turn(sp, i, NULL, NULL, NULL, 0, NULL, t, tl + 1, &tl);+ char id[32];+ snprintf(id, sizeof id, "seg_%d", i);+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, arr);+ yyjson_mut_obj_add_str(d, o, "type", "transcript.text.segment");+ yyjson_mut_obj_add_strcpy(d, o, "id", id);+ yyjson_mut_obj_add_real(d, o, "start", t0);+ yyjson_mut_obj_add_real(d, o, "end", t1);+ yyjson_mut_obj_add_strcpy(d, o, "text", t);+ yyjson_mut_obj_add_strcpy(d, o, "speaker", who);+ free(t);+ }+}++static int reply_diarized(struct srv_req *r, const janas_llm_speech *sp)+{+ char *text = whole_text(sp);+ if (!text)+ return srv_reply_error(r, 500, "server_error", NULL, "out of memory");+ double dur = janas_llm_speech_duration(sp);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_doc_set_root(d, o);+ yyjson_mut_obj_add_str(d, o, "task", "transcribe");+ yyjson_mut_obj_add_real(d, o, "duration", dur);+ yyjson_mut_obj_add_strcpy(d, o, "text", text);+ free(text);+ add_turns(d, yyjson_mut_obj_add_arr(d, o, "segments"), sp);+ yyjson_mut_val *u = yyjson_mut_obj_add_obj(d, o, "usage");+ yyjson_mut_obj_add_str(d, u, "type", "duration");+ yyjson_mut_obj_add_int(d, u, "seconds", (int64_t)ceil(dur));+ size_t len;+ char *json = yyjson_mut_write(d, 0, &len);+ yyjson_mut_doc_free(d);+ return srv_reply_json(r, 200, json, len);+}++/* granularities: 1 segments, 2 words */+static void add_logprobs(yyjson_mut_doc *d, yyjson_mut_val *arr,+ const janas_llm_speech *sp, int32_t i);++static int reply_json(struct srv_req *r, const janas_llm_speech *sp,+ int verbose, int translate, int gran, int logprobs)+{+ char *text = whole_text(sp);+ if (!text)+ return srv_reply_error(r, 500, "server_error", NULL, "out of memory");+ double dur = janas_llm_speech_duration(sp);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_doc_set_root(d, o);+ if (verbose) {+ char code[16];+ int32_t n;+ janas_llm_speech_language(sp, code, sizeof code, &n);+ yyjson_mut_obj_add_str(d, o, "task",+ translate ? "translate" : "transcribe");+ /* OpenAI names the language in English, the translation's too */+ yyjson_mut_obj_add_strcpy(d, o, "language",+ translate ? "english"+ : janas_llm_speech_language_name(code)+ ? janas_llm_speech_language_name(code)+ : code);+ yyjson_mut_obj_add_real(d, o, "duration", dur);+ }+ yyjson_mut_obj_add_strcpy(d, o, "text", text);+ free(text);+ if (logprobs) {+ yyjson_mut_val *a = yyjson_mut_obj_add_arr(d, o, "logprobs");+ for (int32_t i = 0; i < janas_llm_speech_segments(sp); i++)+ add_logprobs(d, a, sp, i);+ }+ if (verbose && (gran & 2)) {+ /* OpenAI's words: without their leading space */+ yyjson_mut_val *ws = yyjson_mut_obj_add_arr(d, o, "words");+ for (int32_t i = 0; i < janas_llm_speech_words(sp); i++) {+ double t0, t1;+ int32_t len = 0;+ janas_llm_speech_word(sp, i, &t0, &t1, NULL, NULL, 0, &len);+ char *t = malloc((size_t)len + 1);+ if (!t)+ break;+ janas_llm_speech_word(sp, i, NULL, NULL, NULL, t, len + 1, &len);+ const char *b = t;+ while (*b == ' ')+ b++;+ yyjson_mut_val *w = yyjson_mut_arr_add_obj(d, ws);+ yyjson_mut_obj_add_strcpy(d, w, "word", b);+ yyjson_mut_obj_add_real(d, w, "start", t0);+ yyjson_mut_obj_add_real(d, w, "end", t1);+ free(t);+ }+ }+ if (verbose && (gran & 1)) {+ yyjson_mut_val *segs = yyjson_mut_obj_add_arr(d, o, "segments");+ for (int32_t i = 0; i < janas_llm_speech_segments(sp); i++) {+ double t0, t1;+ float lp, ns, temp, cr;+ int32_t ids[512], nt = 0;+ char *t = seg_text(sp, i, &t0, &t1);+ janas_llm_speech_segment_info(sp, i, &lp, &ns, &temp, &cr, ids, 512,+ &nt);+ yyjson_mut_val *s = yyjson_mut_arr_add_obj(d, segs);+ yyjson_mut_obj_add_int(d, s, "id", i);+ /* the 10 ms frame it starts at */+ yyjson_mut_obj_add_int(d, s, "seek", (int64_t)lround(t0 * 100));+ yyjson_mut_obj_add_real(d, s, "start", t0);+ yyjson_mut_obj_add_real(d, s, "end", t1);+ yyjson_mut_obj_add_strcpy(d, s, "text", t ? t : "");+ yyjson_mut_val *a = yyjson_mut_obj_add_arr(d, s, "tokens");+ for (int32_t k = 0; k < nt && k < 512; k++)+ yyjson_mut_arr_add_int(d, a, ids[k]);+ yyjson_mut_obj_add_real(d, s, "temperature", temp);+ yyjson_mut_obj_add_real(d, s, "avg_logprob", lp);+ yyjson_mut_obj_add_real(d, s, "compression_ratio", cr);+ yyjson_mut_obj_add_real(d, s, "no_speech_prob", ns);+ free(t);+ }+ }+ yyjson_mut_val *u = yyjson_mut_obj_add_obj(d, o, "usage");+ yyjson_mut_obj_add_str(d, u, "type", "duration");+ yyjson_mut_obj_add_int(d, u, "seconds", (int64_t)ceil(dur));+ size_t len;+ char *json = yyjson_mut_write(d, 0, &len);+ yyjson_mut_doc_free(d);+ return srv_reply_json(r, 200, json, len);+}++/* the log-probabilities of segment i's text tokens, as OpenAI's include[]+ = logprobs gives them: each token's text, log-probability and bytes */+static void add_logprobs(yyjson_mut_doc *d, yyjson_mut_val *arr,+ const janas_llm_speech *sp, int32_t i)+{+ int32_t ids[512], nt = 0, nl = 0;+ float lp[512];+ janas_llm_speech_segment_info(sp, i, NULL, NULL, NULL, NULL, ids, 512, &nt);+ janas_llm_speech_segment_logprobs(sp, i, lp, 512, &nl);+ for (int32_t k = 0; k < nt && k < nl && k < 512; k++) {+ if (!janas_llm_speech_token_is_text(sp, ids[k]))+ continue;+ char tb[256];+ int32_t tl = 0;+ janas_llm_speech_token(sp, ids[k], tb, sizeof tb, &tl);+ if (tl > (int32_t)sizeof tb - 1)+ tl = (int32_t)sizeof tb - 1;+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, arr);+ yyjson_mut_obj_add_strncpy(d, o, "token", tb, (size_t)tl);+ yyjson_mut_obj_add_real(d, o, "logprob", lp[k]);+ yyjson_mut_val *by = yyjson_mut_obj_add_arr(d, o, "bytes");+ for (int32_t j = 0; j < tl; j++)+ yyjson_mut_arr_add_int(d, by, (unsigned char)tb[j]);+ }+}++/* ---- the stream: a thread transcribes, the connection reads its events+ (transcript.text.delta, a segment at a time; transcript.text.done) ---- */++struct sstream {+ struct srv *srv;+ uint8_t *data;+ size_t n;+ char *prompt, lang[64];+ struct janas_llm_transcribe_params p;+ int logprobs, diarize;+ /* the known speakers: names and recordings, the stream's own */+ char *names[8];+ unsigned char *refs[8];+ const char *name_p[8];+ const void *ref_p[8];+ int64_t ref_n[8];+ pthread_t th;+ int started;+ pthread_mutex_t mu;+ pthread_cond_t cv;+ char *buf;+ size_t len, cap, off;+ int finished;+ volatile int32_t stop;+};++static void ss_event(struct sstream *x, yyjson_mut_doc *d)+{+ size_t n;+ char *json = yyjson_mut_write(d, 0, &n);+ yyjson_mut_doc_free(d);+ if (!json)+ return;+ pthread_mutex_lock(&x->mu);+ size_t need = x->len + n + 8;+ if (need > x->cap) {+ size_t nc = need * 2;+ char *b = realloc(x->buf, nc);+ if (b) {+ x->buf = b;+ x->cap = nc;+ }+ }+ if (need <= x->cap) {+ memcpy(x->buf + x->len, "data: ", 6);+ memcpy(x->buf + x->len + 6, json, n);+ memcpy(x->buf + x->len + 6 + n, "\n\n", 2);+ x->len += n + 8;+ }+ pthread_cond_signal(&x->cv);+ pthread_mutex_unlock(&x->mu);+ free(json);+}++static void ss_segment(void *user, int32_t i)+{+ struct sstream *x = user;+ const janas_llm_speech *sp = x->srv->speech;+ double t0, t1;+ char *t = seg_text(sp, i, &t0, &t1);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_doc_set_root(d, o);+ yyjson_mut_obj_add_str(d, o, "type", "transcript.text.delta");+ yyjson_mut_obj_add_strcpy(d, o, "delta", t ? t : "");+ if (x->logprobs)+ add_logprobs(d, yyjson_mut_obj_add_arr(d, o, "logprobs"), sp, i);+ free(t);+ ss_event(x, d);+}++static void *ss_run(void *arg)+{+ struct sstream *x = arg;+ janas_llm_speech *sp = x->srv->speech;+ pthread_mutex_lock(&x->srv->speech_mu);+ int32_t rc = janas_llm_speech_transcribe(sp, x->data, (int64_t)x->n, &x->p);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_doc_set_root(d, o);+ if (rc != JANAS_LLM_OK) {+ yyjson_mut_obj_add_str(d, o, "type", "error");+ yyjson_mut_val *e = yyjson_mut_obj_add_obj(d, o, "error");+ yyjson_mut_obj_add_str(d, e, "type",+ rc == JANAS_LLM_EINVAL ? "invalid_request_error"+ : "server_error");+ yyjson_mut_obj_add_strcpy(d, e, "message", janas_llm_last_error());+ } else {+ if (x->diarize) { /* the turns, once the whole recording is known */+ yyjson_mut_doc *dd = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *arr = yyjson_mut_arr(dd);+ yyjson_mut_doc_set_root(dd, arr);+ add_turns(dd, arr, sp);+ size_t na = yyjson_mut_arr_size(arr);+ for (size_t i = 0; i < na; i++) {+ yyjson_mut_doc *ed = yyjson_mut_doc_new(NULL);+ yyjson_mut_doc_set_root(+ ed,+ yyjson_mut_val_mut_copy(ed, yyjson_mut_arr_get(arr, i)));+ ss_event(x, ed);+ }+ yyjson_mut_doc_free(dd);+ }+ char *text = whole_text(sp);+ double dur = janas_llm_speech_duration(sp);+ yyjson_mut_obj_add_str(d, o, "type", "transcript.text.done");+ yyjson_mut_obj_add_strcpy(d, o, "text", text ? text : "");+ free(text);+ if (x->logprobs) {+ yyjson_mut_val *a = yyjson_mut_obj_add_arr(d, o, "logprobs");+ for (int32_t i = 0; i < janas_llm_speech_segments(sp); i++)+ add_logprobs(d, a, sp, i);+ }+ yyjson_mut_val *u = yyjson_mut_obj_add_obj(d, o, "usage");+ yyjson_mut_obj_add_str(d, u, "type", "duration");+ yyjson_mut_obj_add_int(d, u, "seconds", (int64_t)ceil(dur));+ }+ pthread_mutex_unlock(&x->srv->speech_mu);+ ss_event(x, d);+ pthread_mutex_lock(&x->mu);+ x->finished = 1;+ pthread_cond_signal(&x->cv);+ pthread_mutex_unlock(&x->mu);+ return NULL;+}++static long ss_next(void *ctx, char *buf, size_t cap)+{+ struct sstream *x = ctx;+ pthread_mutex_lock(&x->mu);+ if (x->off == x->len && !x->finished) {+ struct timespec ts;+ clock_gettime(CLOCK_REALTIME, &ts);+ ts.tv_nsec += 200000000;+ if (ts.tv_nsec >= 1000000000) {+ ts.tv_sec++;+ ts.tv_nsec -= 1000000000;+ }+ pthread_cond_timedwait(&x->cv, &x->mu, &ts);+ }+ long n = 0;+ if (x->off < x->len) {+ size_t k = x->len - x->off < cap ? x->len - x->off : cap;+ memcpy(buf, x->buf + x->off, k);+ x->off += k;+ n = (long)k;+ } else if (x->finished) {+ n = -1;+ }+ pthread_mutex_unlock(&x->mu);+ return n;+}++static void ss_done(void *ctx)+{+ struct sstream *x = ctx;+ x->stop = 1; /* a client gone: the transcription stops at its next+ window */+ if (x->started)+ pthread_join(x->th, NULL);+ pthread_mutex_destroy(&x->mu);+ pthread_cond_destroy(&x->cv);+ free(x->buf);+ free(x->data);+ free(x->prompt);+ for (int i = 0; i < 8; i++) {+ free(x->names[i]);+ free(x->refs[i]);+ }+ free(x);+}++static int speech(struct srv_req *r, int translate)+{+ if (!r->srv->speech)+ return srv_reply_error(r, 400, "invalid_request_error",+ "model_not_found",+ "This server has no speech model: start it "+ "with --speech-model FILE (a Whisper model "+ "made by hf2jns_whisper).");+ if (!r->conn)+ return bad(r, "not in a batch");+ struct srv_part parts[32];+ char err[200];+ const char *ct =+ MHD_lookup_connection_value(r->conn, MHD_HEADER_KIND, "Content-Type");+ int n =+ srv_multipart_parse(ct, r->body, r->n_body, parts, 32, err, sizeof err);+ if (n < 0)+ return bad(r, err);+ const struct srv_part *file = srv_multipart_get(parts, n, "file");+ if (!file || !file->n)+ return bad(r, "'file' is required: the recording.");+ char model[256], lang[64] = "", fmt[32] = "json", temp[32] = "";+ char stream[16] = "", *prompt = NULL;+ if (field(parts, n, "model", model, sizeof model) != 1)+ return bad(r, "'model' is required.");+ if ((!translate && field(parts, n, "language", lang, sizeof lang) < 0) ||+ field(parts, n, "response_format", fmt, sizeof fmt) < 0 ||+ field(parts, n, "temperature", temp, sizeof temp) < 0 ||+ field(parts, n, "stream", stream, sizeof stream) < 0)+ return bad(r, "a field too long, or a file where text belongs.");+ int verbose = !strcmp(fmt, "verbose_json");+ int diarized = !strcmp(fmt, "diarized_json");+ if (!verbose && !diarized && strcmp(fmt, "json") && strcmp(fmt, "text") &&+ strcmp(fmt, "srt") && strcmp(fmt, "vtt"))+ return bad(r, "'response_format' is json, text, srt, verbose_json, "+ "vtt or diarized_json.");+ int streaming = stream[0] && strcmp(stream, "false");+ int logprobs = 0;+ for (int i = 0; i < n; i++)+ if (!strcmp(parts[i].name, "include[]") ||+ !strcmp(parts[i].name, "include")) {+ if (parts[i].n == 8 && !memcmp(parts[i].data, "logprobs", 8))+ logprobs = 1;+ else+ return bad(r, "'include' takes logprobs.");+ }+ if (logprobs && strcmp(fmt, "json") && !verbose)+ return bad(r, "'include[]=logprobs' needs 'response_format' json.");+ if (streaming && strcmp(fmt, "json") && strcmp(fmt, "text") && !diarized)+ return bad(r, "'stream' gives text: 'response_format' json, text or "+ "diarized_json.");+ int gran = 0;+ for (int i = 0; i < n; i++)+ if (!strcmp(parts[i].name, "timestamp_granularities[]") ||+ !strcmp(parts[i].name, "timestamp_granularities")) {+ if (parts[i].n == 7 && !memcmp(parts[i].data, "segment", 7))+ gran |= 1;+ else if (parts[i].n == 4 && !memcmp(parts[i].data, "word", 4))+ gran |= 2;+ else+ return bad(r, "'timestamp_granularities' takes word and "+ "segment.");+ }+ /* chunking_strategy: "auto", or {"type": "server_vad", threshold,+ prefix_padding_ms, silence_duration_ms} */+ int chunking = 0, vad_prefix = -1, vad_silence = 0;+ float vad_threshold = 0;+ const struct srv_part *cs =+ srv_multipart_get(parts, n, "chunking_strategy");+ if (cs) {+ if (cs->has_filename)+ return bad(r, "'chunking_strategy' is text.");+ if (cs->n == 4 && !memcmp(cs->data, "auto", 4)) {+ chunking = 1;+ } else {+ yyjson_doc *jd = yyjson_read((const char *)cs->data, cs->n, 0);+ yyjson_val *jo = jd ? yyjson_doc_get_root(jd) : NULL;+ yyjson_val *ty = yyjson_obj_get(jo, "type");+ if (!jo || !yyjson_is_obj(jo) || !yyjson_is_str(ty) ||+ strcmp(yyjson_get_str(ty), "server_vad")) {+ yyjson_doc_free(jd);+ return bad(r, "'chunking_strategy' is \"auto\" or an object "+ "of type server_vad.");+ }+ chunking = 2;+ yyjson_val *t = yyjson_obj_get(jo, "threshold");+ if (yyjson_is_num(t))+ vad_threshold = (float)yyjson_get_num(t);+ t = yyjson_obj_get(jo, "prefix_padding_ms");+ if (yyjson_is_num(t))+ vad_prefix = (int)yyjson_get_num(t);+ t = yyjson_obj_get(jo, "silence_duration_ms");+ if (yyjson_is_num(t))+ vad_silence = (int)yyjson_get_num(t);+ yyjson_doc_free(jd);+ if (vad_threshold < 0 || vad_threshold > 1 || vad_prefix > 60000 ||+ vad_silence < 0 || vad_silence > 60000)+ return bad(r, "'chunking_strategy': a threshold between 0 and "+ "1, durations in milliseconds.");+ }+ }+ if (gran && !verbose)+ return bad(r, "'timestamp_granularities' needs 'response_format' "+ "verbose_json.");+ if (!gran)+ gran = 1;+ float temperature = 0;+ if (temp[0]) {+ char *e;+ temperature = strtof(temp, &e);+ if (*e || !(temperature >= 0 && temperature <= 1))+ return bad(r, "'temperature' is between 0 and 1.");+ }+ const struct srv_part *pp = srv_multipart_get(parts, n, "prompt");+ if (pp) {+ if (pp->has_filename || !(prompt = malloc(pp->n + 1)))+ return bad(r, "'prompt' is text.");+ memcpy(prompt, pp->data, pp->n);+ prompt[pp->n] = 0;+ }+ /* the known speakers, for diarized_json: names and data: URLs */+ char *knames[8] = {0};+ unsigned char *krefs[8] = {0};+ size_t kn[8] = {0};+ int nkn = 0, nkr = 0;+ for (int i = 0; i < n; i++) {+ int is_name = !strcmp(parts[i].name, "known_speaker_names[]") ||+ !strcmp(parts[i].name, "known_speaker_names");+ int is_ref = !strcmp(parts[i].name, "known_speaker_references[]") ||+ !strcmp(parts[i].name, "known_speaker_references");+ if (!is_name && !is_ref)+ continue;+ if (!diarized || (is_name ? nkn : nkr) == 8) {+ for (int k = 0; k < 8; k++) {+ free(knames[k]);+ free(krefs[k]);+ }+ free(prompt);+ return bad(r, diarized ? "at most 8 known speakers."+ : "known speakers need 'response_format' "+ "diarized_json.");+ }+ if (is_name) {+ if ((knames[nkn] = malloc(parts[i].n + 1))) {+ memcpy(knames[nkn], parts[i].data, parts[i].n);+ knames[nkn][parts[i].n] = 0;+ }+ nkn++;+ } else {+ char why[160];+ if (srv_data_url((const char *)parts[i].data, parts[i].n, 64u << 20,+ &krefs[nkr], &kn[nkr], why, sizeof why) != 0) {+ for (int k = 0; k < 8; k++) {+ free(knames[k]);+ free(krefs[k]);+ }+ free(prompt);+ return bad(r, why);+ }+ nkr++;+ }+ }+ if (nkn != nkr) {+ for (int k = 0; k < 8; k++) {+ free(knames[k]);+ free(krefs[k]);+ }+ free(prompt);+ return bad(r, "'known_speaker_names[]' and "+ "'known_speaker_references[]' come in pairs.");+ }+ struct janas_llm_transcribe_params p;+ janas_llm_transcribe_params_default(&p);+ p.language = lang[0] ? lang : NULL;+ p.translate = translate;+ p.prompt = prompt;+ p.temperature = temperature;+ p.words = (gran & 2) != 0;+ p.chunking = chunking;+ p.vad_threshold = vad_threshold;+ p.vad_prefix_ms = vad_prefix;+ p.vad_silence_ms = vad_silence;+ const char *name_p[8];+ const void *ref_p[8];+ int64_t ref_n[8];+ for (int k = 0; k < nkn; k++) {+ name_p[k] = knames[k] ? knames[k] : "";+ ref_p[k] = krefs[k];+ ref_n[k] = (int64_t)kn[k];+ }+ p.diarize = diarized;+ p.n_speakers = nkn;+ p.speaker_names = name_p;+ p.speaker_refs = ref_p;+ p.speaker_ref_bytes = ref_n;+ if (streaming) {+ struct sstream *x = calloc(1, sizeof *x);+ if (!x || !(x->data = malloc(file->n))) {+ free(x);+ free(prompt);+ return srv_reply_error(r, 500, "server_error", NULL,+ "out of memory");+ }+ memcpy(x->data, file->data, file->n);+ x->n = file->n;+ x->srv = r->srv;+ x->prompt = prompt;+ snprintf(x->lang, sizeof x->lang, "%s", lang);+ x->p = p;+ x->p.language = x->lang[0] ? x->lang : NULL;+ x->p.prompt = prompt;+ x->p.on_segment = ss_segment;+ x->p.user = x;+ x->p.stop = &x->stop;+ x->logprobs = logprobs;+ x->diarize = diarized;+ for (int k = 0; k < nkn; k++) { /* the stream's own */+ x->names[k] = knames[k];+ x->refs[k] = krefs[k];+ x->name_p[k] = knames[k] ? knames[k] : "";+ x->ref_p[k] = krefs[k];+ x->ref_n[k] = (int64_t)kn[k];+ knames[k] = NULL;+ krefs[k] = NULL;+ }+ x->p.speaker_names = x->name_p;+ x->p.speaker_refs = x->ref_p;+ x->p.speaker_ref_bytes = x->ref_n;+ pthread_mutex_init(&x->mu, NULL);+ pthread_cond_init(&x->cv, NULL);+ if (pthread_create(&x->th, NULL, ss_run, x) != 0) {+ ss_done(x);+ return srv_reply_error(r, 500, "server_error", NULL,+ "cannot start the transcription");+ }+ x->started = 1;+ return srv_reply_stream(r, ss_next, ss_done, x);+ }+ pthread_mutex_lock(&r->srv->speech_mu);+ int32_t rc = janas_llm_speech_transcribe(r->srv->speech, file->data,+ (int64_t)file->n, &p);+ int ret;+ if (rc != JANAS_LLM_OK) {+ ret = srv_reply_error(r, rc == JANAS_LLM_EINVAL ? 400 : 500,+ rc == JANAS_LLM_EINVAL ? "invalid_request_error"+ : "server_error",+ rc == JANAS_LLM_EINVAL ? "invalid_value" : NULL,+ "%s", janas_llm_last_error());+ } else if (!strcmp(fmt, "text")) {+ char *t = whole_text(r->srv->speech);+ size_t len = t ? strlen(t) : 0;+ if (t) {+ t[len++] = '\n'; /* over its NUL, as OpenAI ends it */+ }+ ret = srv_reply_bytes(r, 200, t, len, "text/plain; charset=utf-8");+ } else if (!strcmp(fmt, "srt") || !strcmp(fmt, "vtt")) {+ ret = reply_cues(r, r->srv->speech, fmt[0] == 'v');+ } else if (diarized) {+ ret = reply_diarized(r, r->srv->speech);+ } else {+ ret = reply_json(r, r->srv->speech, verbose, translate, gran, logprobs);+ }+ pthread_mutex_unlock(&r->srv->speech_mu);+ free(prompt);+ for (int k = 0; k < 8; k++) {+ free(knames[k]);+ free(krefs[k]);+ }+ return ret;+}++int srv_audio_transcriptions(struct srv_req *r)+{+ return speech(r, 0);+}++int srv_audio_translations(struct srv_req *r)+{+ return speech(r, 1);+}diff --git a/src/server/batches.c b/src/server/batches.cnew file mode 100644index 0000000..d0c0da1--- /dev/null+++ b/src/server/batches.c@@ -0,0 +1,886 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * batches.c - OpenAI's Batch API: a JSONL file of requests to one endpoint+ * (/v1/chat/completions, /v1/responses, /v1/completions, /v1/embeddings,+ * /v1/moderations), run by the server one at a time on a thread of its own,+ * each behind every request a client is waiting for (srv_engine_low), and+ * answered in two files: the answers, and the requests that failed.+ *+ * A line runs through the same handler as the HTTP request it stands for,+ * its answer kept instead of sent (struct srv_capture). A batch is a+ * document of the store, saved after every line with how far it got and+ * how long its two files are, so a server stopped halfway goes on from+ * there when it starts again; the files themselves wait beside the+ * uploaded ones (.batches) until the batch ends and they become Files.+ */+#include "server.h"++#include <errno.h>+#include <fcntl.h>+#include <pthread.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include <time.h>+#include <unistd.h>+#include <yyjson.h>++#include "files.h"+#include "files_store.h"++#define KIND "batch"+#define MAX_LINES 50000+#define BATCH_INPUT_MAX ((uint64_t)200 << 20)+#define WINDOW 86400 /* "24h" */+#define MAX_ERRORS 100++enum {+ VALIDATING,+ FAILED,+ IN_PROGRESS,+ FINALIZING,+ COMPLETED,+ EXPIRED,+ CANCELLING,+ CANCELLED+};+static const char *const STATUS[] = {"validating", "failed", "in_progress",+ "finalizing", "completed", "expired",+ "cancelling", "cancelled"};++static const char *const ENDPOINTS[] = {"/v1/chat/completions", "/v1/responses",+ "/v1/completions", "/v1/embeddings",+ "/v1/moderations"};++struct batch {+ char id[48], endpoint[32], input[48], output[48], error[48];+ int status;+ int64_t created, in_progress, expires, finalizing, completed, failed,+ expired, cancelling, cancelled;+ long total, done_ok, done_bad, next;+ long long out_bytes, err_bytes; /* of the files being written */+ long long in_tokens, out_tokens;+ int64_t output_after; /* the outputs' expiry, seconds; 0: 30 days */+ char *metadata; /* JSON text, or NULL */+ char *errors; /* JSON array text of the validation's, or NULL */+};++static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;+static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;+static pthread_t worker;+static int running, stopping;+static struct srv *server;+static char work_dir[4200];++/* ---- the document ---- */++static void add_time(yyjson_mut_doc *d, yyjson_mut_val *o, const char *k,+ int64_t t)+{+ if (t)+ yyjson_mut_obj_add_int(d, o, k, t);+ else+ yyjson_mut_obj_add_null(d, o, k);+}++/* The Batch object as OpenAI writes it (aux: what only the server needs). */+static char *batch_json(const struct batch *b, char **aux)+{+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_doc_set_root(d, o);+ yyjson_mut_obj_add_strcpy(d, o, "id", b->id);+ yyjson_mut_obj_add_str(d, o, "object", "batch");+ yyjson_mut_obj_add_strcpy(d, o, "endpoint", b->endpoint);+ yyjson_mut_obj_add_strcpy(d, o, "model", server->cfg.model_id);+ yyjson_doc *ed =+ b->errors ? yyjson_read(b->errors, strlen(b->errors), 0) : NULL;+ if (ed) {+ yyjson_mut_val *e = yyjson_mut_obj(d);+ yyjson_mut_obj_add_str(d, e, "object", "list");+ yyjson_mut_obj_add_val(d, e, "data",+ yyjson_val_mut_copy(d, yyjson_doc_get_root(ed)));+ yyjson_mut_obj_add_val(d, o, "errors", e);+ } else {+ yyjson_mut_obj_add_null(d, o, "errors");+ }+ yyjson_mut_obj_add_strcpy(d, o, "input_file_id", b->input);+ yyjson_mut_obj_add_str(d, o, "completion_window", "24h");+ yyjson_mut_obj_add_str(d, o, "status", STATUS[b->status]);+ if (b->output[0])+ yyjson_mut_obj_add_strcpy(d, o, "output_file_id", b->output);+ else+ yyjson_mut_obj_add_null(d, o, "output_file_id");+ if (b->error[0])+ yyjson_mut_obj_add_strcpy(d, o, "error_file_id", b->error);+ else+ yyjson_mut_obj_add_null(d, o, "error_file_id");+ yyjson_mut_obj_add_int(d, o, "created_at", b->created);+ add_time(d, o, "in_progress_at", b->in_progress);+ add_time(d, o, "expires_at", b->expires);+ add_time(d, o, "finalizing_at", b->finalizing);+ add_time(d, o, "completed_at", b->completed);+ add_time(d, o, "failed_at", b->failed);+ add_time(d, o, "expired_at", b->expired);+ add_time(d, o, "cancelling_at", b->cancelling);+ add_time(d, o, "cancelled_at", b->cancelled);+ yyjson_mut_val *rc = yyjson_mut_obj(d);+ yyjson_mut_obj_add_int(d, rc, "total", b->total);+ yyjson_mut_obj_add_int(d, rc, "completed", b->done_ok);+ yyjson_mut_obj_add_int(d, rc, "failed", b->done_bad);+ yyjson_mut_obj_add_val(d, o, "request_counts", rc);+ yyjson_mut_val *u = yyjson_mut_obj(d);+ yyjson_mut_obj_add_int(d, u, "input_tokens", b->in_tokens);+ yyjson_mut_obj_add_int(d, u, "output_tokens", b->out_tokens);+ yyjson_mut_obj_add_int(d, u, "total_tokens", b->in_tokens + b->out_tokens);+ yyjson_mut_obj_add_val(d, o, "usage", u);+ yyjson_doc *md =+ b->metadata ? yyjson_read(b->metadata, strlen(b->metadata), 0) : NULL;+ if (md)+ yyjson_mut_obj_add_val(d, o, "metadata",+ yyjson_val_mut_copy(d, yyjson_doc_get_root(md)));+ else+ yyjson_mut_obj_add_null(d, o, "metadata");+ char *out = yyjson_mut_write(d, 0, NULL);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(ed);+ yyjson_doc_free(md);+ if (aux) {+ char tmp[512];+ snprintf(tmp, sizeof tmp,+ "{\"next\":%ld,\"out_bytes\":%lld,\"err_bytes\":%lld,"+ "\"output_after\":%lld}",+ b->next, b->out_bytes, b->err_bytes,+ (long long)b->output_after);+ *aux = strdup(tmp);+ }+ return out;+}++static int64_t num(yyjson_val *o, const char *k)+{+ return yyjson_get_sint(yyjson_obj_get(o, k));+}++static void str(char *dst, size_t cap, yyjson_val *o, const char *k)+{+ const char *s = yyjson_get_str(yyjson_obj_get(o, k));+ snprintf(dst, cap, "%s", s ? s : "");+}++/* A batch read back from the store: 0, or -1. */+static int batch_read(const char *json, const char *aux, struct batch *b)+{+ memset(b, 0, sizeof(*b));+ yyjson_doc *d = yyjson_read(json, strlen(json), 0);+ yyjson_val *o = yyjson_doc_get_root(d);+ if (!yyjson_is_obj(o)) {+ yyjson_doc_free(d);+ return -1;+ }+ str(b->id, sizeof b->id, o, "id");+ str(b->endpoint, sizeof b->endpoint, o, "endpoint");+ str(b->input, sizeof b->input, o, "input_file_id");+ str(b->output, sizeof b->output, o, "output_file_id");+ str(b->error, sizeof b->error, o, "error_file_id");+ const char *st = yyjson_get_str(yyjson_obj_get(o, "status"));+ for (int i = 0; st && i < 8; i++)+ if (!strcmp(st, STATUS[i]))+ b->status = i;+ b->created = num(o, "created_at");+ b->in_progress = num(o, "in_progress_at");+ b->expires = num(o, "expires_at");+ b->finalizing = num(o, "finalizing_at");+ b->completed = num(o, "completed_at");+ b->failed = num(o, "failed_at");+ b->expired = num(o, "expired_at");+ b->cancelling = num(o, "cancelling_at");+ b->cancelled = num(o, "cancelled_at");+ yyjson_val *rc = yyjson_obj_get(o, "request_counts");+ b->total = (long)num(rc, "total");+ b->done_ok = (long)num(rc, "completed");+ b->done_bad = (long)num(rc, "failed");+ yyjson_val *u = yyjson_obj_get(o, "usage");+ b->in_tokens = num(u, "input_tokens");+ b->out_tokens = num(u, "output_tokens");+ yyjson_val *md = yyjson_obj_get(o, "metadata");+ if (yyjson_is_obj(md))+ b->metadata = yyjson_val_write(md, 0, NULL);+ yyjson_val *er = yyjson_obj_get(yyjson_obj_get(o, "errors"), "data");+ if (yyjson_is_arr(er))+ b->errors = yyjson_val_write(er, 0, NULL);+ yyjson_doc_free(d);+ yyjson_doc *a = aux ? yyjson_read(aux, strlen(aux), 0) : NULL;+ yyjson_val *ar = yyjson_doc_get_root(a);+ b->next = (long)num(ar, "next");+ b->out_bytes = num(ar, "out_bytes");+ b->err_bytes = num(ar, "err_bytes");+ b->output_after = num(ar, "output_after");+ yyjson_doc_free(a);+ return 0;+}++static void batch_free(struct batch *b)+{+ free(b->metadata);+ free(b->errors);+ b->metadata = b->errors = NULL;+}++static int save(const struct batch *b, int fresh)+{+ char *aux = NULL, *json = batch_json(b, &aux);+ int rc = -1;+ if (json && aux)+ rc = fresh ? srv_store_put(server->store, KIND, b->id, b->created, json,+ aux)+ : srv_store_update(server->store, KIND, b->id, json, aux);+ free(json);+ free(aux);+ return rc;+}++static int load(const char *id, struct batch *b)+{+ char *json = NULL, *aux = NULL;+ int rc = srv_store_get(server->store, KIND, id, &json, &aux) == 0 && json+ ? batch_read(json, aux, b)+ : -1;+ free(json);+ free(aux);+ return rc;+}++/* ---- running a batch ---- */++static void work_path(char *out, size_t cap, const char *id, const char *suf)+{+ snprintf(out, cap, "%s/%s%s", work_dir, id, suf);+}++/* The input file whole, its lines' starts into *starts: how many, or -1+ with the reason in why. */+static long read_input(const struct batch *b, char **text, long **starts,+ char *why, size_t wl)+{+ struct srv_file f;+ *text = NULL;+ *starts = NULL;+ if (srv_files_get(server->files, b->input, &f) != 0) {+ snprintf(why, wl, "the input file %s is not there", b->input);+ return -1;+ }+ if (f.bytes > BATCH_INPUT_MAX) {+ snprintf(why, wl, "the input file is larger than 200 MB");+ return -1;+ }+ int fd = srv_files_open_content(server->files, b->input);+ char *t = fd >= 0 ? malloc(f.bytes + 1) : NULL;+ size_t got = 0;+ while (t && got < f.bytes) {+ ssize_t r = read(fd, t + got, f.bytes - got);+ if (r <= 0)+ break;+ got += (size_t)r;+ }+ if (fd >= 0)+ close(fd);+ if (!t || got != f.bytes) {+ free(t);+ snprintf(why, wl, "the input file cannot be read");+ return -1;+ }+ t[got] = 0;+ long n = 0, cap = 0;+ long *s = NULL;+ for (size_t i = 0; i < got;) {+ size_t e = i;+ while (e < got && t[e] != '\n')+ e++;+ size_t k = i;+ while (k < e && (t[k] == ' ' || t[k] == '\t' || t[k] == '\r'))+ k++;+ if (k < e) { /* not a blank line */+ if (n == cap) {+ cap = cap ? 2 * cap : 1024;+ long *x = realloc(s, (size_t)cap * sizeof(long));+ if (!x) {+ free(s);+ free(t);+ snprintf(why, wl, "out of memory");+ return -1;+ }+ s = x;+ }+ s[n++] = (long)i;+ }+ t[e < got ? e : got] = 0; /* lines as strings */+ i = e + 1;+ }+ *text = t;+ *starts = s;+ return n;+}++static void add_error(yyjson_mut_doc *d, yyjson_mut_val *arr, const char *code,+ const char *msg, long line, int *count)+{+ if (++*count > MAX_ERRORS)+ return;+ yyjson_mut_val *e = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, e, "code", code);+ yyjson_mut_obj_add_strcpy(d, e, "message", msg);+ yyjson_mut_obj_add_null(d, e, "param");+ yyjson_mut_obj_add_int(d, e, "line", line);+ yyjson_mut_arr_append(arr, e);+}++/* Every line a request to the batch's endpoint with its own custom_id: the+ errors as a JSON array text (NULL: none). */+static char *validate(const struct batch *b, char *text, const long *starts,+ long n)+{+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *arr = yyjson_mut_arr(d);+ int count = 0;+ char **ids = calloc((size_t)(n ? n : 1), sizeof(char *));+ if (n > MAX_LINES)+ add_error(d, arr, "too_many_requests",+ "A batch holds at most 50,000 requests.", 0, &count);+ for (long i = 0; ids && i < n && count <= MAX_ERRORS; i++) {+ const char *line = text + starts[i];+ yyjson_doc *ld = yyjson_read(line, strlen(line), 0);+ yyjson_val *o = yyjson_doc_get_root(ld);+ const char *cid = yyjson_get_str(yyjson_obj_get(o, "custom_id"));+ const char *method = yyjson_get_str(yyjson_obj_get(o, "method"));+ const char *url = yyjson_get_str(yyjson_obj_get(o, "url"));+ if (!yyjson_is_obj(o))+ add_error(d, arr, "invalid_json_line", "Not a JSON object.", i + 1,+ &count);+ else if (!cid || !*cid)+ add_error(d, arr, "missing_required_parameter",+ "Each request needs a custom_id.", i + 1, &count);+ else if (!method || strcmp(method, "POST") != 0)+ add_error(d, arr, "invalid_request",+ "The method of a request is POST.", i + 1, &count);+ else if (!url || strcmp(url, b->endpoint) != 0)+ add_error(d, arr, "mismatched_endpoint",+ "The url of a request is the batch's endpoint.", i + 1,+ &count);+ else if (!yyjson_is_obj(yyjson_obj_get(o, "body")))+ add_error(d, arr, "invalid_request",+ "The body of a request is a JSON object.", i + 1, &count);+ else {+ for (long k = 0; k < i; k++)+ if (ids[k] && !strcmp(ids[k], cid)) {+ add_error(d, arr, "duplicate_custom_id",+ "The custom_id is used twice.", i + 1, &count);+ break;+ }+ ids[i] = strdup(cid);+ }+ yyjson_doc_free(ld);+ }+ for (long i = 0; ids && i < n; i++)+ free(ids[i]);+ free(ids);+ char *out = count ? yyjson_mut_val_write(arr, 0, NULL) : NULL;+ yyjson_mut_doc_free(d);+ return out;+}++/* bytes appended to a work file: 0, or -1 */+static int append(const char *path, const char *s, size_t n)+{+ int fd = open(path, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0600);+ if (fd < 0)+ return -1;+ size_t w = 0;+ while (w < n) {+ ssize_t k = write(fd, s + w, n - w);+ if (k <= 0)+ break;+ w += (size_t)k;+ }+ return close(fd) == 0 && w == n ? 0 : -1;+}++/* One line run: its answer into the output or the error file. */+static void run_line(struct batch *b, const char *line)+{+ yyjson_doc *ld = yyjson_read(line, strlen(line), 0);+ yyjson_val *o = yyjson_doc_get_root(ld);+ yyjson_val *body = yyjson_obj_get(o, "body");+ const char *cid = yyjson_get_str(yyjson_obj_get(o, "custom_id"));+ struct srv_capture cap = {0};+ yyjson_val *stream = yyjson_obj_get(body, "stream");+ if (yyjson_is_true(stream)) {+ cap.status = 400;+ cap.body = strdup("{\"error\":{\"message\":\"A batch's requests are "+ "not streamed.\",\"type\":"+ "\"invalid_request_error\",\"param\":\"stream\","+ "\"code\":null}}");+ } else {+ size_t bn = 0;+ char *bt = yyjson_val_write(body, 0, &bn);+ srv_handler h = srv_route_find("POST", b->endpoint + 3);+ struct srv_req r = {.srv = server,+ .cap = &cap,+ .method = "POST",+ .path = b->endpoint + 3,+ .body = bt ? bt : "{}",+ .n_body = bt ? bn : 2};+ if (h)+ h(&r);+ free(bt);+ if (!cap.body) {+ cap.status = 500;+ cap.body = strdup("{\"error\":{\"message\":\"no answer\","+ "\"type\":\"server_error\",\"param\":null,"+ "\"code\":null}}");+ }+ }+ /* the answer's line */+ yyjson_doc *ad =+ cap.body ? yyjson_read(cap.body, strlen(cap.body), 0) : NULL;+ yyjson_val *ar = yyjson_doc_get_root(ad);+ yyjson_val *usage = yyjson_obj_get(ar, "usage");+ if (usage) {+ int64_t in = num(usage, "prompt_tokens") + num(usage, "input_tokens");+ int64_t out =+ num(usage, "completion_tokens") + num(usage, "output_tokens");+ b->in_tokens += in;+ b->out_tokens += out;+ }+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *x = yyjson_mut_obj(d);+ char rid[48], qid[48];+ srv_new_id(rid, sizeof rid, "batch_req_");+ srv_new_id(qid, sizeof qid, "req_");+ yyjson_mut_obj_add_strcpy(d, x, "id", rid);+ yyjson_mut_obj_add_strcpy(d, x, "custom_id", cid ? cid : "");+ yyjson_mut_val *resp = yyjson_mut_obj(d);+ yyjson_mut_obj_add_uint(d, resp, "status_code", cap.status);+ yyjson_mut_obj_add_strcpy(d, resp, "request_id", qid);+ if (ar)+ yyjson_mut_obj_add_val(d, resp, "body", yyjson_val_mut_copy(d, ar));+ else+ yyjson_mut_obj_add_null(d, resp, "body");+ yyjson_mut_obj_add_val(d, x, "response", resp);+ yyjson_mut_obj_add_null(d, x, "error");+ size_t n = 0;+ char *text = yyjson_mut_val_write(x, 0, &n);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(ad);+ yyjson_doc_free(ld);+ free(cap.body);+ int ok = cap.status == 200;+ char path[4300];+ work_path(path, sizeof path, b->id, ok ? ".out" : ".err");+ if (text && append(path, text, n) == 0 && append(path, "\n", 1) == 0) {+ if (ok) {+ b->out_bytes += (long long)n + 1;+ b->done_ok++;+ } else {+ b->err_bytes += (long long)n + 1;+ b->done_bad++;+ }+ } else {+ b->done_bad++; /* not written: counted failed */+ }+ free(text);+}++/* A work file cut back to what the last save counted (a line half written+ when the server stopped), or removed when nothing was. */+static void trim_work(const struct batch *b, const char *suf, long long n)+{+ char path[4300];+ work_path(path, sizeof path, b->id, suf);+ if (n == 0)+ unlink(path);+ else if (truncate(path, (off_t)n) != 0 && errno != ENOENT)+ fprintf(stderr, "janas-server: batch %s: %s cannot be cut back\n",+ b->id, path);+}++/* The work files as Files of the batch. */+static void finalize(struct batch *b, int status)+{+ int64_t now = (int64_t)time(NULL);+ b->status = FINALIZING;+ b->finalizing = now;+ save(b, 0);+ int64_t after = b->output_after ? b->output_after : 2592000;+ const char *suf[2] = {".out", ".err"};+ long long bytes[2] = {b->out_bytes, b->err_bytes};+ char *ids[2] = {b->output, b->error};+ for (int k = 0; k < 2; k++) {+ char path[4300], name[96], err[200];+ work_path(path, sizeof path, b->id, suf[k]);+ if (bytes[k] > 0) {+ snprintf(name, sizeof name, "%s_%s.jsonl", b->id,+ k ? "error" : "output");+ const char *paths[1] = {path};+ struct srv_file f;+ if (srv_files_add_parts(server->files, name, "batch_output", paths,+ 1, (uint64_t)bytes[k], NULL, after, &f, err,+ sizeof err) == 0)+ snprintf(ids[k], 48, "%s", f.id);+ else+ fprintf(stderr, "janas-server: batch %s: %s\n", b->id, err);+ }+ unlink(path);+ }+ b->status = status;+ now = (int64_t)time(NULL);+ if (status == COMPLETED)+ b->completed = now;+ else if (status == CANCELLED)+ b->cancelled = now;+ else+ b->expired = now;+ save(b, 0);+}++static int status_of(const char *id)+{+ struct batch b;+ int s = load(id, &b) == 0 ? b.status : -1;+ batch_free(&b);+ return s;+}++static void run_batch(const char *id)+{+ struct batch b;+ if (load(id, &b) != 0)+ return;+ char *text = NULL, why[200];+ long *starts = NULL, n = 0;+ if (b.status == VALIDATING || b.status == IN_PROGRESS ||+ b.status == CANCELLING) {+ n = read_input(&b, &text, &starts, why, sizeof why);+ if (n < 0 && b.status != CANCELLING) {+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *arr = yyjson_mut_arr(d);+ int c = 0;+ add_error(d, arr, "invalid_input_file", why, 0, &c);+ free(b.errors);+ b.errors = yyjson_mut_val_write(arr, 0, NULL);+ yyjson_mut_doc_free(d);+ b.status = FAILED;+ b.failed = (int64_t)time(NULL);+ save(&b, 0);+ batch_free(&b);+ return;+ }+ }+ if (b.status == VALIDATING) {+ char *errs = n > 0 ? validate(&b, text, starts, n) : NULL;+ if (n == 0)+ errs = strdup("[{\"code\":\"empty_file\",\"message\":\"The input "+ "file holds no request.\",\"param\":null,"+ "\"line\":null}]");+ if (errs) {+ free(b.errors);+ b.errors = errs;+ b.status = FAILED;+ b.failed = (int64_t)time(NULL);+ save(&b, 0);+ free(text);+ free(starts);+ batch_free(&b);+ return;+ }+ b.status = IN_PROGRESS;+ b.in_progress = (int64_t)time(NULL);+ b.total = n;+ b.next = 0;+ save(&b, 0);+ }+ /* a run stopped halfway: the files back to what was counted */+ trim_work(&b, ".out", b.out_bytes);+ trim_work(&b, ".err", b.err_bytes);+ int end = COMPLETED, stop = 0;+ srv_engine_low = 1;+ while (b.status == IN_PROGRESS && b.next < n) {+ /* a cancel is saved under mu, and so is every line's progress:+ neither overwrites the other */+ pthread_mutex_lock(&mu);+ stop = stopping;+ int s = status_of(b.id);+ pthread_mutex_unlock(&mu);+ if (stop)+ break;+ if (s == CANCELLING) {+ b.status = CANCELLING;+ break;+ }+ if ((int64_t)time(NULL) >= b.expires) {+ end = EXPIRED;+ break;+ }+ run_line(&b, text + starts[b.next]);+ b.next++;+ pthread_mutex_lock(&mu);+ if (status_of(b.id) == CANCELLING)+ b.status = CANCELLING;+ save(&b, 0);+ pthread_mutex_unlock(&mu);+ }+ if (b.status == CANCELLING)+ end = CANCELLED;+ if (!stop && (b.status == IN_PROGRESS || b.status == CANCELLING ||+ b.status == FINALIZING))+ finalize(&b, end);+ free(text);+ free(starts);+ batch_free(&b);+}++/* The oldest batch with work left, into id: 1, or 0. */+static int next_batch(char *id, size_t cap)+{+ char **ids;+ size_t n = srv_store_list(server->store, KIND, &ids);+ int found = 0;+ for (size_t i = 0; i < n && !found; i++) {+ int s = status_of(ids[i]);+ if (s == VALIDATING || s == IN_PROGRESS || s == CANCELLING ||+ s == FINALIZING) {+ snprintf(id, cap, "%s", ids[i]);+ found = 1;+ }+ }+ srv_store_list_free(ids, n);+ return found;+}++static void *worker_main(void *arg)+{+ (void)arg;+ char id[64];+ for (;;) {+ pthread_mutex_lock(&mu);+ while (!stopping && !next_batch(id, sizeof id))+ pthread_cond_wait(&cv, &mu);+ int stop = stopping;+ pthread_mutex_unlock(&mu);+ if (stop)+ break;+ run_batch(id);+ }+ return NULL;+}++int srv_batches_start(struct srv *s)+{+ server = s;+ snprintf(work_dir, sizeof work_dir, "%s/.batches", srv_files_dir(s->files));+ if (mkdir(work_dir, 0700) != 0 && errno != EEXIST)+ return -1;+ stopping = 0;+ if (pthread_create(&worker, NULL, worker_main, NULL) != 0)+ return -1;+ running = 1;+ return 0;+}++void srv_batches_stop(void)+{+ if (!running)+ return;+ pthread_mutex_lock(&mu);+ stopping = 1;+ pthread_cond_broadcast(&cv);+ pthread_mutex_unlock(&mu);+ pthread_join(worker, NULL);+ running = 0;+}++/* ---- the API ---- */++static int reply_batch(struct srv_req *r, const struct batch *b)+{+ char *json = batch_json(b, NULL);+ return srv_reply_json(r, 200, json, json ? strlen(json) : 0);+}++static int bad(struct srv_req *r, const char *msg)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_value",+ "%s", msg);+}++static int no_batch(struct srv_req *r)+{+ return srv_reply_error(r, 404, "invalid_request_error", "not_found",+ "No batch found with id '%s'.", r->param[0]);+}++int srv_batches_create(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ const char *input = yyjson_get_str(yyjson_obj_get(root, "input_file_id"));+ const char *ep = yyjson_get_str(yyjson_obj_get(root, "endpoint"));+ const char *win = yyjson_get_str(yyjson_obj_get(root, "completion_window"));+ yyjson_val *md = yyjson_obj_get(root, "metadata");+ yyjson_val *oe = yyjson_obj_get(root, "output_expires_after");+ int ep_ok = 0;+ for (size_t i = 0; ep && i < sizeof(ENDPOINTS) / sizeof(ENDPOINTS[0]); i++)+ ep_ok |= !strcmp(ep, ENDPOINTS[i]);+ struct srv_file f;+ const char *why = NULL;+ int64_t out_after = 0;+ if (!yyjson_is_obj(root))+ why = "The body is a JSON object: input_file_id, endpoint, "+ "completion_window.";+ else if (!input || srv_files_get(r->srv->files, input, &f) != 0)+ why = "'input_file_id' names no file uploaded here.";+ else if (strcmp(f.purpose, "batch") != 0)+ why = "The input file must be uploaded with purpose batch.";+ else if (!ep_ok)+ why = "'endpoint' is /v1/chat/completions, /v1/responses, "+ "/v1/completions, /v1/embeddings or /v1/moderations here.";+ else if (!win || strcmp(win, "24h") != 0)+ why = "'completion_window' is 24h.";+ else if (md && !yyjson_is_null(md) && !yyjson_is_obj(md))+ why = "'metadata' is an object.";+ else if (oe) {+ yyjson_val *se = yyjson_obj_get(oe, "seconds");+ if (!yyjson_equals_str(yyjson_obj_get(oe, "anchor"), "created_at") ||+ !yyjson_is_int(se) || yyjson_get_sint(se) < 3600 ||+ yyjson_get_sint(se) > 2592000)+ why = "'output_expires_after' is anchor created_at and seconds "+ "from 3600 to 2592000.";+ else+ out_after = yyjson_get_sint(se);+ }+ if (why) {+ yyjson_doc_free(doc);+ return bad(r, why);+ }+ struct batch b = {0};+ srv_new_id(b.id, sizeof b.id, "batch_");+ snprintf(b.endpoint, sizeof b.endpoint, "%s", ep);+ snprintf(b.input, sizeof b.input, "%s", input);+ b.status = VALIDATING;+ b.created = (int64_t)time(NULL);+ b.expires = b.created + WINDOW;+ b.output_after = out_after;+ if (yyjson_is_obj(md))+ b.metadata = yyjson_val_write(md, 0, NULL);+ yyjson_doc_free(doc);+ int rc = save(&b, 1);+ if (rc == 0) {+ pthread_mutex_lock(&mu);+ pthread_cond_broadcast(&cv);+ pthread_mutex_unlock(&mu);+ }+ int ret = rc == 0 ? reply_batch(r, &b)+ : srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the batch");+ batch_free(&b);+ return ret;+}++int srv_batches_get(struct srv_req *r)+{+ struct batch b;+ if (load(r->param[0], &b) != 0)+ return no_batch(r);+ int ret = reply_batch(r, &b);+ batch_free(&b);+ return ret;+}++int srv_batches_cancel(struct srv_req *r)+{+ struct batch b;+ if (load(r->param[0], &b) != 0)+ return no_batch(r);+ if (b.status == VALIDATING || b.status == IN_PROGRESS) {+ pthread_mutex_lock(&mu);+ batch_free(&b);+ if (load(r->param[0], &b) == 0 &&+ (b.status == VALIDATING || b.status == IN_PROGRESS)) {+ b.status = CANCELLING;+ b.cancelling = (int64_t)time(NULL);+ save(&b, 0);+ }+ pthread_cond_broadcast(&cv);+ pthread_mutex_unlock(&mu);+ } else if (b.status != CANCELLING) {+ batch_free(&b);+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_state",+ "The batch %s is %s: it cannot be cancelled.",+ r->param[0], STATUS[b.status]);+ }+ int ret = reply_batch(r, &b);+ batch_free(&b);+ return ret;+}++int srv_batches_list(struct srv_req *r)+{+ const char *l = srv_query(r, "limit"), *after = srv_query(r, "after");+ long limit = 20;+ if (l) {+ char *end;+ limit = strtol(l, &end, 10);+ if (*end || limit < 1 || limit > 100)+ return bad(r, "'limit' is a number from 1 to 100.");+ }+ char **ids;+ size_t n = srv_store_list(r->srv->store, KIND, &ids);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *root = yyjson_mut_obj(d), *data = yyjson_mut_arr(d);+ long shown = 0;+ int past = after == NULL, more = 0;+ char first[64] = "", last[64] = "";+ for (size_t k = n; k-- > 0;) { /* newest first */+ if (!past) {+ past = !strcmp(ids[k], after);+ continue;+ }+ if (shown == limit) {+ more = 1;+ break;+ }+ struct batch b;+ if (load(ids[k], &b) != 0)+ continue;+ char *json = batch_json(&b, NULL);+ yyjson_doc *bd = json ? yyjson_read(json, strlen(json), 0) : NULL;+ if (bd)+ yyjson_mut_arr_append(+ data, yyjson_val_mut_copy(d, yyjson_doc_get_root(bd)));+ yyjson_doc_free(bd);+ free(json);+ if (!shown)+ snprintf(first, sizeof first, "%s", b.id);+ snprintf(last, sizeof last, "%s", b.id);+ shown++;+ batch_free(&b);+ }+ srv_store_list_free(ids, n);+ yyjson_mut_obj_add_str(d, root, "object", "list");+ yyjson_mut_obj_add_val(d, root, "data", data);+ if (shown) {+ yyjson_mut_obj_add_strcpy(d, root, "first_id", first);+ yyjson_mut_obj_add_strcpy(d, root, "last_id", last);+ } else {+ yyjson_mut_obj_add_null(d, root, "first_id");+ yyjson_mut_obj_add_null(d, root, "last_id");+ }+ yyjson_mut_obj_add_bool(d, root, "has_more", more);+ yyjson_mut_doc_set_root(d, root);+ size_t len;+ char *out = yyjson_mut_write(d, 0, &len);+ yyjson_mut_doc_free(d);+ return srv_reply_json(r, 200, out, len);+}diff --git a/src/server/chat.c b/src/server/chat.cindex 229c9d4..b7f70cc 100644--- a/src/server/chat.c+++ b/src/server/chat.c@@ -67,6 +67,15 @@ static int create(struct srv_req *r, int chat)srv_job_release(j);return ret;}+ char ierr[300];+ if (srv_images_fetch(r->srv, j, ierr, sizeof ierr) != 0) {+ ret = srv_reply_error(r, 400, "invalid_request_error", "invalid_image",+ "%s", ierr);+ srv_parsed_free(&pr);+ free(o);+ srv_job_release(j);+ return ret;+ }o->j = j;o->srv = r->srv;o->chat = chat;diff --git a/src/server/chat_stream.c b/src/server/chat_stream.cindex 8e63415..69eaf01 100644--- a/src/server/chat_stream.c+++ b/src/server/chat_stream.c@@ -172,11 +172,21 @@ static int produce(struct out *o)if (!j->done)return bad ? -1 : 0;if (j->err != JANAS_LLM_OK || j->ch[0].finish == JANAS_LLM_FINISH_ERROR) {- char msg[400];- snprintf(msg, sizeof(msg),- "data: {\"error\":{\"message\":\"generation failed\","- "\"type\":\"server_error\",\"param\":null,\"code\":null}}"- "\n\n");+ /* the engine's reason, as the answer without a stream gives it */+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *root = yyjson_mut_obj(d);+ yyjson_mut_val *e = yyjson_mut_obj_add_obj(d, root, "error");+ yyjson_mut_obj_add_strcpy(+ d, e, "message", j->errmsg[0] ? j->errmsg : "generation failed");+ yyjson_mut_obj_add_str(d, e, "type", "server_error");+ yyjson_mut_obj_add_null(d, e, "param");+ yyjson_mut_obj_add_null(d, e, "code");+ char *t = yyjson_mut_val_write(root, 0, NULL);+ yyjson_mut_doc_free(d);+ char msg[600];+ snprintf(msg, sizeof(msg), "data: %s\n\n",+ t ? t : "{\"error\":{\"message\":\"generation failed\"}}");+ free(t);bad |= raw_event(o, msg);} else if (o->include_usage) {yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);diff --git a/src/server/containers.c b/src/server/containers.cnew file mode 100644index 0000000..076ce17--- /dev/null+++ b/src/server/containers.c@@ -0,0 +1,1486 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * containers.c - OpenAI's Containers API and the Python sessions of+ * code_interpreter (containers.h). A container is a directory beside the+ * files (.containers/<id>): meta.json, files.json (the index of its files:+ * ids, paths, sizes, who wrote them) and data/, which the code sees as+ * /mnt/data. Its session is one python3 in the cage of+ * src/common/sandbox.c, started at the first piece of code and kept: a+ * small driver reads pieces of code (their length, then their bytes),+ * runs them in one namespace as Jupyter does (the value of the last+ * expression shown), saves the figures matplotlib drew into /mnt/data+ * (when the code saved no image itself) and answers with what was+ * printed, the traceback and the figures. A piece+ * that runs past two minutes ends the session (the variables are lost).+ * A container unused for its minutes (20 by default) expires: its session+ * ends and its files go.+ *+ * The code in the cage may leave symbolic links, pipes or anything in+ * /mnt/data: the server never follows a link there - the index takes+ * regular files only, and every file is opened one directory at a time+ * with O_NOFOLLOW under the container's own.+ */+#include "containers.h"++#ifdef _WIN32+/* No cage on Windows yet (src/common/sandbox.c): code is not run, and the+ API says so. */+#include <stdlib.h>+#include <string.h>++static const char OFF[] = "This server cannot run code on Windows yet.";++void srv_cell_free(struct srv_cell *c)+{+ free(c->logs);+ free(c->error);+ free(c->file_id);+ free(c->path);+ memset(c, 0, sizeof(*c));+}++const char *srv_containers_off(void)+{+ return OFF;+}++int srv_container_for(struct srv *s, yyjson_val *spec, char *id, size_t cap,+ const char **why)+{+ (void)s, (void)spec, (void)id, (void)cap;+ *why = OFF;+ return -1;+}++int srv_container_run(struct srv *s, const char *id, const char *code,+ struct srv_cell *out)+{+ (void)s, (void)id, (void)code;+ memset(out, 0, sizeof(*out));+ return -1;+}++int srv_container_add_file(struct srv *s, const char *cid, const char *fid,+ const char **why)+{+ (void)s, (void)cid, (void)fid;+ *why = OFF;+ return -1;+}++int srv_container_file_open(const char *cid, const char *cfid, char *name,+ size_t cap)+{+ (void)cid, (void)cfid, (void)name, (void)cap;+ return -1;+}++int srv_container_file_name(struct srv *s, const char *cid, const char *fid,+ char *name, size_t cap)+{+ (void)s, (void)cid, (void)fid, (void)name, (void)cap;+ return -1;+}++int srv_containers_start(struct srv *s)+{+ return s->cfg.code_interpreter ? -1 : 0;+}++void srv_containers_stop(void)+{+}++static int refused(struct srv_req *r)+{+ return srv_reply_error(r, 400, "invalid_request_error", "unsupported_value",+ "%s", OFF);+}++int srv_containers_create(struct srv_req *r)+{+ return refused(r);+}+int srv_containers_list(struct srv_req *r)+{+ return refused(r);+}+int srv_containers_get(struct srv_req *r)+{+ return refused(r);+}+int srv_containers_delete(struct srv_req *r)+{+ return refused(r);+}+int srv_container_files_create(struct srv_req *r)+{+ return refused(r);+}+int srv_container_files_list(struct srv_req *r)+{+ return refused(r);+}+int srv_container_files_get(struct srv_req *r)+{+ return refused(r);+}+int srv_container_files_delete(struct srv_req *r)+{+ return refused(r);+}+int srv_container_files_content(struct srv_req *r)+{+ return refused(r);+}++#else++#include <dirent.h>+#include <errno.h>+#include <fcntl.h>+#include <ftw.h>+#include <microhttpd.h>+#include <poll.h>+#include <pthread.h>+#include <signal.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include <time.h>+#include <unistd.h>++#include "common/sandbox.h"+#include "files_store.h"+#include "llm/json.h"+#include "multipart.h"+#include "store_disk.h"++#define CELL_SECONDS 120+#define DEFAULT_MINUTES 20+#define MAX_FILES 2000++static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;+static pthread_cond_t reaper_cv = PTHREAD_COND_INITIALIZER;+static pthread_t reaper;+static int running, stopping;+static struct srv *server;+static char work[4200];+static char off_why[300] = "Start the server with --code-interpreter to "+ "run code.";+static int on;++/* the Python driver: pieces of code in, results out (see above) */+static const char DRIVER[] =+ "import sys, os, io, ast, json, traceback, contextlib\n"+ "_o = os.fdopen(os.dup(1), 'wb', 0)\n"+ "os.dup2(2, 1)\n"+ "_i = os.fdopen(0, 'rb', 0)\n"+ "_g = {'__name__': '__main__', '__builtins__': __builtins__}\n"+ "os.makedirs('/tmp/mpl', exist_ok=True)\n"+ "def _rd(n):\n"+ " b = b''\n"+ " while len(b) < n:\n"+ " c = _i.read(n - len(b))\n"+ " if not c:\n"+ " os._exit(0)\n"+ " b += c\n"+ " return b\n"+ "def _len():\n"+ " b = b''\n"+ " while not b.endswith(bytes([10])):\n"+ " b += _rd(1)\n"+ " return int(b)\n"+ "def _pngs():\n"+ " try:\n"+ " return {n: os.stat('/mnt/data/' + n).st_mtime_ns for n in\n"+ " os.listdir('/mnt/data') if n.endswith('.png')}\n"+ " except OSError:\n"+ " return {}\n"+ "def _figs(before):\n"+ " m = sys.modules.get('matplotlib.pyplot')\n"+ " out = []\n"+ " if m is None:\n"+ " return out\n"+ " if _pngs() != before:\n"+ " m.close('all')\n"+ " return out\n"+ " for num in m.get_fignums():\n"+ " k = 1\n"+ " while os.path.exists('/mnt/data/figure_%d.png' % k):\n"+ " k += 1\n"+ " p = '/mnt/data/figure_%d.png' % k\n"+ " try:\n"+ " m.figure(num).savefig(p, format='png', dpi=100)\n"+ " out.append(p)\n"+ " except Exception:\n"+ " pass\n"+ " m.close('all')\n"+ " return out\n"+ "while True:\n"+ " code = _rd(_len()).decode('utf-8', 'replace')\n"+ " before = _pngs()\n"+ " buf = io.StringIO()\n"+ " err = None\n"+ " with contextlib.redirect_stdout(buf), "+ "contextlib.redirect_stderr(buf):\n"+ " try:\n"+ " t = ast.parse(code, '<cell>', 'exec')\n"+ " last = None\n"+ " if t.body and isinstance(t.body[-1], ast.Expr):\n"+ " last = ast.Expression(t.body.pop().value)\n"+ " exec(compile(t, '<cell>', 'exec'), _g)\n"+ " if last is not None:\n"+ " v = eval(compile(last, '<cell>', 'eval'), _g)\n"+ " if v is not None:\n"+ " print(repr(v))\n"+ " except SystemExit:\n"+ " pass\n"+ " except BaseException:\n"+ " e = sys.exc_info()\n"+ " tb = e[2].tb_next if e[2] is not None else None\n"+ " err = ''.join(traceback.format_exception(e[0], e[1], tb))\n"+ " try:\n"+ " figs = _figs(before)\n"+ " except Exception:\n"+ " figs = []\n"+ " r = json.dumps({'logs': buf.getvalue()[-200000:], 'error': err,\n"+ " 'images': figs}).encode()\n"+ " _o.write(str(len(r)).encode() + bytes([10]) + r)\n";++/* ---- paths, kept safe from what the code leaves ---- */++static void path_of(char *out, size_t cap, const char *cid, const char *what)+{+ snprintf(out, cap, "%s/%s/%s", work, cid, what);+}++static const char *or_empty(const char *s)+{+ return s ? s : "";+}++static int id_ok(const char *id, const char *prefix)+{+ return id && !strncmp(id, prefix, strlen(prefix)) && srv_disk_name_ok(id);+}++/* The directory holding a file under the container's data/, by the+ file's relative path, opened one directory at a time without following+ links: its fd with the file's name in *leaf, or -1. */+static int parent_under(const char *cid, const char *rel, const char **leaf)+{+ char base[4600];+ path_of(base, sizeof base, cid, "data");+ int dir = open(base, O_DIRECTORY | O_RDONLY | O_CLOEXEC | O_NOFOLLOW);+ if (dir < 0)+ return -1;+ char part[300];+ const char *p = rel;+ for (;;) {+ const char *slash = strchr(p, '/');+ size_t n = slash ? (size_t)(slash - p) : strlen(p);+ if (!n || n >= sizeof part || (n == 1 && p[0] == '.') ||+ (n == 2 && p[0] == '.' && p[1] == '.')) {+ close(dir);+ return -1;+ }+ if (!slash) {+ *leaf = p;+ return dir;+ }+ memcpy(part, p, n);+ part[n] = 0;+ int next =+ openat(dir, part, O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_CLOEXEC);+ close(dir);+ if (next < 0)+ return -1;+ dir = next;+ p = slash + 1;+ }+}++/* That file opened (O_NOFOLLOW): the fd, or -1. */+static int open_under(const char *cid, const char *rel, int flags)+{+ const char *leaf;+ int dir = parent_under(cid, rel, &leaf);+ if (dir < 0)+ return -1;+ int fd = openat(dir, leaf, flags | O_NOFOLLOW | O_CLOEXEC, 0644);+ close(dir);+ return fd;+}++static int rm_entry(const char *p, const struct stat *st, int flag,+ struct FTW *f)+{+ (void)st;+ (void)f;+ return flag == FTW_DP ? rmdir(p) : unlink(p);+}++/* a directory and all under it, links removed and never followed */+static void rm_tree(const char *p)+{+ nftw(p, rm_entry, 16, FTW_DEPTH | FTW_PHYS);+}++/* ---- documents ---- */++static yyjson_doc *read_json(const char *path)+{+ FILE *f = fopen(path, "rb");+ if (!f)+ return NULL;+ char *t = NULL;+ long n = fseek(f, 0, SEEK_END) ? -1 : ftell(f);+ if (n >= 0 && !fseek(f, 0, SEEK_SET) && (t = malloc((size_t)n + 1)) &&+ fread(t, 1, (size_t)n, f) != (size_t)n) {+ free(t);+ t = NULL;+ }+ fclose(f);+ yyjson_doc *d = t ? yyjson_read(t, (size_t)n, 0) : NULL;+ free(t);+ return d;+}++static int write_json(const char *path, yyjson_mut_val *v)+{+ size_t n;+ char *t = yyjson_mut_val_write(v, 0, &n);+ if (!t)+ return -1;+ char tmp[4400];+ snprintf(tmp, sizeof tmp, "%s.tmp", path);+ FILE *f = fopen(tmp, "wb");+ int ok = f && fwrite(t, 1, n, f) == n;+ if (f && fclose(f) != 0)+ ok = 0;+ free(t);+ if (!ok || rename(tmp, path) != 0) {+ unlink(tmp);+ return -1;+ }+ return 0;+}++static int64_t memory_of(const char *m)+{+ if (!m || !strcmp(m, "1g"))+ return (int64_t)1 << 30;+ if (!strcmp(m, "4g"))+ return (int64_t)4 << 30;+ if (!strcmp(m, "16g"))+ return (int64_t)16 << 30;+ if (!strcmp(m, "64g"))+ return (int64_t)64 << 30;+ return -1;+}++/* the container's meta, running (mu held): its document, or NULL */+static yyjson_doc *meta_of(const char *cid)+{+ if (!id_ok(cid, "cntr_"))+ return NULL;+ char p[4600];+ path_of(p, sizeof p, cid, "meta.json");+ return read_json(p);+}++static int is_running(yyjson_doc *m)+{+ return yyjson_equals_str(yyjson_obj_get(yyjson_doc_get_root(m), "status"),+ "running");+}++/* last_active_at now (mu held) */+static void touch(const char *cid)+{+ yyjson_doc *m = meta_of(cid);+ if (!m)+ return;+ yyjson_mut_doc *d = yyjson_doc_mut_copy(m, NULL);+ yyjson_mut_val *o = yyjson_mut_doc_get_root(d);+ yyjson_mut_obj_remove_key(o, "last_active_at");+ yyjson_mut_obj_add_int(d, o, "last_active_at", (int64_t)time(NULL));+ char p[4600];+ path_of(p, sizeof p, cid, "meta.json");+ write_json(p, o);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(m);+}++/* ---- the index of a container's files ---- */++struct walk {+ char rel[MAX_FILES][300];+ int64_t bytes[MAX_FILES], mtime[MAX_FILES];+ int n;+};++static void walk_dir(int dir, const char *prefix, struct walk *w, int depth)+{+ DIR *d = fdopendir(dir);+ if (!d) {+ close(dir);+ return;+ }+ struct dirent *e;+ while ((e = readdir(d)) && w->n < MAX_FILES) {+ if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))+ continue;+ struct stat st;+ if (fstatat(dirfd(d), e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0)+ continue;+ char rel[300];+ if ((size_t)snprintf(rel, sizeof rel, "%s%s", prefix, e->d_name) >=+ sizeof rel)+ continue;+ if (S_ISREG(st.st_mode)) {+ snprintf(w->rel[w->n], sizeof w->rel[0], "%s", rel);+ w->bytes[w->n] = st.st_size;+ w->mtime[w->n] =+ (int64_t)st.st_mtim.tv_sec * 1000000000 + st.st_mtim.tv_nsec;+ w->n++;+ } else if (S_ISDIR(st.st_mode) && depth < 6) {+ int sub = openat(dirfd(d), e->d_name,+ O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_CLOEXEC);+ if (sub >= 0) {+ char pre[300];+ if ((size_t)snprintf(pre, sizeof pre, "%s/", rel) >=+ sizeof pre) {+ close(sub);+ continue;+ }+ walk_dir(sub, pre, w, depth + 1);+ }+ }+ }+ closedir(d);+}++/* The index brought up to what is in data/ (mu held): new files get ids+ (source as given), gone ones go. Those new or changed since the index+ was last brought up are listed in changed (ids), images first. 0, or+ -1. */+static int sync_index(const char *cid, const char *source,+ yyjson_mut_doc *out_d, yyjson_mut_val *changed)+{+ char p[4600];+ path_of(p, sizeof p, cid, "data");+ int dir = open(p, O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_CLOEXEC);+ if (dir < 0)+ return -1;+ struct walk *w = calloc(1, sizeof(*w));+ if (!w) {+ close(dir);+ return -1;+ }+ walk_dir(dir, "", w, 0);+ path_of(p, sizeof p, cid, "files.json");+ yyjson_doc *old = read_json(p);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *arr = yyjson_mut_arr(d);+ int64_t now = (int64_t)time(NULL);+ for (int i = 0; i < w->n; i++) {+ yyjson_val *prev = NULL, *v;+ size_t k, m;+ yyjson_arr_foreach(yyjson_doc_get_root(old), k, m, v)+ {+ if (yyjson_equals_str(yyjson_obj_get(v, "path"), w->rel[i]))+ prev = v;+ }+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, arr);+ char id[48];+ const char *pid = yyjson_get_str(yyjson_obj_get(prev, "id"));+ if (!pid) {+ srv_new_id(id, sizeof id, "cfile_");+ pid = id;+ }+ yyjson_mut_obj_add_strcpy(d, o, "id", pid);+ yyjson_mut_obj_add_strcpy(d, o, "path", w->rel[i]);+ yyjson_mut_obj_add_int(d, o, "bytes", w->bytes[i]);+ yyjson_mut_obj_add_int(d, o, "mtime", w->mtime[i]);+ yyjson_val *ca = yyjson_obj_get(prev, "created_at");+ yyjson_mut_obj_add_int(d, o, "created_at",+ ca ? yyjson_get_sint(ca) : now);+ const char *src = yyjson_get_str(yyjson_obj_get(prev, "source"));+ yyjson_mut_obj_add_strcpy(d, o, "source", src ? src : source);+ int changed_now =+ !prev ||+ yyjson_get_sint(yyjson_obj_get(prev, "mtime")) != w->mtime[i] ||+ yyjson_get_sint(yyjson_obj_get(prev, "bytes")) != w->bytes[i];+ if (changed && changed_now) {+ size_t l = strlen(w->rel[i]);+ int image = l > 4 && !strcmp(w->rel[i] + l - 4, ".png");+ yyjson_mut_val *c = yyjson_mut_obj(out_d);+ yyjson_mut_obj_add_strcpy(out_d, c, "id", pid);+ yyjson_mut_obj_add_strcpy(out_d, c, "path", w->rel[i]);+ yyjson_mut_obj_add_bool(out_d, c, "image", image);+ if (image)+ yyjson_mut_arr_prepend(changed, c);+ else+ yyjson_mut_arr_append(changed, c);+ }+ }+ int rc = write_json(p, arr);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(old);+ free(w);+ return rc;+}++/* ---- the sessions ---- */++struct kernel {+ char cid[64];+ struct janas_sandbox sb;+ pthread_mutex_t run; /* one piece of code at a time */+ int refs; /* the list's, and each piece of code's (mu) */+ struct kernel *next;+};++static struct kernel *kernels;++static void kernel_unref(struct kernel *k)+{+ if (--k->refs > 0)+ return;+ janas_sandbox_kill(&k->sb);+ pthread_mutex_destroy(&k->run);+ free(k);+}++/* the session ended (mu held): out of the list, its process killed (a+ piece of code running sees its end), freed by the last user */+static void kernel_drop(struct kernel *k)+{+ for (struct kernel **p = &kernels; *p; p = &(*p)->next)+ if (*p == k) {+ *p = k->next;+ if (k->sb.pid > 0)+ kill(k->sb.pid, SIGKILL);+ kernel_unref(k);+ return;+ }+}++/* The container's session, started when it has none (mu held): NULL with+ the reason. */+static struct kernel *kernel_of(const char *cid, int64_t mem, char *why,+ size_t n)+{+ for (struct kernel *k = kernels; k; k = k->next)+ if (!strcmp(k->cid, cid))+ return k;+ struct kernel *k = calloc(1, sizeof(*k));+ if (!k) {+ snprintf(why, n, "out of memory");+ return NULL;+ }+ snprintf(k->cid, sizeof k->cid, "%s", cid);+ pthread_mutex_init(&k->run, NULL);+ char data[4400];+ path_of(data, sizeof data, cid, "data");+ struct janas_sandbox_cfg c = {+ .data_dir = data,+ .mem_bytes = (uint64_t)mem + ((uint64_t)1 << 30), /* + the runtime */+ .file_bytes = (uint64_t)512 << 20,+ .tmp_bytes = (uint64_t)256 << 20};+ const char *py =+ server->cfg.code_python ? server->cfg.code_python : "/usr/bin/python3";+ char *argv[] = {(char *)py, "-I", "-c", (char *)DRIVER, NULL};+ char *envp[] = {"PATH=/usr/local/bin:/usr/bin:/bin",+ "HOME=/tmp",+ "LANG=C.UTF-8",+ "MPLBACKEND=Agg",+ "MPLCONFIGDIR=/tmp/mpl",+ "OPENBLAS_NUM_THREADS=4",+ "OMP_NUM_THREADS=4",+ "PYTHONDONTWRITEBYTECODE=1",+ NULL};+ if (janas_sandbox_spawn(&c, argv, envp, &k->sb, why, n) != 0) {+ pthread_mutex_destroy(&k->run);+ free(k);+ return NULL;+ }+ k->refs = 1;+ k->next = kernels;+ kernels = k;+ return k;+}++void srv_cell_free(struct srv_cell *c)+{+ free(c->logs);+ free(c->error);+ free(c->file_id);+ free(c->path);+ memset(c, 0, sizeof(*c));+}++/* the piece of code sent, the answer read (k->run held): the answer's JSON+ text, or NULL with the reason; raw output (C code writing to its fd 1 or+ 2) appended to raw */+static char *exchange(struct kernel *k, const char *code, struct janas_buf *raw,+ char *why, size_t n)+{+ char head[32];+ size_t len = strlen(code);+ int hl = snprintf(head, sizeof head, "%zu\n", len);+ if (write(k->sb.in, head, (size_t)hl) != hl ||+ write(k->sb.in, code, len) != (ssize_t)len) {+ snprintf(why, n, "the Python session is gone");+ return NULL;+ }+ struct janas_buf out = {0};+ time_t end = time(NULL) + CELL_SECONDS;+ long want = -1;+ for (;;) {+ /* the answer: its length, a newline, its bytes */+ if (want < 0) {+ char *nl = out.n ? memchr(out.p, '\n', out.n) : NULL;+ if (nl) {+ want = strtol(out.p, NULL, 10);+ size_t skip = (size_t)(nl - out.p) + 1;+ memmove(out.p, out.p + skip, out.n - skip);+ out.n -= skip;+ }+ }+ if (want >= 0 && out.n >= (size_t)want) {+ janas_buf_put(&out, "", 1);+ if (out.oom)+ break;+ return out.p;+ }+ int left = (int)(end - time(NULL));+ if (left <= 0) {+ snprintf(why, n,+ "The code ran past %d seconds and was stopped; the "+ "Python session started over (its variables are lost).",+ CELL_SECONDS);+ janas_buf_free(&out);+ return NULL;+ }+ struct pollfd p[2] = {{k->sb.out, POLLIN, 0}, {k->sb.err, POLLIN, 0}};+ if (poll(p, 2, left * 1000) <= 0)+ continue;+ for (int i = 0; i < 2; i++) {+ if (!(p[i].revents & (POLLIN | POLLHUP)))+ continue;+ char buf[8192];+ ssize_t got = read(i ? k->sb.err : k->sb.out, buf, sizeof buf);+ if (got <= 0) {+ if (i == 0 || got < 0) {+ snprintf(why, n,+ "The Python session ended (out of memory, or the "+ "code ended it); it starts over at the next "+ "piece of code.");+ janas_buf_free(&out);+ return NULL;+ }+ continue;+ }+ if (i)+ janas_buf_put(raw, buf, (size_t)got);+ else+ janas_buf_put(&out, buf, (size_t)got);+ }+ }+ janas_buf_free(&out);+ snprintf(why, n, "out of memory");+ return NULL;+}++int srv_container_run(struct srv *s, const char *id, const char *code,+ struct srv_cell *out)+{+ (void)s;+ memset(out, 0, sizeof(*out));+ char why[300] = "";+ pthread_mutex_lock(&mu);+ yyjson_doc *m = meta_of(id);+ if (!m || !is_running(m)) {+ yyjson_doc_free(m);+ pthread_mutex_unlock(&mu);+ return -1;+ }+ int64_t mem = memory_of(+ yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(m), "memory_limit")));+ yyjson_doc_free(m);+ touch(id);+ sync_index(id, "user", NULL, NULL); /* what was there before */+ struct kernel *k =+ kernel_of(id, mem > 0 ? mem : (int64_t)1 << 30, why, sizeof why);+ if (k)+ k->refs++;+ pthread_mutex_unlock(&mu);+ if (!k) {+ out->error = strdup(why);+ return 0;+ }+ pthread_mutex_lock(&k->run);+ struct janas_buf raw = {0};+ char *ans = exchange(k, code, &raw, why, sizeof why);+ pthread_mutex_unlock(&k->run);+ pthread_mutex_lock(&mu);+ if (!ans) { /* the session is over */+ kernel_drop(k); /* if still listed */+ out->error = strdup(why);+ } else {+ yyjson_doc *ad = yyjson_read(ans, strlen(ans), 0);+ yyjson_val *a = yyjson_doc_get_root(ad);+ const char *logs = yyjson_get_str(yyjson_obj_get(a, "logs"));+ const char *err = yyjson_get_str(yyjson_obj_get(a, "error"));+ struct janas_buf lb = {0};+ if (raw.n)+ janas_buf_put(&lb, raw.p, raw.n);+ if (logs)+ janas_buf_puts(&lb, logs);+ janas_buf_put(&lb, "", 1);+ out->logs = lb.oom ? NULL : lb.p;+ if (lb.oom)+ janas_buf_free(&lb);+ if (err)+ out->error = strdup(err);+ yyjson_doc_free(ad);+ free(ans);+ }+ janas_buf_free(&raw);+ /* the files it wrote */+ yyjson_mut_doc *cd = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *changed = yyjson_mut_arr(cd);+ sync_index(id, "assistant", cd, changed);+ touch(id);+ size_t nc = yyjson_mut_arr_size(changed);+ if (nc) {+ out->file_id = calloc(nc, sizeof(*out->file_id));+ out->path = calloc(nc, sizeof(*out->path));+ }+ size_t i, n;+ yyjson_mut_val *c;+ yyjson_mut_arr_foreach(changed, i, n, c)+ {+ if (!out->file_id || !out->path)+ break;+ snprintf(out->file_id[out->n_files], sizeof out->file_id[0], "%s",+ or_empty(yyjson_mut_get_str(yyjson_mut_obj_get(c, "id"))));+ snprintf(out->path[out->n_files], sizeof out->path[0], "/mnt/data/%s",+ or_empty(yyjson_mut_get_str(yyjson_mut_obj_get(c, "path"))));+ out->n_files++;+ if (yyjson_mut_get_bool(yyjson_mut_obj_get(c, "image")))+ out->n_images++;+ }+ yyjson_mut_doc_free(cd);+ kernel_unref(k);+ pthread_mutex_unlock(&mu);+ return 0;+}++const char *srv_containers_off(void)+{+ return on ? NULL : off_why;+}++/* ---- containers ---- */++/* A file of the Files API copied into the container's data (mu held):+ NULL, or the reason. */+static const char *copy_file(struct srv *s, const char *cid, const char *fid)+{+ struct srv_file f;+ if (!fid || srv_files_get(s->files, fid, &f) != 0)+ return "A file_id names no file uploaded here.";+ /* its name, never a path */+ char name[256];+ const char *base = strrchr(f.filename, '/');+ snprintf(name, sizeof name, "%s", base ? base + 1 : f.filename);+ if (!name[0] || name[0] == '.')+ snprintf(name, sizeof name, "%s", fid);+ int in = srv_files_open_content(s->files, fid);+ int out =+ in >= 0 ? open_under(cid, name, O_WRONLY | O_CREAT | O_TRUNC) : -1;+ const char *why = NULL;+ if (in < 0 || out < 0)+ why = "The file could not be copied into the container.";+ char buf[65536];+ ssize_t k;+ while (!why && (k = read(in, buf, sizeof buf)) > 0)+ if (write(out, buf, (size_t)k) != k)+ why = "The file could not be copied into the container.";+ if (in >= 0)+ close(in);+ if (out >= 0)+ close(out);+ return why;+}++/* A new container: 0 with its id, -1 with the reason. */+static int make(struct srv *s, const char *name, yyjson_val *file_ids,+ yyjson_val *expires, const char *memory, char *id, size_t cap,+ const char **why)+{+ int64_t minutes = DEFAULT_MINUTES;+ yyjson_val *mn = yyjson_obj_get(expires, "minutes");+ if (expires && !yyjson_is_null(expires)) {+ if (!yyjson_equals_str(yyjson_obj_get(expires, "anchor"),+ "last_active_at") ||+ !yyjson_is_int(mn) || yyjson_get_sint(mn) < 1 ||+ yyjson_get_sint(mn) > 20) {+ *why = "'expires_after' is {anchor: last_active_at, minutes} "+ "with minutes from 1 to 20.";+ return -1;+ }+ minutes = yyjson_get_sint(mn);+ }+ if (memory && memory_of(memory) < 0) {+ *why = "'memory_limit' is 1g, 4g, 16g or 64g.";+ return -1;+ }+ if (file_ids &&+ (!yyjson_is_arr(file_ids) || yyjson_arr_size(file_ids) > 100)) {+ *why = "'file_ids' is a list of at most 100 files.";+ return -1;+ }+ srv_new_id(id, cap, "cntr_");+ char p[4600];+ snprintf(p, sizeof p, "%s/%s", work, id);+ pthread_mutex_lock(&mu);+ *why = NULL;+ if (mkdir(p, 0700) != 0) {+ *why = "The container could not be made.";+ } else {+ path_of(p, sizeof p, id, "data");+ if (mkdir(p, 0700) != 0)+ *why = "The container could not be made.";+ }+ size_t i, n;+ yyjson_val *v;+ yyjson_arr_foreach(file_ids, i, n, v)+ {+ if (!*why)+ *why = copy_file(s, id, yyjson_get_str(v));+ }+ if (!*why) {+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ int64_t now = (int64_t)time(NULL);+ yyjson_mut_obj_add_strcpy(d, o, "id", id);+ yyjson_mut_obj_add_str(d, o, "object", "container");+ yyjson_mut_obj_add_int(d, o, "created_at", now);+ yyjson_mut_obj_add_str(d, o, "status", "running");+ yyjson_mut_val *ea = yyjson_mut_obj_add_obj(d, o, "expires_after");+ yyjson_mut_obj_add_str(d, ea, "anchor", "last_active_at");+ yyjson_mut_obj_add_int(d, ea, "minutes", minutes);+ yyjson_mut_obj_add_int(d, o, "last_active_at", now);+ yyjson_mut_obj_add_strcpy(d, o, "memory_limit", memory ? memory : "1g");+ yyjson_mut_obj_add_strcpy(d, o, "name", name ? name : "");+ path_of(p, sizeof p, id, "meta.json");+ if (write_json(p, o) != 0 || sync_index(id, "user", NULL, NULL) != 0)+ *why = "The container could not be made.";+ yyjson_mut_doc_free(d);+ }+ if (*why) {+ snprintf(p, sizeof p, "%s/%s", work, id);+ rm_tree(p);+ }+ pthread_mutex_unlock(&mu);+ return *why ? -1 : 0;+}++int srv_container_for(struct srv *s, yyjson_val *spec, char *id, size_t cap,+ const char **why)+{+ if (yyjson_is_str(spec) && strcmp(yyjson_get_str(spec), "auto")) {+ pthread_mutex_lock(&mu);+ yyjson_doc *m = meta_of(yyjson_get_str(spec));+ int ok = m && is_running(m);+ yyjson_doc_free(m);+ pthread_mutex_unlock(&mu);+ if (!ok) {+ *why = "The container named is not here, or has expired.";+ return -1;+ }+ snprintf(id, cap, "%s", yyjson_get_str(spec));+ return 0;+ }+ if (spec && !yyjson_is_str(spec) &&+ !yyjson_equals_str(yyjson_obj_get(spec, "type"), "auto")) {+ *why = "A code_interpreter's container is an id, or {type: auto}.";+ return -1;+ }+ return make(s, "code_interpreter", yyjson_obj_get(spec, "file_ids"), NULL,+ yyjson_get_str(yyjson_obj_get(spec, "memory_limit")), id, cap,+ why);+}++int srv_container_add_file(struct srv *s, const char *cid, const char *fid,+ const char **why)+{+ pthread_mutex_lock(&mu);+ yyjson_doc *m = meta_of(cid);+ int ok = m && is_running(m);+ yyjson_doc_free(m);+ *why = ok ? copy_file(s, cid, fid) : "The container is gone.";+ if (!*why)+ sync_index(cid, "user", NULL, NULL);+ pthread_mutex_unlock(&mu);+ return *why ? -1 : 0;+}++int srv_container_file_open(const char *cid, const char *cfid, char *name,+ size_t cap)+{+ char p[4600];+ int fd = -1;+ pthread_mutex_lock(&mu);+ if (id_ok(cid, "cntr_")) {+ path_of(p, sizeof p, cid, "files.json");+ yyjson_doc *x = read_json(p);+ size_t i, n;+ yyjson_val *v;+ yyjson_arr_foreach(yyjson_doc_get_root(x), i, n, v)+ {+ const char *rel = yyjson_get_str(yyjson_obj_get(v, "path"));+ if (fd < 0 && rel &&+ yyjson_equals_str(yyjson_obj_get(v, "id"), cfid)) {+ fd = open_under(cid, rel, O_RDONLY);+ const char *b = strrchr(rel, '/');+ snprintf(name, cap, "%s", b ? b + 1 : rel);+ }+ }+ yyjson_doc_free(x);+ }+ pthread_mutex_unlock(&mu);+ struct stat st;+ if (fd >= 0 && (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode))) {+ close(fd);+ fd = -1;+ }+ return fd;+}++int srv_container_file_name(struct srv *s, const char *cid, const char *fid,+ char *name, size_t cap)+{+ (void)s;+ char p[4600];+ int rc = -1;+ pthread_mutex_lock(&mu);+ if (id_ok(cid, "cntr_")) {+ path_of(p, sizeof p, cid, "files.json");+ yyjson_doc *x = read_json(p);+ size_t i, n;+ yyjson_val *v;+ yyjson_arr_foreach(yyjson_doc_get_root(x), i, n, v)+ {+ if (yyjson_equals_str(yyjson_obj_get(v, "id"), fid)) {+ const char *rel = yyjson_get_str(yyjson_obj_get(v, "path"));+ const char *b = rel ? strrchr(rel, '/') : NULL;+ snprintf(name, cap, "%s", b ? b + 1 : rel ? rel : "");+ rc = 0;+ }+ }+ yyjson_doc_free(x);+ }+ pthread_mutex_unlock(&mu);+ return rc;+}++/* ---- expiry ---- */++static void expire(const char *cid)+{+ char p[4600];+ for (struct kernel *k = kernels; k; k = k->next)+ if (!strcmp(k->cid, cid)) {+ kernel_drop(k); /* a piece of code running ends with it */+ break;+ }+ path_of(p, sizeof p, cid, "data");+ rm_tree(p);+ path_of(p, sizeof p, cid, "files.json");+ unlink(p);+ yyjson_doc *m = meta_of(cid);+ if (m) {+ yyjson_mut_doc *d = yyjson_doc_mut_copy(m, NULL);+ yyjson_mut_val *o = yyjson_mut_doc_get_root(d);+ yyjson_mut_obj_remove_key(o, "status");+ yyjson_mut_obj_add_str(d, o, "status", "expired");+ path_of(p, sizeof p, cid, "meta.json");+ write_json(p, o);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(m);+ }+}++static void *reaper_main(void *arg)+{+ (void)arg;+ pthread_mutex_lock(&mu);+ while (!stopping) {+ struct timespec ts;+ clock_gettime(CLOCK_REALTIME, &ts);+ ts.tv_sec += 30;+ pthread_cond_timedwait(&reaper_cv, &mu, &ts);+ if (stopping)+ break;+ DIR *d = opendir(work);+ struct dirent *e;+ int64_t now = (int64_t)time(NULL);+ while (d && (e = readdir(d))) {+ if (!id_ok(e->d_name, "cntr_"))+ continue;+ yyjson_doc *m = meta_of(e->d_name);+ yyjson_val *o = yyjson_doc_get_root(m);+ int64_t last = yyjson_get_sint(yyjson_obj_get(o, "last_active_at"));+ int64_t mins = yyjson_get_sint(+ yyjson_obj_get(yyjson_obj_get(o, "expires_after"), "minutes"));+ int busy = 0; /* a piece of code running: not idle */+ for (struct kernel *k = kernels; k; k = k->next)+ busy |= !strcmp(k->cid, e->d_name) && k->refs > 1;+ if (m && is_running(m) && !busy && now > last + mins * 60)+ expire(e->d_name);+ yyjson_doc_free(m);+ }+ if (d)+ closedir(d);+ }+ pthread_mutex_unlock(&mu);+ return NULL;+}++int srv_containers_start(struct srv *s)+{+ server = s;+ snprintf(work, sizeof work, "%s/.containers", srv_files_dir(s->files));+ if (mkdir(work, 0700) != 0 && errno != EEXIST)+ return -1;+ if (!s->cfg.code_interpreter)+ return 0;+ char why[256];+ const char *py =+ s->cfg.code_python ? s->cfg.code_python : "/usr/bin/python3";+ if (!janas_sandbox_available(why, sizeof why)) {+ snprintf(off_why, sizeof off_why,+ "This server cannot run code here: %s.", why);+ return -1;+ }+ if (access(py, X_OK) != 0) {+ snprintf(off_why, sizeof off_why,+ "This server cannot run code: there is no %s.", py);+ return -1;+ }+ on = 1;+ stopping = 0;+ if (pthread_create(&reaper, NULL, reaper_main, NULL) == 0)+ running = 1;+ return 0;+}++void srv_containers_stop(void)+{+ pthread_mutex_lock(&mu);+ stopping = 1;+ pthread_cond_broadcast(&reaper_cv);+ pthread_mutex_unlock(&mu);+ if (running)+ pthread_join(reaper, NULL);+ running = 0;+ pthread_mutex_lock(&mu);+ while (kernels)+ kernel_drop(kernels);+ pthread_mutex_unlock(&mu);+}++/* ---- the API ---- */++static int bad(struct srv_req *r, const char *msg)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_value",+ "%s", msg);+}++static int not_found(struct srv_req *r, const char *what, const char *id)+{+ return srv_reply_error(r, 404, "invalid_request_error", "not_found",+ "No %s found with id '%s'.", what, id);+}++static int refused(struct srv_req *r)+{+ return srv_reply_error(r, 400, "invalid_request_error", "unsupported_value",+ "%s", off_why);+}++static int reply_doc_val(struct srv_req *r, yyjson_val *v)+{+ size_t n;+ char *t = yyjson_val_write(v, 0, &n);+ return srv_reply_json(r, 200, t, t ? n : 0);+}++/* A page of objects (oldest first) as OpenAI's lists answer. */+static int page(struct srv_req *r, yyjson_mut_doc *d, yyjson_mut_val *items)+{+ const char *l = srv_query(r, "limit"), *o = srv_query(r, "order");+ const char *after = srv_query(r, "after");+ long limit = l ? strtol(l, NULL, 10) : 20;+ if (limit < 1 || limit > 100)+ return bad(r, "'limit' is a number from 1 to 100.");+ int desc = !o || !strcmp(o, "desc");+ size_t n = yyjson_mut_arr_size(items);+ yyjson_mut_val *root = yyjson_mut_obj(d), *data = yyjson_mut_arr(d);+ long shown = 0;+ int more = 0, past = !after;+ const char *first = NULL, *last = NULL;+ for (size_t k = 0; k < n; k++) {+ yyjson_mut_val *v = yyjson_mut_arr_get(items, desc ? n - 1 - k : k);+ const char *id = yyjson_mut_get_str(yyjson_mut_obj_get(v, "id"));+ if (!past) {+ past = id && !strcmp(id, after);+ continue;+ }+ if (shown == limit) {+ more = 1;+ break;+ }+ yyjson_mut_arr_append(data, yyjson_mut_val_mut_copy(d, v));+ if (!shown)+ first = id;+ last = id;+ shown++;+ }+ yyjson_mut_obj_add_str(d, root, "object", "list");+ yyjson_mut_obj_add_val(d, root, "data", data);+ if (first) {+ yyjson_mut_obj_add_strcpy(d, root, "first_id", first);+ yyjson_mut_obj_add_strcpy(d, root, "last_id", last);+ } else {+ yyjson_mut_obj_add_null(d, root, "first_id");+ yyjson_mut_obj_add_null(d, root, "last_id");+ }+ yyjson_mut_obj_add_bool(d, root, "has_more", more);+ size_t len;+ char *t = yyjson_mut_val_write(root, 0, &len);+ return srv_reply_json(r, 200, t, t ? len : 0);+}++int srv_containers_create(struct srv_req *r)+{+ if (!on)+ return refused(r);+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ const char *name = yyjson_get_str(yyjson_obj_get(root, "name"));+ char id[48];+ const char *why = NULL;+ if (!yyjson_is_obj(root) || !name)+ why = "A container has a name.";+ else+ make(r->srv, name, yyjson_obj_get(root, "file_ids"),+ yyjson_obj_get(root, "expires_after"),+ yyjson_get_str(yyjson_obj_get(root, "memory_limit")), id,+ sizeof id, &why);+ yyjson_doc_free(doc);+ if (why)+ return bad(r, why);+ pthread_mutex_lock(&mu);+ yyjson_doc *m = meta_of(id);+ pthread_mutex_unlock(&mu);+ int ret = reply_doc_val(r, yyjson_doc_get_root(m));+ yyjson_doc_free(m);+ return ret;+}++int srv_containers_list(struct srv_req *r)+{+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *items = yyjson_mut_arr(d);+ pthread_mutex_lock(&mu);+ DIR *dir = opendir(work);+ struct dirent *e;+ while (dir && (e = readdir(dir))) {+ yyjson_doc *m = id_ok(e->d_name, "cntr_") ? meta_of(e->d_name) : NULL;+ if (m) {+ /* oldest first: by created_at */+ yyjson_mut_val *v = yyjson_val_mut_copy(d, yyjson_doc_get_root(m));+ int64_t at =+ yyjson_mut_get_sint(yyjson_mut_obj_get(v, "created_at"));+ size_t k = 0, n = yyjson_mut_arr_size(items);+ while (k < n &&+ yyjson_mut_get_sint(yyjson_mut_obj_get(+ yyjson_mut_arr_get(items, k), "created_at")) <= at)+ k++;+ yyjson_mut_arr_insert(items, v, k);+ }+ yyjson_doc_free(m);+ }+ if (dir)+ closedir(dir);+ pthread_mutex_unlock(&mu);+ int ret = page(r, d, items);+ yyjson_mut_doc_free(d);+ return ret;+}++int srv_containers_get(struct srv_req *r)+{+ pthread_mutex_lock(&mu);+ yyjson_doc *m = meta_of(r->param[0]);+ pthread_mutex_unlock(&mu);+ if (!m)+ return not_found(r, "container", r->param[0]);+ int ret = reply_doc_val(r, yyjson_doc_get_root(m));+ yyjson_doc_free(m);+ return ret;+}++int srv_containers_delete(struct srv_req *r)+{+ pthread_mutex_lock(&mu);+ yyjson_doc *m = meta_of(r->param[0]);+ if (m) {+ expire(r->param[0]);+ char p[4600];+ snprintf(p, sizeof p, "%s/%s", work, r->param[0]);+ rm_tree(p);+ }+ pthread_mutex_unlock(&mu);+ yyjson_doc_free(m);+ if (!m)+ return not_found(r, "container", r->param[0]);+ char out[512];+ snprintf(out, sizeof out,+ "{\"id\":\"%s\",\"object\":\"container.deleted\",\"deleted\":"+ "true}",+ r->param[0]);+ char *t = strdup(out);+ return srv_reply_json(r, 200, t, t ? strlen(t) : 0);+}++/* A container's file as OpenAI writes it. */+static yyjson_mut_val *file_obj(yyjson_mut_doc *d, const char *cid,+ yyjson_val *v)+{+ yyjson_mut_val *o = yyjson_mut_obj(d);+ char path[320];+ snprintf(path, sizeof path, "/mnt/data/%s",+ or_empty(yyjson_get_str(yyjson_obj_get(v, "path"))));+ yyjson_mut_obj_add_strcpy(d, o, "id",+ yyjson_get_str(yyjson_obj_get(v, "id")));+ yyjson_mut_obj_add_str(d, o, "object", "container.file");+ yyjson_mut_obj_add_int(d, o, "created_at",+ yyjson_get_sint(yyjson_obj_get(v, "created_at")));+ yyjson_mut_obj_add_int(d, o, "bytes",+ yyjson_get_sint(yyjson_obj_get(v, "bytes")));+ yyjson_mut_obj_add_strcpy(d, o, "container_id", cid);+ yyjson_mut_obj_add_strcpy(d, o, "path", path);+ yyjson_mut_obj_add_strcpy(d, o, "source",+ yyjson_get_str(yyjson_obj_get(v, "source")));+ return o;+}++/* The container's index, up to date (mu held): NULL when it is not there+ or has expired. */+static yyjson_doc *index_of(const char *cid)+{+ yyjson_doc *m = meta_of(cid);+ int ok = m && is_running(m);+ yyjson_doc_free(m);+ if (!ok || sync_index(cid, "assistant", NULL, NULL) != 0)+ return NULL;+ char p[4600];+ path_of(p, sizeof p, cid, "files.json");+ return read_json(p);+}++int srv_container_files_create(struct srv_req *r)+{+ const char *cid = r->param[0];+ if (!on)+ return refused(r);+ const char *ct = r->conn ? MHD_lookup_connection_value(+ r->conn, MHD_HEADER_KIND, "Content-Type")+ : NULL;+ const char *why = NULL;+ char name[256] = "";+ pthread_mutex_lock(&mu);+ yyjson_doc *m = meta_of(cid);+ int ok = m && is_running(m);+ yyjson_doc_free(m);+ if (!ok) {+ pthread_mutex_unlock(&mu);+ return not_found(r, "container", cid);+ }+ if (ct && !strncmp(ct, "multipart/", 10)) {+ struct srv_part parts[4];+ char err[200];+ int n = srv_multipart_parse(ct, r->body, r->n_body, parts, 4, err,+ sizeof err);+ const struct srv_part *f =+ n > 0 ? srv_multipart_get(parts, n, "file") : NULL;+ if (!f || !f->has_filename) {+ why = "'file' is the file's bytes, with a name.";+ } else {+ const char *b = strrchr(f->filename, '/');+ snprintf(name, sizeof name, "%s", b ? b + 1 : f->filename);+ int fd = name[0] && name[0] != '.'+ ? open_under(cid, name, O_WRONLY | O_CREAT | O_TRUNC)+ : -1;+ if (fd < 0 || write(fd, f->data, f->n) != (ssize_t)f->n)+ why = "The file could not be written into the container.";+ if (fd >= 0)+ close(fd);+ }+ } else {+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ const char *fid =+ yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(doc), "file_id"));+ struct srv_file f;+ if (fid && srv_files_get(r->srv->files, fid, &f) == 0) {+ const char *b = strrchr(f.filename, '/');+ snprintf(name, sizeof name, "%s", b ? b + 1 : f.filename);+ }+ why = copy_file(r->srv, cid, fid);+ yyjson_doc_free(doc);+ }+ if (!why) /* the client's */+ sync_index(cid, "user", NULL, NULL);+ yyjson_doc *x = why ? NULL : index_of(cid);+ pthread_mutex_unlock(&mu);+ if (why)+ return bad(r, why);+ int ret = -1;+ size_t i, n;+ yyjson_val *v;+ yyjson_arr_foreach(yyjson_doc_get_root(x), i, n, v)+ {+ if (ret < 0 && yyjson_equals_str(yyjson_obj_get(v, "path"), name)) {+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ size_t len;+ char *t = yyjson_mut_val_write(file_obj(d, cid, v), 0, &len);+ ret = srv_reply_json(r, 200, t, t ? len : 0);+ yyjson_mut_doc_free(d);+ }+ }+ yyjson_doc_free(x);+ return ret < 0 ? srv_reply_error(r, 500, "server_error", NULL,+ "the file is not in the container")+ : ret;+}++int srv_container_files_list(struct srv_req *r)+{+ pthread_mutex_lock(&mu);+ yyjson_doc *x = index_of(r->param[0]);+ pthread_mutex_unlock(&mu);+ if (!x)+ return not_found(r, "container", r->param[0]);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *items = yyjson_mut_arr(d);+ size_t i, n;+ yyjson_val *v;+ yyjson_arr_foreach(yyjson_doc_get_root(x), i, n, v)+ yyjson_mut_arr_append(items, file_obj(d, r->param[0], v));+ yyjson_doc_free(x);+ int ret = page(r, d, items);+ yyjson_mut_doc_free(d);+ return ret;+}++/* the file's entry in x, or NULL */+static yyjson_val *entry(yyjson_doc *x, const char *fid)+{+ size_t i, n;+ yyjson_val *v;+ yyjson_arr_foreach(yyjson_doc_get_root(x), i, n, v)+ {+ if (yyjson_equals_str(yyjson_obj_get(v, "id"), fid))+ return v;+ }+ return NULL;+}++int srv_container_files_get(struct srv_req *r)+{+ pthread_mutex_lock(&mu);+ yyjson_doc *x = index_of(r->param[0]);+ pthread_mutex_unlock(&mu);+ if (!x)+ return not_found(r, "container", r->param[0]);+ yyjson_val *v = entry(x, r->param[1]);+ int ret;+ if (!v) {+ ret = not_found(r, "container file", r->param[1]);+ } else {+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ size_t len;+ char *t = yyjson_mut_val_write(file_obj(d, r->param[0], v), 0, &len);+ ret = srv_reply_json(r, 200, t, t ? len : 0);+ yyjson_mut_doc_free(d);+ }+ yyjson_doc_free(x);+ return ret;+}++int srv_container_files_delete(struct srv_req *r)+{+ pthread_mutex_lock(&mu);+ yyjson_doc *x = index_of(r->param[0]);+ yyjson_val *v = x ? entry(x, r->param[1]) : NULL;+ int gone = 0;+ if (v) {+ /* removed in its directory reached without links: never what a+ link points to */+ const char *leaf;+ int dir = parent_under(+ r->param[0], yyjson_get_str(yyjson_obj_get(v, "path")), &leaf);+ gone = dir >= 0 && unlinkat(dir, leaf, 0) == 0;+ if (dir >= 0)+ close(dir);+ sync_index(r->param[0], "assistant", NULL, NULL);+ }+ pthread_mutex_unlock(&mu);+ yyjson_doc_free(x);+ if (!x)+ return not_found(r, "container", r->param[0]);+ if (!gone)+ return not_found(r, "container file", r->param[1]);+ char out[512];+ snprintf(out, sizeof out,+ "{\"id\":\"%s\",\"object\":\"container.file.deleted\","+ "\"deleted\":true}",+ r->param[1]);+ char *t = strdup(out);+ return srv_reply_json(r, 200, t, t ? strlen(t) : 0);+}++int srv_container_files_content(struct srv_req *r)+{+ pthread_mutex_lock(&mu);+ yyjson_doc *x = index_of(r->param[0]);+ yyjson_val *v = x ? entry(x, r->param[1]) : NULL;+ int fd = v ? open_under(r->param[0],+ yyjson_get_str(yyjson_obj_get(v, "path")), O_RDONLY)+ : -1;+ pthread_mutex_unlock(&mu);+ yyjson_doc_free(x);+ struct stat st;+ if (fd >= 0 && (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode))) {+ close(fd);+ fd = -1;+ }+ if (fd < 0)+ return !x ? not_found(r, "container", r->param[0])+ : not_found(r, "container file", r->param[1]);+ return srv_reply_fd(r, fd, (uint64_t)st.st_size,+ "application/octet-stream");+}++#endifdiff --git a/src/server/containers.h b/src/server/containers.hnew file mode 100644index 0000000..5c5c56d--- /dev/null+++ b/src/server/containers.h@@ -0,0 +1,57 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * containers.h - code_interpreter's containers (containers.c): a directory+ * of files, /mnt/data to the code, and a Python session that keeps its+ * variables from one piece of code to the next, in the cage of+ * src/common/sandbox.c. Only with --code-interpreter.+ */+#ifndef JANAS_SERVER_CONTAINERS_H+#define JANAS_SERVER_CONTAINERS_H++#include "server.h"++#include <yyjson.h>++/* What a piece of code did. */+struct srv_cell {+ char *logs; /* what it printed, and the value of its last expression */+ char *error; /* its traceback, or why it did not run; NULL when fine */+ /* the files it wrote or changed (container file ids and paths under+ /mnt/data), images (the figures it drew) first */+ int n_files, n_images;+ char (*file_id)[48];+ char (*path)[300];+};++void srv_cell_free(struct srv_cell *c);++/* NULL when the server runs code (--code-interpreter, and the cage can be+ built here), else why not. */+const char *srv_containers_off(void);++/* A container for a request's tool: an id given (it must be there and+ running), or {type: auto, file_ids, memory_limit} made now. 0 with its id+ in id, -1 with the reason. */+int srv_container_for(struct srv *s, yyjson_val *spec, char *id, size_t cap,+ const char **why);++/* code run in the container's session: 0 with out filled (the code's own+ errors in out->error), -1 when the container is not there or running. */+int srv_container_run(struct srv *s, const char *id, const char *code,+ struct srv_cell *out);++/* A file of the Files API copied into a running container: 0, or -1 with+ the reason. */+int srv_container_add_file(struct srv *s, const char *cid, const char *fid,+ const char **why);+/* A container file opened for reading (a regular file, links never+ followed), its name (last path part) into name: the fd, or -1. */+int srv_container_file_open(const char *cid, const char *cfid, char *name,+ size_t cap);++/* A container file's name (its last path part) into name: 0, or -1. */+int srv_container_file_name(struct srv *s, const char *cid, const char *fid,+ char *name, size_t cap);++#endifdiff --git a/src/server/conversations.c b/src/server/conversations.cindex fef005a..0c171fe 100644--- a/src/server/conversations.c+++ b/src/server/conversations.c@@ -83,34 +83,36 @@ static int take_items(yyjson_mut_doc *d, yyjson_mut_val *arr, yyjson_val *in,int conv_append(struct srv *srv, const char *conv, const char *items_json){+ /* two arrays as texts joined: no reading back and writing again of a+ conversation that only grows */+ size_t nb = strlen(items_json);+ if (nb < 2 || items_json[0] != '[' || items_json[nb - 1] != ']')+ return -2;pthread_mutex_lock(&conv_mu);char *items = NULL;int rc = srv_store_get(srv->store, CONV_KIND, conv, NULL, &items);- yyjson_doc *a =- rc == 0 && items ? yyjson_read(items, strlen(items), 0) : NULL;- yyjson_doc *b = yyjson_read(items_json, strlen(items_json), 0);- free(items);- if (rc == 0) {- yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);- yyjson_mut_val *arr = yyjson_mut_arr(d);- size_t idx, max;- yyjson_val *it;- yyjson_arr_foreach(yyjson_doc_get_root(a), idx, max, it)- yyjson_mut_arr_append(arr, yyjson_val_mut_copy(d, it));- yyjson_arr_foreach(yyjson_doc_get_root(b), idx, max, it)- yyjson_mut_arr_append(arr, yyjson_val_mut_copy(d, it));- size_t n;+ if (rc == 0 && nb > 2) {+ const char *a = items ? items : "[]";+ size_t na = strlen(a);char *out =- yyjson_mut_val_write(arr, YYJSON_WRITE_ALLOW_INVALID_UNICODE, &n);- rc =- out ? srv_store_update(srv->store, CONV_KIND, conv, NULL, out) : -1;- free(out);- yyjson_mut_doc_free(d);+ na >= 2 && a[0] == '[' && a[na - 1] == ']' ? malloc(na + nb) : NULL;+ if (!out)+ rc = -2;+ else {+ size_t w = na - 1;+ memcpy(out, a, w); /* without its ']' */+ if (na > 2)+ out[w++] = ',';+ memcpy(out + w, items_json + 1, nb - 1); /* without the '[' */+ w += nb - 1;+ out[w] = 0;+ rc = srv_store_update(srv->store, CONV_KIND, conv, NULL, out);+ free(out);+ }}- yyjson_doc_free(a);- yyjson_doc_free(b);+ free(items);pthread_mutex_unlock(&conv_mu);- return rc == 0 ? 0 : -1;+ return rc;}char *conv_items(struct srv *srv, const char *conv)@@ -311,6 +313,11 @@ int srv_conv_items_create(struct srv_req *r)if (rc != 0) {free(its);yyjson_mut_doc_free(d);+ if (rc == -3)+ return srv_reply_error(r, 400, "invalid_request_error",+ "conversation_too_large",+ "The conversation '%s' would pass %zu MiB.",+ r->param[0], SRV_STORE_DOC_MAX >> 20);return rc == -1 ? no_conv(r) : oom(r);}yyjson_doc *added = yyjson_read(its, n, 0);diff --git a/src/server/engine.c b/src/server/engine.cindex 86e5971..7b774af 100644--- a/src/server/engine.c+++ b/src/server/engine.c@@ -28,8 +28,10 @@ struct srv_engine {pthread_mutex_t mu;pthread_cond_t cv;struct srv_job *head, *tail;+ struct srv_job *running; /* the job being run, or NULL */uint32_t queued, max_queue;int stop;+ int closing; /* the server is stopping: no client's request taken */int verbose;uint32_t max_input, warn_input;struct srv_fim *fim; /* the windows of fill-in-the-middle requests */@@ -98,6 +100,7 @@ void srv_job_release(struct srv_job *j)}free(m->call_name);free(m->call_args);+ srv_imgs_free(m->imgs, m->n_imgs);}free(j->msg);for (int i = 0; i < j->n_prompts; i++) {@@ -157,6 +160,13 @@ static int grow(char **buf, size_t *n, size_t *cap, const char *s, size_t k)return 0;}+/* The client went away, or the submitter's flag says stop (j->mu held, or+ the flag alone). */+static int stopped(const struct srv_job *j)+{+ return j->cancel || (j->watch && atomic_load(j->watch));+}+/* The first of the stop strings in the answer at or after `from`, or -1. */static long find_stop(const struct srv_job *j, const struct srv_choice *c,size_t from)@@ -294,13 +304,12 @@ static int32_t setup(struct srv_engine *e, struct srv_job *j)*/static int32_t ids_text(struct srv_engine *e, struct srv_prompt *pp){- char buf[256];+ char buf[1024]; /* as long as the library's longest token */size_t cap = 0;for (size_t i = 0; i < pp->n_ids; i++) {int32_t n = 0;if (janas_llm_token_text(e->llm, pp->ids[i], buf, sizeof(buf), &n) !=- JANAS_LLM_OK &&- n < (int32_t)sizeof(buf))+ JANAS_LLM_OK)return JANAS_LLM_EINVAL; /* not a token of this model */if (pp->n + (size_t)n + 1 > cap) {cap = 2 * (pp->n + (size_t)n) + 256;@@ -363,7 +372,12 @@ static int32_t load(struct srv_engine *e, struct srv_job *j, int k)int32_t rc = janas_llm_chat_begin(c);for (int32_t i = 0; rc == JANAS_LLM_OK && i < j->n_msg; i++) {const struct srv_msg *m = &j->msg[i];- rc = janas_llm_chat_add(c, m->role, m->text, (int32_t)m->n);+ /* its pictures first: they go with the message added next */+ for (int k = 0; rc == JANAS_LLM_OK && k < m->n_imgs; k++)+ rc =+ janas_llm_chat_image(c, m->imgs[k].data, (int64_t)m->imgs[k].n);+ if (rc == JANAS_LLM_OK)+ rc = janas_llm_chat_add(c, m->role, m->text, (int32_t)m->n);for (int k = 0; rc == JANAS_LLM_OK && k < m->n_calls; k++)rc = janas_llm_chat_add_call(c, m->call_name[k], -1,m->call_args[k], -1);@@ -460,7 +474,7 @@ static int32_t run_one(struct srv_engine *e, struct srv_job *j, int k)ch->by_stop = 1;}}- int quit = j->cancel || ch->by_stop || bad || rc == JANAS_LLM_DONE;+ int quit = stopped(j) || ch->by_stop || bad || rc == JANAS_LLM_DONE;pthread_cond_broadcast(&j->cv);pthread_mutex_unlock(&j->mu);if (bad) {@@ -505,7 +519,12 @@ static int32_t count(struct srv_engine *e, struct srv_job *j)int32_t rc = janas_llm_chat_begin(c), n = 0;for (int32_t i = 0; rc == JANAS_LLM_OK && i < j->n_msg; i++) {const struct srv_msg *m = &j->msg[i];- rc = janas_llm_chat_add(c, m->role, m->text, (int32_t)m->n);+ /* its pictures first: they go with the message added next */+ for (int k = 0; rc == JANAS_LLM_OK && k < m->n_imgs; k++)+ rc =+ janas_llm_chat_image(c, m->imgs[k].data, (int64_t)m->imgs[k].n);+ if (rc == JANAS_LLM_OK)+ rc = janas_llm_chat_add(c, m->role, m->text, (int32_t)m->n);for (int k = 0; rc == JANAS_LLM_OK && k < m->n_calls; k++)rc = janas_llm_chat_add_call(c, m->call_name[k], -1,m->call_args[k], -1);@@ -539,10 +558,14 @@ static int32_t embed(struct srv_engine *e, struct srv_job *j)for (int i = 0; i < j->n_prompts && rc == JANAS_LLM_OK; i++) {struct srv_prompt *pp = &j->prompts[i];if (pp->ids && !pp->text) {- char buf[256];+ char buf[1024]; /* as long as the library's longest token */for (size_t k = 0; k < pp->n_ids && rc == JANAS_LLM_OK; k++) {int32_t n = 0;- janas_llm_token_text(e->emb, pp->ids[k], buf, sizeof(buf), &n);+ if (janas_llm_token_text(e->emb, pp->ids[k], buf, sizeof(buf),+ &n) != JANAS_LLM_OK) {+ rc = JANAS_LLM_EINVAL;+ break;+ }char *t = realloc(pp->text, pp->n + (size_t)n + 1);if (!t) {rc = JANAS_LLM_ENOMEM;@@ -584,7 +607,7 @@ static void run(struct srv_engine *e, struct srv_job *j)for (; rc == JANAS_LLM_OK && k < j->n_choices; k++) {pthread_mutex_lock(&j->mu);j->cur = k;- int cancel = j->cancel;+ int cancel = stopped(j);pthread_mutex_unlock(&j->mu);if (cancel)break;@@ -646,12 +669,13 @@ static void *engine_main(void *arg)if (!e->head)e->tail = NULL;e->queued--;+ e->running = j;number_queue(e);pthread_mutex_unlock(&e->mu);pthread_mutex_lock(&j->mu);j->started = 1;j->queue_pos = 0;- int skip = j->cancel; /* gone while it waited */+ int skip = stopped(j); /* gone while it waited */pthread_mutex_unlock(&j->mu);if (skip) {pthread_mutex_lock(&j->mu);@@ -663,15 +687,23 @@ static void *engine_main(void *arg)} else {run(e, j);}+ pthread_mutex_lock(&e->mu);+ e->running = NULL;+ pthread_mutex_unlock(&e->mu);srv_job_release(j);}return NULL;}+_Thread_local int srv_engine_low;+_Thread_local const _Atomic int *srv_engine_watch;+int srv_engine_submit(struct srv_engine *e, struct srv_job *j){+ j->low = srv_engine_low;+ j->watch = srv_engine_watch;pthread_mutex_lock(&e->mu);- if (e->queued >= e->max_queue || e->stop) {+ if (e->queued >= e->max_queue || e->stop || (e->closing && !j->low)) {pthread_mutex_unlock(&e->mu);return -1;}@@ -679,11 +711,14 @@ int srv_engine_submit(struct srv_engine *e, struct srv_job *j)j->refs++; /* the engine's */pthread_mutex_unlock(&j->mu);j->next = NULL;- if (e->tail)- e->tail->next = j;- else- e->head = j;- e->tail = j;+ /* a request of a client before the batches' waiting */+ struct srv_job **at = &e->head;+ while (*at && (j->low || !(*at)->low))+ at = &(*at)->next;+ j->next = *at;+ *at = j;+ if (!j->next)+ e->tail = j;e->queued++;number_queue(e);pthread_cond_signal(&e->cv);@@ -769,6 +804,33 @@ struct srv_engine *srv_engine_start(const struct srv_config *cfg, char *err,return e;}+/* A client's job ended because the server stops: an error that says so,+ for the client still there. */+static void cancel_job(struct srv_job *j)+{+ pthread_mutex_lock(&j->mu);+ j->cancel = 1;+ if (j->err == JANAS_LLM_OK) {+ j->err = JANAS_LLM_EFAIL;+ snprintf(j->errmsg, sizeof(j->errmsg), "the server is stopping");+ }+ pthread_mutex_unlock(&j->mu);+}++void srv_engine_cancel_clients(struct srv_engine *e)+{+ if (!e)+ return;+ pthread_mutex_lock(&e->mu);+ e->closing = 1;+ if (e->running && !e->running->low)+ cancel_job(e->running);+ for (struct srv_job *j = e->head; j; j = j->next)+ if (!j->low)+ cancel_job(j);+ pthread_mutex_unlock(&e->mu);+}+void srv_engine_stop(struct srv_engine *e){if (!e)diff --git a/src/server/evals.c b/src/server/evals.cnew file mode 100644index 0000000..ce5a9b2--- /dev/null+++ b/src/server/evals.c@@ -0,0 +1,1273 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * evals.c - OpenAI's Evals API: an eval is a data schema and graders; a+ * run of it takes rows of data (given in the request, or a JSONL file),+ * has the model write a sample for each where the run asks (a template of+ * messages filled from the row), and grades each row: string_check,+ * text_similarity (evals_metrics.c), label_model and score_model, the+ * model itself answering through a JSON schema that holds it to a label or+ * a score. A run goes on a thread of its own, behind every client's+ * request, each row through the same handlers as /v1/chat/completions and+ * /v1/embeddings; its rows' results are written as they come, beside the+ * files (.evals), and the run saved after each, so a stopped server goes+ * on from there.+ */+#include "server.h"++#include <ctype.h>+#include <errno.h>+#include <fcntl.h>+#include <pthread.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include <time.h>+#include <unistd.h>+#include <yyjson.h>++#include "evals_metrics.h"+#include "files_store.h"+#include "llm/json.h"+#include "store_disk.h"++#define EVAL "eval"+#define RUN "eval.run"+#define MAX_ROWS 10000++static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;+static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;+static pthread_t worker;+static int running, stopping;+static struct srv *server;+static char work[4200];++/* ---- helpers ---- */++static int bad(struct srv_req *r, const char *msg)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_value",+ "%s", msg);+}++static int not_found(struct srv_req *r, const char *what, const char *id)+{+ return srv_reply_error(r, 404, "invalid_request_error", "not_found",+ "No %s found with id '%s'.", what, id);+}++static int reply_text(struct srv_req *r, char *json)+{+ return srv_reply_json(r, 200, json, json ? strlen(json) : 0);+}++static char *store_get(const char *kind, const char *id, char **aux)+{+ char *j = NULL;+ if (srv_store_get(server->store, kind, id, &j, aux) != 0)+ return NULL;+ return j;+}++/* A document of the store as a mutable JSON object (NULL: none). */+static yyjson_mut_val *load(yyjson_mut_doc *d, const char *kind, const char *id)+{+ char *t = store_get(kind, id, NULL);+ yyjson_doc *x = t ? yyjson_read(t, strlen(t), 0) : NULL;+ free(t);+ yyjson_mut_val *o =+ x ? yyjson_val_mut_copy(d, yyjson_doc_get_root(x)) : NULL;+ yyjson_doc_free(x);+ return o;+}++static int save(const char *kind, const char *id, yyjson_mut_val *o,+ const char *aux, int fresh)+{+ char *t = yyjson_mut_val_write(o, 0, NULL);+ int rc = -1;+ if (t)+ rc = fresh ? srv_store_put(server->store, kind, id, (int64_t)time(NULL),+ t, aux)+ : srv_store_update(server->store, kind, id, t, aux);+ free(t);+ return rc;+}++static int metadata_ok(yyjson_val *m)+{+ if (!m || yyjson_is_null(m))+ return 1;+ if (!yyjson_is_obj(m) || yyjson_obj_size(m) > 16)+ return 0;+ size_t i, n;+ yyjson_val *k, *v;+ yyjson_obj_foreach(m, i, n, k, v) if (!yyjson_is_str(v) ||+ yyjson_get_len(v) > 512) return 0;+ return 1;+}++/* ---- templates: {{item.a.b}}, {{sample.output_text}} ---- */++static yyjson_val *path(yyjson_val *root, const char *p, size_t n)+{+ yyjson_val *v = root;+ while (v && n) {+ size_t k = 0;+ while (k < n && p[k] != '.')+ k++;+ char key[128];+ if (k >= sizeof key)+ return NULL;+ memcpy(key, p, k);+ key[k] = 0;+ if (yyjson_is_arr(v) && isdigit((unsigned char)key[0]))+ v = yyjson_arr_get(v, (size_t)atol(key));+ else+ v = yyjson_obj_get(v, key);+ p += k;+ n -= k;+ if (n && *p == '.')+ p++, n--;+ }+ return v;+}++/* A template filled from the namespaces item and sample (malloc'd). */+static char *render(const char *t, yyjson_val *item, yyjson_val *sample)+{+ struct janas_buf b = {0};+ for (const char *p = t; *p;) {+ const char *open = strstr(p, "{{");+ if (!open) {+ janas_buf_put(&b, p, strlen(p));+ break;+ }+ janas_buf_put(&b, p, (size_t)(open - p));+ const char *close = strstr(open + 2, "}}");+ if (!close) {+ janas_buf_put(&b, open, strlen(open));+ break;+ }+ const char *a = open + 2, *e = close;+ while (a < e && *a == ' ')+ a++;+ while (e > a && e[-1] == ' ')+ e--;+ yyjson_val *v = NULL;+ if ((size_t)(e - a) >= 5 && !strncmp(a, "item.", 5))+ v = path(item, a + 5, (size_t)(e - a) - 5);+ else if ((size_t)(e - a) == 4 && !strncmp(a, "item", 4))+ v = item;+ else if ((size_t)(e - a) >= 7 && !strncmp(a, "sample.", 7))+ v = path(sample, a + 7, (size_t)(e - a) - 7);+ if (yyjson_is_str(v))+ janas_buf_put(&b, yyjson_get_str(v), yyjson_get_len(v));+ else if (v) {+ size_t n;+ char *j = yyjson_val_write(v, 0, &n);+ if (j)+ janas_buf_put(&b, j, n);+ free(j);+ }+ p = close + 2;+ }+ janas_buf_put(&b, "", 1);+ if (b.oom) {+ janas_buf_free(&b);+ return NULL;+ }+ return b.p;+}++/* A message's text: content a string, or {type: input_text, text}, or a+ list of those. */+static const char *message_text(yyjson_val *m)+{+ yyjson_val *c = yyjson_obj_get(m, "content");+ if (yyjson_is_str(c))+ return yyjson_get_str(c);+ if (yyjson_is_obj(c))+ return yyjson_get_str(yyjson_obj_get(c, "text"));+ if (yyjson_is_arr(c))+ return yyjson_get_str(yyjson_obj_get(yyjson_arr_get_first(c), "text"));+ return NULL;+}++/* ---- the model, asked from inside ---- */++/* A chat completion of messages (a JSON array text) with the run's+ sampling, and an optional response_format (JSON text): the status, the+ answer's body in *out (malloc'd). */+static unsigned chat(const char *messages, yyjson_val *sampling,+ const char *format, char **out)+{+ *out = NULL;+ srv_handler h = srv_route_find("POST", "/chat/completions");+ if (!h)+ return 500;+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_doc_set_root(d, o);+ yyjson_doc *md = yyjson_read(messages, strlen(messages), 0);+ yyjson_mut_obj_add_val(d, o, "messages",+ yyjson_val_mut_copy(d, yyjson_doc_get_root(md)));+ static const char *const keys[] = {"temperature", "top_p", "seed"};+ for (int i = 0; i < 3; i++) {+ yyjson_val *v = yyjson_obj_get(sampling, keys[i]);+ if (yyjson_is_num(v))+ yyjson_mut_obj_add_val(d, o, keys[i], yyjson_val_mut_copy(d, v));+ }+ yyjson_val *mx = yyjson_obj_get(sampling, "max_completion_tokens");+ if (!mx)+ mx = yyjson_obj_get(sampling, "max_completions_tokens");+ yyjson_mut_obj_add_int(d, o, "max_completion_tokens",+ yyjson_is_int(mx) ? yyjson_get_sint(mx) : 1024);+ /* graders and samples answer, they do not muse at length */+ yyjson_val *re = yyjson_obj_get(sampling, "reasoning_effort");+ yyjson_mut_obj_add_strcpy(d, o, "reasoning_effort",+ yyjson_is_str(re) ? yyjson_get_str(re) : "none");+ yyjson_doc *fd = format ? yyjson_read(format, strlen(format), 0) : NULL;+ if (fd)+ yyjson_mut_obj_add_val(d, o, "response_format",+ yyjson_val_mut_copy(d, yyjson_doc_get_root(fd)));+ size_t bn;+ char *body = yyjson_mut_write(d, 0, &bn);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(md);+ yyjson_doc_free(fd);+ struct srv_capture cap = {0};+ struct srv_req r = {.srv = server,+ .cap = &cap,+ .method = "POST",+ .path = "/chat/completions",+ .body = body ? body : "{}",+ .n_body = body ? bn : 2};+ h(&r);+ free(body);+ *out = cap.body;+ return cap.status;+}++/* The messages of a template filled from a row, as a JSON array text. */+static char *fill_messages(yyjson_val *tmpl, yyjson_val *item,+ yyjson_val *sample)+{+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *arr = yyjson_mut_arr(d);+ size_t i, n;+ yyjson_val *m;+ yyjson_arr_foreach(tmpl, i, n, m)+ {+ const char *role = yyjson_get_str(yyjson_obj_get(m, "role"));+ const char *text = message_text(m);+ char *t = render(text ? text : "", item, sample);+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, arr);+ yyjson_mut_obj_add_strcpy(d, o, "role",+ role && !strcmp(role, "developer") ? "system"+ : role ? role+ : "user");+ yyjson_mut_obj_add_strcpy(d, o, "content", t ? t : "");+ free(t);+ }+ char *out = yyjson_mut_val_write(arr, 0, NULL);+ yyjson_mut_doc_free(d);+ return out;+}++/* ---- graders ---- */++/* One grader on a row: its result object into results; 1 passed, 0+ failed, -1 an error. */+static int grade(yyjson_mut_doc *d, yyjson_mut_val *results, yyjson_val *g,+ yyjson_val *item, yyjson_val *sample)+{+ const char *type = yyjson_get_str(yyjson_obj_get(g, "type"));+ const char *name = yyjson_get_str(yyjson_obj_get(g, "name"));+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, results);+ yyjson_mut_obj_add_strcpy(d, o, "name", name ? name : "");+ yyjson_mut_obj_add_strcpy(d, o, "type", type ? type : "");+ double score = 0;+ int passed = 0, err = 0;+ if (type && !strcmp(type, "string_check")) {+ char *in =+ render(yyjson_get_str(yyjson_obj_get(g, "input")), item, sample);+ char *ref = render(yyjson_get_str(yyjson_obj_get(g, "reference")), item,+ sample);+ const char *op = yyjson_get_str(yyjson_obj_get(g, "operation"));+ if (in && ref && op) {+ if (!strcmp(op, "eq"))+ passed = !strcmp(in, ref);+ else if (!strcmp(op, "ne"))+ passed = strcmp(in, ref) != 0;+ else if (!strcmp(op, "like"))+ passed = strstr(in, ref) != NULL;+ else if (!strcmp(op, "ilike"))+ passed = strcasestr(in, ref) != NULL;+ } else {+ err = 1;+ }+ score = passed;+ free(in);+ free(ref);+ } else if (type && !strcmp(type, "text_similarity")) {+ char *in =+ render(yyjson_get_str(yyjson_obj_get(g, "input")), item, sample);+ char *ref = render(yyjson_get_str(yyjson_obj_get(g, "reference")), item,+ sample);+ const char *m = yyjson_get_str(yyjson_obj_get(g, "evaluation_metric"));+ if (in && ref && m)+ score = srv_eval_similarity(m, in, ref);+ else+ err = 1;+ yyjson_val *th = yyjson_obj_get(g, "pass_threshold");+ passed = !err && score >= (th ? yyjson_get_num(th) : 1);+ free(in);+ free(ref);+ } else if (type &&+ (!strcmp(type, "label_model") || !strcmp(type, "score_model"))) {+ int label = type[0] == 'l';+ char *msgs = fill_messages(yyjson_obj_get(g, "input"), item, sample);+ /* the answer held to a label of the list, or a number */+ yyjson_mut_doc *fd = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *f = yyjson_mut_obj(fd);+ yyjson_mut_doc_set_root(fd, f);+ yyjson_mut_obj_add_str(fd, f, "type", "json_schema");+ yyjson_mut_val *js = yyjson_mut_obj_add_obj(fd, f, "json_schema");+ yyjson_mut_obj_add_str(fd, js, "name", "grade");+ yyjson_mut_val *sc = yyjson_mut_obj_add_obj(fd, js, "schema");+ yyjson_mut_obj_add_str(fd, sc, "type", "object");+ yyjson_mut_val *pr = yyjson_mut_obj_add_obj(fd, sc, "properties");+ yyjson_mut_val *res = yyjson_mut_obj_add_obj(fd, pr, "result");+ yyjson_val *range = yyjson_obj_get(g, "range");+ double lo = 0, hi = 1;+ if (yyjson_arr_size(range) == 2) {+ lo = yyjson_get_num(yyjson_arr_get(range, 0));+ hi = yyjson_get_num(yyjson_arr_get(range, 1));+ }+ if (label) {+ yyjson_mut_obj_add_str(fd, res, "type", "string");+ yyjson_mut_obj_add_val(+ fd, res, "enum",+ yyjson_val_mut_copy(fd, yyjson_obj_get(g, "labels")));+ } else {+ yyjson_mut_obj_add_str(fd, res, "type", "number");+ yyjson_mut_obj_add_real(fd, res, "minimum", lo);+ yyjson_mut_obj_add_real(fd, res, "maximum", hi);+ }+ yyjson_mut_val *rq = yyjson_mut_obj_add_arr(fd, sc, "required");+ yyjson_mut_arr_add_str(fd, rq, "result");+ yyjson_mut_obj_add_bool(fd, sc, "additionalProperties", false);+ char *fmt = yyjson_mut_write(fd, 0, NULL);+ yyjson_mut_doc_free(fd);+ char *body = NULL;+ unsigned st =+ msgs && fmt+ ? chat(msgs, yyjson_obj_get(g, "sampling_params"), fmt, &body)+ : 500;+ free(msgs);+ free(fmt);+ yyjson_doc *bd = body ? yyjson_read(body, strlen(body), 0) : NULL;+ const char *content = yyjson_get_str(yyjson_obj_get(+ yyjson_obj_get(yyjson_arr_get_first(yyjson_obj_get(+ yyjson_doc_get_root(bd), "choices")),+ "message"),+ "content"));+ yyjson_doc *cd =+ content ? yyjson_read(content, strlen(content), 0) : NULL;+ yyjson_val *rv = yyjson_obj_get(yyjson_doc_get_root(cd), "result");+ if (st != 200 || !rv) {+ err = 1;+ } else if (label) {+ const char *l = yyjson_get_str(rv);+ size_t i, n;+ yyjson_val *x;+ yyjson_arr_foreach(yyjson_obj_get(g, "passing_labels"), i, n, x)+ passed |= l && yyjson_equals_str(x, l);+ score = passed;+ yyjson_mut_val *smp = yyjson_mut_obj_add_obj(d, o, "sample");+ yyjson_mut_obj_add_strcpy(d, smp, "label", l ? l : "");+ } else {+ score = yyjson_get_num(rv);+ yyjson_val *th = yyjson_obj_get(g, "pass_threshold");+ passed = score >= (th ? yyjson_get_num(th) : (lo + hi) / 2);+ }+ yyjson_doc_free(cd);+ yyjson_doc_free(bd);+ free(body);+ } else {+ err = 1;+ }+ yyjson_mut_obj_add_real(d, o, "score", score);+ yyjson_mut_obj_add_bool(d, o, "passed", passed && !err);+ if (err)+ yyjson_mut_obj_add_str(d, o, "error", "the grader could not run");+ return err ? -1 : passed;+}++/* ---- a run, row by row ---- */++static void out_path(char *p, size_t cap, const char *run)+{+ snprintf(p, cap, "%s/%s.jsonl", work, run);+}++/* The rows of a run's data source: a JSON array text of {item, sample?}+ (malloc'd), or NULL with the reason. */+static char *rows_of(yyjson_val *ds, char *why, size_t wl)+{+ yyjson_val *src = yyjson_obj_get(ds, "source");+ const char *st = yyjson_get_str(yyjson_obj_get(src, "type"));+ if (st && !strcmp(st, "file_content")) {+ yyjson_val *c = yyjson_obj_get(src, "content");+ if (!yyjson_is_arr(c)) {+ snprintf(why, wl, "the source's content is a list of rows");+ return NULL;+ }+ return yyjson_val_write(c, 0, NULL);+ }+ if (st && !strcmp(st, "file_id")) {+ const char *id = yyjson_get_str(yyjson_obj_get(src, "id"));+ struct srv_file f;+ if (!id || srv_files_get(server->files, id, &f) != 0 ||+ f.bytes > ((size_t)64 << 20)) {+ snprintf(why, wl, "the source's file is not here");+ return NULL;+ }+ int fd = srv_files_open_content(server->files, id);+ char *t = fd >= 0 ? malloc(f.bytes + 1) : NULL;+ size_t got = 0;+ while (t && got < f.bytes) {+ ssize_t k = read(fd, t + got, f.bytes - got);+ if (k <= 0)+ break;+ got += (size_t)k;+ }+ if (fd >= 0)+ close(fd);+ if (!t) {+ snprintf(why, wl, "the source's file cannot be read");+ return NULL;+ }+ t[got] = 0;+ /* its JSON lines into an array */+ struct janas_buf b = {0};+ janas_buf_put(&b, "[", 1);+ int first = 1;+ for (char *l = t; *l;) {+ char *e = strchr(l, '\n');+ size_t n = e ? (size_t)(e - l) : strlen(l);+ size_t k = 0;+ while (k < n && isspace((unsigned char)l[k]))+ k++;+ if (k < n) {+ if (!first)+ janas_buf_put(&b, ",", 1);+ janas_buf_put(&b, l, n);+ first = 0;+ }+ l = e ? e + 1 : l + n;+ }+ janas_buf_put(&b, "]", 2);+ free(t);+ if (b.oom || !yyjson_read(b.p, b.n - 1, 0)) {+ janas_buf_free(&b);+ snprintf(why, wl, "the source's file is not JSON lines");+ return NULL;+ }+ return b.p;+ }+ snprintf(why, wl, "the source is file_content or file_id");+ return NULL;+}++static void run_one(const char *run_id)+{+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ char *aux = NULL;+ char *rt = store_get(RUN, run_id, &aux);+ yyjson_doc *rd = rt ? yyjson_read(rt, strlen(rt), 0) : NULL;+ free(rt);+ yyjson_val *run = yyjson_doc_get_root(rd);+ const char *eval_id = yyjson_get_str(yyjson_obj_get(run, "eval_id"));+ char *et = eval_id ? store_get(EVAL, eval_id, NULL) : NULL;+ yyjson_doc *ed = et ? yyjson_read(et, strlen(et), 0) : NULL;+ free(et);+ yyjson_val *criteria =+ yyjson_obj_get(yyjson_doc_get_root(ed), "testing_criteria");+ yyjson_val *ds = yyjson_obj_get(run, "data_source");+ char why[200] = "";+ char *rows = run && criteria ? rows_of(ds, why, sizeof why) : NULL;+ yyjson_doc *rowsd = rows ? yyjson_read(rows, strlen(rows), 0) : NULL;+ free(rows);+ yyjson_val *all = yyjson_doc_get_root(rowsd);+ yyjson_mut_val *o = run ? yyjson_val_mut_copy(d, run) : NULL;+ long next = aux ? atol(aux) : 0;+ free(aux);+ if (!o) {+ yyjson_doc_free(rd);+ yyjson_doc_free(ed);+ yyjson_doc_free(rowsd);+ yyjson_mut_doc_free(d);+ return;+ }+ if (!all || yyjson_arr_size(all) > MAX_ROWS) {+ yyjson_mut_obj_remove_key(o, "status");+ yyjson_mut_obj_add_str(d, o, "status", "failed");+ yyjson_mut_obj_remove_key(o, "error");+ yyjson_mut_val *e = yyjson_mut_obj_add_obj(d, o, "error");+ yyjson_mut_obj_add_str(d, e, "code", "invalid_data_source");+ yyjson_mut_obj_add_strcpy(d, e, "message",+ all ? "more than 10,000 rows" : why);+ save(RUN, run_id, o, "0", 0);+ yyjson_doc_free(rd);+ yyjson_doc_free(ed);+ yyjson_doc_free(rowsd);+ yyjson_mut_doc_free(d);+ return;+ }+ yyjson_mut_obj_remove_key(o, "status");+ yyjson_mut_obj_add_str(d, o, "status", "in_progress");+ const char *dtype = yyjson_get_str(yyjson_obj_get(ds, "type"));+ int sampled = dtype && strcmp(dtype, "jsonl") != 0;+ yyjson_val *tmpl =+ yyjson_obj_get(yyjson_obj_get(ds, "input_messages"), "template");+ const char *iref = yyjson_get_str(+ yyjson_obj_get(yyjson_obj_get(ds, "input_messages"), "item_reference"));+ yyjson_val *sampling = yyjson_obj_get(ds, "sampling_params");+ size_t total = yyjson_arr_size(all), nc = yyjson_arr_size(criteria);+ char op[4600];+ out_path(op, sizeof op, run_id);+ for (size_t i = (size_t)next; i < total; i++) {+ pthread_mutex_lock(&mu);+ int stop = stopping;+ pthread_mutex_unlock(&mu);+ /* a cancel saved meanwhile */+ char *now = store_get(RUN, run_id, NULL);+ int cancelled = now && strstr(now, "\"status\":\"canceled\"");+ free(now);+ if (stop || cancelled)+ break;+ yyjson_val *row = yyjson_arr_get(all, i);+ yyjson_val *item = yyjson_obj_get(row, "item");+ if (!item)+ item = row;+ yyjson_mut_doc *od = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *oi = yyjson_mut_obj(od);+ char oid[48];+ srv_new_id(oid, sizeof oid, "outputitem_");+ yyjson_mut_obj_add_str(od, oi, "object", "eval.run.output_item");+ yyjson_mut_obj_add_strcpy(od, oi, "id", oid);+ yyjson_mut_obj_add_strcpy(od, oi, "run_id", run_id);+ yyjson_mut_obj_add_strcpy(od, oi, "eval_id", eval_id);+ yyjson_mut_obj_add_int(od, oi, "created_at", (int64_t)time(NULL));+ yyjson_mut_obj_add_int(od, oi, "datasource_item_id", (int64_t)i);+ yyjson_mut_obj_add_val(od, oi, "datasource_item",+ yyjson_val_mut_copy(od, item));+ /* the sample: the row's, or the model's answer to the template */+ yyjson_doc *sd = NULL;+ int errored = 0;+ yyjson_mut_val *smp = yyjson_mut_obj(od);+ if (sampled) {+ char *msgs = NULL;+ if (tmpl)+ msgs = fill_messages(tmpl, item, NULL);+ else if (iref) {+ const char *p = !strncmp(iref, "item.", 5) ? iref + 5 : iref;+ yyjson_val *ms = path(item, p, strlen(p));+ msgs = ms ? fill_messages(ms, item, NULL) : NULL;+ }+ char *body = NULL;+ unsigned st = msgs ? chat(msgs, sampling, NULL, &body) : 400;+ yyjson_doc *bd = body ? yyjson_read(body, strlen(body), 0) : NULL;+ yyjson_val *ch0 = yyjson_arr_get_first(+ yyjson_obj_get(yyjson_doc_get_root(bd), "choices"));+ const char *text = yyjson_get_str(+ yyjson_obj_get(yyjson_obj_get(ch0, "message"), "content"));+ yyjson_doc *md = msgs ? yyjson_read(msgs, strlen(msgs), 0) : NULL;+ yyjson_mut_obj_add_val(+ od, smp, "input",+ md ? yyjson_val_mut_copy(od, yyjson_doc_get_root(md))+ : yyjson_mut_arr(od));+ yyjson_mut_val *outs = yyjson_mut_obj_add_arr(od, smp, "output");+ yyjson_mut_val *om = yyjson_mut_arr_add_obj(od, outs);+ yyjson_mut_obj_add_str(od, om, "role", "assistant");+ yyjson_mut_obj_add_strcpy(od, om, "content", text ? text : "");+ const char *fr =+ yyjson_get_str(yyjson_obj_get(ch0, "finish_reason"));+ yyjson_mut_obj_add_strcpy(od, smp, "finish_reason",+ fr ? fr : "error");+ yyjson_mut_obj_add_strcpy(od, smp, "model", server->cfg.model_id);+ yyjson_val *u = yyjson_obj_get(yyjson_doc_get_root(bd), "usage");+ if (u)+ yyjson_mut_obj_add_val(od, smp, "usage",+ yyjson_val_mut_copy(od, u));+ yyjson_mut_obj_add_strcpy(od, smp, "output_text", text ? text : "");+ if (st != 200) {+ errored = 1;+ yyjson_mut_val *e = yyjson_mut_obj_add_obj(od, smp, "error");+ yyjson_mut_obj_add_str(od, e, "code", "sample_failed");+ yyjson_mut_obj_add_strcpy(od, e, "message",+ body ? body : "no answer");+ }+ yyjson_doc_free(md);+ yyjson_doc_free(bd);+ free(body);+ free(msgs);+ } else if (yyjson_obj_get(row, "sample")) {+ yyjson_val *rs = yyjson_obj_get(row, "sample");+ smp = yyjson_val_mut_copy(od, rs);+ }+ /* the graders see the sample as written */+ char *st = yyjson_mut_val_write(smp, 0, NULL);+ sd = st ? yyjson_read(st, strlen(st), 0) : NULL;+ free(st);+ yyjson_mut_val *results = yyjson_mut_arr(od);+ int all_pass = !errored;+ yyjson_val *g;+ size_t gi, gm;+ yyjson_arr_foreach(criteria, gi, gm, g)+ {+ int p = errored+ ? -1+ : grade(od, results, g, item, yyjson_doc_get_root(sd));+ if (p < 0)+ errored = 1;+ if (p != 1)+ all_pass = 0;+ }+ yyjson_doc_free(sd);+ yyjson_mut_obj_add_val(od, oi, "results", results);+ yyjson_mut_obj_add_val(od, oi, "sample", smp);+ yyjson_mut_obj_add_str(od, oi, "status",+ errored ? "error"+ : all_pass ? "pass"+ : "fail");+ size_t ln;+ char *line = yyjson_mut_val_write(oi, 0, &ln);+ int fd = open(op, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0600);+ if (fd >= 0 && line) {+ ssize_t w1 = write(fd, line, ln), w2 = write(fd, "\n", 1);+ (void)w1;+ (void)w2;+ }+ if (fd >= 0)+ close(fd);+ free(line);+ /* the run's counts */+ yyjson_mut_val *rc = yyjson_mut_obj_get(o, "result_counts");+ static const char *const K[] = {"total", "errored", "failed", "passed"};+ int64_t c[4];+ for (int k = 0; k < 4; k++)+ c[k] = yyjson_mut_get_sint(yyjson_mut_obj_get(rc, K[k]));+ c[0]++;+ c[errored ? 1 : all_pass ? 3 : 2]++;+ yyjson_mut_obj_remove_key(o, "result_counts");+ rc = yyjson_mut_obj_add_obj(d, o, "result_counts");+ for (int k = 0; k < 4; k++)+ yyjson_mut_obj_add_int(d, rc, K[k], c[k]);+ /* per grader: its passes and fails */+ yyjson_mut_val *pt =+ yyjson_mut_obj_get(o, "per_testing_criteria_results");+ size_t ri;+ yyjson_mut_val *rv;+ size_t rmax;+ yyjson_mut_arr_foreach(results, ri, rmax, rv)+ {+ yyjson_mut_val *slot = yyjson_mut_arr_get(pt, ri);+ if (!slot || ri >= nc)+ continue;+ const char *key =+ yyjson_mut_get_bool(yyjson_mut_obj_get(rv, "passed"))+ ? "passed"+ : "failed";+ int64_t v = yyjson_mut_get_sint(yyjson_mut_obj_get(slot, key));+ yyjson_mut_obj_remove_key(slot, key);+ yyjson_mut_obj_add_int(d, slot, key, v + 1);+ }+ yyjson_mut_doc_free(od);+ char nx[32];+ snprintf(nx, sizeof nx, "%zu", i + 1);+ pthread_mutex_lock(&mu);+ char *cur = store_get(RUN, run_id, NULL);+ int was_cancelled = cur && strstr(cur, "\"status\":\"canceled\"");+ free(cur);+ if (was_cancelled) {+ yyjson_mut_obj_remove_key(o, "status");+ yyjson_mut_obj_add_str(d, o, "status", "canceled");+ }+ save(RUN, run_id, o, nx, 0);+ pthread_mutex_unlock(&mu);+ if (was_cancelled)+ break;+ }+ pthread_mutex_lock(&mu);+ char *cur = store_get(RUN, run_id, NULL);+ int was_cancelled = cur && strstr(cur, "\"status\":\"canceled\"");+ free(cur);+ if (!stopping && !was_cancelled) {+ yyjson_mut_obj_remove_key(o, "status");+ yyjson_mut_obj_add_str(d, o, "status", "completed");+ char nx[32];+ snprintf(nx, sizeof nx, "%zu", total);+ save(RUN, run_id, o, nx, 0);+ }+ pthread_mutex_unlock(&mu);+ yyjson_doc_free(rd);+ yyjson_doc_free(ed);+ yyjson_doc_free(rowsd);+ yyjson_mut_doc_free(d);+}++/* The oldest run with work left: 1 with its id. */+static int next_run(char *id, size_t cap)+{+ char **ids;+ size_t n = srv_store_list(server->store, RUN, &ids);+ int found = 0;+ for (size_t i = 0; i < n && !found; i++) {+ char *t = store_get(RUN, ids[i], NULL);+ if (t && (strstr(t, "\"status\":\"queued\"") ||+ strstr(t, "\"status\":\"in_progress\""))) {+ snprintf(id, cap, "%s", ids[i]);+ found = 1;+ }+ free(t);+ }+ srv_store_list_free(ids, n);+ return found;+}++static void *worker_main(void *arg)+{+ (void)arg;+ srv_engine_low = 1;+ char id[64];+ for (;;) {+ pthread_mutex_lock(&mu);+ while (!stopping && !next_run(id, sizeof id))+ pthread_cond_wait(&cv, &mu);+ int stop = stopping;+ pthread_mutex_unlock(&mu);+ if (stop)+ break;+ run_one(id);+ }+ return NULL;+}++int srv_evals_start(struct srv *s)+{+ server = s;+ snprintf(work, sizeof work, "%s/.evals", srv_files_dir(s->files));+ if (mkdir(work, 0700) != 0 && errno != EEXIST)+ return -1;+ stopping = 0;+ if (pthread_create(&worker, NULL, worker_main, NULL) != 0)+ return -1;+ running = 1;+ return 0;+}++void srv_evals_stop(void)+{+ if (!running)+ return;+ pthread_mutex_lock(&mu);+ stopping = 1;+ pthread_cond_broadcast(&cv);+ pthread_mutex_unlock(&mu);+ pthread_join(worker, NULL);+ running = 0;+}++/* ---- the API: evals ---- */++static const char *grader_error(yyjson_val *g)+{+ const char *type = yyjson_get_str(yyjson_obj_get(g, "type"));+ if (!type || !yyjson_get_str(yyjson_obj_get(g, "name")))+ return "Every grader has a type and a name.";+ if (!strcmp(type, "string_check")) {+ const char *op = yyjson_get_str(yyjson_obj_get(g, "operation"));+ if (!yyjson_get_str(yyjson_obj_get(g, "input")) ||+ !yyjson_get_str(yyjson_obj_get(g, "reference")) || !op ||+ (strcmp(op, "eq") && strcmp(op, "ne") && strcmp(op, "like") &&+ strcmp(op, "ilike")))+ return "A string_check grader has input, reference and an "+ "operation of eq, ne, like or ilike.";+ return NULL;+ }+ if (!strcmp(type, "text_similarity"))+ return yyjson_get_str(yyjson_obj_get(g, "input")) &&+ yyjson_get_str(yyjson_obj_get(g, "reference")) &&+ srv_eval_metric_ok(yyjson_get_str(+ yyjson_obj_get(g, "evaluation_metric")))+ ? NULL+ : "A text_similarity grader has input, reference and an "+ "evaluation_metric.";+ if (!strcmp(type, "label_model"))+ return yyjson_is_arr(yyjson_obj_get(g, "input")) &&+ yyjson_arr_size(yyjson_obj_get(g, "labels")) &&+ yyjson_is_arr(yyjson_obj_get(g, "passing_labels"))+ ? NULL+ : "A label_model grader has input messages, labels and "+ "passing_labels.";+ if (!strcmp(type, "score_model"))+ return yyjson_is_arr(yyjson_obj_get(g, "input"))+ ? NULL+ : "A score_model grader has input messages.";+ if (!strcmp(type, "python"))+ return "python graders are not run here.";+ return "A grader is string_check, text_similarity, label_model or "+ "score_model.";+}++int srv_evals_create(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_val *dsc = yyjson_obj_get(root, "data_source_config");+ yyjson_val *tc = yyjson_obj_get(root, "testing_criteria");+ yyjson_val *md = yyjson_obj_get(root, "metadata");+ const char *dtype = yyjson_get_str(yyjson_obj_get(dsc, "type"));+ const char *why = NULL;+ if (!yyjson_is_obj(root))+ why = "The body is a JSON object.";+ else if (!dtype || (strcmp(dtype, "custom") && strcmp(dtype, "logs") &&+ strcmp(dtype, "stored_completions")))+ why = "'data_source_config' is custom, logs or stored_completions.";+ else if (!strcmp(dtype, "custom") &&+ !yyjson_is_obj(yyjson_obj_get(dsc, "item_schema")))+ why = "A custom data source config has an item_schema.";+ else if (!yyjson_is_arr(tc) || !yyjson_arr_size(tc))+ why = "'testing_criteria' is a list of graders.";+ else if (!metadata_ok(md))+ why = "'metadata' holds at most 16 strings.";+ size_t i, n;+ yyjson_val *g;+ yyjson_arr_foreach(tc, i, n, g) if (!why) why = grader_error(g);+ if (why) {+ yyjson_doc_free(doc);+ return bad(r, why);+ }+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ char id[48];+ srv_new_id(id, sizeof id, "eval_");+ yyjson_mut_obj_add_str(d, o, "object", "eval");+ yyjson_mut_obj_add_strcpy(d, o, "id", id);+ const char *name = yyjson_get_str(yyjson_obj_get(root, "name"));+ yyjson_mut_obj_add_strcpy(d, o, "name", name ? name : "");+ /* the config as OpenAI gives it back: its schema of item and sample */+ yyjson_mut_val *c = yyjson_mut_obj_add_obj(d, o, "data_source_config");+ yyjson_mut_obj_add_strcpy(d, c, "type", dtype);+ yyjson_mut_val *sch = yyjson_mut_obj_add_obj(d, c, "schema");+ yyjson_mut_obj_add_str(d, sch, "type", "object");+ yyjson_mut_val *props = yyjson_mut_obj_add_obj(d, sch, "properties");+ yyjson_val *is = yyjson_obj_get(dsc, "item_schema");+ yyjson_mut_obj_add_val(d, props, "item",+ is ? yyjson_val_mut_copy(d, is) : yyjson_mut_obj(d));+ yyjson_mut_val *req = yyjson_mut_obj_add_arr(d, sch, "required");+ yyjson_mut_arr_add_str(d, req, "item");+ if (yyjson_get_bool(yyjson_obj_get(dsc, "include_sample_schema"))) {+ yyjson_mut_val *s = yyjson_mut_obj_add_obj(d, props, "sample");+ yyjson_mut_obj_add_str(d, s, "type", "object");+ yyjson_mut_arr_add_str(d, req, "sample");+ }+ yyjson_mut_obj_add_val(d, o, "testing_criteria",+ yyjson_val_mut_copy(d, tc));+ yyjson_mut_obj_add_int(d, o, "created_at", (int64_t)time(NULL));+ yyjson_mut_obj_add_val(d, o, "metadata",+ yyjson_is_obj(md) ? yyjson_val_mut_copy(d, md)+ : yyjson_mut_obj(d));+ yyjson_doc_free(doc);+ int rc = save(EVAL, id, o, NULL, 1);+ char *out = rc == 0 ? yyjson_mut_val_write(o, 0, NULL) : NULL;+ yyjson_mut_doc_free(d);+ if (!out)+ return srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the eval");+ return reply_text(r, out);+}++int srv_evals_get(struct srv_req *r)+{+ char *t = store_get(EVAL, r->param[0], NULL);+ return t ? reply_text(r, t) : not_found(r, "eval", r->param[0]);+}++int srv_evals_update(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = load(d, EVAL, r->param[0]);+ yyjson_val *name = yyjson_obj_get(root, "name");+ yyjson_val *md = yyjson_obj_get(root, "metadata");+ int ret;+ if (!o) {+ ret = not_found(r, "eval", r->param[0]);+ } else if (!yyjson_is_obj(root) || !metadata_ok(md)) {+ ret = bad(r, "The body is {name, metadata}, metadata at most 16 "+ "strings.");+ } else {+ if (yyjson_is_str(name)) {+ yyjson_mut_obj_remove_key(o, "name");+ yyjson_mut_obj_add_strcpy(d, o, "name", yyjson_get_str(name));+ }+ if (md) {+ yyjson_mut_obj_remove_key(o, "metadata");+ yyjson_mut_obj_add_val(d, o, "metadata",+ yyjson_is_obj(md)+ ? yyjson_val_mut_copy(d, md)+ : yyjson_mut_obj(d));+ }+ ret = save(EVAL, r->param[0], o, NULL, 0) == 0+ ? reply_text(r, yyjson_mut_val_write(o, 0, NULL))+ : srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the eval");+ }+ yyjson_mut_doc_free(d);+ yyjson_doc_free(doc);+ return ret;+}++int srv_evals_delete(struct srv_req *r)+{+ if (srv_store_del(r->srv->store, EVAL, r->param[0]) != 0)+ return not_found(r, "eval", r->param[0]);+ char out[512];+ snprintf(+ out, sizeof out,+ "{\"object\":\"eval.deleted\",\"deleted\":true,\"eval_id\":\"%s\"}",+ r->param[0]);+ return reply_text(r, strdup(out));+}++/* A page of documents of kind (those of eval_id for runs), filtered by a+ key's value; asc by default as OpenAI lists evals. */+static int list_page(struct srv_req *r, const char *kind, const char *eval_id,+ const char *status)+{+ const char *l = srv_query(r, "limit"), *o = srv_query(r, "order");+ const char *after = srv_query(r, "after");+ long limit = 20;+ if (l) {+ char *end;+ limit = strtol(l, &end, 10);+ if (*end || limit < 1 || limit > 100)+ return bad(r, "'limit' is a number from 1 to 100.");+ }+ int desc = o && !strcmp(o, "desc");+ char **ids;+ size_t n = srv_store_list(r->srv->store, kind, &ids);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *root = yyjson_mut_obj(d), *data = yyjson_mut_arr(d);+ long shown = 0;+ int more = 0, past = after == NULL;+ char first[64] = "", last[64] = "";+ for (size_t k = 0; k < n; k++) {+ const char *id = ids[desc ? n - 1 - k : k];+ char *t = store_get(kind, id, NULL);+ yyjson_doc *x = t ? yyjson_read(t, strlen(t), 0) : NULL;+ free(t);+ yyjson_val *v = yyjson_doc_get_root(x);+ int keep =+ v &&+ (!eval_id ||+ yyjson_equals_str(yyjson_obj_get(v, "eval_id"), eval_id)) &&+ (!status || yyjson_equals_str(yyjson_obj_get(v, "status"), status));+ if (keep && !past) {+ past = !strcmp(id, after);+ keep = 0;+ }+ if (keep && shown == limit) {+ more = 1;+ yyjson_doc_free(x);+ break;+ }+ if (keep) {+ yyjson_mut_arr_append(data, yyjson_val_mut_copy(d, v));+ if (!shown)+ snprintf(first, sizeof first, "%s", id);+ snprintf(last, sizeof last, "%s", id);+ shown++;+ }+ yyjson_doc_free(x);+ }+ srv_store_list_free(ids, n);+ yyjson_mut_obj_add_str(d, root, "object", "list");+ yyjson_mut_obj_add_val(d, root, "data", data);+ if (shown) {+ yyjson_mut_obj_add_strcpy(d, root, "first_id", first);+ yyjson_mut_obj_add_strcpy(d, root, "last_id", last);+ } else {+ yyjson_mut_obj_add_null(d, root, "first_id");+ yyjson_mut_obj_add_null(d, root, "last_id");+ }+ yyjson_mut_obj_add_bool(d, root, "has_more", more);+ char *out = yyjson_mut_val_write(root, 0, NULL);+ yyjson_mut_doc_free(d);+ return reply_text(r, out);+}++int srv_evals_list(struct srv_req *r)+{+ return list_page(r, EVAL, NULL, NULL);+}++/* ---- the API: runs ---- */++int srv_eval_runs_create(struct srv_req *r)+{+ char *et = store_get(EVAL, r->param[0], NULL);+ yyjson_doc *ed = et ? yyjson_read(et, strlen(et), 0) : NULL;+ free(et);+ if (!ed)+ return not_found(r, "eval", r->param[0]);+ yyjson_val *ev = yyjson_doc_get_root(ed);+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_val *ds = yyjson_obj_get(root, "data_source");+ yyjson_val *md = yyjson_obj_get(root, "metadata");+ const char *dtype = yyjson_get_str(yyjson_obj_get(ds, "type"));+ const char *why = NULL;+ char tmp[200];+ if (!yyjson_is_obj(root) || !dtype)+ why = "'data_source' is jsonl, completions or responses.";+ else if (strcmp(dtype, "jsonl") && strcmp(dtype, "completions") &&+ strcmp(dtype, "responses"))+ why = "'data_source' is jsonl, completions or responses.";+ else if (strcmp(dtype, "jsonl") && !yyjson_obj_get(ds, "input_messages"))+ why = "A completions data source has input_messages (a template or "+ "an item_reference).";+ else if (!metadata_ok(md))+ why = "'metadata' holds at most 16 strings.";+ else {+ char *rows = rows_of(ds, tmp, sizeof tmp);+ if (!rows)+ why = tmp;+ free(rows);+ }+ if (why) {+ yyjson_doc_free(doc);+ yyjson_doc_free(ed);+ return bad(r, why);+ }+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ char id[48];+ srv_new_id(id, sizeof id, "evalrun_");+ yyjson_mut_obj_add_str(d, o, "object", "eval.run");+ yyjson_mut_obj_add_strcpy(d, o, "id", id);+ yyjson_mut_obj_add_strcpy(d, o, "eval_id", r->param[0]);+ yyjson_mut_obj_add_str(d, o, "status", "queued");+ yyjson_mut_obj_add_strcpy(d, o, "model", r->srv->cfg.model_id);+ const char *name = yyjson_get_str(yyjson_obj_get(root, "name"));+ yyjson_mut_obj_add_strcpy(d, o, "name", name ? name : id);+ yyjson_mut_obj_add_int(d, o, "created_at", (int64_t)time(NULL));+ yyjson_mut_obj_add_str(d, o, "report_url", "");+ yyjson_mut_val *rc = yyjson_mut_obj_add_obj(d, o, "result_counts");+ yyjson_mut_obj_add_int(d, rc, "total", 0);+ yyjson_mut_obj_add_int(d, rc, "errored", 0);+ yyjson_mut_obj_add_int(d, rc, "failed", 0);+ yyjson_mut_obj_add_int(d, rc, "passed", 0);+ yyjson_mut_obj_add_arr(d, o, "per_model_usage");+ yyjson_mut_val *pt =+ yyjson_mut_obj_add_arr(d, o, "per_testing_criteria_results");+ size_t i, n;+ yyjson_val *g;+ yyjson_arr_foreach(yyjson_obj_get(ev, "testing_criteria"), i, n, g)+ {+ yyjson_mut_val *s = yyjson_mut_arr_add_obj(d, pt);+ const char *gn = yyjson_get_str(yyjson_obj_get(g, "name"));+ yyjson_mut_obj_add_strcpy(d, s, "testing_criteria", gn ? gn : "");+ yyjson_mut_obj_add_int(d, s, "passed", 0);+ yyjson_mut_obj_add_int(d, s, "failed", 0);+ }+ yyjson_mut_obj_add_val(d, o, "data_source", yyjson_val_mut_copy(d, ds));+ yyjson_mut_obj_add_val(d, o, "metadata",+ yyjson_is_obj(md) ? yyjson_val_mut_copy(d, md)+ : yyjson_mut_obj(d));+ yyjson_mut_obj_add_null(d, o, "error");+ yyjson_doc_free(doc);+ yyjson_doc_free(ed);+ int ok = save(RUN, id, o, "0", 1) == 0;+ char *out = ok ? yyjson_mut_val_write(o, 0, NULL) : NULL;+ yyjson_mut_doc_free(d);+ if (!out)+ return srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the run");+ pthread_mutex_lock(&mu);+ pthread_cond_broadcast(&cv);+ pthread_mutex_unlock(&mu);+ return reply_text(r, out);+}++static yyjson_doc *run_of(struct srv_req *r)+{+ char *t = store_get(RUN, r->param[1], NULL);+ yyjson_doc *x = t ? yyjson_read(t, strlen(t), 0) : NULL;+ free(t);+ if (x &&+ !yyjson_equals_str(yyjson_obj_get(yyjson_doc_get_root(x), "eval_id"),+ r->param[0])) {+ yyjson_doc_free(x);+ x = NULL;+ }+ return x;+}++int srv_eval_runs_get(struct srv_req *r)+{+ yyjson_doc *x = run_of(r);+ if (!x)+ return not_found(r, "run", r->param[1]);+ char *out = yyjson_val_write(yyjson_doc_get_root(x), 0, NULL);+ yyjson_doc_free(x);+ return reply_text(r, out);+}++int srv_eval_runs_list(struct srv_req *r)+{+ if (srv_store_get(r->srv->store, EVAL, r->param[0], NULL, NULL) != 0)+ return not_found(r, "eval", r->param[0]);+ return list_page(r, RUN, r->param[0], srv_query(r, "status"));+}++int srv_eval_runs_cancel(struct srv_req *r)+{+ yyjson_doc *x = run_of(r);+ if (!x)+ return not_found(r, "run", r->param[1]);+ yyjson_mut_doc *d = yyjson_doc_mut_copy(x, NULL);+ yyjson_mut_val *o = yyjson_mut_doc_get_root(d);+ const char *st = yyjson_mut_get_str(yyjson_mut_obj_get(o, "status"));+ if (st && (!strcmp(st, "queued") || !strcmp(st, "in_progress"))) {+ yyjson_mut_obj_remove_key(o, "status");+ yyjson_mut_obj_add_str(d, o, "status", "canceled");+ pthread_mutex_lock(&mu);+ save(RUN, r->param[1], o, NULL, 0);+ pthread_mutex_unlock(&mu);+ }+ char *out = yyjson_mut_val_write(o, 0, NULL);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(x);+ return reply_text(r, out);+}++int srv_eval_runs_delete(struct srv_req *r)+{+ yyjson_doc *x = run_of(r);+ if (!x)+ return not_found(r, "run", r->param[1]);+ yyjson_doc_free(x);+ pthread_mutex_lock(&mu);+ srv_store_del(r->srv->store, RUN, r->param[1]);+ pthread_mutex_unlock(&mu);+ char p[4600];+ if (srv_disk_name_ok(r->param[1])) {+ out_path(p, sizeof p, r->param[1]);+ unlink(p);+ }+ char out[512];+ snprintf(out, sizeof out,+ "{\"object\":\"eval.run.deleted\",\"deleted\":true,"+ "\"run_id\":\"%s\"}",+ r->param[1]);+ return reply_text(r, strdup(out));+}++/* The output items of a run, from its file: those with id (NULL: all,+ paged), filtered by status. */+static int items(struct srv_req *r, const char *id)+{+ yyjson_doc *x = run_of(r);+ if (!x)+ return not_found(r, "run", r->param[1]);+ yyjson_doc_free(x);+ char p[4600];+ if (!srv_disk_name_ok(r->param[1]))+ return not_found(r, "run", r->param[1]);+ out_path(p, sizeof p, r->param[1]);+ FILE *f = fopen(p, "rb");+ const char *status = srv_query(r, "status"), *after = srv_query(r, "after");+ const char *l = srv_query(r, "limit"), *o = srv_query(r, "order");+ long limit = l ? strtol(l, NULL, 10) : 20;+ if (limit < 1 || limit > 100)+ limit = 20;+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *data = yyjson_mut_arr(d);+ char *line = NULL;+ size_t cap = 0;+ ssize_t n;+ yyjson_mut_val *found = NULL;+ while (f && (n = getline(&line, &cap, f)) > 0) {+ yyjson_doc *ld = yyjson_read(line, (size_t)n, 0);+ yyjson_val *v = yyjson_doc_get_root(ld);+ if (v && (!id || yyjson_equals_str(yyjson_obj_get(v, "id"), id)) &&+ (!status || yyjson_equals_str(yyjson_obj_get(v, "status"), status)))+ yyjson_mut_arr_append(data, yyjson_val_mut_copy(d, v));+ if (v && id && yyjson_equals_str(yyjson_obj_get(v, "id"), id))+ found = yyjson_mut_arr_get_last(data);+ yyjson_doc_free(ld);+ }+ free(line);+ if (f)+ fclose(f);+ if (id) {+ int ret = found ? reply_text(r, yyjson_mut_val_write(found, 0, NULL))+ : not_found(r, "output item", id);+ yyjson_mut_doc_free(d);+ return ret;+ }+ /* a page of them */+ size_t total = yyjson_mut_arr_size(data);+ int desc = o && !strcmp(o, "desc");+ yyjson_mut_val *root = yyjson_mut_obj(d), *page = yyjson_mut_arr(d);+ long shown = 0;+ int more = 0, past = after == NULL;+ char first[64] = "", last[64] = "";+ for (size_t k = 0; k < total; k++) {+ yyjson_mut_val *v = yyjson_mut_arr_get(data, desc ? total - 1 - k : k);+ const char *vid = yyjson_mut_get_str(yyjson_mut_obj_get(v, "id"));+ if (!past) {+ past = vid && !strcmp(vid, after);+ continue;+ }+ if (shown == limit) {+ more = 1;+ break;+ }+ yyjson_mut_arr_append(page, yyjson_mut_val_mut_copy(d, v));+ if (!shown)+ snprintf(first, sizeof first, "%s", vid ? vid : "");+ snprintf(last, sizeof last, "%s", vid ? vid : "");+ shown++;+ }+ yyjson_mut_obj_add_str(d, root, "object", "list");+ yyjson_mut_obj_add_val(d, root, "data", page);+ if (shown) {+ yyjson_mut_obj_add_strcpy(d, root, "first_id", first);+ yyjson_mut_obj_add_strcpy(d, root, "last_id", last);+ } else {+ yyjson_mut_obj_add_null(d, root, "first_id");+ yyjson_mut_obj_add_null(d, root, "last_id");+ }+ yyjson_mut_obj_add_bool(d, root, "has_more", more);+ char *out = yyjson_mut_val_write(root, 0, NULL);+ yyjson_mut_doc_free(d);+ return reply_text(r, out);+}++int srv_eval_items_list(struct srv_req *r)+{+ return items(r, NULL);+}++int srv_eval_items_get(struct srv_req *r)+{+ return items(r, r->param[2]);+}diff --git a/src/server/evals_metrics.c b/src/server/evals_metrics.cnew file mode 100644index 0000000..a4e9d69--- /dev/null+++ b/src/server/evals_metrics.c@@ -0,0 +1,267 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * evals_metrics.c - the text similarity metrics of the evals' graders (see+ * evals_metrics.h): words lower-cased and split at what is not a letter or+ * a digit (bytes of UTF-8 characters count as letters), then the usual+ * formulas - fuzzy match as Levenshtein's ratio over characters, BLEU with+ * add-one smoothing past the unigrams, GLEU as Google's sentence one,+ * METEOR on exact unigram matches with its fragmentation penalty, ROUGE-n+ * and ROUGE-L as F1, cosine over word counts.+ */+#include "evals_metrics.h"++#include <ctype.h>+#include <math.h>+#include <stdlib.h>+#include <string.h>++#define MAX_WORDS 4096++struct words {+ char *buf;+ char *w[MAX_WORDS];+ int n;+};++static void split(const char *s, struct words *o)+{+ size_t n = strlen(s);+ o->buf = malloc(n + 1);+ o->n = 0;+ if (!o->buf)+ return;+ size_t k = 0;+ for (size_t i = 0; i <= n && o->n < MAX_WORDS;) {+ while (i < n &&+ !(isalnum((unsigned char)s[i]) || (unsigned char)s[i] >= 0x80))+ i++;+ if (i >= n)+ break;+ o->w[o->n++] = o->buf + k;+ while (i < n &&+ (isalnum((unsigned char)s[i]) || (unsigned char)s[i] >= 0x80))+ o->buf[k++] = (char)tolower((unsigned char)s[i++]);+ o->buf[k++] = 0;+ }+}++static int same_gram(char *const *a, char *const *b, int n)+{+ for (int i = 0; i < n; i++)+ if (strcmp(a[i], b[i]) != 0)+ return 0;+ return 1;+}++/* n-grams of h also in r, clipped by their count in r */+static int clipped(const struct words *h, const struct words *r, int n)+{+ if (h->n < n || r->n < n)+ return 0;+ char *used = calloc((size_t)(r->n - n + 1), 1);+ int m = 0;+ for (int i = 0; used && i + n <= h->n; i++)+ for (int j = 0; j + n <= r->n; j++)+ if (!used[j] && same_gram(h->w + i, r->w + j, n)) {+ used[j] = 1;+ m++;+ break;+ }+ free(used);+ return m;+}++static double f1(double match, double a, double b)+{+ if (match <= 0 || a <= 0 || b <= 0)+ return 0;+ double p = match / a, r = match / b;+ return 2 * p * r / (p + r);+}++static double lcs(const struct words *a, const struct words *b)+{+ int *prev = calloc((size_t)b->n + 1, sizeof(int)),+ *cur = calloc((size_t)b->n + 1, sizeof(int));+ if (!prev || !cur) {+ free(prev);+ free(cur);+ return 0;+ }+ for (int i = 1; i <= a->n; i++) {+ for (int j = 1; j <= b->n; j++)+ cur[j] = !strcmp(a->w[i - 1], b->w[j - 1])+ ? prev[j - 1] + 1+ : (prev[j] > cur[j - 1] ? prev[j] : cur[j - 1]);+ int *t = prev;+ prev = cur;+ cur = t;+ }+ double v = prev[b->n];+ free(prev);+ free(cur);+ return v;+}++static double levenshtein_ratio(const char *a, const char *b)+{+ size_t n = strlen(a), m = strlen(b);+ if (!n && !m)+ return 1;+ if (n > 20000 || m > 20000)+ return a[0] == b[0] && !strcmp(a, b);+ size_t *prev = malloc((m + 1) * sizeof(size_t)),+ *cur = malloc((m + 1) * sizeof(size_t));+ if (!prev || !cur) {+ free(prev);+ free(cur);+ return 0;+ }+ for (size_t j = 0; j <= m; j++)+ prev[j] = j;+ for (size_t i = 1; i <= n; i++) {+ cur[0] = i;+ for (size_t j = 1; j <= m; j++) {+ size_t sub = prev[j - 1] + (a[i - 1] != b[j - 1]);+ size_t del = prev[j] + 1, ins = cur[j - 1] + 1;+ cur[j] =+ sub < del ? (sub < ins ? sub : ins) : (del < ins ? del : ins);+ }+ size_t *t = prev;+ prev = cur;+ cur = t;+ }+ double d = (double)prev[m];+ free(prev);+ free(cur);+ return 1.0 - d / (double)(n > m ? n : m);+}++static double bleu(const struct words *h, const struct words *r)+{+ if (!h->n || !r->n)+ return 0;+ double logp = 0;+ for (int n = 1; n <= 4; n++) {+ double grams = h->n - n + 1;+ double m = clipped(h, r, n);+ if (grams <= 0)+ grams = 0;+ /* add-one past the unigrams, so a short sentence is not 0 */+ double p = n == 1 ? (grams ? m / grams : 0) : (m + 1) / (grams + 1);+ if (p <= 0)+ return 0;+ logp += log(p) / 4;+ }+ double bp = h->n >= r->n ? 1 : exp(1 - (double)r->n / h->n);+ return bp * exp(logp);+}++static double gleu(const struct words *h, const struct words *r)+{+ double match = 0, hg = 0, rg = 0;+ for (int n = 1; n <= 4; n++) {+ match += clipped(h, r, n);+ hg += h->n >= n ? h->n - n + 1 : 0;+ rg += r->n >= n ? r->n - n + 1 : 0;+ }+ if (!hg || !rg)+ return 0;+ double p = match / hg, rc = match / rg;+ return p < rc ? p : rc;+}++static double meteor(const struct words *h, const struct words *r)+{+ double m = clipped(h, r, 1);+ if (m <= 0)+ return 0;+ double p = m / h->n, rc = m / r->n;+ double fmean = 10 * p * rc / (rc + 9 * p);+ /* chunks: runs of matched words in the same order in both */+ int chunks = 0, in = 0;+ for (int i = 0; i < h->n; i++) {+ int hit = 0;+ for (int j = 0; j < r->n && !hit; j++)+ hit = !strcmp(h->w[i], r->w[j]);+ if (hit && !in)+ chunks++;+ in = hit;+ }+ double pen = 0.5 * pow(chunks / m, 3);+ return fmean * (1 - pen);+}++static double cosine_words(const struct words *a, const struct words *b)+{+ double dot = 0, na = 0, nb = 0;+ for (int i = 0; i < a->n; i++) {+ int seen = 0;+ for (int k = 0; k < i && !seen; k++)+ seen = !strcmp(a->w[k], a->w[i]);+ if (seen)+ continue;+ double ca = 0, cb = 0;+ for (int k = 0; k < a->n; k++)+ ca += !strcmp(a->w[k], a->w[i]);+ for (int k = 0; k < b->n; k++)+ cb += !strcmp(b->w[k], a->w[i]);+ dot += ca * cb;+ na += ca * ca;+ }+ for (int i = 0; i < b->n; i++) {+ int seen = 0;+ for (int k = 0; k < i && !seen; k++)+ seen = !strcmp(b->w[k], b->w[i]);+ if (seen)+ continue;+ double cb = 0;+ for (int k = 0; k < b->n; k++)+ cb += !strcmp(b->w[k], b->w[i]);+ nb += cb * cb;+ }+ return na > 0 && nb > 0 ? dot / sqrt(na * nb) : 0;+}++double srv_eval_similarity(const char *metric, const char *input,+ const char *reference)+{+ if (!strcmp(metric, "fuzzy_match"))+ return levenshtein_ratio(input, reference);+ struct words h, r;+ split(input, &h);+ split(reference, &r);+ double v = 0;+ if (!h.buf || !r.buf)+ v = 0;+ else if (!strcmp(metric, "bleu"))+ v = bleu(&h, &r);+ else if (!strcmp(metric, "gleu"))+ v = gleu(&h, &r);+ else if (!strcmp(metric, "meteor"))+ v = meteor(&h, &r);+ else if (!strcmp(metric, "rouge_l"))+ v = f1(lcs(&h, &r), h.n, r.n);+ else if (!strncmp(metric, "rouge_", 6) && metric[6] >= '1' &&+ metric[6] <= '5' && !metric[7]) {+ int n = metric[6] - '0';+ v = f1(clipped(&h, &r, n), h.n >= n ? h.n - n + 1 : 0,+ r.n >= n ? r.n - n + 1 : 0);+ } else if (!strcmp(metric, "cosine"))+ v = cosine_words(&h, &r);+ free(h.buf);+ free(r.buf);+ return v < 0 ? 0 : v > 1 ? 1 : v;+}++int srv_eval_metric_ok(const char *metric)+{+ static const char *const M[] = {+ "cosine", "fuzzy_match", "bleu", "gleu", "meteor", "rouge_1",+ "rouge_2", "rouge_3", "rouge_4", "rouge_5", "rouge_l"};+ for (size_t i = 0; metric && i < sizeof(M) / sizeof(M[0]); i++)+ if (!strcmp(metric, M[i]))+ return 1;+ return 0;+}diff --git a/src/server/evals_metrics.h b/src/server/evals_metrics.hnew file mode 100644index 0000000..ea76c31--- /dev/null+++ b/src/server/evals_metrics.h@@ -0,0 +1,17 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * evals_metrics.h - the text similarity metrics of the evals' graders+ * (evals_metrics.c).+ */+#ifndef JANAS_SERVER_EVALS_METRICS_H+#define JANAS_SERVER_EVALS_METRICS_H++/* The score, 0 to 1, of input against reference by metric: cosine,+ fuzzy_match, bleu, gleu, meteor, rouge_1 to rouge_5, rouge_l. */+double srv_eval_similarity(const char *metric, const char *input,+ const char *reference);+/* 1 when metric is one of them. */+int srv_eval_metric_ok(const char *metric);++#endifdiff --git a/src/server/files.c b/src/server/files.cnew file mode 100644index 0000000..7d20935--- /dev/null+++ b/src/server/files.c@@ -0,0 +1,197 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * files.c - OpenAI's Files API: a file uploaded (multipart/form-data, as+ * the SDKs send it), listed, read, its bytes downloaded, deleted. The files+ * themselves are files_store.c's; what a batch or a vector store does with+ * them comes later.+ */+#include "server.h"++#include <microhttpd.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <yyjson.h>++#include "files.h"+#include "files_store.h"+#include "multipart.h"++static const char *const PURPOSES[] = {"assistants", "batch", "fine-tune",+ "vision", "user_data", "evals"};++static int purpose_ok(const char *p)+{+ for (size_t i = 0; i < sizeof(PURPOSES) / sizeof(PURPOSES[0]); i++)+ if (strcmp(p, PURPOSES[i]) == 0)+ return 1;+ return 0;+}++yyjson_mut_val *srv_file_json(yyjson_mut_doc *d, const struct srv_file *x)+{+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, o, "id", x->id);+ yyjson_mut_obj_add_str(d, o, "object", "file");+ yyjson_mut_obj_add_uint(d, o, "bytes", x->bytes);+ yyjson_mut_obj_add_int(d, o, "created_at", x->created_at);+ if (x->expires_at)+ yyjson_mut_obj_add_int(d, o, "expires_at", x->expires_at);+ else+ yyjson_mut_obj_add_null(d, o, "expires_at");+ yyjson_mut_obj_add_strcpy(d, o, "filename", x->filename);+ yyjson_mut_obj_add_strcpy(d, o, "purpose", x->purpose);+ /* deprecated at OpenAI, still read by older clients */+ yyjson_mut_obj_add_str(d, o, "status", "processed");+ yyjson_mut_obj_add_null(d, o, "status_details");+ return o;+}++static int reply_doc(struct srv_req *r, yyjson_mut_doc *d, yyjson_mut_val *o)+{+ yyjson_mut_doc_set_root(d, o);+ size_t n;+ char *out = yyjson_mut_write(d, 0, &n);+ yyjson_mut_doc_free(d);+ return srv_reply_json(r, 200, out, n);+}++static int bad(struct srv_req *r, const char *msg)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_value",+ "%s", msg);+}++static int no_file(struct srv_req *r)+{+ return srv_reply_error(r, 404, "invalid_request_error", "not_found",+ "No such File object: %s", r->param[0]);+}++/* The text of a form field, NUL-terminated into buf: 0, or -1. */+static int field(const struct srv_part *p, char *buf, size_t cap)+{+ if (!p || p->n >= cap || p->has_filename)+ return -1;+ memcpy(buf, p->data, p->n);+ buf[p->n] = 0;+ return 0;+}++int srv_files_create(struct srv_req *r)+{+ struct srv_part parts[8];+ char err[200];+ const char *ct =+ MHD_lookup_connection_value(r->conn, MHD_HEADER_KIND, "Content-Type");+ int n =+ srv_multipart_parse(ct, r->body, r->n_body, parts, 8, err, sizeof err);+ if (n < 0)+ return bad(r, err);+ const struct srv_part *file = srv_multipart_get(parts, n, "file");+ char purpose[32], anchor[32] = "", secs[32] = "";+ if (!file || !file->has_filename)+ return bad(r, "'file' is required: the file's bytes, with a name.");+ if (field(srv_multipart_get(parts, n, "purpose"), purpose,+ sizeof purpose) != 0 ||+ !purpose_ok(purpose))+ return bad(r, "'purpose' is required: assistants, batch, fine-tune, "+ "vision, user_data or evals.");+ const struct srv_part *pa =+ srv_multipart_get(parts, n, "expires_after[anchor]");+ const struct srv_part *ps =+ srv_multipart_get(parts, n, "expires_after[seconds]");+ long long after = 0;+ if (pa || ps) {+ char *end = NULL;+ if (field(pa, anchor, sizeof anchor) != 0 ||+ strcmp(anchor, "created_at") != 0 ||+ field(ps, secs, sizeof secs) != 0 ||+ (after = strtoll(secs, &end, 10), *end) || after < 3600 ||+ after > 2592000)+ return bad(r, "'expires_after' is anchor created_at and seconds "+ "from 3600 to 2592000.");+ } else if (strcmp(purpose, "batch") == 0) {+ after = 2592000; /* as OpenAI: a batch's files go after 30 days */+ }+ struct srv_file x;+ int rc = srv_files_add(r->srv->files, file->filename, purpose, file->data,+ file->n, after, &x, err, sizeof err);+ if (rc == -2)+ return srv_reply_error(r, 413, "invalid_request_error",+ "files_limit_reached", "%s", err);+ if (rc != 0)+ return srv_reply_error(r, 500, "server_error", NULL, "%s", err);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ return reply_doc(r, d, srv_file_json(d, &x));+}++int srv_files_list_req(struct srv_req *r)+{+ const char *purpose = srv_query(r, "purpose"),+ *after = srv_query(r, "after");+ const char *l = srv_query(r, "limit"), *o = srv_query(r, "order");+ long limit = 10000;+ if (l) {+ char *end;+ limit = strtol(l, &end, 10);+ if (*end || limit < 1 || limit > 10000)+ return bad(r, "'limit' is a number from 1 to 10000.");+ }+ if (o && strcmp(o, "asc") != 0 && strcmp(o, "desc") != 0)+ return bad(r, "'order' is asc or desc.");+ struct srv_file *all;+ size_t n =+ srv_files_list(r->srv->files, purpose && *purpose ? purpose : NULL,+ o && !strcmp(o, "asc"), after, &all);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *root = yyjson_mut_obj(d), *data = yyjson_mut_arr(d);+ size_t shown = n < (size_t)limit ? n : (size_t)limit;+ for (size_t i = 0; i < shown; i++)+ yyjson_mut_arr_append(data, srv_file_json(d, &all[i]));+ yyjson_mut_obj_add_str(d, root, "object", "list");+ yyjson_mut_obj_add_val(d, root, "data", data);+ if (shown) {+ yyjson_mut_obj_add_strcpy(d, root, "first_id", all[0].id);+ yyjson_mut_obj_add_strcpy(d, root, "last_id", all[shown - 1].id);+ } else {+ yyjson_mut_obj_add_null(d, root, "first_id");+ yyjson_mut_obj_add_null(d, root, "last_id");+ }+ yyjson_mut_obj_add_bool(d, root, "has_more", n > shown);+ free(all);+ return reply_doc(r, d, root);+}++int srv_files_retrieve(struct srv_req *r)+{+ struct srv_file x;+ if (srv_files_get(r->srv->files, r->param[0], &x) != 0)+ return no_file(r);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ return reply_doc(r, d, srv_file_json(d, &x));+}++int srv_files_delete_req(struct srv_req *r)+{+ if (srv_files_delete(r->srv->files, r->param[0]) != 0)+ return no_file(r);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, o, "id", r->param[0]);+ yyjson_mut_obj_add_str(d, o, "object", "file");+ yyjson_mut_obj_add_bool(d, o, "deleted", true);+ return reply_doc(r, d, o);+}++int srv_files_content(struct srv_req *r)+{+ struct srv_file x;+ if (srv_files_get(r->srv->files, r->param[0], &x) != 0)+ return no_file(r);+ int fd = srv_files_open_content(r->srv->files, r->param[0]);+ if (fd < 0)+ return no_file(r);+ return srv_reply_fd(r, fd, x.bytes, "application/octet-stream");+}diff --git a/src/server/files.h b/src/server/files.hnew file mode 100644index 0000000..8ef4f5d--- /dev/null+++ b/src/server/files.h@@ -0,0 +1,17 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * files.h - a file as OpenAI's API writes it (files.c), for the APIs that+ * hand files back: uploads, batches.+ */+#ifndef JANAS_SERVER_FILES_H+#define JANAS_SERVER_FILES_H++#include <yyjson.h>++#include "files_store.h"++/* The File object of x, made in d. */+yyjson_mut_val *srv_file_json(yyjson_mut_doc *d, const struct srv_file *x);++#endifdiff --git a/src/server/files_store.c b/src/server/files_store.cnew file mode 100644index 0000000..5d34723--- /dev/null+++ b/src/server/files_store.c@@ -0,0 +1,469 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * files_store.c - the files clients upload (see files_store.h). On disk,+ * <id> holds the bytes and <id>.meta one line of what is said of them:+ * created_at expires_at bytes purpose, then the name to the end of the+ * line. Both are written aside and renamed, the bytes first, so a file a+ * crash interrupted is either whole or not there. The index is in memory+ * and read back from the .meta files when the server starts.+ */+#define _GNU_SOURCE+#include "files_store.h"++#include <dirent.h>+#include <errno.h>+#include <fcntl.h>+#include <inttypes.h>+#include <pthread.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <strings.h>+#include <sys/stat.h>+#include <sys/statvfs.h>+#include <time.h>+#include <unistd.h>+#ifdef _WIN32+#include <io.h> /* _commit */+#endif++#include "common/md5.h"+#include "server.h" /* srv_new_id */+#include "store_disk.h" /* srv_disk_name_ok */++struct srv_files {+ pthread_mutex_t mu;+ char dir[4096];+ int temp; /* made for this run: removed at the end */+ struct srv_file *e;+ size_t n, cap;+ uint64_t total;+};++static int path_of(const struct srv_files *f, const char *id, const char *suf,+ char *out, size_t cap)+{+ if (!srv_disk_name_ok(id))+ return -1;+ int n = snprintf(out, cap, "%s/%s%s", f->dir, id, suf);+ return n > 0 && (size_t)n < cap ? 0 : -1;+}++static int keep(struct srv_files *f, const struct srv_file *x)+{+ if (f->n == f->cap) {+ size_t cap = f->cap ? 2 * f->cap : 64;+ struct srv_file *t = realloc(f->e, cap * sizeof(*t));+ if (!t)+ return -1;+ f->e = t;+ f->cap = cap;+ }+ f->e[f->n++] = *x;+ f->total += x->bytes;+ return 0;+}++static void forget(struct srv_files *f, size_t i)+{+ char p[4200];+ if (path_of(f, f->e[i].id, ".meta", p, sizeof p) == 0)+ unlink(p);+ if (path_of(f, f->e[i].id, "", p, sizeof p) == 0)+ unlink(p);+ f->total -= f->e[i].bytes;+ memmove(&f->e[i], &f->e[i + 1], (f->n - i - 1) * sizeof(f->e[0]));+ f->n--;+}++/* The files past their time go, under f->mu. */+static void expire(struct srv_files *f)+{+ int64_t now = (int64_t)time(NULL);+ for (size_t i = f->n; i-- > 0;)+ if (f->e[i].expires_at && f->e[i].expires_at <= now)+ forget(f, i);+}++static int by_created(const void *a, const void *b)+{+ const struct srv_file *x = a, *y = b;+ if (x->created_at != y->created_at)+ return x->created_at < y->created_at ? -1 : 1;+ return strcmp(x->id, y->id);+}++static void load(struct srv_files *f)+{+ DIR *d = opendir(f->dir);+ struct dirent *de;+ while (d && (de = readdir(d))) {+ size_t n = strlen(de->d_name);+ char p[4200];+ if (de->d_name[0] == '.') { /* a write that did not finish */+ if (n > 4 && !strcmp(de->d_name + n - 4, ".tmp") &&+ snprintf(p, sizeof p, "%s/%s", f->dir, de->d_name) <+ (int)sizeof p)+ unlink(p);+ continue;+ }+ if (n <= 5 || strcmp(de->d_name + n - 5, ".meta") != 0 || n - 5 >= 48)+ continue;+ struct srv_file x = {0};+ memcpy(x.id, de->d_name, n - 5);+ struct stat st;+ char line[512];+ FILE *m =+ path_of(f, x.id, ".meta", p, sizeof p) == 0 ? fopen(p, "r") : NULL;+ int ok = m && fgets(line, sizeof line, m) &&+ sscanf(line, "%" SCNd64 " %" SCNd64 " %" SCNu64 " %31s",+ &x.created_at, &x.expires_at, &x.bytes, x.purpose) == 4;+ if (m)+ fclose(m);+ char *name = ok ? strchr(line, '\t') : NULL;+ ok = ok && name && path_of(f, x.id, "", p, sizeof p) == 0 &&+ stat(p, &st) == 0 && (uint64_t)st.st_size == x.bytes;+ if (!ok)+ continue; /* left as it is */+ name++;+ name[strcspn(name, "\n")] = 0;+ snprintf(x.filename, sizeof x.filename, "%s", name);+ keep(f, &x);+ }+ if (d)+ closedir(d);+ if (f->n)+ qsort(f->e, f->n, sizeof(f->e[0]), by_created);+ expire(f);+}++struct srv_files *srv_files_open(const char *store_dir, char *err,+ size_t err_len)+{+ struct srv_files *f = calloc(1, sizeof(*f));+ if (!f) {+ snprintf(err, err_len, "out of memory");+ return NULL;+ }+ pthread_mutex_init(&f->mu, NULL);+ if (store_dir) {+ snprintf(f->dir, sizeof f->dir, "%s/.files", store_dir);+ if (mkdir(f->dir, 0700) != 0 && errno != EEXIST) {+ snprintf(err, err_len, "cannot make %s: %s", f->dir,+ strerror(errno));+ srv_files_close(f);+ return NULL;+ }+ } else {+ const char *t = getenv("TMPDIR");+ snprintf(f->dir, sizeof f->dir, "%s/janas-server-files-XXXXXX",+ t && *t ? t : "/tmp");+ if (!mkdtemp(f->dir)) {+ snprintf(err, err_len, "cannot make a directory for files: %s",+ strerror(errno));+ f->dir[0] = 0;+ srv_files_close(f);+ return NULL;+ }+ f->temp = 1;+ }+ load(f);+ return f;+}++void srv_files_close(struct srv_files *f)+{+ if (!f)+ return;+ if (f->temp) { /* this run's alone: what is in it goes with it */+ while (f->n)+ forget(f, f->n - 1);+ rmdir(f->dir);+ }+ free(f->e);+ pthread_mutex_destroy(&f->mu);+ free(f);+}++/* buf's n bytes into path through a file aside, renamed: 0, or -1. */+static int write_aside(const struct srv_files *f, const char *id,+ const char *suf, const void *buf, size_t n)+{+ char tmp[4200], dst[4200];+ if (path_of(f, id, suf, dst, sizeof dst) != 0 ||+ snprintf(tmp, sizeof tmp, "%s/.%s%s.tmp", f->dir, id, suf) >=+ (int)sizeof tmp)+ return -1;+ int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);+ if (fd < 0)+ return -1;+ const char *p = buf;+ size_t done = 0;+ while (done < n) {+ ssize_t w = write(fd, p + done, n - done);+ if (w <= 0) {+ close(fd);+ unlink(tmp);+ return -1;+ }+ done += (size_t)w;+ }+#ifdef _WIN32+ int synced = _commit(fd);+#else+ int synced = fsync(fd);+#endif+ if (synced != 0 || close(fd) != 0 || rename(tmp, dst) != 0) {+ unlink(tmp);+ return -1;+ }+ return 0;+}++/* The name shown: the last part of what the client said, no control+ characters. */+static void shown_name(char *out, size_t cap, const char *filename)+{+ const char *base = filename ? filename : "";+ for (const char *s = base; *s; s++)+ if (*s == '/' || *s == '\\')+ base = s + 1;+ size_t w = 0;+ for (const char *s = base; *s && w + 1 < cap; s++)+ if ((unsigned char)*s >= 0x20 && *s != 0x7f)+ out[w++] = *s;+ out[w] = 0;+}++int srv_files_add(struct srv_files *f, const char *filename,+ const char *purpose, const void *data, size_t n,+ int64_t expires_after, struct srv_file *out, char *err,+ size_t err_len)+{+ struct srv_file x = {0};+ srv_new_id(x.id, sizeof x.id, "file-");+ shown_name(x.filename, sizeof x.filename, filename);+ snprintf(x.purpose, sizeof x.purpose, "%s", purpose);+ x.bytes = n;+ x.created_at = (int64_t)time(NULL);+ x.expires_at = expires_after > 0 ? x.created_at + expires_after : 0;+ pthread_mutex_lock(&f->mu);+ expire(f);+ struct statvfs vs;+ uint64_t avail = statvfs(f->dir, &vs) == 0+ ? (uint64_t)vs.f_bavail * (uint64_t)vs.f_frsize+ : UINT64_MAX;+ int rc = 0;+ if (f->total + n > SRV_FILES_MAX) {+ snprintf(err, err_len,+ "the files kept would pass %" PRIu64 " GiB: delete some",+ SRV_FILES_MAX >> 30);+ rc = -2;+ } else if (avail < n + SRV_FILES_DISK_FREE) {+ snprintf(err, err_len, "not enough disk for it");+ rc = -2;+ } else {+ char meta[512];+ int mn = snprintf(+ meta, sizeof meta, "%" PRId64 " %" PRId64 " %" PRIu64 " %s\t%s\n",+ x.created_at, x.expires_at, x.bytes, x.purpose, x.filename);+ if (mn <= 0 || (size_t)mn >= sizeof meta ||+ write_aside(f, x.id, "", data, n) != 0 ||+ write_aside(f, x.id, ".meta", meta, (size_t)mn) != 0 ||+ keep(f, &x) != 0) {+ char p[4200];+ if (path_of(f, x.id, "", p, sizeof p) == 0)+ unlink(p);+ if (path_of(f, x.id, ".meta", p, sizeof p) == 0)+ unlink(p);+ snprintf(err, err_len, "cannot write the file");+ rc = -1;+ }+ }+ pthread_mutex_unlock(&f->mu);+ if (rc == 0)+ *out = x;+ return rc;+}++const char *srv_files_dir(const struct srv_files *f)+{+ return f->dir;+}++int srv_files_add_parts(struct srv_files *f, const char *filename,+ const char *purpose, const char *const *paths, size_t n,+ uint64_t bytes, const char *md5, int64_t expires_after,+ struct srv_file *out, char *err, size_t err_len)+{+ struct srv_file x = {0};+ srv_new_id(x.id, sizeof x.id, "file-");+ shown_name(x.filename, sizeof x.filename, filename);+ snprintf(x.purpose, sizeof x.purpose, "%s", purpose);+ x.bytes = bytes;+ x.created_at = (int64_t)time(NULL);+ x.expires_at = expires_after > 0 ? x.created_at + expires_after : 0;+ char tmp[4200], dst[4200], meta[512];+ if (path_of(f, x.id, "", dst, sizeof dst) != 0 ||+ snprintf(tmp, sizeof tmp, "%s/.%s.tmp", f->dir, x.id) >=+ (int)sizeof tmp) {+ snprintf(err, err_len, "cannot write the file");+ return -1;+ }+ pthread_mutex_lock(&f->mu);+ expire(f);+ struct statvfs vs;+ uint64_t avail = statvfs(f->dir, &vs) == 0+ ? (uint64_t)vs.f_bavail * (uint64_t)vs.f_frsize+ : UINT64_MAX;+ int rc = 0;+ if (f->total + bytes > SRV_FILES_MAX) {+ snprintf(err, err_len,+ "the files kept would pass %" PRIu64 " GiB: delete some",+ SRV_FILES_MAX >> 30);+ rc = -2;+ } else if (avail < bytes + SRV_FILES_DISK_FREE) {+ snprintf(err, err_len, "not enough disk for it");+ rc = -2;+ }+ pthread_mutex_unlock(&f->mu);+ if (rc)+ return rc;+ /* the parts joined outside the lock: gigabytes take a while */+ int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);+ struct janas_md5 h;+ janas_md5_init(&h);+ uint64_t done = 0;+ char *buf = malloc((size_t)1 << 20);+ if (!buf)+ rc = -1;+ for (size_t i = 0; fd >= 0 && rc == 0 && i < n; i++) {+ int in = open(paths[i], O_RDONLY | O_CLOEXEC);+ if (in < 0) {+ rc = -1;+ break;+ }+ ssize_t r;+ while (rc == 0 && (r = read(in, buf, (size_t)1 << 20)) > 0) {+ janas_md5_update(&h, buf, (size_t)r);+ done += (uint64_t)r;+ for (ssize_t w = 0; w < r;) {+ ssize_t k = write(fd, buf + w, (size_t)(r - w));+ if (k <= 0) {+ rc = -1;+ break;+ }+ w += k;+ }+ }+ close(in);+ }+ free(buf);+ char got[33];+ janas_md5_hex(&h, got);+ if (fd < 0 || rc != 0) {+ snprintf(err, err_len, "cannot write the file");+ rc = -1;+ } else if (done != bytes) {+ snprintf(err, err_len,+ "the parts make %" PRIu64 " bytes, not the %" PRIu64+ " the upload was created with",+ done, bytes);+ rc = -3;+ } else if (md5 && strcasecmp(md5, got) != 0) {+ snprintf(err, err_len, "the parts' MD5 is %s, not %s", got, md5);+ rc = -3;+ }+#ifdef _WIN32+ if (rc == 0 && _commit(fd) != 0)+#else+ if (rc == 0 && fsync(fd) != 0)+#endif+ rc = -1;+ if (fd >= 0 && close(fd) != 0 && rc == 0)+ rc = -1;+ int mn = snprintf(+ meta, sizeof meta, "%" PRId64 " %" PRId64 " %" PRIu64 " %s\t%s\n",+ x.created_at, x.expires_at, x.bytes, x.purpose, x.filename);+ pthread_mutex_lock(&f->mu);+ if (rc == 0 &&+ (mn <= 0 || (size_t)mn >= sizeof meta || rename(tmp, dst) != 0 ||+ write_aside(f, x.id, ".meta", meta, (size_t)mn) != 0 ||+ keep(f, &x) != 0)) {+ unlink(dst);+ snprintf(err, err_len, "cannot write the file");+ rc = -1;+ }+ pthread_mutex_unlock(&f->mu);+ if (rc != 0)+ unlink(tmp);+ else+ *out = x;+ return rc;+}++static long find(struct srv_files *f, const char *id)+{+ for (size_t i = 0; i < f->n; i++)+ if (strcmp(f->e[i].id, id) == 0)+ return (long)i;+ return -1;+}++int srv_files_get(struct srv_files *f, const char *id, struct srv_file *out)+{+ pthread_mutex_lock(&f->mu);+ expire(f);+ long i = find(f, id);+ if (i >= 0)+ *out = f->e[i];+ pthread_mutex_unlock(&f->mu);+ return i >= 0 ? 0 : -1;+}++int srv_files_delete(struct srv_files *f, const char *id)+{+ pthread_mutex_lock(&f->mu);+ expire(f);+ long i = find(f, id);+ if (i >= 0)+ forget(f, (size_t)i);+ pthread_mutex_unlock(&f->mu);+ return i >= 0 ? 0 : -1;+}++int srv_files_open_content(struct srv_files *f, const char *id)+{+ pthread_mutex_lock(&f->mu);+ expire(f);+ char p[4200];+ int fd = find(f, id) >= 0 && path_of(f, id, "", p, sizeof p) == 0+ ? open(p, O_RDONLY | O_CLOEXEC)+ : -1;+ pthread_mutex_unlock(&f->mu);+ return fd;+}++size_t srv_files_list(struct srv_files *f, const char *purpose, int asc,+ const char *after, struct srv_file **list)+{+ *list = NULL;+ pthread_mutex_lock(&f->mu);+ expire(f);+ struct srv_file *out = f->n ? malloc(f->n * sizeof(*out)) : NULL;+ size_t w = 0;+ int past = after == NULL;+ for (size_t k = 0; out && k < f->n; k++) {+ const struct srv_file *x = &f->e[asc ? k : f->n - 1 - k];+ if (!past) {+ past = strcmp(x->id, after) == 0;+ continue;+ }+ if (!purpose || strcmp(x->purpose, purpose) == 0)+ out[w++] = *x;+ }+ pthread_mutex_unlock(&f->mu);+ *list = out;+ return w;+}diff --git a/src/server/files_store.h b/src/server/files_store.hnew file mode 100644index 0000000..42665e8--- /dev/null+++ b/src/server/files_store.h@@ -0,0 +1,64 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * files_store.h - the files clients upload (files_store.c): their bytes in+ * a directory of the server's, one file each under its id, and what OpenAI+ * says of each (name, purpose, size, times) beside it. The client's name+ * for a file is only shown, never part of a path.+ */+#ifndef JANAS_SERVER_FILES_STORE_H+#define JANAS_SERVER_FILES_STORE_H++#include <stddef.h>+#include <stdint.h>++struct srv_files;++/* All the files' bytes at most, and the disk they leave free at least. */+#define SRV_FILES_MAX ((uint64_t)16 << 30)+#define SRV_FILES_DISK_FREE ((uint64_t)1 << 30)++struct srv_file {+ char id[48];+ char filename[256];+ char purpose[32];+ uint64_t bytes;+ int64_t created_at, expires_at; /* expires_at 0: never */+};++/* In store_dir/.files (made), with what an earlier run left there read+ back; store_dir NULL: a directory of its own under TMPDIR, removed by+ srv_files_close. NULL with the reason in err. */+struct srv_files *srv_files_open(const char *store_dir, char *err,+ size_t err_len);+void srv_files_close(struct srv_files *f);++/* A new file of n bytes: 0 with *out filled, -1 memory or disk (err), -2+ past the limits (err says which). */+int srv_files_add(struct srv_files *f, const char *filename,+ const char *purpose, const void *data, size_t n,+ int64_t expires_after, struct srv_file *out, char *err,+ size_t err_len);+/* A new file of the n files at paths joined in order (an upload's parts),+ which must make `bytes` bytes and, when md5 is not NULL, have that MD5:+ 0 with *out filled; -1 disk or memory, -2 past the limits, -3 the size or+ the MD5 not what was said (err says which). */+int srv_files_add_parts(struct srv_files *f, const char *filename,+ const char *purpose, const char *const *paths, size_t n,+ uint64_t bytes, const char *md5, int64_t expires_after,+ struct srv_file *out, char *err, size_t err_len);+/* The directory the files are in (an upload keeps its parts beside). */+const char *srv_files_dir(const struct srv_files *f);+/* 0 with *out, -1 no such file (or expired). */+int srv_files_get(struct srv_files *f, const char *id, struct srv_file *out);+/* 0, -1 no such file. */+int srv_files_delete(struct srv_files *f, const char *id);+/* The file's bytes opened for reading: a descriptor, or -1. */+int srv_files_open_content(struct srv_files *f, const char *id);+/* The files, newest first (asc: oldest first), of purpose (NULL: any),+ after the id after (NULL: from the first): how many, into a malloc'd+ *list (free it). */+size_t srv_files_list(struct srv_files *f, const char *purpose, int asc,+ const char *after, struct srv_file **list);++#endifdiff --git a/src/server/http.c b/src/server/http.cindex b643472..1d095f3 100644--- a/src/server/http.c+++ b/src/server/http.c@@ -6,16 +6,20 @@* anybody else. Every operation of OpenAI's specification has a row in the* route table; the ones not written yet answer 501 and say why.*/+#include "origin.h"#include "server.h"#include "server_test_html.h" /* made by build.sh from test.html */#include <microhttpd.h>#include <stdarg.h>+#include <stdatomic.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/random.h>#include <sys/socket.h>+#include <time.h>+#include <unistd.h>enum route_kind { implemented, planned, other_model, training, cloud };@@ -41,10 +45,22 @@ struct req_ctx {void srv_new_id(char *buf, size_t cap, const char *prefix){unsigned char r[12];- if (getrandom(r, sizeof(r), 0) != (ssize_t)sizeof(r))- memset(r, 0, sizeof(r));+ if (getrandom(r, sizeof(r), 0) != (ssize_t)sizeof(r)) {+ /* no entropy to be had: still unique, from a counter and the clock */+ static _Atomic uint64_t count;+ struct timespec ts;+ clock_gettime(CLOCK_REALTIME, &ts);+ uint64_t x = (uint64_t)ts.tv_sec * 1000000000u + (uint64_t)ts.tv_nsec;+ uint64_t c = ++count;+ for (size_t i = 0; i < 8; i++)+ r[i] = (unsigned char)(x >> (8 * i));+ for (size_t i = 0; i < 4; i++)+ r[8 + i] = (unsigned char)(c >> (8 * i));+ }static const char hex[] = "0123456789abcdef";size_t w = (size_t)snprintf(buf, cap, "%s", prefix);+ if (w >= cap)+ w = cap - 1;for (size_t i = 0; i < sizeof(r) && w + 2 < cap; i++) {buf[w++] = hex[r[i] >> 4];buf[w++] = hex[r[i] & 15];@@ -54,30 +70,95 @@ void srv_new_id(char *buf, size_t cap, const char *prefix)const char *srv_query(struct srv_req *r, const char *key){+ if (!r->conn) /* a request the server makes of itself: no query */+ return NULL;return MHD_lookup_connection_value(r->conn, MHD_GET_ARGUMENT_KIND, key);}+/* The origin of a page named with --allow-origin that this thread's+ request comes from (a thread per connection), for the CORS headers of+ the answer; NULL otherwise. */+static _Thread_local const char *cors_origin;+static enum MHD_Result queue(struct MHD_Connection *c, unsigned status,struct MHD_Response *resp, const char *type){if (!resp)return MHD_NO;- MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE, type);+ if (type)+ MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE, type);+ if (cors_origin) {+ MHD_add_response_header(resp, "Access-Control-Allow-Origin",+ cors_origin);+ MHD_add_response_header(resp, "Vary", "Origin");+ }enum MHD_Result r = MHD_queue_response(c, status, resp);MHD_destroy_response(resp);return r;}+/* The answer kept (r->cap): a copy of n bytes. MHD_YES. */+static int capture(struct srv_req *r, unsigned status, const char *body,+ size_t n)+{+ free(r->cap->body);+ r->cap->body = malloc(n + 1);+ r->cap->status = r->cap->body ? status : 500;+ r->cap->n = r->cap->body ? n : 0;+ if (r->cap->body) {+ memcpy(r->cap->body, body, n);+ r->cap->body[n] = 0;+ }+ return MHD_YES;+}+int srv_reply_json(struct srv_req *r, unsigned status, char *json, size_t n){if (!json)return srv_reply_error(r, 500, "server_error", NULL,"cannot write the answer");+ if (r->cap) {+ capture(r, status, json, n);+ free(json);+ return MHD_YES;+ }struct MHD_Response *resp = MHD_create_response_from_buffer_copy(n, json);free(json);return queue(r->conn, status, resp, "application/json");}+int srv_reply_bytes(struct srv_req *r, unsigned status, char *body, size_t n,+ const char *type)+{+ if (!body)+ return srv_reply_error(r, 500, "server_error", NULL,+ "cannot write the answer");+ if (r->cap) {+ capture(r, status, body, n);+ free(body);+ return MHD_YES;+ }+ struct MHD_Response *resp = MHD_create_response_from_buffer_copy(n, body);+ free(body);+ return queue(r->conn, status, resp, type);+}++int srv_reply_fd(struct srv_req *r, int fd, uint64_t n, const char *type)+{+ if (r->cap) { /* no request the server makes of itself wants bytes */+ close(fd);+ return srv_reply_error(r, 400, "invalid_request_error", NULL,+ "not in a batch");+ }+ struct MHD_Response *resp = MHD_create_response_from_fd64(n, fd);+ if (!resp) {+ close(fd);+ return srv_reply_error(r, 500, "server_error", NULL,+ "cannot send the file");+ }+ return queue(r->conn, 200, resp, type);+}+/* s as a JSON string, into buf (quotes included) */static size_t json_quote(char *buf, size_t cap, const char *s){@@ -118,11 +199,13 @@ int srv_reply_error(struct srv_req *r, unsigned status, const char *type,"{\"error\":{\"message\":%s,\"type\":\"%s\",""\"param\":null,\"code\":%s}}",qm, type, qc);- struct MHD_Response *resp =- MHD_create_response_from_buffer_copy((size_t)n, out);if (r->srv->cfg.verbose)fprintf(stderr, "janas-server: %s %s: %u %s\n", r->method, r->path,status, msg);+ if (r->cap)+ return capture(r, status, out, (size_t)n);+ struct MHD_Response *resp =+ MHD_create_response_from_buffer_copy((size_t)n, out);return queue(r->conn, status, resp, "application/json");}@@ -148,6 +231,8 @@ int srv_reply_limit(struct srv_req *r, const struct srv_job *j)qm, in, max, in > max ? in - max : 0, j->st.prompt_system,j->st.prompt_tools, j->st.prompt_history,j->st.prompt_last, j->st.context_size);+ if (r->cap)+ return capture(r, 400, out, (size_t)n);struct MHD_Response *resp =MHD_create_response_from_buffer_copy((size_t)n, out);if (r->srv->cfg.verbose)@@ -191,6 +276,28 @@ int srv_reply_stream(struct srv_req *r, srv_stream_next next,return MHD_NO;}*s = (struct stream){next, done, ctx};+ if (r->cap) { /* the whole stream, kept, or handed on as it comes */+ char *all = NULL, buf[4096];+ size_t n = 0;+ long k;+ while ((k = next(ctx, buf, sizeof buf)) >= 0) {+ if (r->cap->on_data) {+ if (k)+ r->cap->on_data(r->cap->arg, buf, (size_t)k);+ continue;+ }+ char *t = k ? realloc(all, n + (size_t)k + 1) : all;+ if (k && !t)+ continue;+ all = t;+ if (k)+ memcpy(all + n, buf, (size_t)k), n += (size_t)k;+ }+ stream_free(s);+ capture(r, 200, all ? all : "", n);+ free(all);+ return MHD_YES;+ }struct MHD_Response *resp = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 4096, stream_reader, s, stream_free);if (!resp) {@@ -257,8 +364,43 @@ static int key_ok(struct srv_req *r)return diff == 0;}+srv_handler srv_route_find(const char *method, const char *path)+{+ struct srv_req r = {0};+ for (size_t i = 0; i < sizeof(routes) / sizeof(routes[0]); i++)+ if (routes[i].h && !strcmp(routes[i].method, method) &&+ match(routes[i].path, path, &r))+ return routes[i].h;+ return NULL;+}+static enum MHD_Result dispatch(struct srv_req *r){+ const char *origin =+ MHD_lookup_connection_value(r->conn, MHD_HEADER_KIND, "Origin");+ int o = srv_origin_check(+ &r->srv->cfg, origin,+ MHD_lookup_connection_value(r->conn, MHD_HEADER_KIND, "Host"));+ if (o == SRV_ORIGIN_REFUSED)+ return (enum MHD_Result)srv_reply_error(+ r, 403, "invalid_request_error", "origin_not_allowed",+ "Pages of %s may not call this server: start it with "+ "--allow-origin %s to let them.",+ origin, origin);+ cors_origin = o == SRV_ORIGIN_LISTED ? origin : NULL;+ if (o == SRV_ORIGIN_LISTED && strcmp(r->method, "OPTIONS") == 0) {+ /* the browser's question before the request itself */+ struct MHD_Response *resp =+ MHD_create_response_from_buffer_static(0, "");+ if (!resp)+ return MHD_NO;+ MHD_add_response_header(resp, "Access-Control-Allow-Methods",+ "GET, POST, DELETE, OPTIONS");+ MHD_add_response_header(resp, "Access-Control-Allow-Headers",+ "Authorization, Content-Type");+ MHD_add_response_header(resp, "Access-Control-Max-Age", "600");+ return queue(r->conn, 204, resp, NULL);+ }if (strcmp(r->path, "/health") == 0 && strcmp(r->method, "GET") == 0) {static const char ok[] = "{\"status\":\"ok\"}";return queue(r->conn, 200,@@ -316,6 +458,24 @@ static enum MHD_Result dispatch(struct srv_req *r)"Unknown request URL: %s %s.", r->method, r->path);}+/* The largest body a request may have: an upload's part may hold 64 MiB+ (and its multipart envelope) whatever --max-body says. */+static size_t body_limit(const struct srv *srv, const char *url)+{+ size_t n = strlen(url);+ if (strncmp(url, "/v1/", 4) == 0)+ url += 3, n -= 3;+ if (strncmp(url, "/uploads/", 9) == 0 && n > 15 &&+ strcmp(url + n - 6, "/parts") == 0 &&+ srv->cfg.max_body < SRV_UPLOAD_PART_MAX + (1 << 20))+ return SRV_UPLOAD_PART_MAX + (1 << 20);+ return srv->cfg.max_body;+}++/* requests between their headers and their end: at stop, the server waits+ a little for them to answer */+static atomic_int in_flight;+static enum MHD_Result access_handler(void *cls, struct MHD_Connection *conn,const char *url, const char *method,const char *version,@@ -330,19 +490,21 @@ static enum MHD_Result access_handler(void *cls, struct MHD_Connection *conn,if (!ctx)return MHD_NO;*req_cls = ctx;+ atomic_fetch_add(&in_flight, 1);return MHD_YES;}if (*upload_data_size) { /* a piece of the body */size_t n = *upload_data_size;*upload_data_size = 0;- if (ctx->too_big || ctx->n + n > srv->cfg.max_body) {+ size_t limit = body_limit(srv, url);+ if (ctx->too_big || ctx->n + n > limit) {ctx->too_big = 1;return MHD_YES;}if (ctx->n + n + 1 > ctx->cap) {size_t cap = 2 * (ctx->n + n) + 1024;- if (cap > srv->cfg.max_body + 1)- cap = srv->cfg.max_body + 1;+ if (cap > limit + 1)+ cap = limit + 1;char *b = realloc(ctx->body, cap);if (!b)return MHD_NO;@@ -355,6 +517,7 @@ static enum MHD_Result access_handler(void *cls, struct MHD_Connection *conn,return MHD_YES;}/* the whole request is here */+ cors_origin = NULL; /* until dispatch knows the page */struct srv_req r = {.srv = srv,.conn = conn,.method = method,@@ -369,7 +532,7 @@ static enum MHD_Result access_handler(void *cls, struct MHD_Connection *conn,if (ctx->too_big)return (enum MHD_Result)srv_reply_error(&r, 413, "invalid_request_error", "request_too_large",- "The request body is larger than %zu bytes.", srv->cfg.max_body);+ "The request body is larger than %zu bytes.", body_limit(srv, url));return dispatch(&r);}@@ -384,33 +547,59 @@ static void completed(void *cls, struct MHD_Connection *conn, void **req_cls,free(ctx->body);free(ctx);*req_cls = NULL;+ atomic_fetch_sub(&in_flight, 1);}}int srv_http_start(struct srv *s, const struct sockaddr *addr, char *err,size_t err_len){- s->mhd = MHD_start_daemon(- MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD |- MHD_USE_ERROR_LOG |- (addr->sa_family == AF_INET6 ? MHD_USE_IPv6 : 0),- s->cfg.port, NULL, NULL, access_handler, s, MHD_OPTION_SOCK_ADDR, addr,- MHD_OPTION_NOTIFY_COMPLETED, completed, NULL,- MHD_OPTION_CONNECTION_LIMIT, (unsigned)s->cfg.max_connections,- MHD_OPTION_CONNECTION_TIMEOUT, (unsigned)600,- MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t)(256 * 1024),- MHD_OPTION_END);+ /* with HTTPS the server listens itself (tls.c) and hands+ libmicrohttpd connections in clear */+ int tls = s->cfg.tls_cert != NULL;+ unsigned flags = MHD_USE_THREAD_PER_CONNECTION |+ MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG |+ (addr->sa_family == AF_INET6 ? MHD_USE_IPv6 : 0);+ if (tls) /* no socket of its own: tls.c adds the connections */+ s->mhd = MHD_start_daemon(+ flags | MHD_USE_NO_LISTEN_SOCKET | MHD_USE_ITC, 0, NULL, NULL,+ access_handler, s, MHD_OPTION_NOTIFY_COMPLETED, completed, NULL,+ MHD_OPTION_CONNECTION_LIMIT, (unsigned)s->cfg.max_connections,+ MHD_OPTION_CONNECTION_TIMEOUT, (unsigned)600,+ MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t)(256 * 1024),+ MHD_OPTION_END);+ else+ s->mhd = MHD_start_daemon(+ flags, s->cfg.port, NULL, NULL, access_handler, s,+ MHD_OPTION_SOCK_ADDR, addr, MHD_OPTION_NOTIFY_COMPLETED, completed,+ NULL, MHD_OPTION_CONNECTION_LIMIT, (unsigned)s->cfg.max_connections,+ MHD_OPTION_CONNECTION_TIMEOUT, (unsigned)600,+ MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t)(256 * 1024),+ MHD_OPTION_END);if (!s->mhd) {snprintf(err, err_len, "cannot listen on %s:%u", s->cfg.host,(unsigned)s->cfg.port);return -1;}+ if (tls && srv_tls_start(s, addr, err, err_len) != 0) {+ MHD_stop_daemon(s->mhd);+ s->mhd = NULL;+ return -1;+ }return 0;}void srv_http_stop(struct srv *s){- if (s->mhd)+ if (s->cfg.tls_cert)+ srv_tls_stop();+ if (s->mhd) {+ /* the requests running answered first, for 2 s at most: their+ replies are cut short and new ones refused by the engine+ (srv_engine_cancel_clients) */+ for (int i = 0; i < 100 && atomic_load(&in_flight) > 0; i++)+ usleep(20000);MHD_stop_daemon(s->mhd);+ }s->mhd = NULL;}diff --git a/src/server/images.c b/src/server/images.cnew file mode 100644index 0000000..ccfff0c--- /dev/null+++ b/src/server/images.c@@ -0,0 +1,215 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * images.c - the pictures of a request's messages: chat's image_url parts+ * ({"url": a data: URL or http(s)}), the Responses' input_image parts (an+ * image_url, or a file_id of the Files API). The parsers keep each one's+ * reference (srv_image_part); the handler, which has the server, turns+ * them into bytes before the job goes to the engine (srv_images_fetch): a+ * data: URL decoded from base64, an http(s) one fetched at a public address+ * only (the MCP policy: --mcp-allow names the exceptions), a file read from+ * the store. The engine gives them to the library (janas_llm_chat_image),+ * which reads PNG and JPEG.+ */+#include "server.h"++#include <stdlib.h>+#include <string.h>+#include <unistd.h>++#include "files_store.h"+#include "services/common/https_get.h"++#define MAX_IMAGE_BYTES ((size_t)16 << 20)+#define MAX_IMAGES 16++void srv_imgs_free(struct srv_img *imgs, int n)+{+ for (int i = 0; i < n; i++) {+ free(imgs[i].ref);+ free(imgs[i].data);+ }+ free(imgs);+}++int srv_image_part(yyjson_val *part, const char *type, struct srv_img **imgs,+ int *n_imgs, char *err, size_t el)+{+ const char *ref = NULL;+ int is_file = 0;+ if (type && !strcmp(type, "image_url")) { /* chat */+ yyjson_val *iu = yyjson_obj_get(part, "image_url");+ ref = yyjson_is_str(iu) ? yyjson_get_str(iu)+ : yyjson_get_str(yyjson_obj_get(iu, "url"));+ } else if (type && !strcmp(type, "input_image")) { /* Responses */+ ref = yyjson_get_str(yyjson_obj_get(part, "image_url"));+ if (!ref) {+ ref = yyjson_get_str(yyjson_obj_get(part, "file_id"));+ is_file = ref != NULL;+ }+ } else {+ return 0;+ }+ if (!ref || !*ref) {+ snprintf(err, el, "a picture part has no image_url (or file_id)");+ return -1;+ }+ if (*n_imgs >= MAX_IMAGES) {+ snprintf(err, el, "at most %d pictures a request", MAX_IMAGES);+ return -1;+ }+ struct srv_img *t = realloc(*imgs, sizeof(*t) * (size_t)(*n_imgs + 1));+ char *r = strdup(ref);+ if (!t || !r) {+ free(r);+ if (t)+ *imgs = t;+ snprintf(err, el, "out of memory");+ return -1;+ }+ *imgs = t;+ t[(*n_imgs)++] = (struct srv_img){.ref = r, .is_file = is_file};+ return 1;+}++static int b64v(int c)+{+ return c >= 'A' && c <= 'Z' ? c - 'A'+ : c >= 'a' && c <= 'z' ? c - 'a' + 26+ : c >= '0' && c <= '9' ? c - '0' + 52+ : c == '+' || c == '-' ? 62+ : c == '/' || c == '_' ? 63+ : -1;+}++int srv_data_url(const char *url, size_t len, size_t max, unsigned char **data,+ size_t *n_data, char *err, size_t el)+{+ const char *end = url + len;+ const char *comma = memchr(url, ',', len);+ int b64 = 0;+ for (const char *q = url; comma && q + 7 <= comma; q++)+ b64 |= !memcmp(q, ";base64", 7);+ if (len < 5 || memcmp(url, "data:", 5) || !comma || !b64) {+ snprintf(err, el, "a data: URL in base64 is expected");+ return -1;+ }+ const char *p = comma + 1;+ size_t n = (size_t)(end - p);+ if (n / 4 * 3 > max) {+ snprintf(err, el, "a data: URL of more than %zu MiB", max >> 20);+ return -1;+ }+ unsigned char *out = malloc(n / 4 * 3 + 3);+ if (!out) {+ snprintf(err, el, "out of memory");+ return -1;+ }+ size_t w = 0;+ unsigned acc = 0;+ int bits = 0;+ for (; p < end; p++) {+ if (*p == '=' || *p == '\n' || *p == '\r' || *p == ' ')+ continue;+ int v = b64v((unsigned char)*p);+ if (v < 0) {+ free(out);+ snprintf(err, el, "a data: URL that is not base64");+ return -1;+ }+ acc = acc << 6 | (unsigned)v;+ bits += 6;+ if (bits >= 8) {+ bits -= 8;+ out[w++] = (unsigned char)(acc >> bits);+ }+ }+ *data = out;+ *n_data = w;+ return 0;+}++/* data:[type][;base64],<data> */+static int from_data_url(struct srv_img *im, char *err, size_t el)+{+ return srv_data_url(im->ref, strlen(im->ref), MAX_IMAGE_BYTES, &im->data,+ &im->n, err, el);+}++static int from_file(struct srv *s, struct srv_img *im, char *err, size_t el)+{+ struct srv_file f;+ if (srv_files_get(s->files, im->ref, &f) != 0) {+ snprintf(err, el, "no file %s here", im->ref);+ return -1;+ }+ if (f.bytes > MAX_IMAGE_BYTES) {+ snprintf(err, el, "a picture of more than 16 MiB");+ return -1;+ }+ int fd = srv_files_open_content(s->files, im->ref);+ unsigned char *b = fd >= 0 ? malloc(f.bytes ? f.bytes : 1) : NULL;+ size_t got = 0;+ while (b && got < f.bytes) {+ ssize_t k = read(fd, b + got, f.bytes - got);+ if (k <= 0)+ break;+ got += (size_t)k;+ }+ if (fd >= 0)+ close(fd);+ if (!b || got != f.bytes) {+ free(b);+ snprintf(err, el, "the file %s cannot be read", im->ref);+ return -1;+ }+ im->data = b;+ im->n = got;+ return 0;+}++static int from_url(struct srv *s, struct srv_img *im, char *err, size_t el)+{+ struct janas_buf b = {0};+ char why[300];+ int st = janas_https_get_policy(im->ref, &s->cfg.mcp_policy, 15000, &b, why,+ sizeof why);+ if (st != 200) {+ janas_buf_free(&b);+ if (st < 0)+ snprintf(err, el, "the picture %.200s cannot be fetched: %s",+ im->ref, why);+ else+ snprintf(err, el, "the picture %.200s answered HTTP %d", im->ref,+ st);+ return -1;+ }+ im->data = (unsigned char *)b.p;+ im->n = b.n;+ return 0;+}++int srv_images_fetch(struct srv *s, struct srv_job *j, char *err, size_t el)+{+ for (int32_t i = 0; i < j->n_msg; i++) {+ struct srv_msg *m = &j->msg[i];+ for (int k = 0; k < m->n_imgs; k++) {+ struct srv_img *im = &m->imgs[k];+ if (im->data)+ continue;+ int rc = im->is_file ? from_file(s, im, err, el)+ : !strncmp(im->ref, "data:", 5)+ ? from_data_url(im, err, el)+ : !strncmp(im->ref, "http://", 7) ||+ !strncmp(im->ref, "https://", 8)+ ? from_url(s, im, err, el)+ : (snprintf(err, el,+ "a picture is a data: URL, an http(s) "+ "URL or a file_id"),+ -1);+ if (rc != 0)+ return -1;+ }+ }+ return 0;+}diff --git a/src/server/main.c b/src/server/main.cindex c78fa14..c432b0d 100644--- a/src/server/main.c+++ b/src/server/main.c@@ -5,9 +5,14 @@* the clients written for it (SDKs, chat front ends, editors) can use a* model running on this machine. See server.h.*/+#include "files_store.h"#include "server.h"+#include "assistants.h"+#include "containers.h"+#include "vectors.h"#include "common/devices.h"+#include "common/sandbox.h"#include "common/env_names.h"#include "common/sock.h"#include "common/stop.h"@@ -29,7 +34,8 @@ static void usage(FILE *f)"Serves the model through an HTTP API that follows OpenAI's: models,\n""chat completions (tools, JSON schemas, log-probabilities, stored\n""completions), completions, responses and conversations, and with\n"- "an embedding model the embeddings; moderations ask the model.\n"+ "an embedding model the embeddings, with a speech model the\n"+ "transcriptions and translations; moderations ask the model.\n""Every other operation of OpenAI's API answers 501 and says why.\n""\n"" --host ADDR address to listen on (default 127.0.0.1: this\n"@@ -48,6 +54,11 @@ static void usage(FILE *f)" --no-mtp none, not even one found beside the model\n"" --draft FILE a small model of the same family, for speed\n"" --mode M auto, eco or max (default auto)\n"+ " --quality Q the lowest quality the engine may choose to be\n"+ " faster where memory is short: low (default)\n"+ " the experts at four bits and the light ones not\n"+ " in memory left out, medium four bits only,\n"+ " high nothing lowered\n"" --no-gpu never give the GPU work\n"" --prompt-on DEV what reads the prompts: auto (default: what\n"" measures fastest here), cpu, gpu, npu, npu+gpu\n"@@ -64,6 +75,14 @@ static void usage(FILE *f)" (dense Qwen3 models)\n"" --embedding-model FILE a model for /v1/embeddings (such as\n"" Qwen3-Embedding), run beside the chat model\n"+ " --speech-model FILE a Whisper model (made by hf2jns_whisper)\n"+ " for /v1/audio/transcriptions and translations\n"+ " --vad-model FILE Silero VAD (made by silero2jns) for their\n"+ " chunking_strategy (default: silero-vad.jns\n"+ " beside the speech model)\n"+ " --speaker-model FILE speaker embeddings (WeSpeaker, made by\n"+ " wespeaker2jns) for diarized_json (default:\n"+ " wespeaker-resnet34.jns beside the speech model)\n"" --think on|off reasoning before replying, for models that do\n"" it, when a request does not say (default: the\n"" model's own)\n"@@ -108,7 +127,28 @@ static void usage(FILE *f)" prompt is read (30 with --verbose)\n"" --no-mcp refuse tools of type mcp in /responses: by\n"" default the server reaches the MCP servers a\n"- " request names, as OpenAI's does\n"+ " request names, as OpenAI's does, at public\n"+ " addresses only\n"+ " --tls-cert FILE serve HTTPS with this certificate (PEM, the\n"+ " chain after it), with --tls-key\n"+ " --tls-key FILE its private key (PEM, not encrypted)\n"+ " --no-web refuse the web_search tool in /responses: by\n"+ " default the server searches Wikipedia when a\n"+ " request gives the model that tool\n"+ " --code-interpreter run the code a model writes (code_interpreter\n"+ " in /responses and the Assistants, the\n"+ " Containers API) in a cage: no network, no\n"+ " other files or processes, bounded memory\n"+ " and time (Linux, user namespaces); off by\n"+ " default\n"+ " --code-interpreter-python PATH the python3 inside the cage (a\n"+ " path under /usr; the system's by default)\n"+ " --mcp-allow HOST[:PORT] an MCP server at a private or local\n"+ " address that requests may name all the same\n"+ " (again for more)\n"+ " --allow-origin ORIGIN a web page of another origin that may\n"+ " call the server, as http://host:port (again\n"+ " for more; * for any): others are refused\n"" --test serve a chat page for trying the server out, at\n"" http://<host>:<port>/test\n"" --verbose one line per request on stderr\n"@@ -125,20 +165,56 @@ static const char *arg(int argc, char **argv, int *i)}/* every option, for the nearest to one mistyped */-static const char *const OPTIONS[] = {- "--help", "--version", "--host",- "--port", "--api-key", "--name",- "--ctx", "--reserve", "--cache",- "--mtp", "--draft", "--mode",- "--think", "--embedding-model", "--reasoning-budget",- "--no-mtp", "--no-gpu", "--keep",- "--keep-memory", "--keep-disk", "--store",- "--no-store", "--queue", "--max-body",- "--connections", "--max-input", "--warn-input",- "--fim-prefix", "--fim-suffix", "--fim-max",- "--metrics", "--progress", "--no-mcp",- "--test", "--verbose", "--prompt-on",- "--drafts-on", "--reply-on", "--npu-int8"};+static const char *const OPTIONS[] = {"--help",+ "--version",+ "--host",+ "--port",+ "--api-key",+ "--name",+ "--ctx",+ "--reserve",+ "--cache",+ "--mtp",+ "--draft",+ "--mode",+ "--quality",+ "--think",+ "--embedding-model",+ "--speech-model",+ "--vad-model",+ "--speaker-model",+ "--reasoning-budget",+ "--no-mtp",+ "--no-gpu",+ "--keep",+ "--keep-memory",+ "--keep-disk",+ "--store",+ "--no-store",+ "--queue",+ "--max-body",+ "--connections",+ "--max-input",+ "--warn-input",+ "--fim-prefix",+ "--fim-suffix",+ "--fim-max",+ "--metrics",+ "--progress",+ "--no-mcp",+ "--mcp-allow",+ "--no-web",+ "--code-interpreter",+ "--code-interpreter-python",+ "--tls-cert",+ "--tls-key",+ "--allow-origin",+ "--test",+ "--verbose",+ "--prompt-on",+ "--drafts-on",+ "--reply-on",+ "--npu-int8"};/* the value of option argv[*i], read whole within [lo, hi]; a mistakeends the program, as a missing value does */@@ -204,6 +280,7 @@ static char *model_name(const char *path)int main(int argc, char **argv){+ janas_sandbox_main(argc, argv); /* a cage of code_interpreter's */janas_env_check("janas-server");struct srv s = {0};struct srv_config *c = &s.cfg;@@ -222,6 +299,7 @@ int main(int argc, char **argv)c->api_key = getenv("JANAS_API_KEY");c->thinking = -1;c->reasoning_budget = -1;+ c->mcp_policy.public_only = 1;for (int i = 1; i < argc; i++) {const char *a = argv[i];char dwhy[240];@@ -252,6 +330,9 @@ int main(int argc, char **argv)c->llm.mtp_path = arg(argc, argv, &i);} else if (!strcmp(a, "--draft")) {c->llm.draft_path = arg(argc, argv, &i);+ } else if (!strcmp(a, "--quality")) {+ static const char *const levels[] = {"low", "medium", "high"};+ c->llm.min_quality = choice(argc, argv, &i, levels, 3);} else if (!strcmp(a, "--mode")) {static const char *const modes[] = {"auto", "eco", "max"};int m = choice(argc, argv, &i, modes, 3);@@ -265,6 +346,12 @@ int main(int argc, char **argv)c->reasoning_budget = (int)num(argc, argv, &i, -1, INT32_MAX);} else if (!strcmp(a, "--embedding-model")) {c->embed_path = arg(argc, argv, &i);+ } else if (!strcmp(a, "--speech-model")) {+ c->speech_path = arg(argc, argv, &i);+ } else if (!strcmp(a, "--vad-model")) {+ c->vad_path = arg(argc, argv, &i);+ } else if (!strcmp(a, "--speaker-model")) {+ c->speaker_path = arg(argc, argv, &i);} else if (!strcmp(a, "--no-mtp")) {c->no_mtp = 1;} else if (!strcmp(a, "--no-gpu")) {@@ -307,6 +394,36 @@ int main(int argc, char **argv)setenv("JANAS_PROGRESS", argv[i], 1);} else if (!strcmp(a, "--no-mcp")) {c->no_mcp = 1;+ } else if (!strcmp(a, "--no-web")) {+ c->no_web = 1;+ } else if (!strcmp(a, "--code-interpreter")) {+ c->code_interpreter = 1;+ } else if (!strcmp(a, "--code-interpreter-python")) {+ c->code_python = arg(argc, argv, &i);+ c->code_interpreter = 1;+ } else if (!strcmp(a, "--tls-cert")) {+ c->tls_cert = arg(argc, argv, &i);+ } else if (!strcmp(a, "--tls-key")) {+ c->tls_key = arg(argc, argv, &i);+ } else if (!strcmp(a, "--mcp-allow") || !strcmp(a, "--allow-origin")) {+ /* argv lives as long as the server: pointers into it */+ int mcp = a[2] == 'm';+ const char *v = arg(argc, argv, &i);+ size_t n = mcp ? c->mcp_policy.n_allow : (size_t)c->n_allow_origin;+ void *t = realloc(mcp ? (void *)c->mcp_policy.allow+ : (void *)c->allow_origin,+ (n + 1) * sizeof(char *));+ if (!t) {+ fprintf(stderr, "janas-server: out of memory\n");+ return 1;+ }+ if (mcp) {+ c->mcp_policy.allow = t;+ c->mcp_policy.allow[c->mcp_policy.n_allow++] = (char *)v;+ } else {+ c->allow_origin = t;+ c->allow_origin[c->n_allow_origin++] = v;+ }} else if (!strcmp(a, "--test")) {c->test = 1;} else if (!strcmp(a, "--verbose")) {@@ -340,16 +457,32 @@ int main(int argc, char **argv)}if (c->api_key && !*c->api_key)c->api_key = NULL;- char *name = NULL, *embed_name = NULL;+ char *name = NULL, *embed_name = NULL, *speech_name = NULL;if (!c->model_id)c->model_id = name = model_name(c->model_path);if (c->embed_path)c->embed_id = embed_name = model_name(c->embed_path);+ if (c->speech_path)+ c->speech_id = speech_name = model_name(c->speech_path);if (!c->model_id || c->max_queue < 1 || c->keep < 0 || c->keep > 1024 ||c->max_body < 1024 || c->max_connections < 1) {fprintf(stderr, "janas-server: invalid options\n");return 2;}+ /* the certificate read now: a mistake there should not wait for the+ model */+ if ((c->tls_cert != NULL) != (c->tls_key != NULL)) {+ fprintf(stderr, "janas-server: --tls-cert and --tls-key go "+ "together\n");+ return 2;+ }+ if (c->tls_cert) {+ char terr[300];+ if (srv_tls_setup(c->tls_cert, c->tls_key, terr, sizeof terr) != 0) {+ fprintf(stderr, "janas-server: %s\n", terr);+ return 2;+ }+ }/* the address first: a mistake there should not wait for the model */char port[8];@@ -399,27 +532,66 @@ int main(int argc, char **argv)failed);fputc('\n', stderr);}+ if (!(s.files = srv_files_open(c->store_dir, err, sizeof(err)))) {+ fprintf(stderr, "janas-server: %s\n", err);+ freeaddrinfo(ai);+ srv_store_free(s.store);+ return 1;+ }+ /* the speech model before the chat model, which pins this thread to+ one CPU: the speech model's threads would inherit it */+ if (c->speech_path)+ fprintf(stderr, "janas-server: opening %s...\n", c->speech_path);+ if (srv_speech_start(&s, err, sizeof err) != 0) {+ fprintf(stderr, "janas-server: %s\n", err);+ freeaddrinfo(ai);+ srv_files_close(s.files);+ srv_store_free(s.store);+ return 1;+ }fprintf(stderr, "janas-server: opening %s...\n", c->model_path);s.engine = srv_engine_start(c, err, sizeof(err));if (!s.engine) {fprintf(stderr, "janas-server: %s\n", err);freeaddrinfo(ai);+ srv_speech_stop(&s);+ srv_files_close(s.files);srv_store_free(s.store);return 1;}+ if (srv_containers_start(&s) != 0)+ fprintf(stderr, "janas-server: code_interpreter is off: %s\n",+ srv_containers_off());+ if (srv_vectors_start(&s) != 0)+ fprintf(stderr, "janas-server: vector stores cannot be indexed: no "+ "directory or thread\n");+ if (srv_batches_start(&s) != 0)+ fprintf(stderr, "janas-server: batches cannot run: no work "+ "directory or thread\n");+ srv_runs_start(&s);+ if (srv_evals_start(&s) != 0)+ fprintf(stderr, "janas-server: evals cannot run: no work "+ "directory or thread\n");if (srv_http_start(&s, ai->ai_addr, err, sizeof(err)) != 0) {fprintf(stderr, "janas-server: %s\n", err);freeaddrinfo(ai);+ srv_runs_stop();+ srv_containers_stop();+ srv_evals_stop();+ srv_batches_stop();+ srv_vectors_stop();srv_engine_stop(s.engine);+ srv_speech_stop(&s);+ srv_files_close(s.files);srv_store_free(s.store);return 1;}freeaddrinfo(ai);fprintf(stderr,"janas-server: %s\n"- "janas-server: model '%s' on http://%s%s%s:%u/v1%s; Ctrl+C "+ "janas-server: model '%s' on http%s://%s%s%s:%u/v1%s; Ctrl+C ""stops it\n",- srv_engine_describe(s.engine), c->model_id,+ srv_engine_describe(s.engine), c->model_id, c->tls_cert ? "s" : "",strchr(c->host, ':') ? "[" : "", c->host,strchr(c->host, ':') ? "]" : "", (unsigned)c->port,c->api_key ? ", with a key" : "");@@ -430,11 +602,23 @@ int main(int argc, char **argv)strchr(c->host, ':') ? "]" : "", (unsigned)c->port);janas_stop_wait();fprintf(stderr, "janas-server: stopping\n");+ srv_engine_cancel_clients(s.engine);srv_http_stop(&s);+ srv_runs_stop(); /* their passes cut short: again at next start */+ srv_containers_stop(); /* the Python sessions end; the files stay */+ srv_evals_stop(); /* a row being graded finishes; the rest at next start */+ srv_batches_stop(); /* a line running finishes; the rest at next start */+ srv_vectors_stop(); /* a file being indexed: again at next start */srv_engine_stop(s.engine);+ srv_speech_stop(&s);srv_responses_shutdown();+ srv_uploads_shutdown();+ srv_files_close(s.files);srv_store_free(s.store);free(name);free(embed_name);+ free(speech_name);+ free(c->mcp_policy.allow);+ free(c->allow_origin);return 0;}diff --git a/src/server/models.c b/src/server/models.cindex 8a5c13a..d3a7c66 100644--- a/src/server/models.c+++ b/src/server/models.c@@ -24,6 +24,17 @@ static yyjson_mut_val *embed_obj(struct srv_req *r, yyjson_mut_doc *d)return o;}+/* The speech model, when there is one. */+static yyjson_mut_val *speech_obj(struct srv_req *r, yyjson_mut_doc *d)+{+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_str(d, o, "id", r->srv->cfg.speech_id);+ yyjson_mut_obj_add_str(d, o, "object", "model");+ yyjson_mut_obj_add_int(d, o, "created", srv_engine_created(r->srv->engine));+ yyjson_mut_obj_add_str(d, o, "owned_by", "janas");+ return o;+}+static yyjson_mut_val *model_obj(struct srv_req *r, yyjson_mut_doc *d){yyjson_mut_val *o = yyjson_mut_obj(d);@@ -61,6 +72,8 @@ int srv_models_list(struct srv_req *r)yyjson_mut_arr_append(arr, model_obj(r, d));if (r->srv->cfg.embed_id)yyjson_mut_arr_append(arr, embed_obj(r, d));+ if (r->srv->cfg.speech_id)+ yyjson_mut_arr_append(arr, speech_obj(r, d));return reply_doc(r, d);}@@ -71,6 +84,11 @@ int srv_models_get(struct srv_req *r)yyjson_mut_doc_set_root(d, embed_obj(r, d));return reply_doc(r, d);}+ if (r->srv->cfg.speech_id && !strcmp(r->param[0], r->srv->cfg.speech_id)) {+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_doc_set_root(d, speech_obj(r, d));+ return reply_doc(r, d);+ }if (strcmp(r->param[0], r->srv->cfg.model_id) != 0)return srv_reply_error(r, 404, "invalid_request_error","model_not_found",diff --git a/src/server/multipart.c b/src/server/multipart.cnew file mode 100644index 0000000..74e464b--- /dev/null+++ b/src/server/multipart.c@@ -0,0 +1,144 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * multipart.c - a multipart/form-data body taken apart (see multipart.h).+ * What a client sends is input from outside: every length is checked+ * against the body, and the parts point into it, never past it.+ */+#include "multipart.h"++#include <stdio.h>+#include <string.h>+#include <strings.h>++static const char *mp_find(const char *p, const char *end, const char *s,+ size_t n)+{+ for (; p && (size_t)(end - p) >= n; p++) {+ p = memchr(p, s[0], (size_t)(end - p) - n + 1);+ if (!p)+ return NULL;+ if (memcmp(p, s, n) == 0)+ return p;+ }+ return NULL;+}++/* The value of param (boundary, name, filename) in a header's text: quoted+ or not; 0 with it in out, -1 absent or too long. */+static int param(const char *h, size_t hn, const char *key, char *out,+ size_t cap)+{+ size_t kn = strlen(key);+ for (size_t i = 0; i + kn + 1 <= hn; i++) {+ /* at the start or after ';' and spaces, key= */+ if (strncasecmp(h + i, key, kn) != 0 || h[i + kn] != '=')+ continue;+ size_t j = i;+ while (j > 0 && (h[j - 1] == ' ' || h[j - 1] == '\t'))+ j--;+ if (j > 0 && h[j - 1] != ';')+ continue;+ const char *v = h + i + kn + 1, *e = h + hn;+ size_t n = 0;+ if (v < e && *v == '"') {+ for (v++; v < e && *v != '"'; v++) {+ if (*v == '\\' && v + 1 < e)+ v++;+ if (n + 1 >= cap)+ return -1;+ out[n++] = *v;+ }+ if (v >= e)+ return -1;+ } else {+ for (; v < e && *v != ';' && *v != ' ' && *v != '\t'; v++) {+ if (n + 1 >= cap)+ return -1;+ out[n++] = *v;+ }+ }+ out[n] = 0;+ return 0;+ }+ return -1;+}++int srv_multipart_parse(const char *ctype, const char *body, size_t n,+ struct srv_part *parts, int max, char *err,+ size_t err_len)+{+ char bnd[80] = "--";+ if (!ctype || strncasecmp(ctype, "multipart/form-data", 19) != 0 ||+ param(ctype, strlen(ctype), "boundary", bnd + 2, sizeof(bnd) - 2) !=+ 0 ||+ !bnd[2]) {+ snprintf(err, err_len,+ "the body is multipart/form-data, with its boundary");+ return -1;+ }+ size_t bl = strlen(bnd);+ const char *end = body + n;+ const char *p = mp_find(body, end, bnd, bl);+ int count = 0;+ while (p) {+ p += bl;+ if (end - p >= 2 && p[0] == '-' && p[1] == '-')+ return count; /* the closing boundary */+ if (end - p < 2 || p[0] != '\r' || p[1] != '\n')+ break;+ p += 2;+ const char *he = mp_find(p, end, "\r\n\r\n", 4);+ if (!he)+ break;+ struct srv_part pt = {0};+ int named = 0;+ for (const char *l = p; l < he;) {+ const char *le = mp_find(l, he + 2, "\r\n", 2);+ if (!le)+ le = he;+ size_t ln = (size_t)(le - l);+ if (ln > 20 && strncasecmp(l, "Content-Disposition:", 20) == 0) {+ named = param(l, ln, "name", pt.name, sizeof(pt.name)) == 0;+ pt.has_filename = param(l, ln, "filename", pt.filename,+ sizeof(pt.filename)) == 0;+ } else if (ln > 13 && strncasecmp(l, "Content-Type:", 13) == 0) {+ const char *v = l + 13;+ while (v < le && *v == ' ')+ v++;+ size_t vn = (size_t)(le - v);+ if (vn >= sizeof(pt.type))+ vn = sizeof(pt.type) - 1;+ memcpy(pt.type, v, vn);+ pt.type[vn] = 0;+ }+ l = le + 2;+ }+ const char *data = he + 4;+ /* the part ends before CRLF and the next boundary */+ char next[84];+ snprintf(next, sizeof(next), "\r\n%s", bnd);+ const char *q = mp_find(data, end, next, bl + 2);+ if (!q || !named)+ break;+ pt.data = data;+ pt.n = (size_t)(q - data);+ if (count == max) {+ snprintf(err, err_len, "more than %d parts", max);+ return -1;+ }+ parts[count++] = pt;+ p = q + 2;+ }+ snprintf(err, err_len, "the multipart body is cut or malformed");+ return -1;+}++const struct srv_part *srv_multipart_get(const struct srv_part *parts, int n,+ const char *name)+{+ for (int i = 0; i < n; i++)+ if (strcmp(parts[i].name, name) == 0)+ return &parts[i];+ return NULL;+}diff --git a/src/server/multipart.h b/src/server/multipart.hnew file mode 100644index 0000000..d070426--- /dev/null+++ b/src/server/multipart.h@@ -0,0 +1,30 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * multipart.h - a multipart/form-data body (an upload) taken apart+ * (multipart.c).+ */+#ifndef JANAS_SERVER_MULTIPART_H+#define JANAS_SERVER_MULTIPART_H++#include <stddef.h>++struct srv_part {+ char name[64]; /* the form field */+ char filename[256]; /* as the client sent it: never a path here */+ int has_filename;+ char type[128]; /* its Content-Type, or "" */+ const char *data; /* into the body */+ size_t n;+};++/* The parts of body (ctype: the request's Content-Type, its boundary in+ it), at most max: how many, or -1 with the reason in err. */+int srv_multipart_parse(const char *ctype, const char *body, size_t n,+ struct srv_part *parts, int max, char *err,+ size_t err_len);+/* The part of that name, or NULL. */+const struct srv_part *srv_multipart_get(const struct srv_part *parts, int n,+ const char *name);++#endifdiff --git a/src/server/origin.c b/src/server/origin.cnew file mode 100644index 0000000..f39f5be--- /dev/null+++ b/src/server/origin.c@@ -0,0 +1,99 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * origin.c - which web pages may call the server (see origin.h). A page of+ * another site can make the browser send a request to 127.0.0.1 - it+ * cannot read the answer, but the model still runs, and a tool of type mcp+ * still connects - and a site whose name it makes resolve to 127.0.0.1+ * (DNS rebinding) even reads it, being then of the same origin by name.+ * The browser says which page asks, in Origin: the server's own page and+ * the pages named with --allow-origin pass, the others are refused,+ * whatever the key.+ */+#include "origin.h"++#include <stdio.h>+#include <string.h>+#include <strings.h>+#include <sys/utsname.h>++/* host[:port] split, [v6] kept bracketed; the port "" when absent. */+static int split(const char *s, size_t n, char *host, size_t hc, char *port,+ size_t pc)+{+ const char *colon = NULL;+ if (n && s[0] == '[') {+ const char *e = memchr(s, ']', n);+ if (!e)+ return -1;+ if ((size_t)(e + 1 - s) < n) {+ if (e[1] != ':')+ return -1;+ colon = e + 1;+ }+ } else {+ colon = memchr(s, ':', n);+ }+ size_t hn = colon ? (size_t)(colon - s) : n;+ size_t pn = colon ? n - hn - 1 : 0;+ if (!hn || hn >= hc || pn >= pc)+ return -1;+ memcpy(host, s, hn);+ host[hn] = 0;+ memcpy(port, colon ? colon + 1 : "", pn);+ port[pn] = 0;+ return 0;+}++/* A name only this machine answers to, or an address: what a rebinding+ site cannot be called. */+static int trusted_host(const char *h, const char *listen)+{+ size_t n = strlen(h);+ if (!strcasecmp(h, "localhost") ||+ (n > 10 && !strcasecmp(h + n - 10, ".localhost")))+ return 1;+ if (h[0] == '[') /* an IPv6 address */+ return 1;+ if (strspn(h, "0123456789.") == n) /* an IPv4 address */+ return 1;+ if (listen && !strcasecmp(h, listen))+ return 1;+ struct utsname u; /* this machine's name (Windows too, win_posix.c) */+ if (uname(&u) == 0) {+ const char *me = u.nodename;+ size_t m = strlen(me);+ if (!strcasecmp(h, me) || (n == m + 6 && !strncasecmp(h, me, m) &&+ !strcasecmp(h + m, ".local")))+ return 1;+ }+ return 0;+}++int srv_origin_check(const struct srv_config *c, const char *origin,+ const char *host)+{+ if (!origin)+ return SRV_ORIGIN_NONE; /* not a browser's page: a program */+ for (int i = 0; i < c->n_allow_origin; i++)+ if (!strcmp(c->allow_origin[i], "*") ||+ !strcasecmp(c->allow_origin[i], origin))+ return SRV_ORIGIN_LISTED;+ /* the server's own page: the origin is the address it was asked at */+ const char *def;+ if (!strncasecmp(origin, "http://", 7))+ def = "80";+ else if (!strncasecmp(origin, "https://", 8))+ def = "443";+ else+ return SRV_ORIGIN_REFUSED; /* "null", an extension's... */+ const char *o = strstr(origin, "://") + 3;+ char oh[256], op[8], hh[256], hp[8];+ if (!host || split(o, strlen(o), oh, sizeof oh, op, sizeof op) != 0 ||+ split(host, strlen(host), hh, sizeof hh, hp, sizeof hp) != 0)+ return SRV_ORIGIN_REFUSED;+ if (strcasecmp(oh, hh) != 0 ||+ strcmp(op[0] ? op : def, hp[0] ? hp : "80") != 0)+ return SRV_ORIGIN_REFUSED;+ return trusted_host(hh, c->host) ? SRV_ORIGIN_SAME : SRV_ORIGIN_REFUSED;+}diff --git a/src/server/origin.h b/src/server/origin.hnew file mode 100644index 0000000..0f17246--- /dev/null+++ b/src/server/origin.h@@ -0,0 +1,23 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * origin.h - which web pages may call the server (origin.c).+ */+#ifndef JANAS_SERVER_ORIGIN_H+#define JANAS_SERVER_ORIGIN_H++#include "server.h"++enum {+ SRV_ORIGIN_REFUSED, /* a page of another site */+ SRV_ORIGIN_NONE, /* no Origin: a program, not a page */+ SRV_ORIGIN_SAME, /* the server's own page (/test) */+ SRV_ORIGIN_LISTED /* named with --allow-origin: answered with CORS */+};++/* What a request with these Origin and Host headers (either may be NULL)+ is. */+int srv_origin_check(const struct srv_config *c, const char *origin,+ const char *host);++#endifdiff --git a/src/server/request.c b/src/server/request.cindex 4a1dd41..2aa4497 100644--- a/src/server/request.c+++ b/src/server/request.c@@ -70,10 +70,21 @@ void srv_parsed_free(struct srv_parsed *pr)pr->metadata = pr->messages = NULL;}+/* A text part's text: "refusal" for a refusal (its OpenAI field), else "text";+ NULL when absent. */+yyjson_val *srv_part_text(yyjson_val *part, const char *type)+{+ yyjson_val *tx = NULL;+ if (type && !strcmp(type, "refusal"))+ tx = yyjson_obj_get(part, "refusal");+ return tx ? tx : yyjson_obj_get(part, "text");+}+/* The text of a message's content: a string, or the text parts of an arrayjoined; NULL content is empty. Anything else (images, audio) refused. */static int content_text(struct srv_perr *e, struct srv_parsed *pr,- yyjson_val *c, int i, char **out, size_t *n)+ yyjson_val *c, int i, char **out, size_t *n,+ struct srv_img **imgs, int *n_imgs){if (!c || yyjson_is_null(c)) {*out = srv_dup_n("", 0);@@ -95,12 +106,25 @@ static int content_text(struct srv_perr *e, struct srv_parsed *pr,yyjson_arr_foreach(c, idx, max, part){const char *type = yyjson_get_str(yyjson_obj_get(part, "type"));+ char why[200];+ int pic = srv_image_part(part, type, imgs, n_imgs, why, sizeof why);+ if (pic < 0)+ return srv_pfail(pr,+ BAD("invalid_value", "messages[%d]: %s.", i, why));+ if (pic)+ continue;if (!type || (strcmp(type, "text") != 0 && strcmp(type, "refusal")))return srv_pfail(pr, BAD("unsupported_content",- "messages[%d]: only text parts are "- "supported (got '%s').",+ "messages[%d]: only text and picture "+ "parts are supported (got '%s').",i, type ? type : "?"));- total += yyjson_get_len(yyjson_obj_get(part, "text"));+ yyjson_val *tx = srv_part_text(part, type);+ if (tx && !yyjson_is_str(tx))+ return srv_pfail(pr, BAD("invalid_value",+ "messages[%d]: a part's text must be "+ "a string.",+ i));+ total += tx ? yyjson_get_len(tx) : 0;}char *t = malloc(total + 1);if (!t)@@ -108,9 +132,11 @@ static int content_text(struct srv_perr *e, struct srv_parsed *pr,size_t w = 0;yyjson_arr_foreach(c, idx, max, part){- yyjson_val *tx = yyjson_obj_get(part, "text");- memcpy(t + w, yyjson_get_str(tx) ? yyjson_get_str(tx) : "",- yyjson_get_len(tx));+ yyjson_val *tx =+ srv_part_text(part, yyjson_get_str(yyjson_obj_get(part, "type")));+ if (!tx)+ continue;+ memcpy(t + w, yyjson_get_str(tx), yyjson_get_len(tx));w += yyjson_get_len(tx);}t[w] = 0;@@ -214,8 +240,21 @@ static int read_messages(struct srv_perr *e, struct srv_parsed *pr,pr, BAD("invalid_value", "messages[%d] has no role.", i));char *t;size_t tn;- if (content_text(e, pr, yyjson_obj_get(m, "content"), i, &t, &tn))+ struct srv_img *imgs = NULL;+ int n_imgs = 0;+ if (content_text(e, pr, yyjson_obj_get(m, "content"), i, &t, &tn, &imgs,+ &n_imgs)) {+ srv_imgs_free(imgs, n_imgs);return -1;+ }+ if (n_imgs && strcmp(role, "user")) {+ free(t);+ srv_imgs_free(imgs, n_imgs);+ return srv_pfail(pr, BAD("invalid_value",+ "messages[%d]: pictures go in a user's "+ "message.",+ i));+ }if (strcmp(role, "system") == 0 || strcmp(role, "developer") == 0) {char *s = realloc(sys->text, sys->n + tn + 3);if (!s) {@@ -249,6 +288,8 @@ static int read_messages(struct srv_perr *e, struct srv_parsed *pr,x->role = role_id;x->text = t;x->n = tn;+ x->imgs = imgs;+ x->n_imgs = n_imgs;if (role_id == JANAS_LLM_ROLE_ASSISTANT &&read_calls(e, pr, m, x, i) != 0)return -1;diff --git a/src/server/request.h b/src/server/request.hindex 0d581ef..2223a00 100644--- a/src/server/request.h+++ b/src/server/request.h@@ -23,6 +23,9 @@ char *srv_dup_n(const char *s, size_t n);/* A number between lo and hi: 1 with *out set, 0 absent (or null), -1 not anumber or out of range. */int srv_num(yyjson_val *v, double lo, double hi, double *out);+/* A text part's text ("refusal" for a refusal, else "text"), NULL if absent;+ it may be a value of any kind, the caller checks it is a string. */+yyjson_val *srv_part_text(yyjson_val *part, const char *type);/* A ceiling on the reasoning by llama.cpp's names, reasoning_budget_tokensor thinking_budget_tokens: -1 none, 0 no reasoning, N at most N tokens ofit, then the answer. 0, or -1 when the value is not one of those. */diff --git a/src/server/responses.h b/src/server/responses.hindex 1db12dd..1c628eb 100644--- a/src/server/responses.h+++ b/src/server/responses.h@@ -95,6 +95,10 @@ int resp_mcp_owns(const struct resp *x, const char *name);/* The job is over: its MCP calls run or held for approval; 1 when a nextround was submitted (x->j is the new job), 0 when the response is over. */int resp_mcp_round(struct resp *x);+/* The url_citation annotations of an output text: each [n] it holds that+ names a source the web_search calls found (responses_mcp.c). */+void resp_web_annotate(yyjson_mut_doc *d, yyjson_mut_val *arr,+ const struct resp *x, const char *text, size_t n);void resp_mcp_free(struct resp_mcp *mc);/* Stops the MCP calls running for x, from another thread. */void resp_mcp_cancel(struct resp_mcp *mc);@@ -103,8 +107,9 @@ void resp_mcp_cancel(struct resp_mcp *mc);behind; -1 (nothing changed) when x was cancelled or the queue is full. */int resp_adopt(struct resp *x, struct srv_job *nj);-/* conversations.c: items added to a conversation (a JSON array); 0, or -1- when there is no such conversation (or memory). */+/* conversations.c: items added to a conversation (a JSON array text): 0,+ -1 no such conversation, -2 memory (or not an array), -3 it would grow+ past SRV_STORE_DOC_MAX. */int conv_append(struct srv *srv, const char *conv, const char *items_json);/* Its items, as a JSON array text (NULL: no such conversation). */char *conv_items(struct srv *srv, const char *conv);diff --git a/src/server/responses_in.c b/src/server/responses_in.cindex 5f29d25..4720a06 100644--- a/src/server/responses_in.c+++ b/src/server/responses_in.c@@ -42,7 +42,8 @@ void resp_item_id(yyjson_mut_doc *d, yyjson_mut_val *item)/* The text of a content: a string, or its text parts joined. NULL with errset when it holds something else (an image, a file). */-static char *content_text(yyjson_val *c, size_t *n, char *err, size_t el)+static char *content_text(yyjson_val *c, size_t *n, struct srv_img **imgs,+ int *n_imgs, char *err, size_t el){if (!c || yyjson_is_null(c)) {*n = 0;@@ -61,13 +62,24 @@ static char *content_text(yyjson_val *c, size_t *n, char *err, size_t el)yyjson_arr_foreach(c, idx, max, p){const char *t = yyjson_get_str(yyjson_obj_get(p, "type"));+ int pic = imgs ? srv_image_part(p, t, imgs, n_imgs, err, el) : 0;+ if (pic < 0)+ return NULL;+ if (pic)+ continue;if (!t || (strcmp(t, "input_text") && strcmp(t, "output_text") &&strcmp(t, "text") && strcmp(t, "refusal"))) {- snprintf(err, el, "only text parts are supported (got '%s')",+ snprintf(err, el,+ "only text and picture parts are supported (got '%s')",t ? t : "?");return NULL;}- total += yyjson_get_len(yyjson_obj_get(p, "text"));+ yyjson_val *tx = srv_part_text(p, t);+ if (tx && !yyjson_is_str(tx)) {+ snprintf(err, el, "a part's text must be a string");+ return NULL;+ }+ total += tx ? yyjson_get_len(tx) : 0;}char *s = malloc(total + 1);if (!s) {@@ -77,9 +89,11 @@ static char *content_text(yyjson_val *c, size_t *n, char *err, size_t el)size_t w = 0;yyjson_arr_foreach(c, idx, max, p){- yyjson_val *tx = yyjson_obj_get(p, "text");- memcpy(s + w, yyjson_get_str(tx) ? yyjson_get_str(tx) : "",- yyjson_get_len(tx));+ yyjson_val *tx =+ srv_part_text(p, yyjson_get_str(yyjson_obj_get(p, "type")));+ if (!tx)+ continue;+ memcpy(s + w, yyjson_get_str(tx), yyjson_get_len(tx));w += yyjson_get_len(tx);}s[w] = 0;@@ -160,9 +174,21 @@ int resp_items_to_msgs(struct srv_job *j, yyjson_val *items, char *err,if (!type || !strcmp(type, "message")) {const char *role = yyjson_get_str(yyjson_obj_get(it, "role"));size_t n;- char *t = content_text(yyjson_obj_get(it, "content"), &n, err, el);- if (!t)+ struct srv_img *imgs = NULL;+ int n_imgs = 0;+ char *t = content_text(yyjson_obj_get(it, "content"), &n, &imgs,+ &n_imgs, err, el);+ if (!t) {+ srv_imgs_free(imgs, n_imgs);return -1;+ }+ if (n_imgs && (!role || strcmp(role, "user"))) {+ free(t);+ srv_imgs_free(imgs, n_imgs);+ snprintf(err, el, "input[%zu]: pictures go in a user's message",+ idx);+ return -1;+ }int32_t rid =!role ? -1: !strcmp(role, "user") ? JANAS_LLM_ROLE_USER@@ -183,8 +209,13 @@ int resp_items_to_msgs(struct srv_job *j, yyjson_val *items, char *err,goto oom;continue;}- if (!resp_push(j, rid, t, n))+ struct srv_msg *um = resp_push(j, rid, t, n);+ if (!um) {+ srv_imgs_free(imgs, n_imgs);goto oom;+ }+ um->imgs = imgs;+ um->n_imgs = n_imgs;} else if (!strcmp(type, "function_call")) {const char *name = yyjson_get_str(yyjson_obj_get(it, "name"));yyjson_val *a = yyjson_obj_get(it, "arguments");@@ -204,7 +235,8 @@ int resp_items_to_msgs(struct srv_job *j, yyjson_val *items, char *err,goto oom;} else if (!strcmp(type, "function_call_output")) {size_t n;- char *t = content_text(yyjson_obj_get(it, "output"), &n, err, el);+ char *t = content_text(yyjson_obj_get(it, "output"), &n, NULL, NULL,+ err, el);if (!t)return -1;if (!resp_push(j, JANAS_LLM_ROLE_TOOL, t, n))@@ -442,12 +474,19 @@ static int read_tools(struct srv_req *r, struct srv_job *j, yyjson_val *root,yyjson_arr_foreach(tools, idx, max, t){const char *type = yyjson_get_str(yyjson_obj_get(t, "type"));+ /* web_search (and its dated and preview names): the server+ searches (responses_mcp.c) */if (!type ||- (strcmp(type, "function") != 0 && strcmp(type, "mcp") != 0)) {+ (strcmp(type, "function") != 0 && strcmp(type, "mcp") != 0 &&+ strncmp(type, "web_search", 10) != 0 &&+ strcmp(type, "file_search") != 0 &&+ strcmp(type, "code_interpreter") != 0)) {*ret = srv_reply_error(r, 400, "invalid_request_error","unsupported_value",- "tools[%zu]: only functions and MCP "- "servers can be called here (got '%s').",+ "tools[%zu]: only functions, MCP "+ "servers, web_search, file_search and "+ "code_interpreter can be called here "+ "(got '%s').",idx, type ? type : "?");return -1;}@@ -642,6 +681,8 @@ int resp_read(struct srv_req *r, struct resp *x, int *ret)char err[256];int e = resp_items_to_msgs(j, yyjson_doc_get_root(cd), err, sizeof(err));yyjson_doc_free(cd);+ if (e == 0) /* the pictures' bytes, before the engine has the job */+ e = srv_images_fetch(r->srv, j, err, sizeof(err));if (e != 0) {bad(r, ret, err);goto out;diff --git a/src/server/responses_mcp.c b/src/server/responses_mcp.cindex ba9c62f..82786db 100644--- a/src/server/responses_mcp.c+++ b/src/server/responses_mcp.c@@ -26,7 +26,11 @@#include <stdlib.h>#include <string.h>+#include "common/mcp_client.h"+#include "containers.h"#include "janas/mcp.h"+#include "vectors.h"+#include "web_search.h"#define MAX_ROUNDS 8#define CALL_SECONDS 120@@ -47,12 +51,39 @@ struct server {struct filter always, never;};+/* a source the web_search and file_search calls found: cited as [n], n+ its place + 1; a page (url) or a file (file_id) */+struct web_src {+ char url[512], title[256];+ char file_id[48];+};+struct resp_mcp {struct server *s;int n;int rounds;+ int web; /* a web_search tool: the server searches */+ struct web_src *src;+ int n_src;+ /* a file_search tool: its vector stores, results, filters, threshold */+ char **vs;+ int n_vs, fs_max;+ char *fs_filters;+ double fs_threshold;+ /* a code_interpreter tool: its container, and the files the code+ wrote (cited where the answer names them) */+ char ci[48];+ struct ci_file {+ char id[48], name[256];+ } *ci_files;+ int n_ci_files;};+#define WEB_NAME "web_search"+#define FILE_NAME "file_search"+#define CODE_NAME "python"+#define WEB_HITS 5+static void filter_free(struct filter *f){for (int i = 0; i < f->n_names; i++)@@ -71,6 +102,12 @@ void resp_mcp_free(struct resp_mcp *mc)filter_free(&mc->s[i].never);}free(mc->s);+ free(mc->src);+ for (int i = 0; i < mc->n_vs; i++)+ free(mc->vs[i]);+ free(mc->vs);+ free(mc->fs_filters);+ free(mc->ci_files);free(mc);}@@ -152,6 +189,12 @@ void resp_mcp_cancel(struct resp_mcp *mc)int resp_mcp_owns(const struct resp *x, const char *name){+ if (x->mcp && x->mcp->web && name && !strcmp(name, WEB_NAME))+ return 1;+ if (x->mcp && x->mcp->n_vs && name && !strcmp(name, FILE_NAME))+ return 1;+ if (x->mcp && x->mcp->ci[0] && name && !strcmp(name, CODE_NAME))+ return 1;const char *tool;return x->mcp && owner(x->mcp, name, &tool) != NULL;}@@ -266,6 +309,329 @@ static void run_call(yyjson_mut_doc *d, yyjson_mut_val *arr, struct server *s,free(out);}+/* A web_search call: Wikipedia asked, a web_search_call item with its+ sources, and for the model the pages' first sentences, numbered on from+ the sources found before so that [n] means one page in the response. */+static void run_web(struct resp_mcp *mc, yyjson_mut_doc *d, yyjson_mut_val *arr,+ const char *args, char **text)+{+ yyjson_doc *ad = yyjson_read(args, strlen(args), 0);+ yyjson_val *a = yyjson_doc_get_root(ad);+ const char *q = yyjson_get_str(yyjson_obj_get(a, "query"));+ const char *lang = yyjson_get_str(yyjson_obj_get(a, "language"));+ struct srv_web_hit hits[WEB_HITS];+ char err[256] = "";+ int n =+ q && *q ? srv_web_search(q, lang, hits, WEB_HITS, err, sizeof err) : -1;+ if (!q || !*q)+ snprintf(err, sizeof err, "no query");+ yyjson_mut_val *it = yyjson_mut_arr_add_obj(d, arr);+ char id[48];+ srv_new_id(id, sizeof(id), "ws_");+ yyjson_mut_obj_add_strcpy(d, it, "id", id);+ yyjson_mut_obj_add_str(d, it, "type", "web_search_call");+ yyjson_mut_obj_add_str(d, it, "status", n >= 0 ? "completed" : "failed");+ yyjson_mut_val *act = yyjson_mut_obj_add_obj(d, it, "action");+ yyjson_mut_obj_add_str(d, act, "type", "search");+ yyjson_mut_obj_add_strcpy(d, act, "query", q ? q : "");+ yyjson_mut_val *srcs = yyjson_mut_obj_add_arr(d, act, "sources");+ struct janas_buf t = {0};+ if (n < 0) {+ janas_buf_printf(&t, "The search failed: %s.", err);+ } else if (n == 0) {+ janas_buf_printf(&t, "Wikipedia has no page for \"%s\".", q);+ } else {+ janas_buf_printf(&t,+ "Wikipedia's pages for \"%s\", best first. Answer "+ "from them, and put the number of the page a fact "+ "comes from right after it, as [1].\n",+ q);+ struct web_src *ns =+ realloc(mc->src, (size_t)(mc->n_src + n) * sizeof(*ns));+ for (int i = 0; ns && i < n; i++) {+ mc->src = ns;+ struct web_src *w = &mc->src[mc->n_src++];+ snprintf(w->url, sizeof w->url, "%s", hits[i].url);+ snprintf(w->title, sizeof w->title, "%s", hits[i].title);+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, srcs);+ yyjson_mut_obj_add_str(d, o, "type", "url");+ yyjson_mut_obj_add_strcpy(d, o, "url", hits[i].url);+ janas_buf_printf(&t, "\n[%d] %s (%s)\n%s\n", mc->n_src,+ hits[i].title, hits[i].url, hits[i].text);+ }+ }+ yyjson_doc_free(ad);+ *text = t.oom ? NULL : (janas_buf_put(&t, "", 1), t.p);+ if (t.oom)+ janas_buf_free(&t);+}++/* A file_search call: the tool's vector stores searched, a+ file_search_call item with the results, and for the model the chunks+ numbered on from the sources found before. */+static void run_file(struct resp_mcp *mc, yyjson_mut_doc *d,+ yyjson_mut_val *arr, const char *args, char **text)+{+ yyjson_doc *ad = yyjson_read(args, strlen(args), 0);+ const char *q =+ yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(ad), "query"));+ yyjson_doc *fd =+ mc->fs_filters ? yyjson_read(mc->fs_filters, strlen(mc->fs_filters), 0)+ : NULL;+ /* every store's best, then the best of them all */+ struct srv_vhit *all = NULL;+ int n = 0;+ char err[200] = "";+ for (int i = 0; q && *q && i < mc->n_vs; i++) {+ struct srv_vhit *h;+ int k = srv_vectors_search(mc->vs[i], &q, 1, yyjson_doc_get_root(fd),+ mc->fs_max, mc->fs_threshold, &h, err,+ sizeof err);+ if (k <= 0) {+ srv_vhits_free(h, 0);+ continue;+ }+ struct srv_vhit *t = realloc(all, (size_t)(n + k) * sizeof(*t));+ if (!t) {+ srv_vhits_free(h, k);+ continue;+ }+ all = t;+ memcpy(all + n, h, (size_t)k * sizeof(*h));+ n += k;+ free(h); /* the texts moved */+ }+ yyjson_doc_free(fd);+ for (int i = 1; i < n; i++) /* best first, insertion: a few dozen */+ for (int j = i; j > 0 && all[j].score > all[j - 1].score; j--) {+ struct srv_vhit t = all[j];+ all[j] = all[j - 1];+ all[j - 1] = t;+ }+ int keep = n < mc->fs_max ? n : mc->fs_max;+ yyjson_mut_val *it = yyjson_mut_arr_add_obj(d, arr);+ char id[48];+ srv_new_id(id, sizeof(id), "fs_");+ yyjson_mut_obj_add_strcpy(d, it, "id", id);+ yyjson_mut_obj_add_str(d, it, "type", "file_search_call");+ yyjson_mut_obj_add_str(d, it, "status", q && *q ? "completed" : "failed");+ yyjson_mut_val *qs = yyjson_mut_obj_add_arr(d, it, "queries");+ yyjson_mut_arr_add_strcpy(d, qs, q ? q : "");+ yyjson_mut_val *res = yyjson_mut_obj_add_arr(d, it, "results");+ struct janas_buf t = {0};+ if (!q || !*q)+ janas_buf_printf(&t, "The search needs a query.");+ else if (!keep)+ janas_buf_printf(&t, "The files hold nothing about \"%s\".", q);+ else+ janas_buf_printf(&t,+ "The files' passages for \"%s\", best first. "+ "Answer from them, and put the number of the "+ "passage a fact comes from right after it, as "+ "[1].\n",+ q);+ for (int i = 0; i < keep; i++) {+ struct web_src *ns =+ realloc(mc->src, (size_t)(mc->n_src + 1) * sizeof(*ns));+ if (!ns)+ break;+ mc->src = ns;+ struct web_src *w = &mc->src[mc->n_src++];+ memset(w, 0, sizeof(*w));+ snprintf(w->file_id, sizeof w->file_id, "%s", all[i].file_id);+ snprintf(w->title, sizeof w->title, "%s", all[i].filename);+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, res);+ yyjson_mut_obj_add_strcpy(d, o, "file_id", all[i].file_id);+ yyjson_mut_obj_add_strcpy(d, o, "filename", all[i].filename);+ yyjson_mut_obj_add_real(d, o, "score", all[i].score);+ yyjson_mut_obj_add_strcpy(d, o, "text", all[i].text);+ yyjson_doc *atd =+ all[i].attributes+ ? yyjson_read(all[i].attributes, strlen(all[i].attributes), 0)+ : NULL;+ if (atd)+ yyjson_mut_obj_add_val(+ d, o, "attributes",+ yyjson_val_mut_copy(d, yyjson_doc_get_root(atd)));+ yyjson_doc_free(atd);+ janas_buf_printf(&t, "\n[%d] %s\n%s\n", mc->n_src, all[i].filename,+ all[i].text);+ }+ srv_vhits_free(all, n);+ yyjson_doc_free(ad);+ *text = t.oom ? NULL : (janas_buf_put(&t, "", 1), t.p);+ if (t.oom)+ janas_buf_free(&t);+}++/* The tail of t, at most max bytes (whole characters), into b. */+static void put_tail(struct janas_buf *b, const char *t, size_t max)+{+ size_t n = strlen(t);+ if (n > max) {+ t += n - max;+ while ((*t & 0xc0) == 0x80)+ t++;+ janas_buf_puts(b, "[...]\n");+ }+ janas_buf_puts(b, t);+}++/* A code_interpreter call: the code run in the container's session, a+ code_interpreter_call item with its output and images, and for the model+ what it printed, its error and the files it wrote. */+static void run_code(struct resp_mcp *mc, yyjson_mut_doc *d,+ yyjson_mut_val *arr, const char *args, char **text)+{+ yyjson_doc *ad = yyjson_read(args, strlen(args), 0);+ const char *code =+ yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(ad), "code"));+ struct srv_cell c = {0};+ int rc = code ? srv_container_run(NULL, mc->ci, code, &c) : -1;+ yyjson_mut_val *it = yyjson_mut_arr_add_obj(d, arr);+ char id[48];+ srv_new_id(id, sizeof(id), "ci_");+ yyjson_mut_obj_add_strcpy(d, it, "id", id);+ yyjson_mut_obj_add_str(d, it, "type", "code_interpreter_call");+ yyjson_mut_obj_add_str(d, it, "status", rc == 0 ? "completed" : "failed");+ yyjson_mut_obj_add_strcpy(d, it, "container_id", mc->ci);+ yyjson_mut_obj_add_strcpy(d, it, "code", code ? code : "");+ yyjson_mut_val *outs = yyjson_mut_obj_add_arr(d, it, "outputs");+ struct janas_buf t = {0};+ if (!code) {+ janas_buf_puts(&t, "The call needs the code to run, as "+ "{\"code\": \"...\"}.");+ } else if (rc != 0) {+ janas_buf_puts(&t, "The container is gone (it expired): the code "+ "was not run.");+ } else {+ struct janas_buf lg = {0};+ if (c.logs)+ janas_buf_puts(&lg, c.logs);+ if (c.error) {+ if (lg.n && lg.p[lg.n - 1] != '\n')+ janas_buf_put(&lg, "\n", 1);+ janas_buf_puts(&lg, c.error);+ }+ janas_buf_put(&lg, "", 1);+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, outs);+ yyjson_mut_obj_add_str(d, o, "type", "logs");+ yyjson_mut_obj_add_strcpy(d, o, "logs", lg.oom ? "" : lg.p);+ janas_buf_free(&lg);+ if (c.logs && *c.logs) {+ janas_buf_puts(&t, "Output:\n");+ put_tail(&t, c.logs, 8000);+ janas_buf_puts(&t, "\n");+ }+ if (c.error) {+ janas_buf_puts(&t, "Error:\n");+ put_tail(&t, c.error, 4000);+ janas_buf_puts(&t, "\n");+ }+ for (int i = 0; i < c.n_files; i++) {+ if (i < c.n_images) {+ char url[200];+ snprintf(url, sizeof url, "/v1/containers/%s/files/%s/content",+ mc->ci, c.file_id[i]);+ yyjson_mut_val *im = yyjson_mut_arr_add_obj(d, outs);+ yyjson_mut_obj_add_str(d, im, "type", "image");+ yyjson_mut_obj_add_strcpy(d, im, "url", url);+ }+ janas_buf_printf(&t, "%s %s\n",+ i ? " " : "Files written:", c.path[i]);+ struct ci_file *nf = realloc(+ mc->ci_files, (size_t)(mc->n_ci_files + 1) * sizeof(*nf));+ if (nf) {+ mc->ci_files = nf;+ struct ci_file *f = &mc->ci_files[mc->n_ci_files++];+ snprintf(f->id, sizeof f->id, "%s", c.file_id[i]);+ const char *b = strrchr(c.path[i], '/');+ snprintf(f->name, sizeof f->name, "%s", b ? b + 1 : c.path[i]);+ }+ }+ if (!t.n)+ janas_buf_puts(&t, "The code ran and printed nothing.");+ }+ srv_cell_free(&c);+ yyjson_doc_free(ad);+ janas_buf_put(&t, "", 1);+ *text = t.oom ? NULL : t.p;+ if (t.oom)+ janas_buf_free(&t);+}++/* The files the code wrote, where the text names them: their+ container_file_citation annotations (offsets in characters). */+static void code_annotate(yyjson_mut_doc *d, yyjson_mut_val *arr,+ const struct resp_mcp *mc, const char *text, size_t n)+{+ for (int i = 0; i < mc->n_ci_files; i++) {+ const struct ci_file *f = &mc->ci_files[i];+ size_t fl = strlen(f->name);+ const char *hit = NULL;+ for (const char *p = text; fl && p + fl <= text + n; p++)+ if (!memcmp(p, f->name, fl)) {+ hit = p;+ break;+ }+ if (!hit)+ continue;+ size_t at = 0, len = 0;+ for (const char *p = text; p < hit; p++)+ at += ((unsigned char)*p & 0xc0) != 0x80;+ for (size_t k = 0; k < fl; k++)+ len += ((unsigned char)f->name[k] & 0xc0) != 0x80;+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, arr);+ yyjson_mut_obj_add_str(d, o, "type", "container_file_citation");+ yyjson_mut_obj_add_strcpy(d, o, "container_id", mc->ci);+ yyjson_mut_obj_add_strcpy(d, o, "file_id", f->id);+ yyjson_mut_obj_add_strcpy(d, o, "filename", f->name);+ yyjson_mut_obj_add_uint(d, o, "start_index", at);+ yyjson_mut_obj_add_uint(d, o, "end_index", at + len);+ }+}++void resp_web_annotate(yyjson_mut_doc *d, yyjson_mut_val *arr,+ const struct resp *x, const char *text, size_t n)+{+ if (!x || !x->mcp || !text)+ return;+ code_annotate(d, arr, x->mcp, text, n);+ if (!x->mcp->n_src)+ return;+ /* offsets in characters, as OpenAI counts them, not bytes */+ size_t chars = 0;+ for (size_t i = 0; i < n; i++) {+ if (text[i] == '[') {+ size_t k = i + 1;+ long v = 0;+ while (k < n && text[k] >= '0' && text[k] <= '9' && v < 100000)+ v = v * 10 + (text[k++] - '0');+ if (k > i + 1 && k < n && text[k] == ']' && v >= 1 &&+ v <= x->mcp->n_src) {+ const struct web_src *w = &x->mcp->src[v - 1];+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, arr);+ if (w->file_id[0]) { /* a passage of a file */+ yyjson_mut_obj_add_str(d, o, "type", "file_citation");+ yyjson_mut_obj_add_strcpy(d, o, "file_id", w->file_id);+ yyjson_mut_obj_add_strcpy(d, o, "filename", w->title);+ yyjson_mut_obj_add_uint(d, o, "index", chars);+ if (((unsigned char)text[i] & 0xc0) != 0x80)+ chars++;+ continue;+ }+ yyjson_mut_obj_add_str(d, o, "type", "url_citation");+ yyjson_mut_obj_add_uint(d, o, "start_index", chars);+ yyjson_mut_obj_add_uint(d, o, "end_index", chars + (k + 1 - i));+ yyjson_mut_obj_add_strcpy(d, o, "url", w->url);+ yyjson_mut_obj_add_strcpy(d, o, "title", w->title);+ }+ }+ if (((unsigned char)text[i] & 0xc0) != 0x80)+ chars++;+ }+}+/* The approval responses of the input: the calls they approve run now,their mcp_call items first in the output, their outputs kept on the jobfor the messages (resp_items_to_msgs). */@@ -386,7 +752,8 @@ static int reach(struct srv_req *r, struct server *s, yyjson_val *t, int *ret)nh++;}}- int32_t rc = janas_mcp_open_url(s->label, url, nh, h, 30, &s->m);+ int32_t rc = janas_mcp_open_url_policy(s->label, url, nh, h, 30,+ &r->srv->cfg.mcp_policy, &s->m);for (int i = 0; i < nh; i++)free(own[i]);if (rc != JANAS_MCP_OK) {@@ -402,16 +769,32 @@ static int reach(struct srv_req *r, struct server *s, yyjson_val *t, int *ret)int resp_mcp_read(struct srv_req *r, struct resp *x, yyjson_val *root, int *ret){yyjson_val *tools = yyjson_obj_get(root, "tools");- size_t idx, max, count = 0;+ size_t idx, max, count = 0, web = 0, files = 0, code = 0;yyjson_val *t;yyjson_arr_foreach(tools, idx, max, t){const char *type = yyjson_get_str(yyjson_obj_get(t, "type"));count += type && !strcmp(type, "mcp");+ /* web_search and its dated and preview names */+ web += type && !strncmp(type, "web_search", 10);+ files += type && !strcmp(type, "file_search");+ code += type && !strcmp(type, "code_interpreter");}- if (!count)+ if (!count && !web && !files && !code)return 0;- if (r->srv->cfg.no_mcp) {+ if (code && srv_containers_off()) {+ *ret = srv_reply_error(r, 400, "invalid_request_error",+ "unsupported_value", "%s", srv_containers_off());+ return -1;+ }+ if (web && r->srv->cfg.no_web) {+ *ret = srv_reply_error(r, 400, "invalid_request_error",+ "unsupported_value",+ "This server was started with --no-web: it "+ "does not search.");+ return -1;+ }+ if (count && r->srv->cfg.no_mcp) {*ret = srv_reply_error(r, 400, "invalid_request_error","unsupported_value","This server was started with --no-mcp: it "@@ -420,9 +803,62 @@ int resp_mcp_read(struct srv_req *r, struct resp *x, yyjson_val *root, int *ret)}x->mcp = calloc(1, sizeof(*x->mcp));if (x->mcp)- x->mcp->s = calloc(count, sizeof(*x->mcp->s));+ x->mcp->s = calloc(count ? count : 1, sizeof(*x->mcp->s));if (!x->mcp || !x->mcp->s)goto oom;+ x->mcp->web = web > 0;+ yyjson_arr_foreach(tools, idx, max, t)+ {+ const char *type = yyjson_get_str(yyjson_obj_get(t, "type"));+ if (!type || strcmp(type, "code_interpreter") != 0 || x->mcp->ci[0])+ continue;+ /* its container: one named, or one made now */+ const char *why = NULL;+ if (srv_container_for(r->srv, yyjson_obj_get(t, "container"),+ x->mcp->ci, sizeof x->mcp->ci, &why) != 0) {+ *ret = srv_reply_error(r, 400, "invalid_request_error",+ "invalid_value", "tools[%zu]: %s", idx, why);+ return -1;+ }+ }+ yyjson_arr_foreach(tools, idx, max, t)+ {+ const char *type = yyjson_get_str(yyjson_obj_get(t, "type"));+ if (!type || strcmp(type, "file_search") != 0)+ continue;+ yyjson_val *ids = yyjson_obj_get(t, "vector_store_ids");+ yyjson_val *mx = yyjson_obj_get(t, "max_num_results");+ yyjson_val *f = yyjson_obj_get(t, "filters");+ yyjson_val *th = yyjson_obj_get(yyjson_obj_get(t, "ranking_options"),+ "score_threshold");+ if (!yyjson_is_arr(ids) || !yyjson_arr_size(ids) ||+ (mx && (!yyjson_is_int(mx) || yyjson_get_sint(mx) < 1 ||+ yyjson_get_sint(mx) > 50))) {+ *ret = srv_reply_error(r, 400, "invalid_request_error",+ "invalid_value",+ "tools[%zu]: file_search needs "+ "'vector_store_ids', and max_num_results "+ "from 1 to 50.",+ idx);+ return -1;+ }+ size_t i2, m2;+ yyjson_val *v;+ yyjson_arr_foreach(ids, i2, m2, v)+ {+ char **nv =+ realloc(x->mcp->vs, (size_t)(x->mcp->n_vs + 1) * sizeof(*nv));+ if (!nv || !yyjson_is_str(v))+ continue;+ x->mcp->vs = nv;+ x->mcp->vs[x->mcp->n_vs++] = strdup(yyjson_get_str(v));+ }+ /* a few passages a call: they all go into the model's context */+ x->mcp->fs_max = mx ? (int)yyjson_get_sint(mx) : 6;+ if (f && !yyjson_is_null(f))+ x->mcp->fs_filters = yyjson_val_write(f, 0, NULL);+ x->mcp->fs_threshold = th ? yyjson_get_num(th) : 0;+ }yyjson_doc *cd = yyjson_read(x->context, strlen(x->context), 0);yyjson_val *ctx = yyjson_doc_get_root(cd);/* the model's tools: the functions of the request, then the servers' */@@ -434,6 +870,63 @@ int resp_mcp_read(struct srv_req *r, struct resp *x, yyjson_val *root, int *ret)yyjson_mut_val *items = yyjson_mut_arr(d);yyjson_doc_free(fd);int rc = 0;+ if (files) { /* the files' search, as a function the model calls */+ static const char FILE_TOOL[] =+ "{\"type\":\"function\",\"function\":{\"name\":\"" FILE_NAME+ "\",\"description\":\"Search the user's files for passages "+ "that answer the question. Returns the passages, numbered; cite "+ "them as [1], [2] after the facts they give.\",\"parameters\":"+ "{\"type\":\"object\",\"properties\":{\"query\":{\"type\":"+ "\"string\",\"description\":\"what to look for, in a few "+ "words or a question\"}},\"required\":[\"query\"]}}}";+ yyjson_doc *fd2 = yyjson_read(FILE_TOOL, sizeof(FILE_TOOL) - 1, 0);+ if (!fd2)+ goto oom_in;+ yyjson_mut_arr_append(all,+ yyjson_val_mut_copy(d, yyjson_doc_get_root(fd2)));+ yyjson_doc_free(fd2);+ }+ if (code) { /* the session, as a function the model calls */+ static const char CODE_TOOL[] =+ "{\"type\":\"function\",\"function\":{\"name\":\"" CODE_NAME+ "\",\"description\":\"Run Python 3 code in a sandboxed session, "+ "as in a Jupyter notebook: variables and imports stay from one "+ "call to the next, the value of the last expression is shown, "+ "and print() output is returned. numpy, scipy, sympy, "+ "matplotlib and PIL are there; there is no internet. The user's "+ "files are in /mnt/data; save there the files you make for the "+ "user (plots drawn with matplotlib are saved there by "+ "themselves). Use it for calculations, data analysis and "+ "charts.\",\"parameters\":{\"type\":\"object\",\"properties\":"+ "{\"code\":{\"type\":\"string\",\"description\":\"the Python "+ "code to run\"}},\"required\":[\"code\"]}}}";+ yyjson_doc *cd2 = yyjson_read(CODE_TOOL, sizeof(CODE_TOOL) - 1, 0);+ if (!cd2)+ goto oom_in;+ yyjson_mut_arr_append(all,+ yyjson_val_mut_copy(d, yyjson_doc_get_root(cd2)));+ yyjson_doc_free(cd2);+ }+ if (web) { /* the search, as a function the model calls */+ static const char WEB_TOOL[] =+ "{\"type\":\"function\",\"function\":{\"name\":\"" WEB_NAME+ "\",\"description\":\"Search the web (Wikipedia) for facts to "+ "answer with: people, places, history, science, definitions. "+ "Returns the matching pages' first sentences, numbered; cite "+ "them as [1], [2] after the facts they give. Not for news or "+ "today's events.\",\"parameters\":{\"type\":\"object\","+ "\"properties\":{\"query\":{\"type\":\"string\","+ "\"description\":\"a few words, as for a search engine\"},"+ "\"language\":{\"type\":\"string\",\"description\":\"the "+ "Wikipedia to search, as a code: en, it, fr...; the user's "+ "language\"}},\"required\":[\"query\"]}}}";+ yyjson_doc *wd = yyjson_read(WEB_TOOL, sizeof(WEB_TOOL) - 1, 0);+ if (!wd)+ goto oom_in;+ yyjson_mut_arr_append(all,+ yyjson_val_mut_copy(d, yyjson_doc_get_root(wd)));+ yyjson_doc_free(wd);+ }yyjson_arr_foreach(tools, idx, max, t){const char *type = yyjson_get_str(yyjson_obj_get(t, "type"));@@ -528,10 +1021,19 @@ int resp_mcp_read(struct srv_req *r, struct resp *x, yyjson_val *root, int *ret)yyjson_mut_val_write(items, YYJSON_WRITE_ALLOW_INVALID_UNICODE, &n);if (!x->j->tools || !x->done_items)goto oom_in;- /* tool_choice {type: mcp, server_label, name} */+ /* tool_choice {type: mcp, server_label, name}, {type: web_search} */yyjson_val *tc = yyjson_obj_get(root, "tool_choice");const char *tt = yyjson_get_str(yyjson_obj_get(tc, "type"));- if (tt && !strcmp(tt, "mcp")) {+ if (tt && code && !strcmp(tt, "code_interpreter")) {+ x->j->tool_choice = JANAS_LLM_TOOLS_FUNCTION;+ snprintf(x->j->tool_name, sizeof(x->j->tool_name), "%s", CODE_NAME);+ } else if (tt && files && !strcmp(tt, "file_search")) {+ x->j->tool_choice = JANAS_LLM_TOOLS_FUNCTION;+ snprintf(x->j->tool_name, sizeof(x->j->tool_name), "%s", FILE_NAME);+ } else if (tt && web && !strncmp(tt, "web_search", 10)) {+ x->j->tool_choice = JANAS_LLM_TOOLS_FUNCTION;+ snprintf(x->j->tool_name, sizeof(x->j->tool_name), "%s", WEB_NAME);+ } else if (tt && !strcmp(tt, "mcp")) {const char *l = yyjson_get_str(yyjson_obj_get(tc, "server_label"));const char *nm = yyjson_get_str(yyjson_obj_get(tc, "name"));if (l && nm) {@@ -625,6 +1127,18 @@ int resp_mcp_round(struct resp *x)int pending = 0;for (int i = 0; texts && i < ch->n_calls; i++) {const struct srv_call *k = &ch->calls[i];+ if (x->mcp->web && !strcmp(k->name, WEB_NAME)) {+ run_web(x->mcp, d, arr, k->args, &texts[i]);+ continue;+ }+ if (x->mcp->n_vs && !strcmp(k->name, FILE_NAME)) {+ run_file(x->mcp, d, arr, k->args, &texts[i]);+ continue;+ }+ if (x->mcp->ci[0] && !strcmp(k->name, CODE_NAME)) {+ run_code(x->mcp, d, arr, k->args, &texts[i]);+ continue;+ }const char *tool;struct server *s = owner(x->mcp, k->name, &tool);if (!s)diff --git a/src/server/responses_out.c b/src/server/responses_out.cindex 3d8f73d..2ad40f3 100644--- a/src/server/responses_out.c+++ b/src/server/responses_out.c@@ -61,17 +61,18 @@ static yyjson_mut_val *reasoning_item(yyjson_mut_doc *d, struct resp *x,return it;}-static yyjson_mut_val *text_part(yyjson_mut_doc *d, const struct srv_choice *ch,- int done)+static yyjson_mut_val *text_part(yyjson_mut_doc *d, const struct resp *x,+ const struct srv_choice *ch, int done){yyjson_mut_val *p = yyjson_mut_obj(d);yyjson_mut_obj_add_str(d, p, "type", "output_text");- if (done)- yyjson_mut_obj_add_val(- d, p, "text", trimmed(d, ch->text ? ch->text : "", ch->n_text));- else- yyjson_mut_obj_add_str(d, p, "text", "");- yyjson_mut_obj_add_arr(d, p, "annotations");+ yyjson_mut_val *t = done ? trimmed(d, ch->text ? ch->text : "", ch->n_text)+ : yyjson_mut_str(d, "");+ yyjson_mut_obj_add_val(d, p, "text", t);+ yyjson_mut_val *an = yyjson_mut_obj_add_arr(d, p, "annotations");+ if (done) /* the sources of web_search it cites */+ resp_web_annotate(d, an, x, yyjson_mut_get_str(t),+ yyjson_mut_get_len(t));if (done && ch->n_lp)yyjson_mut_obj_add_val(d, p, "logprobs",@@ -91,7 +92,7 @@ static yyjson_mut_val *message_item(yyjson_mut_doc *d, struct resp *x,yyjson_mut_obj_add_str(d, it, "status", done ? "completed" : "in_progress");yyjson_mut_val *c = yyjson_mut_obj_add_arr(d, it, "content");if (done)- yyjson_mut_arr_append(c, text_part(d, ch, 1));+ yyjson_mut_arr_append(c, text_part(d, x, ch, 1));return it;}@@ -264,8 +265,11 @@ void resp_keep(struct resp *x)size_t m;char *items =yyjson_mut_val_write(arr, YYJSON_WRITE_ALLOW_INVALID_UNICODE, &m);- if (items)- conv_append(x->srv, x->conv, items);+ if (!items || conv_append(x->srv, x->conv, items) != 0)+ fprintf(stderr,+ "janas-server: response %s not added to conversation "+ "%s\n",+ x->id, x->conv);free(items);yyjson_mut_doc_free(d);yyjson_doc_free(in);@@ -374,7 +378,7 @@ static int close_text(struct resp *x, const struct srv_choice *ch)d = yyjson_mut_doc_new(NULL);e = item_ev(d, x, x->item_text);yyjson_mut_obj_add_int(d, e, "content_index", 0);- yyjson_mut_obj_add_val(d, e, "part", text_part(d, ch, 1));+ yyjson_mut_obj_add_val(d, e, "part", text_part(d, x, ch, 1));bad |= event(x, d, e, "response.content_part.done");d = yyjson_mut_doc_new(NULL);bad |= item_added(x, d, message_item(d, x, ch, 1),@@ -504,7 +508,7 @@ static int produce(struct resp *x)d = yyjson_mut_doc_new(NULL);yyjson_mut_val *e = item_ev(d, x, x->item_text);yyjson_mut_obj_add_int(d, e, "content_index", 0);- yyjson_mut_obj_add_val(d, e, "part", text_part(d, ch, 0));+ yyjson_mut_obj_add_val(d, e, "part", text_part(d, x, ch, 0));bad |= event(x, d, e, "response.content_part.added");x->text_open = 1;}diff --git a/src/server/routes.inc b/src/server/routes.incindex 2a57680..e01b452 100644--- a/src/server/routes.inc+++ b/src/server/routes.inc@@ -1,6 +1,6 @@/* Generated by tools/openapi_routes.c - do not edit.OpenAI OpenAPI 2.3.0, sha256 d6085f8c39db2048f29cbc813452f20c0e7e882457d5b07c9468cec4eae7fb19,- 345 operations, 27 implemented. */+ 345 operations, 102 implemented. */JANAS_ROUTE(GET, "/agents", "listAgents", "Agents", cloud, NULL)JANAS_ROUTE(POST, "/agents", "createAgent", "Agents", cloud, NULL)JANAS_ROUTE(GET, "/agents/environments/templates", "listAgentEnvironmentTemplates", "Agents", cloud, NULL)@@ -34,24 +34,24 @@ JANAS_ROUTE(GET, "/agents/sessions/{session_id}/turns/{turn_id}", "retrieveAgentJANAS_ROUTE(DELETE, "/agents/{agent_id}", "deleteAgent", "Agents", cloud, NULL)JANAS_ROUTE(GET, "/agents/{agent_id}", "retrieveAgent", "Agents", cloud, NULL)JANAS_ROUTE(POST, "/agents/{agent_id}", "updateAgent", "Agents", cloud, NULL)-JANAS_ROUTE(GET, "/assistants", "listAssistants", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/assistants", "createAssistant", "Assistants", planned, NULL)-JANAS_ROUTE(DELETE, "/assistants/{assistant_id}", "deleteAssistant", "Assistants", planned, NULL)-JANAS_ROUTE(GET, "/assistants/{assistant_id}", "getAssistant", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/assistants/{assistant_id}", "modifyAssistant", "Assistants", planned, NULL)+JANAS_ROUTE(GET, "/assistants", "listAssistants", "Assistants", implemented, srv_assistants_list)+JANAS_ROUTE(POST, "/assistants", "createAssistant", "Assistants", implemented, srv_assistants_create)+JANAS_ROUTE(DELETE, "/assistants/{assistant_id}", "deleteAssistant", "Assistants", implemented, srv_assistants_delete)+JANAS_ROUTE(GET, "/assistants/{assistant_id}", "getAssistant", "Assistants", implemented, srv_assistants_get)+JANAS_ROUTE(POST, "/assistants/{assistant_id}", "modifyAssistant", "Assistants", implemented, srv_assistants_modify)JANAS_ROUTE(POST, "/audio/speech", "createSpeech", "Audio", other_model, NULL)-JANAS_ROUTE(POST, "/audio/transcriptions", "createTranscription", "Audio", other_model, NULL)-JANAS_ROUTE(POST, "/audio/translations", "createTranslation", "Audio", other_model, NULL)+JANAS_ROUTE(POST, "/audio/transcriptions", "createTranscription", "Audio", implemented, srv_audio_transcriptions)+JANAS_ROUTE(POST, "/audio/translations", "createTranslation", "Audio", implemented, srv_audio_translations)JANAS_ROUTE(GET, "/audio/voice_consents", "listVoiceConsents", "Audio", other_model, NULL)JANAS_ROUTE(POST, "/audio/voice_consents", "createVoiceConsent", "Audio", other_model, NULL)JANAS_ROUTE(DELETE, "/audio/voice_consents/{consent_id}", "deleteVoiceConsent", "Audio", other_model, NULL)JANAS_ROUTE(GET, "/audio/voice_consents/{consent_id}", "getVoiceConsent", "Audio", other_model, NULL)JANAS_ROUTE(POST, "/audio/voice_consents/{consent_id}", "updateVoiceConsent", "Audio", other_model, NULL)JANAS_ROUTE(POST, "/audio/voices", "createVoice", "Audio", other_model, NULL)-JANAS_ROUTE(GET, "/batches", "listBatches", "Batch", planned, NULL)-JANAS_ROUTE(POST, "/batches", "createBatch", "Batch", planned, NULL)-JANAS_ROUTE(GET, "/batches/{batch_id}", "retrieveBatch", "Batch", planned, NULL)-JANAS_ROUTE(POST, "/batches/{batch_id}/cancel", "cancelBatch", "Batch", planned, NULL)+JANAS_ROUTE(GET, "/batches", "listBatches", "Batch", implemented, srv_batches_list)+JANAS_ROUTE(POST, "/batches", "createBatch", "Batch", implemented, srv_batches_create)+JANAS_ROUTE(GET, "/batches/{batch_id}", "retrieveBatch", "Batch", implemented, srv_batches_get)+JANAS_ROUTE(POST, "/batches/{batch_id}/cancel", "cancelBatch", "Batch", implemented, srv_batches_cancel)JANAS_ROUTE(GET, "/chat/completions", "listChatCompletions", "Chat", implemented, srv_stored_list)JANAS_ROUTE(POST, "/chat/completions", "createChatCompletion", "Chat", implemented, srv_chat_create)JANAS_ROUTE(DELETE, "/chat/completions/{completion_id}", "deleteChatCompletion", "Chat", implemented, srv_stored_delete)@@ -65,15 +65,15 @@ JANAS_ROUTE(DELETE, "/chatkit/threads/{thread_id}", "DeleteThreadMethod", "-", cJANAS_ROUTE(GET, "/chatkit/threads/{thread_id}", "GetThreadMethod", "-", cloud, NULL)JANAS_ROUTE(GET, "/chatkit/threads/{thread_id}/items", "ListThreadItemsMethod", "-", cloud, NULL)JANAS_ROUTE(POST, "/completions", "createCompletion", "Completions", implemented, srv_completion_create)-JANAS_ROUTE(GET, "/containers", "ListContainers", "-", cloud, NULL)-JANAS_ROUTE(POST, "/containers", "CreateContainer", "-", cloud, NULL)-JANAS_ROUTE(DELETE, "/containers/{container_id}", "DeleteContainer", "-", cloud, NULL)-JANAS_ROUTE(GET, "/containers/{container_id}", "RetrieveContainer", "-", cloud, NULL)-JANAS_ROUTE(GET, "/containers/{container_id}/files", "ListContainerFiles", "-", cloud, NULL)-JANAS_ROUTE(POST, "/containers/{container_id}/files", "CreateContainerFile", "-", cloud, NULL)-JANAS_ROUTE(DELETE, "/containers/{container_id}/files/{file_id}", "DeleteContainerFile", "-", cloud, NULL)-JANAS_ROUTE(GET, "/containers/{container_id}/files/{file_id}", "RetrieveContainerFile", "-", cloud, NULL)-JANAS_ROUTE(GET, "/containers/{container_id}/files/{file_id}/content", "RetrieveContainerFileContent", "-", cloud, NULL)+JANAS_ROUTE(GET, "/containers", "ListContainers", "-", implemented, srv_containers_list)+JANAS_ROUTE(POST, "/containers", "CreateContainer", "-", implemented, srv_containers_create)+JANAS_ROUTE(DELETE, "/containers/{container_id}", "DeleteContainer", "-", implemented, srv_containers_delete)+JANAS_ROUTE(GET, "/containers/{container_id}", "RetrieveContainer", "-", implemented, srv_containers_get)+JANAS_ROUTE(GET, "/containers/{container_id}/files", "ListContainerFiles", "-", implemented, srv_container_files_list)+JANAS_ROUTE(POST, "/containers/{container_id}/files", "CreateContainerFile", "-", implemented, srv_container_files_create)+JANAS_ROUTE(DELETE, "/containers/{container_id}/files/{file_id}", "DeleteContainerFile", "-", implemented, srv_container_files_delete)+JANAS_ROUTE(GET, "/containers/{container_id}/files/{file_id}", "RetrieveContainerFile", "-", implemented, srv_container_files_get)+JANAS_ROUTE(GET, "/containers/{container_id}/files/{file_id}/content", "RetrieveContainerFileContent", "-", implemented, srv_container_files_content)JANAS_ROUTE(POST, "/content_provenance_checks", "Createcontentprovenancecheck", "-", cloud, NULL)JANAS_ROUTE(POST, "/conversations", "createConversation", "Conversations", implemented, srv_conv_create)JANAS_ROUTE(DELETE, "/conversations/{conversation_id}", "deleteConversation", "Conversations", implemented, srv_conv_delete)@@ -84,23 +84,23 @@ JANAS_ROUTE(POST, "/conversations/{conversation_id}/items", "createConversationIJANAS_ROUTE(DELETE, "/conversations/{conversation_id}/items/{item_id}", "deleteConversationItem", "Conversations", implemented, srv_conv_item_delete)JANAS_ROUTE(GET, "/conversations/{conversation_id}/items/{item_id}", "getConversationItem", "Conversations", implemented, srv_conv_item_get)JANAS_ROUTE(POST, "/embeddings", "createEmbedding", "Embeddings", implemented, srv_embeddings_create)-JANAS_ROUTE(GET, "/evals", "listEvals", "Evals", planned, NULL)-JANAS_ROUTE(POST, "/evals", "createEval", "Evals", planned, NULL)-JANAS_ROUTE(DELETE, "/evals/{eval_id}", "deleteEval", "Evals", planned, NULL)-JANAS_ROUTE(GET, "/evals/{eval_id}", "getEval", "Evals", planned, NULL)-JANAS_ROUTE(POST, "/evals/{eval_id}", "updateEval", "Evals", planned, NULL)-JANAS_ROUTE(GET, "/evals/{eval_id}/runs", "getEvalRuns", "Evals", planned, NULL)-JANAS_ROUTE(POST, "/evals/{eval_id}/runs", "createEvalRun", "Evals", planned, NULL)-JANAS_ROUTE(DELETE, "/evals/{eval_id}/runs/{run_id}", "deleteEvalRun", "Evals", planned, NULL)-JANAS_ROUTE(GET, "/evals/{eval_id}/runs/{run_id}", "getEvalRun", "Evals", planned, NULL)-JANAS_ROUTE(POST, "/evals/{eval_id}/runs/{run_id}", "cancelEvalRun", "Evals", planned, NULL)-JANAS_ROUTE(GET, "/evals/{eval_id}/runs/{run_id}/output_items", "getEvalRunOutputItems", "Evals", planned, NULL)-JANAS_ROUTE(GET, "/evals/{eval_id}/runs/{run_id}/output_items/{output_item_id}", "getEvalRunOutputItem", "Evals", planned, NULL)-JANAS_ROUTE(GET, "/files", "listFiles", "Files", planned, NULL)-JANAS_ROUTE(POST, "/files", "createFile", "Files", planned, NULL)-JANAS_ROUTE(DELETE, "/files/{file_id}", "deleteFile", "Files", planned, NULL)-JANAS_ROUTE(GET, "/files/{file_id}", "retrieveFile", "Files", planned, NULL)-JANAS_ROUTE(GET, "/files/{file_id}/content", "downloadFile", "Files", planned, NULL)+JANAS_ROUTE(GET, "/evals", "listEvals", "Evals", implemented, srv_evals_list)+JANAS_ROUTE(POST, "/evals", "createEval", "Evals", implemented, srv_evals_create)+JANAS_ROUTE(DELETE, "/evals/{eval_id}", "deleteEval", "Evals", implemented, srv_evals_delete)+JANAS_ROUTE(GET, "/evals/{eval_id}", "getEval", "Evals", implemented, srv_evals_get)+JANAS_ROUTE(POST, "/evals/{eval_id}", "updateEval", "Evals", implemented, srv_evals_update)+JANAS_ROUTE(GET, "/evals/{eval_id}/runs", "getEvalRuns", "Evals", implemented, srv_eval_runs_list)+JANAS_ROUTE(POST, "/evals/{eval_id}/runs", "createEvalRun", "Evals", implemented, srv_eval_runs_create)+JANAS_ROUTE(DELETE, "/evals/{eval_id}/runs/{run_id}", "deleteEvalRun", "Evals", implemented, srv_eval_runs_delete)+JANAS_ROUTE(GET, "/evals/{eval_id}/runs/{run_id}", "getEvalRun", "Evals", implemented, srv_eval_runs_get)+JANAS_ROUTE(POST, "/evals/{eval_id}/runs/{run_id}", "cancelEvalRun", "Evals", implemented, srv_eval_runs_cancel)+JANAS_ROUTE(GET, "/evals/{eval_id}/runs/{run_id}/output_items", "getEvalRunOutputItems", "Evals", implemented, srv_eval_items_list)+JANAS_ROUTE(GET, "/evals/{eval_id}/runs/{run_id}/output_items/{output_item_id}", "getEvalRunOutputItem", "Evals", implemented, srv_eval_items_get)+JANAS_ROUTE(GET, "/files", "listFiles", "Files", implemented, srv_files_list_req)+JANAS_ROUTE(POST, "/files", "createFile", "Files", implemented, srv_files_create)+JANAS_ROUTE(DELETE, "/files/{file_id}", "deleteFile", "Files", implemented, srv_files_delete_req)+JANAS_ROUTE(GET, "/files/{file_id}", "retrieveFile", "Files", implemented, srv_files_retrieve)+JANAS_ROUTE(GET, "/files/{file_id}/content", "downloadFile", "Files", implemented, srv_files_content)JANAS_ROUTE(POST, "/fine_tuning/alpha/graders/run", "runGrader", "Fine-tuning", training, NULL)JANAS_ROUTE(POST, "/fine_tuning/alpha/graders/validate", "validateGrader", "Fine-tuning", training, NULL)JANAS_ROUTE(GET, "/fine_tuning/checkpoints/{fine_tuned_model_checkpoint}/permissions", "listFineTuningCheckpointPermissions", "Fine-tuning", training, NULL)@@ -281,28 +281,28 @@ JANAS_ROUTE(POST, "/skills/{skill_id}/versions", "CreateSkillVersion", "Skills",JANAS_ROUTE(DELETE, "/skills/{skill_id}/versions/{version}", "DeleteSkillVersion", "Skills", cloud, NULL)JANAS_ROUTE(GET, "/skills/{skill_id}/versions/{version}", "GetSkillVersion", "Skills", cloud, NULL)JANAS_ROUTE(GET, "/skills/{skill_id}/versions/{version}/content", "GetSkillVersionContent", "Skills", cloud, NULL)-JANAS_ROUTE(POST, "/threads", "createThread", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/threads/runs", "createThreadAndRun", "Assistants", planned, NULL)-JANAS_ROUTE(DELETE, "/threads/{thread_id}", "deleteThread", "Assistants", planned, NULL)-JANAS_ROUTE(GET, "/threads/{thread_id}", "getThread", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/threads/{thread_id}", "modifyThread", "Assistants", planned, NULL)-JANAS_ROUTE(GET, "/threads/{thread_id}/messages", "listMessages", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/threads/{thread_id}/messages", "createMessage", "Assistants", planned, NULL)-JANAS_ROUTE(DELETE, "/threads/{thread_id}/messages/{message_id}", "deleteMessage", "Assistants", planned, NULL)-JANAS_ROUTE(GET, "/threads/{thread_id}/messages/{message_id}", "getMessage", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/threads/{thread_id}/messages/{message_id}", "modifyMessage", "Assistants", planned, NULL)-JANAS_ROUTE(GET, "/threads/{thread_id}/runs", "listRuns", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/threads/{thread_id}/runs", "createRun", "Assistants", planned, NULL)-JANAS_ROUTE(GET, "/threads/{thread_id}/runs/{run_id}", "getRun", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/threads/{thread_id}/runs/{run_id}", "modifyRun", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/threads/{thread_id}/runs/{run_id}/cancel", "cancelRun", "Assistants", planned, NULL)-JANAS_ROUTE(GET, "/threads/{thread_id}/runs/{run_id}/steps", "listRunSteps", "Assistants", planned, NULL)-JANAS_ROUTE(GET, "/threads/{thread_id}/runs/{run_id}/steps/{step_id}", "getRunStep", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/threads/{thread_id}/runs/{run_id}/submit_tool_outputs", "submitToolOuputsToRun", "Assistants", planned, NULL)-JANAS_ROUTE(POST, "/uploads", "createUpload", "Uploads", planned, NULL)-JANAS_ROUTE(POST, "/uploads/{upload_id}/cancel", "cancelUpload", "Uploads", planned, NULL)-JANAS_ROUTE(POST, "/uploads/{upload_id}/complete", "completeUpload", "Uploads", planned, NULL)-JANAS_ROUTE(POST, "/uploads/{upload_id}/parts", "addUploadPart", "Uploads", planned, NULL)+JANAS_ROUTE(POST, "/threads", "createThread", "Assistants", implemented, srv_threads_create)+JANAS_ROUTE(POST, "/threads/runs", "createThreadAndRun", "Assistants", implemented, srv_runs_create_thread)+JANAS_ROUTE(DELETE, "/threads/{thread_id}", "deleteThread", "Assistants", implemented, srv_threads_delete)+JANAS_ROUTE(GET, "/threads/{thread_id}", "getThread", "Assistants", implemented, srv_threads_get)+JANAS_ROUTE(POST, "/threads/{thread_id}", "modifyThread", "Assistants", implemented, srv_threads_modify)+JANAS_ROUTE(GET, "/threads/{thread_id}/messages", "listMessages", "Assistants", implemented, srv_messages_list)+JANAS_ROUTE(POST, "/threads/{thread_id}/messages", "createMessage", "Assistants", implemented, srv_messages_create)+JANAS_ROUTE(DELETE, "/threads/{thread_id}/messages/{message_id}", "deleteMessage", "Assistants", implemented, srv_messages_delete)+JANAS_ROUTE(GET, "/threads/{thread_id}/messages/{message_id}", "getMessage", "Assistants", implemented, srv_messages_get)+JANAS_ROUTE(POST, "/threads/{thread_id}/messages/{message_id}", "modifyMessage", "Assistants", implemented, srv_messages_modify)+JANAS_ROUTE(GET, "/threads/{thread_id}/runs", "listRuns", "Assistants", implemented, srv_runs_list)+JANAS_ROUTE(POST, "/threads/{thread_id}/runs", "createRun", "Assistants", implemented, srv_runs_create)+JANAS_ROUTE(GET, "/threads/{thread_id}/runs/{run_id}", "getRun", "Assistants", implemented, srv_runs_get)+JANAS_ROUTE(POST, "/threads/{thread_id}/runs/{run_id}", "modifyRun", "Assistants", implemented, srv_runs_modify)+JANAS_ROUTE(POST, "/threads/{thread_id}/runs/{run_id}/cancel", "cancelRun", "Assistants", implemented, srv_runs_cancel)+JANAS_ROUTE(GET, "/threads/{thread_id}/runs/{run_id}/steps", "listRunSteps", "Assistants", implemented, srv_run_steps_list)+JANAS_ROUTE(GET, "/threads/{thread_id}/runs/{run_id}/steps/{step_id}", "getRunStep", "Assistants", implemented, srv_run_steps_get)+JANAS_ROUTE(POST, "/threads/{thread_id}/runs/{run_id}/submit_tool_outputs", "submitToolOuputsToRun", "Assistants", implemented, srv_runs_submit)+JANAS_ROUTE(POST, "/uploads", "createUpload", "Uploads", implemented, srv_uploads_create)+JANAS_ROUTE(POST, "/uploads/{upload_id}/cancel", "cancelUpload", "Uploads", implemented, srv_uploads_cancel)+JANAS_ROUTE(POST, "/uploads/{upload_id}/complete", "completeUpload", "Uploads", implemented, srv_uploads_complete)+JANAS_ROUTE(POST, "/uploads/{upload_id}/parts", "addUploadPart", "Uploads", implemented, srv_uploads_part)JANAS_ROUTE(GET, "/vaults", "listVaults", "Vaults", cloud, NULL)JANAS_ROUTE(POST, "/vaults", "createVault", "Vaults", cloud, NULL)JANAS_ROUTE(DELETE, "/vaults/{vault_id}", "deleteVault", "Vaults", cloud, NULL)@@ -312,22 +312,22 @@ JANAS_ROUTE(POST, "/vaults/{vault_id}/credentials", "createVaultCredential", "VaJANAS_ROUTE(DELETE, "/vaults/{vault_id}/credentials/{credential_id}", "deleteVaultCredential", "Vaults", cloud, NULL)JANAS_ROUTE(GET, "/vaults/{vault_id}/credentials/{credential_id}", "retrieveVaultCredential", "Vaults", cloud, NULL)JANAS_ROUTE(POST, "/vaults/{vault_id}/credentials/{credential_id}", "rotateVaultCredential", "Vaults", cloud, NULL)-JANAS_ROUTE(GET, "/vector_stores", "listVectorStores", "Vector stores", planned, NULL)-JANAS_ROUTE(POST, "/vector_stores", "createVectorStore", "Vector stores", planned, NULL)-JANAS_ROUTE(DELETE, "/vector_stores/{vector_store_id}", "deleteVectorStore", "Vector stores", planned, NULL)-JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}", "getVectorStore", "Vector stores", planned, NULL)-JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}", "modifyVectorStore", "Vector stores", planned, NULL)-JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}/file_batches", "createVectorStoreFileBatch", "Vector stores", planned, NULL)-JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}/file_batches/{batch_id}", "getVectorStoreFileBatch", "Vector stores", planned, NULL)-JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}/file_batches/{batch_id}/cancel", "cancelVectorStoreFileBatch", "Vector stores", planned, NULL)-JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}/file_batches/{batch_id}/files", "listFilesInVectorStoreBatch", "Vector stores", planned, NULL)-JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}/files", "listVectorStoreFiles", "Vector stores", planned, NULL)-JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}/files", "createVectorStoreFile", "Vector stores", planned, NULL)-JANAS_ROUTE(DELETE, "/vector_stores/{vector_store_id}/files/{file_id}", "deleteVectorStoreFile", "Vector stores", planned, NULL)-JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}/files/{file_id}", "getVectorStoreFile", "Vector stores", planned, NULL)-JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}/files/{file_id}", "updateVectorStoreFileAttributes", "Vector stores", planned, NULL)-JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}/files/{file_id}/content", "retrieveVectorStoreFileContent", "Vector stores", planned, NULL)-JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}/search", "searchVectorStore", "Vector stores", planned, NULL)+JANAS_ROUTE(GET, "/vector_stores", "listVectorStores", "Vector stores", implemented, srv_vs_list)+JANAS_ROUTE(POST, "/vector_stores", "createVectorStore", "Vector stores", implemented, srv_vs_create)+JANAS_ROUTE(DELETE, "/vector_stores/{vector_store_id}", "deleteVectorStore", "Vector stores", implemented, srv_vs_delete)+JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}", "getVectorStore", "Vector stores", implemented, srv_vs_get)+JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}", "modifyVectorStore", "Vector stores", implemented, srv_vs_modify)+JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}/file_batches", "createVectorStoreFileBatch", "Vector stores", implemented, srv_vs_batch_create)+JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}/file_batches/{batch_id}", "getVectorStoreFileBatch", "Vector stores", implemented, srv_vs_batch_get)+JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}/file_batches/{batch_id}/cancel", "cancelVectorStoreFileBatch", "Vector stores", implemented, srv_vs_batch_cancel)+JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}/file_batches/{batch_id}/files", "listFilesInVectorStoreBatch", "Vector stores", implemented, srv_vs_batch_files)+JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}/files", "listVectorStoreFiles", "Vector stores", implemented, srv_vs_files_list)+JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}/files", "createVectorStoreFile", "Vector stores", implemented, srv_vs_file_create)+JANAS_ROUTE(DELETE, "/vector_stores/{vector_store_id}/files/{file_id}", "deleteVectorStoreFile", "Vector stores", implemented, srv_vs_file_delete)+JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}/files/{file_id}", "getVectorStoreFile", "Vector stores", implemented, srv_vs_file_get)+JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}/files/{file_id}", "updateVectorStoreFileAttributes", "Vector stores", implemented, srv_vs_file_update)+JANAS_ROUTE(GET, "/vector_stores/{vector_store_id}/files/{file_id}/content", "retrieveVectorStoreFileContent", "Vector stores", implemented, srv_vs_file_content)+JANAS_ROUTE(POST, "/vector_stores/{vector_store_id}/search", "searchVectorStore", "Vector stores", implemented, srv_vs_search)JANAS_ROUTE(GET, "/videos", "ListVideos", "Videos", other_model, NULL)JANAS_ROUTE(POST, "/videos", "createVideo", "Videos", other_model, NULL)JANAS_ROUTE(POST, "/videos/characters", "CreateVideoCharacter", "Videos", other_model, NULL)diff --git a/src/server/runs.c b/src/server/runs.cnew file mode 100644index 0000000..91ffa52--- /dev/null+++ b/src/server/runs.c@@ -0,0 +1,1626 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * runs.c - the runs of the Assistants API and their steps. A run goes on a+ * thread of its own: the thread's messages, the assistant's instructions+ * and tools (the run's where it gives them) made into a request to+ * /v1/responses, answered inside the server; its output becomes the run's+ * steps - a file_search call, the message written, added to the thread+ * with its citations as file_citation annotations - or, when the model+ * calls functions, the run waits (requires_action) for their outputs+ * (submit_tool_outputs) and goes on with them. The run is kept after each+ * step, its steps and the items it added to the input (calls and their+ * outputs) as its aux text: a server stopped mid-run takes it up again at+ * the next start.+ */+#include "assistants.h"++#include <pthread.h>+#include <stdatomic.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <time.h>+#include <unistd.h>++#include "containers.h"+#include "files_store.h"+#include "llm/json.h"++#define RUN_EXPIRES 600 /* seconds a run may wait for tool outputs */++static pthread_mutex_t live_mu = PTHREAD_MUTEX_INITIALIZER;+static pthread_cond_t live_cv = PTHREAD_COND_INITIALIZER;+static int live, stopping;+static struct srv *server;++/* the passes running, each with the flag its model's job watches */+struct pass {+ char id[64];+ _Atomic int cancel;+ struct pass *next;+ /* the pass reading /responses streamed: the run's thread and+ assistant, the events not yet whole, the response at the end */+ char thread[64], asst[64];+ struct janas_buf sse;+ yyjson_doc *final;+ char err[256];+ /* the message being written, made at its first piece of text */+ char msg_id[48], step_id[48];+ int64_t msg_at;+ int msg_open;+ /* the search steps told already, by the id of their item */+ char fs_item[8][48], fs_step[8][48];+ int n_fs;+ /* the thread's container, and its files made Files (the images and+ the files the answer names), each once */+ char cid[48];+ char conv_from[16][48], conv_to[16][48];+ int n_conv;+};+static struct pass *passes;++static int bad(struct srv_req *r, const char *msg)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_value",+ "%s", msg);+}++static int not_found(struct srv_req *r, const char *what, const char *id)+{+ return srv_reply_error(r, 404, "invalid_request_error", "not_found",+ "No %s found with id '%s'.", what, id);+}++static int reply_val(struct srv_req *r, yyjson_mut_val *o)+{+ size_t n = 0;+ char *t = yyjson_mut_val_write(o, 0, &n);+ if (!t)+ return srv_reply_error(r, 500, "server_error", NULL, "out of memory");+ return srv_reply_json(r, 200, t, n);+}++static void set_str(yyjson_mut_doc *d, yyjson_mut_val *o, const char *k,+ const char *v)+{+ yyjson_mut_obj_remove_key(o, k);+ if (v)+ yyjson_mut_obj_add_strcpy(d, o, k, v);+ else+ yyjson_mut_obj_add_null(d, o, k);+}++static void set_int(yyjson_mut_doc *d, yyjson_mut_val *o, const char *k,+ int64_t v)+{+ yyjson_mut_obj_remove_key(o, k);+ yyjson_mut_obj_add_int(d, o, k, v);+}++static void set_val(yyjson_mut_doc *d, yyjson_mut_val *o, const char *k,+ yyjson_mut_val *v)+{+ yyjson_mut_obj_remove_key(o, k);+ yyjson_mut_obj_add_val(d, o, k, v ? v : yyjson_mut_null(d));+}++/* ---- a run as kept: its object and its aux {steps, items} ---- */++struct run {+ yyjson_mut_doc *d;+ yyjson_mut_val *o; /* the run */+ yyjson_mut_val *steps; /* its steps */+ yyjson_mut_val *items; /* what it added to the input */+};++static void run_free(struct run *x)+{+ yyjson_mut_doc_free(x->d);+ memset(x, 0, sizeof(*x));+}++/* (as_lock held) 0, or -1 when there is no such run */+static int run_load(struct srv *s, const char *id, struct run *x)+{+ memset(x, 0, sizeof(*x));+ char *aux = NULL;+ char *t = as_get(s, AS_RUN, id, &aux);+ yyjson_doc *rd = t ? yyjson_read(t, strlen(t), 0) : NULL;+ yyjson_doc *ad = aux ? yyjson_read(aux, strlen(aux), 0) : NULL;+ free(t);+ free(aux);+ if (!rd) {+ yyjson_doc_free(ad);+ return -1;+ }+ x->d = yyjson_mut_doc_new(NULL);+ x->o = yyjson_val_mut_copy(x->d, yyjson_doc_get_root(rd));+ yyjson_val *steps = yyjson_obj_get(yyjson_doc_get_root(ad), "steps");+ yyjson_val *items = yyjson_obj_get(yyjson_doc_get_root(ad), "items");+ x->steps = steps ? yyjson_val_mut_copy(x->d, steps) : yyjson_mut_arr(x->d);+ x->items = items ? yyjson_val_mut_copy(x->d, items) : yyjson_mut_arr(x->d);+ yyjson_doc_free(rd);+ yyjson_doc_free(ad);+ return 0;+}++/* (as_lock held) */+static int run_save(struct srv *s, struct run *x, int fresh)+{+ yyjson_mut_doc *ad = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *a = yyjson_mut_obj(ad);+ yyjson_mut_obj_add_val(ad, a, "steps",+ yyjson_mut_val_mut_copy(ad, x->steps));+ yyjson_mut_obj_add_val(ad, a, "items",+ yyjson_mut_val_mut_copy(ad, x->items));+ char *aux = yyjson_mut_val_write(a, 0, NULL);+ yyjson_mut_doc_free(ad);+ const char *id = yyjson_mut_get_str(yyjson_mut_obj_get(x->o, "id"));+ int rc = aux ? as_save(s, AS_RUN, id, x->o, aux, fresh) : -1;+ free(aux);+ return rc;+}++static const char *run_status(const struct run *x)+{+ return yyjson_mut_get_str(yyjson_mut_obj_get(x->o, "status"));+}++static const char *run_str(const struct run *x, const char *k)+{+ return yyjson_mut_get_str(yyjson_mut_obj_get(x->o, k));+}++/* A run waiting for tool outputs past its time: expired. */+static void run_expire(struct run *x)+{+ const char *st = run_status(x);+ int64_t exp = yyjson_mut_get_sint(yyjson_mut_obj_get(x->o, "expires_at"));+ if (st && !strcmp(st, "requires_action") && exp &&+ (int64_t)time(NULL) > exp) {+ set_str(x->d, x->o, "status", "expired");+ set_val(x->d, x->o, "required_action", NULL);+ }+}++static void add_usage(yyjson_mut_doc *d, yyjson_mut_val *o, int64_t in,+ int64_t out)+{+ yyjson_mut_val *u = yyjson_mut_obj_get(o, "usage");+ if (u && yyjson_mut_is_obj(u)) {+ in += yyjson_mut_get_sint(yyjson_mut_obj_get(u, "prompt_tokens"));+ out += yyjson_mut_get_sint(yyjson_mut_obj_get(u, "completion_tokens"));+ }+ yyjson_mut_val *nu = yyjson_mut_obj(d);+ yyjson_mut_obj_add_int(d, nu, "prompt_tokens", in);+ yyjson_mut_obj_add_int(d, nu, "completion_tokens", out);+ yyjson_mut_obj_add_int(d, nu, "total_tokens", in + out);+ set_val(d, o, "usage", nu);+}++/* A step's object (id NULL: a new one; at 0: now). */+static yyjson_mut_val *step_obj(yyjson_mut_doc *d, const char *id, int64_t at,+ const char *run, const char *asst,+ const char *thread, const char *type,+ const char *status)+{+ yyjson_mut_val *s = yyjson_mut_obj(d);+ char nid[48];+ if (!id) {+ srv_new_id(nid, sizeof nid, "step_");+ id = nid;+ }+ int64_t now = (int64_t)time(NULL);+ yyjson_mut_obj_add_strcpy(d, s, "id", id);+ yyjson_mut_obj_add_str(d, s, "object", "thread.run.step");+ yyjson_mut_obj_add_int(d, s, "created_at", at ? at : now);+ yyjson_mut_obj_add_strcpy(d, s, "run_id", run);+ yyjson_mut_obj_add_strcpy(d, s, "assistant_id", asst);+ yyjson_mut_obj_add_strcpy(d, s, "thread_id", thread);+ yyjson_mut_obj_add_strcpy(d, s, "type", type);+ yyjson_mut_obj_add_strcpy(d, s, "status", status);+ yyjson_mut_obj_add_null(d, s, "cancelled_at");+ if (!strcmp(status, "completed"))+ yyjson_mut_obj_add_int(d, s, "completed_at", now);+ else+ yyjson_mut_obj_add_null(d, s, "completed_at");+ yyjson_mut_obj_add_null(d, s, "expired_at");+ yyjson_mut_obj_add_null(d, s, "failed_at");+ yyjson_mut_obj_add_null(d, s, "last_error");+ yyjson_mut_obj_add_obj(d, s, "metadata");+ yyjson_mut_obj_add_null(d, s, "usage");+ return s;+}++/* A new step of the run (id NULL: a new one). */+static yyjson_mut_val *step_new(struct run *x, const char *id, int64_t at,+ const char *type, const char *status)+{+ yyjson_mut_val *s =+ step_obj(x->d, id, at, run_str(x, "id"), run_str(x, "assistant_id"),+ run_str(x, "thread_id"), type, status);+ yyjson_mut_arr_append(x->steps, s);+ return s;+}++/* A file_search_call item of /responses as the details of a step. */+static yyjson_mut_val *search_details(yyjson_mut_doc *d, yyjson_val *it)+{+ yyjson_mut_val *det = yyjson_mut_obj(d);+ yyjson_mut_obj_add_str(d, det, "type", "tool_calls");+ yyjson_mut_val *tc = yyjson_mut_obj_add_arr(d, det, "tool_calls");+ yyjson_mut_val *c1 = yyjson_mut_arr_add_obj(d, tc);+ const char *cid = yyjson_get_str(yyjson_obj_get(it, "id"));+ yyjson_mut_obj_add_strcpy(d, c1, "id", cid ? cid : "");+ yyjson_mut_obj_add_str(d, c1, "type", "file_search");+ yyjson_mut_val *fs = yyjson_mut_obj_add_obj(d, c1, "file_search");+ yyjson_mut_val *ro = yyjson_mut_obj_add_obj(d, fs, "ranking_options");+ yyjson_mut_obj_add_str(d, ro, "ranker", "default_2024_08_21");+ yyjson_mut_obj_add_real(d, ro, "score_threshold", 0);+ yyjson_mut_val *res = yyjson_mut_obj_add_arr(d, fs, "results");+ size_t k, m;+ yyjson_val *h;+ yyjson_arr_foreach(yyjson_obj_get(it, "results"), k, m, h)+ {+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, res);+ const char *fid = yyjson_get_str(yyjson_obj_get(h, "file_id"));+ const char *fn = yyjson_get_str(yyjson_obj_get(h, "filename"));+ const char *tx = yyjson_get_str(yyjson_obj_get(h, "text"));+ yyjson_mut_obj_add_strcpy(d, o, "file_id", fid ? fid : "");+ yyjson_mut_obj_add_strcpy(d, o, "file_name", fn ? fn : "");+ yyjson_mut_obj_add_real(d, o, "score",+ yyjson_get_num(yyjson_obj_get(h, "score")));+ yyjson_mut_val *ct = yyjson_mut_obj_add_arr(d, o, "content");+ yyjson_mut_val *c2 = yyjson_mut_arr_add_obj(d, ct);+ yyjson_mut_obj_add_str(d, c2, "type", "text");+ yyjson_mut_obj_add_strcpy(d, c2, "text", tx ? tx : "");+ }+ return det;+}++/* A message_creation step's details. */+static yyjson_mut_val *message_details(yyjson_mut_doc *d, const char *msg)+{+ yyjson_mut_val *det = yyjson_mut_obj(d);+ yyjson_mut_obj_add_str(d, det, "type", "message_creation");+ yyjson_mut_val *mc = yyjson_mut_obj_add_obj(d, det, "message_creation");+ yyjson_mut_obj_add_strcpy(d, mc, "message_id", msg);+ return det;+}++/* ---- the request to /v1/responses ---- */++/* The run's tools as the Responses API takes them; file_search with the+ vector stores of the assistant and of the thread. */+static yyjson_mut_val *tools_for(yyjson_mut_doc *d, yyjson_val *tools,+ yyjson_val *asst, yyjson_val *thread,+ const char *cid)+{+ yyjson_mut_val *out = yyjson_mut_arr(d);+ size_t i, n;+ yyjson_val *t;+ yyjson_arr_foreach(tools, i, n, t)+ {+ const char *type = yyjson_get_str(yyjson_obj_get(t, "type"));+ if (type && !strcmp(type, "function")) {+ yyjson_val *f = yyjson_obj_get(t, "function");+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, out);+ yyjson_mut_obj_add_str(d, o, "type", "function");+ static const char *const K[] = {"name", "description", "parameters",+ "strict"};+ for (int k = 0; k < 4; k++) {+ yyjson_val *v = yyjson_obj_get(f, K[k]);+ if (v && !yyjson_is_null(v))+ yyjson_mut_obj_add_val(d, o, K[k],+ yyjson_val_mut_copy(d, v));+ }+ } else if (type && !strcmp(type, "code_interpreter")) {+ if (!cid || !*cid)+ continue;+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, out);+ yyjson_mut_obj_add_str(d, o, "type", "code_interpreter");+ yyjson_mut_obj_add_strcpy(d, o, "container", cid);+ } else if (type && !strcmp(type, "file_search")) {+ yyjson_mut_val *ids = yyjson_mut_arr(d);+ yyjson_val *src[2] = {asst, thread};+ for (int k = 0; k < 2; k++) {+ size_t j, m;+ yyjson_val *v;+ yyjson_arr_foreach(+ yyjson_obj_get(+ yyjson_obj_get(yyjson_obj_get(src[k], "tool_resources"),+ "file_search"),+ "vector_store_ids"),+ j, m, v)+ yyjson_mut_arr_append(ids, yyjson_val_mut_copy(d, v));+ }+ if (!yyjson_mut_arr_size(ids))+ continue; /* nothing to search */+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, out);+ yyjson_mut_obj_add_str(d, o, "type", "file_search");+ yyjson_mut_obj_add_val(d, o, "vector_store_ids", ids);+ yyjson_val *fs = yyjson_obj_get(t, "file_search");+ yyjson_val *mx = yyjson_obj_get(fs, "max_num_results");+ if (yyjson_is_int(mx))+ yyjson_mut_obj_add_val(d, o, "max_num_results",+ yyjson_val_mut_copy(d, mx));+ yyjson_val *ro = yyjson_obj_get(fs, "ranking_options");+ if (yyjson_is_obj(ro))+ yyjson_mut_obj_add_val(d, o, "ranking_options",+ yyjson_val_mut_copy(d, ro));+ }+ }+ return out;+}++#define AS_THREAD_CONTAINER "thread.container"++/* The thread's container for code_interpreter (as_lock held): kept from+ run to run (its variables too), made again once it expired; the files of+ the assistant's and the thread's code_interpreter resources copied in+ once. Its id into cid, or "" when it cannot be had. */+static void thread_container(struct srv *s, const char *thread,+ yyjson_val *asst, yyjson_val *th, char *cid,+ size_t cap)+{+ cid[0] = 0;+ char *t = as_get(s, AS_THREAD_CONTAINER, thread, NULL);+ yyjson_doc *md = t ? yyjson_read(t, strlen(t), 0) : NULL;+ free(t);+ yyjson_mut_doc *d =+ md ? yyjson_doc_mut_copy(md, NULL) : yyjson_mut_doc_new(NULL);+ yyjson_doc_free(md);+ yyjson_mut_val *o = yyjson_mut_doc_get_root(d);+ if (!o) {+ o = yyjson_mut_obj(d);+ yyjson_mut_doc_set_root(d, o);+ }+ const char *have = yyjson_mut_get_str(yyjson_mut_obj_get(o, "container"));+ const char *why = NULL;+ if (have) {+ yyjson_mut_val *hv = yyjson_mut_strcpy(d, have);+ yyjson_doc *hd = yyjson_mut_val_imut_copy(hv, NULL);+ if (srv_container_for(s, yyjson_doc_get_root(hd), cid, cap, &why) != 0)+ have = NULL; /* expired: a new one, the files again */+ yyjson_doc_free(hd);+ }+ if (!have) {+ static const char AUTO[] = "{\"type\":\"auto\"}";+ yyjson_doc *spec = yyjson_read(AUTO, sizeof AUTO - 1, 0);+ if (srv_container_for(s, yyjson_doc_get_root(spec), cid, cap, &why) !=+ 0)+ cid[0] = 0;+ yyjson_doc_free(spec);+ yyjson_mut_obj_remove_key(o, "container");+ yyjson_mut_obj_add_strcpy(d, o, "container", cid);+ yyjson_mut_obj_remove_key(o, "files");+ yyjson_mut_obj_add_arr(d, o, "files");+ }+ if (cid[0]) {+ yyjson_mut_val *done = yyjson_mut_obj_get(o, "files");+ yyjson_val *src[2] = {asst, th};+ for (int k = 0; k < 2; k++) {+ size_t i, n;+ yyjson_val *v;+ yyjson_arr_foreach(+ yyjson_obj_get(+ yyjson_obj_get(yyjson_obj_get(src[k], "tool_resources"),+ "code_interpreter"),+ "file_ids"),+ i, n, v)+ {+ int copied = 0;+ size_t q, qn;+ yyjson_mut_val *c;+ yyjson_mut_arr_foreach(done, q, qn, c)+ {+ copied |= yyjson_mut_equals_str(c, yyjson_get_str(v));+ }+ if (!copied && yyjson_is_str(v) &&+ srv_container_add_file(s, cid, yyjson_get_str(v), &why) ==+ 0)+ yyjson_mut_arr_add_strcpy(d, done, yyjson_get_str(v));+ }+ }+ }+ char *out = yyjson_mut_write(d, 0, NULL);+ if (out)+ srv_store_put(s->store, AS_THREAD_CONTAINER, thread,+ (int64_t)time(NULL), out, NULL);+ free(out);+ yyjson_mut_doc_free(d);+}++/* Whether the run's tools have one of type t. */+static int has_tool(yyjson_val *tools, const char *t)+{+ size_t i, n;+ yyjson_val *v;+ yyjson_arr_foreach(tools, i, n, v)+ {+ if (yyjson_equals_str(yyjson_obj_get(v, "type"), t))+ return 1;+ }+ return 0;+}++/* The body of the request for the run as it stands (as_lock held); the+ thread's container, when the run has code_interpreter, into cid. */+static char *request_for(struct srv *s, struct run *x, char *cid, size_t cap)+{+ char *at = as_get(s, AS_ASSISTANT, run_str(x, "assistant_id"), NULL);+ char *maux = NULL;+ char *tt = as_get(s, AS_THREAD, run_str(x, "thread_id"), &maux);+ yyjson_doc *ad = at ? yyjson_read(at, strlen(at), 0) : NULL;+ yyjson_doc *td = tt ? yyjson_read(tt, strlen(tt), 0) : NULL;+ yyjson_doc *md = maux ? yyjson_read(maux, strlen(maux), 0) : NULL;+ free(at);+ free(tt);+ free(maux);+ char *rt = yyjson_mut_val_write(x->o, 0, NULL);+ yyjson_doc *rd = rt ? yyjson_read(rt, strlen(rt), 0) : NULL;+ free(rt);+ yyjson_val *run = yyjson_doc_get_root(rd);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_doc_set_root(d, o);+ yyjson_mut_obj_add_strcpy(d, o, "model", s->cfg.model_id);+ yyjson_mut_obj_add_bool(d, o, "store", false);+ yyjson_mut_obj_add_bool(d, o, "stream", true); /* the text told as it+ comes (on_data) */+ const char *ins = yyjson_get_str(yyjson_obj_get(run, "instructions"));+ if (ins && *ins)+ yyjson_mut_obj_add_strcpy(d, o, "instructions", ins);+ /* the messages, the last ones only when the run says so */+ yyjson_mut_val *in = yyjson_mut_obj_add_arr(d, o, "input");+ yyjson_val *msgs = yyjson_doc_get_root(md);+ size_t total = yyjson_arr_size(msgs), from = 0;+ yyjson_val *ts = yyjson_obj_get(run, "truncation_strategy");+ if (yyjson_equals_str(yyjson_obj_get(ts, "type"), "last_messages")) {+ yyjson_val *lm = yyjson_obj_get(ts, "last_messages");+ if (yyjson_is_int(lm) && (size_t)yyjson_get_sint(lm) < total)+ from = total - (size_t)yyjson_get_sint(lm);+ }+ for (size_t i = from; i < total; i++) {+ yyjson_val *m = yyjson_arr_get(msgs, i);+ yyjson_mut_val *mi = yyjson_mut_arr_add_obj(d, in);+ const char *role = yyjson_get_str(yyjson_obj_get(m, "role"));+ yyjson_mut_obj_add_strcpy(d, mi, "role", role ? role : "user");+ struct janas_buf b = {0};+ size_t k, n;+ yyjson_val *p;+ yyjson_arr_foreach(yyjson_obj_get(m, "content"), k, n, p)+ {+ const char *v = yyjson_get_str(+ yyjson_obj_get(yyjson_obj_get(p, "text"), "value"));+ if (v) {+ if (b.n)+ janas_buf_put(&b, "\n", 1);+ janas_buf_puts(&b, v);+ }+ }+ janas_buf_put(&b, "", 1);+ yyjson_mut_obj_add_strcpy(d, mi, "content", b.oom ? "" : b.p);+ janas_buf_free(&b);+ }+ size_t k, n;+ yyjson_mut_val *it;+ yyjson_mut_arr_foreach(x->items, k, n, it)+ yyjson_mut_arr_append(in, yyjson_mut_val_mut_copy(d, it));+ yyjson_val *tools = yyjson_obj_get(run, "tools");+ cid[0] = 0;+ if (has_tool(tools, "code_interpreter"))+ thread_container(s, run_str(x, "thread_id"), yyjson_doc_get_root(ad),+ yyjson_doc_get_root(td), cid, cap);+ yyjson_mut_val *rtools = tools_for(d, tools, yyjson_doc_get_root(ad),+ yyjson_doc_get_root(td), cid);+ if (yyjson_mut_arr_size(rtools)) {+ yyjson_mut_obj_add_val(d, o, "tools", rtools);+ yyjson_val *tc = yyjson_obj_get(run, "tool_choice");+ if (yyjson_is_str(tc))+ yyjson_mut_obj_add_strcpy(d, o, "tool_choice", yyjson_get_str(tc));+ else if (yyjson_is_obj(tc)) {+ yyjson_mut_val *c = yyjson_mut_obj_add_obj(d, o, "tool_choice");+ const char *ty = yyjson_get_str(yyjson_obj_get(tc, "type"));+ yyjson_mut_obj_add_strcpy(d, c, "type", ty ? ty : "auto");+ const char *fn = yyjson_get_str(+ yyjson_obj_get(yyjson_obj_get(tc, "function"), "name"));+ if (fn)+ yyjson_mut_obj_add_strcpy(d, c, "name", fn);+ }+ yyjson_val *ptc = yyjson_obj_get(run, "parallel_tool_calls");+ if (yyjson_is_bool(ptc))+ yyjson_mut_obj_add_bool(d, o, "parallel_tool_calls",+ yyjson_get_bool(ptc));+ }+ static const char *const NUM[] = {"temperature", "top_p"};+ for (int i = 0; i < 2; i++) {+ yyjson_val *v = yyjson_obj_get(run, NUM[i]);+ if (yyjson_is_num(v))+ yyjson_mut_obj_add_real(d, o, NUM[i], yyjson_get_num(v));+ }+ yyjson_val *mx = yyjson_obj_get(run, "max_completion_tokens");+ if (yyjson_is_int(mx))+ yyjson_mut_obj_add_int(d, o, "max_output_tokens", yyjson_get_sint(mx));+ yyjson_val *re = yyjson_obj_get(run, "reasoning_effort");+ if (yyjson_is_str(re)) {+ yyjson_mut_val *r = yyjson_mut_obj_add_obj(d, o, "reasoning");+ yyjson_mut_obj_add_strcpy(d, r, "effort", yyjson_get_str(re));+ }+ /* response_format into text.format */+ yyjson_val *rf = yyjson_obj_get(run, "response_format");+ const char *rft = yyjson_get_str(yyjson_obj_get(rf, "type"));+ if (rft && (!strcmp(rft, "json_object") || !strcmp(rft, "json_schema"))) {+ yyjson_mut_val *tx = yyjson_mut_obj_add_obj(d, o, "text");+ yyjson_mut_val *f = yyjson_mut_obj_add_obj(d, tx, "format");+ yyjson_mut_obj_add_strcpy(d, f, "type", rft);+ yyjson_val *js = yyjson_obj_get(rf, "json_schema");+ static const char *const K[] = {"name", "description", "schema",+ "strict"};+ for (int i = 0; i < 4; i++) {+ yyjson_val *v = yyjson_obj_get(js, K[i]);+ if (v && !yyjson_is_null(v))+ yyjson_mut_obj_add_val(d, f, K[i], yyjson_val_mut_copy(d, v));+ }+ }+ char *body = yyjson_mut_write(d, 0, NULL);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(ad);+ yyjson_doc_free(td);+ yyjson_doc_free(md);+ yyjson_doc_free(rd);+ return body;+}++/* A container's file made a File of the Files API (purpose+ assistants_output), once per pass: its id into out, or "". */+static void file_of(struct srv *s, struct pass *me, const char *cfid, char *out,+ size_t cap)+{+ out[0] = 0;+ for (int i = 0; i < me->n_conv; i++)+ if (!strcmp(me->conv_from[i], cfid)) {+ snprintf(out, cap, "%s", me->conv_to[i]);+ return;+ }+ char name[256];+ int fd = srv_container_file_open(me->cid, cfid, name, sizeof name);+ if (fd < 0)+ return;+ struct janas_buf b = {0};+ char buf[65536];+ ssize_t k;+ while ((k = read(fd, buf, sizeof buf)) > 0 && b.n < ((size_t)64 << 20))+ janas_buf_put(&b, buf, (size_t)k);+ close(fd);+ struct srv_file f;+ char err[200];+ if (!b.oom &&+ srv_files_add(s->files, name, "assistants_output", b.p ? b.p : "", b.n,+ 0, &f, err, sizeof err) == 0) {+ snprintf(out, cap, "%s", f.id);+ if (me->n_conv < 16) {+ snprintf(me->conv_from[me->n_conv], sizeof me->conv_from[0], "%s",+ cfid);+ snprintf(me->conv_to[me->n_conv], sizeof me->conv_to[0], "%s",+ f.id);+ me->n_conv++;+ }+ }+ janas_buf_free(&b);+}++/* A code_interpreter_call item of /responses as the details of a step:+ the code, its logs, its images as Files. */+static yyjson_mut_val *code_details(struct srv *s, struct pass *me,+ yyjson_mut_doc *d, yyjson_val *it)+{+ yyjson_mut_val *det = yyjson_mut_obj(d);+ yyjson_mut_obj_add_str(d, det, "type", "tool_calls");+ yyjson_mut_val *tc = yyjson_mut_obj_add_arr(d, det, "tool_calls");+ yyjson_mut_val *c1 = yyjson_mut_arr_add_obj(d, tc);+ const char *cid = yyjson_get_str(yyjson_obj_get(it, "id"));+ const char *code = yyjson_get_str(yyjson_obj_get(it, "code"));+ yyjson_mut_obj_add_strcpy(d, c1, "id", cid ? cid : "");+ yyjson_mut_obj_add_str(d, c1, "type", "code_interpreter");+ yyjson_mut_val *ci = yyjson_mut_obj_add_obj(d, c1, "code_interpreter");+ yyjson_mut_obj_add_strcpy(d, ci, "input", code ? code : "");+ yyjson_mut_val *outs = yyjson_mut_obj_add_arr(d, ci, "outputs");+ size_t i, n;+ yyjson_val *o;+ yyjson_arr_foreach(yyjson_obj_get(it, "outputs"), i, n, o)+ {+ const char *type = yyjson_get_str(yyjson_obj_get(o, "type"));+ if (type && !strcmp(type, "logs")) {+ yyjson_mut_val *l = yyjson_mut_arr_add_obj(d, outs);+ const char *logs = yyjson_get_str(yyjson_obj_get(o, "logs"));+ yyjson_mut_obj_add_str(d, l, "type", "logs");+ yyjson_mut_obj_add_strcpy(d, l, "logs", logs ? logs : "");+ } else if (type && !strcmp(type, "image")) {+ /* /v1/containers/<c>/files/<f>/content: the f */+ const char *url = yyjson_get_str(yyjson_obj_get(o, "url"));+ const char *f = url ? strstr(url, "/files/") : NULL;+ char cf[48] = "", fid[48];+ if (f)+ sscanf(f + 7, "%47[^/]", cf);+ file_of(s, me, cf, fid, sizeof fid);+ if (!fid[0])+ continue;+ yyjson_mut_val *im = yyjson_mut_arr_add_obj(d, outs);+ yyjson_mut_obj_add_str(d, im, "type", "image");+ yyjson_mut_val *io = yyjson_mut_obj_add_obj(d, im, "image");+ yyjson_mut_obj_add_strcpy(d, io, "file_id", fid);+ }+ }+ return det;+}++/* Responses' annotations of text as the Assistants API writes them:+ file_citation (index: the character of its '['): the marker [n], its+ start and end in characters, the file; container_file_citation: a+ file_path to the container's file made a File. */+static yyjson_mut_val *annotations(struct srv *s, struct pass *me,+ yyjson_mut_doc *d, yyjson_val *anns,+ const char *text)+{+ yyjson_mut_val *out = yyjson_mut_arr(d);+ size_t i, n;+ yyjson_val *a;+ yyjson_arr_foreach(anns, i, n, a)+ {+ if (yyjson_equals_str(yyjson_obj_get(a, "type"),+ "container_file_citation")) {+ char fid[48];+ const char *cf = yyjson_get_str(yyjson_obj_get(a, "file_id"));+ file_of(s, me, cf ? cf : "", fid, sizeof fid);+ const char *fn = yyjson_get_str(yyjson_obj_get(a, "filename"));+ if (!fid[0])+ continue;+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, out);+ yyjson_mut_obj_add_str(d, o, "type", "file_path");+ yyjson_mut_obj_add_strcpy(d, o, "text", fn ? fn : "");+ yyjson_mut_obj_add_uint(+ d, o, "start_index",+ yyjson_get_uint(yyjson_obj_get(a, "start_index")));+ yyjson_mut_obj_add_uint(+ d, o, "end_index",+ yyjson_get_uint(yyjson_obj_get(a, "end_index")));+ yyjson_mut_val *fp = yyjson_mut_obj_add_obj(d, o, "file_path");+ yyjson_mut_obj_add_strcpy(d, fp, "file_id", fid);+ continue;+ }+ if (!yyjson_equals_str(yyjson_obj_get(a, "type"), "file_citation"))+ continue;+ size_t at = (size_t)yyjson_get_uint(yyjson_obj_get(a, "index"));+ /* the byte of that character */+ const char *p = text;+ for (size_t c = 0; *p && c < at; p++)+ if (((unsigned char)p[1] & 0xc0) != 0x80)+ c++;+ size_t len = 0;+ if (*p == '[')+ while (p[len] && p[len] != ']' && len < 12)+ len++;+ if (p[len] != ']')+ continue;+ len++;+ char marker[16];+ snprintf(marker, sizeof marker, "%.*s", (int)len, p);+ yyjson_mut_val *o = yyjson_mut_arr_add_obj(d, out);+ yyjson_mut_obj_add_str(d, o, "type", "file_citation");+ yyjson_mut_obj_add_strcpy(d, o, "text", marker);+ yyjson_mut_obj_add_uint(d, o, "start_index", at);+ yyjson_mut_obj_add_uint(d, o, "end_index", at + len);+ yyjson_mut_val *fc = yyjson_mut_obj_add_obj(d, o, "file_citation");+ yyjson_mut_obj_add_strcpy(d, fc, "file_id",+ yyjson_get_str(yyjson_obj_get(a, "file_id")));+ }+ return out;+}++static void fail(struct run *x, const char *code, const char *msg)+{+ set_str(x->d, x->o, "status", "failed");+ set_int(x->d, x->o, "failed_at", (int64_t)time(NULL));+ yyjson_mut_val *e = yyjson_mut_obj(x->d);+ yyjson_mut_obj_add_strcpy(x->d, e, "code", code);+ yyjson_mut_obj_add_strcpy(x->d, e, "message", msg);+ set_val(x->d, x->o, "last_error", e);+}++static void cancelled(struct run *x)+{+ set_str(x->d, x->o, "status", "cancelled");+ set_int(x->d, x->o, "cancelled_at", (int64_t)time(NULL));+ set_val(x->d, x->o, "required_action", NULL);+}++/* The message the pass writes, told at its first piece of text: its step+ and the message itself, in progress. */+static void message_open(struct pass *me)+{+ me->msg_open = 1;+ me->msg_at = (int64_t)time(NULL);+ srv_new_id(me->msg_id, sizeof me->msg_id, "msg_");+ srv_new_id(me->step_id, sizeof me->step_id, "step_");+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *sp =+ step_obj(d, me->step_id, me->msg_at, me->id, me->asst, me->thread,+ "message_creation", "in_progress");+ set_val(d, sp, "step_details", message_details(d, me->msg_id));+ as_emit(me->id, "thread.run.step.created", sp);+ as_emit(me->id, "thread.run.step.in_progress", sp);+ yyjson_mut_val *m = as_message(d, me->thread, "assistant", "", NULL, NULL,+ me->asst, me->id);+ set_str(d, m, "id", me->msg_id);+ set_str(d, m, "status", "in_progress");+ set_val(d, m, "completed_at", NULL);+ set_val(d, m, "content", yyjson_mut_arr(d));+ as_emit(me->id, "thread.message.created", m);+ as_emit(me->id, "thread.message.in_progress", m);+ yyjson_mut_doc_free(d);+}++/* One event of /responses streamed. */+static void sse_event(struct pass *me, const char *data, size_t n)+{+ yyjson_doc *ed = yyjson_read(data, n, 0);+ yyjson_val *e = yyjson_doc_get_root(ed);+ const char *type = yyjson_get_str(yyjson_obj_get(e, "type"));+ if (!type) {+ yyjson_doc_free(ed);+ return;+ }+ if (!strcmp(type, "response.output_text.delta")) {+ const char *dt = yyjson_get_str(yyjson_obj_get(e, "delta"));+ if (!me->msg_open)+ message_open(me);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, o, "id", me->msg_id);+ yyjson_mut_obj_add_str(d, o, "object", "thread.message.delta");+ yyjson_mut_val *dl = yyjson_mut_obj_add_obj(d, o, "delta");+ yyjson_mut_val *c = yyjson_mut_obj_add_arr(d, dl, "content");+ yyjson_mut_val *p = yyjson_mut_arr_add_obj(d, c);+ yyjson_mut_obj_add_int(d, p, "index", 0);+ yyjson_mut_obj_add_str(d, p, "type", "text");+ yyjson_mut_val *tx = yyjson_mut_obj_add_obj(d, p, "text");+ yyjson_mut_obj_add_strcpy(d, tx, "value", dt ? dt : "");+ yyjson_mut_obj_add_arr(d, tx, "annotations");+ as_emit(me->id, "thread.message.delta", o);+ yyjson_mut_doc_free(d);+ } else if (!strcmp(type, "response.output_item.done")) {+ /* a search, told when done, before the text that uses it */+ yyjson_val *it = yyjson_obj_get(e, "item");+ const char *iid = yyjson_get_str(yyjson_obj_get(it, "id"));+ int search =+ yyjson_equals_str(yyjson_obj_get(it, "type"), "file_search_call");+ int code = yyjson_equals_str(yyjson_obj_get(it, "type"),+ "code_interpreter_call");+ if ((search || code) && iid && me->n_fs < 8) {+ snprintf(me->fs_item[me->n_fs], sizeof me->fs_item[0], "%s", iid);+ srv_new_id(me->fs_step[me->n_fs], sizeof me->fs_step[0], "step_");+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *sp =+ step_obj(d, me->fs_step[me->n_fs], 0, me->id, me->asst,+ me->thread, "tool_calls", "completed");+ set_val(d, sp, "step_details",+ search ? search_details(d, it)+ : code_details(server, me, d, it));+ as_emit(me->id, "thread.run.step.created", sp);+ as_emit(me->id, "thread.run.step.completed", sp);+ yyjson_mut_doc_free(d);+ me->n_fs++;+ }+ } else if (!strcmp(type, "response.completed") ||+ !strcmp(type, "response.incomplete") ||+ !strcmp(type, "response.failed")) {+ /* the whole response, as without a stream */+ yyjson_val *resp = yyjson_obj_get(e, "response");+ char *t = resp ? yyjson_val_write(resp, 0, NULL) : NULL;+ yyjson_doc_free(me->final);+ me->final = t ? yyjson_read(t, strlen(t), 0) : NULL;+ free(t);+ } else if (!strcmp(type, "error")) {+ const char *msg = yyjson_get_str(yyjson_obj_get(e, "message"));+ if (!msg)+ msg = yyjson_get_str(+ yyjson_obj_get(yyjson_obj_get(e, "error"), "message"));+ snprintf(me->err, sizeof me->err, "%s", msg ? msg : "error");+ }+ yyjson_doc_free(ed);+}++/* The bytes of /responses streamed: whole events handed to sse_event. */+static void on_data(void *arg, const char *p, size_t n)+{+ struct pass *me = arg;+ janas_buf_put(&me->sse, p, n);+ if (me->sse.oom)+ return;+ size_t at = 0;+ for (;;) {+ char *end = NULL;+ for (size_t i = at; i + 1 < me->sse.n; i++)+ if (me->sse.p[i] == '\n' && me->sse.p[i + 1] == '\n') {+ end = me->sse.p + i;+ break;+ }+ if (!end)+ break;+ /* its data: line */+ const char *ev = me->sse.p + at;+ for (const char *l = ev; l < end;) {+ const char *le = memchr(l, '\n', (size_t)(end - l));+ if (!le)+ le = end;+ if (le - l > 6 && !strncmp(l, "data: ", 6))+ sse_event(me, l + 6, (size_t)(le - l - 6));+ l = le + 1;+ }+ at = (size_t)(end - me->sse.p) + 2;+ }+ memmove(me->sse.p, me->sse.p + at, me->sse.n - at);+ me->sse.n -= at;+}++/* The run's event for its status, and done when it ends or waits. */+static void tell_status(struct run *x)+{+ const char *id = run_str(x, "id"), *st = run_status(x);+ if (!st)+ return;+ char ev[64];+ snprintf(ev, sizeof ev, "thread.run.%s", st);+ as_emit(id, ev, x->o);+ if (strcmp(st, "queued") && strcmp(st, "in_progress") &&+ strcmp(st, "cancelling"))+ as_emit_done(id);+}++/* One pass of the run: the model asked, its output made into steps; the+ run ends, or waits for tool outputs. */+static void run_pass(struct srv *s, struct pass *me)+{+ const char *id = me->id;+ struct run x;+ as_lock();+ if (run_load(s, id, &x) != 0) {+ as_unlock();+ return;+ }+ const char *st = run_status(&x);+ if (st && !strcmp(st, "cancelling")) {+ cancelled(&x);+ run_save(s, &x, 0);+ tell_status(&x);+ }+ if (!st || (strcmp(st, "queued") && strcmp(st, "in_progress"))) {+ as_unlock();+ run_free(&x);+ return;+ }+ snprintf(me->thread, sizeof me->thread, "%s", run_str(&x, "thread_id"));+ snprintf(me->asst, sizeof me->asst, "%s", run_str(&x, "assistant_id"));+ set_str(x.d, x.o, "status", "in_progress");+ if (!yyjson_mut_is_int(yyjson_mut_obj_get(x.o, "started_at")))+ set_int(x.d, x.o, "started_at", (int64_t)time(NULL));+ run_save(s, &x, 0);+ tell_status(&x);+ char *body = request_for(s, &x, me->cid, sizeof me->cid);+ as_unlock();+ run_free(&x);+ /* /responses streamed, the text told as it comes */+ struct srv_capture c = {.on_data = on_data, .arg = me};+ srv_handler h = srv_route_find("POST", "/responses");+ if (body && h) {+ struct srv_req r = {.srv = s,+ .cap = &c,+ .method = "POST",+ .path = "/responses",+ .body = body,+ .n_body = strlen(body)};+ h(&r);+ }+ free(body);+ yyjson_doc *errd = c.body ? yyjson_read(c.body, c.n, 0) : NULL;+ /* an answer not streamed is the response itself */+ if (!me->final && c.status == 200 &&+ yyjson_equals_str(yyjson_obj_get(yyjson_doc_get_root(errd), "object"),+ "response")) {+ me->final = errd;+ errd = NULL;+ }+ yyjson_val *resp = yyjson_doc_get_root(me->final);+ as_lock();+ pthread_mutex_lock(&live_mu);+ int stop = stopping;+ pthread_mutex_unlock(&live_mu);+ if (run_load(s, id, &x) != 0 || stop) {+ /* gone, or the server stops: taken up again at the next start */+ as_unlock();+ run_free(&x);+ yyjson_doc_free(errd);+ free(c.body);+ return;+ }+ st = run_status(&x);+ const char *rs = yyjson_get_str(yyjson_obj_get(resp, "status"));+ if (st && !strcmp(st, "cancelling")) {+ cancelled(&x);+ } else if (!resp || (rs && !strcmp(rs, "failed"))) {+ /* refused before the stream (an error body), or failed in it */+ const char *msg = yyjson_get_str(yyjson_obj_get(+ yyjson_obj_get(yyjson_doc_get_root(errd), "error"), "message"));+ if (!msg)+ msg = yyjson_get_str(+ yyjson_obj_get(yyjson_obj_get(resp, "error"), "message"));+ fail(&x, "server_error",+ msg ? msg+ : me->err[0] ? me->err+ : "the model did not answer");+ } else {+ yyjson_val *u = yyjson_obj_get(resp, "usage");+ int64_t in = yyjson_get_sint(yyjson_obj_get(u, "input_tokens"));+ int64_t out = yyjson_get_sint(yyjson_obj_get(u, "output_tokens"));+ add_usage(x.d, x.o, in, out);+ yyjson_mut_val *calls = yyjson_mut_arr(x.d);+ size_t i, n;+ yyjson_val *it;+ yyjson_arr_foreach(yyjson_obj_get(resp, "output"), i, n, it)+ {+ const char *type = yyjson_get_str(yyjson_obj_get(it, "type"));+ if (!type)+ continue;+ if (!strcmp(type, "file_search_call") ||+ !strcmp(type, "code_interpreter_call")) {+ /* the step told while streaming keeps its id */+ const char *iid = yyjson_get_str(yyjson_obj_get(it, "id"));+ const char *sid = NULL;+ for (int k = 0; k < me->n_fs && iid; k++)+ if (!strcmp(me->fs_item[k], iid))+ sid = me->fs_step[k];+ yyjson_mut_val *sp =+ step_new(&x, sid, 0, "tool_calls", "completed");+ set_val(x.d, sp, "step_details",+ type[0] == 'f' ? search_details(x.d, it)+ : code_details(s, me, x.d, it));+ if (!sid) {+ as_emit(id, "thread.run.step.created", sp);+ as_emit(id, "thread.run.step.completed", sp);+ }+ } else if (!strcmp(type, "message")) {+ yyjson_val *part =+ yyjson_arr_get_first(yyjson_obj_get(it, "content"));+ const char *text = yyjson_get_str(yyjson_obj_get(part, "text"));+ if (!me->msg_open)+ message_open(me);+ yyjson_mut_val *m =+ as_message(x.d, me->thread, "assistant", text ? text : "",+ NULL, NULL, me->asst, id);+ set_str(x.d, m, "id", me->msg_id);+ set_int(x.d, m, "created_at", me->msg_at);+ yyjson_mut_val *tv = yyjson_mut_obj_get(+ yyjson_mut_arr_get_first(yyjson_mut_obj_get(m, "content")),+ "text");+ set_val(x.d, tv, "annotations",+ annotations(s, me, x.d,+ yyjson_obj_get(part, "annotations"),+ text ? text : ""));+ as_message_append(s, me->thread, m);+ yyjson_mut_val *sp = step_new(&x, me->step_id, me->msg_at,+ "message_creation", "completed");+ set_val(x.d, sp, "step_details",+ message_details(x.d, me->msg_id));+ as_emit(id, "thread.message.completed", m);+ as_emit(id, "thread.run.step.completed", sp);+ me->msg_open = 0; /* a later message gets its own */+ } else if (!strcmp(type, "function_call")) {+ yyjson_mut_arr_append(calls, yyjson_val_mut_copy(x.d, it));+ }+ }+ if (yyjson_mut_arr_size(calls)) {+ /* the run waits for the outputs of the calls */+ yyjson_mut_val *sp =+ step_new(&x, NULL, 0, "tool_calls", "in_progress");+ yyjson_mut_val *det = yyjson_mut_obj(x.d);+ yyjson_mut_obj_add_str(x.d, det, "type", "tool_calls");+ yyjson_mut_val *tc = yyjson_mut_obj_add_arr(x.d, det, "tool_calls");+ yyjson_mut_val *ra = yyjson_mut_obj(x.d);+ yyjson_mut_obj_add_str(x.d, ra, "type", "submit_tool_outputs");+ yyjson_mut_val *sto =+ yyjson_mut_obj_add_obj(x.d, ra, "submit_tool_outputs");+ yyjson_mut_val *rtc =+ yyjson_mut_obj_add_arr(x.d, sto, "tool_calls");+ yyjson_mut_val *fc;+ yyjson_mut_arr_foreach(calls, i, n, fc)+ {+ const char *cid =+ yyjson_mut_get_str(yyjson_mut_obj_get(fc, "call_id"));+ const char *name =+ yyjson_mut_get_str(yyjson_mut_obj_get(fc, "name"));+ const char *args =+ yyjson_mut_get_str(yyjson_mut_obj_get(fc, "arguments"));+ for (int q = 0; q < 2; q++) {+ yyjson_mut_val *c1 =+ yyjson_mut_arr_add_obj(x.d, q ? rtc : tc);+ yyjson_mut_obj_add_strcpy(x.d, c1, "id", cid ? cid : "");+ yyjson_mut_obj_add_str(x.d, c1, "type", "function");+ yyjson_mut_val *f =+ yyjson_mut_obj_add_obj(x.d, c1, "function");+ yyjson_mut_obj_add_strcpy(x.d, f, "name", name ? name : "");+ yyjson_mut_obj_add_strcpy(x.d, f, "arguments",+ args ? args : "{}");+ if (!q)+ yyjson_mut_obj_add_null(x.d, f, "output");+ }+ yyjson_mut_val *item = yyjson_mut_arr_add_obj(x.d, x.items);+ yyjson_mut_obj_add_str(x.d, item, "type", "function_call");+ yyjson_mut_obj_add_strcpy(x.d, item, "call_id", cid ? cid : "");+ yyjson_mut_obj_add_strcpy(x.d, item, "name", name ? name : "");+ yyjson_mut_obj_add_strcpy(x.d, item, "arguments",+ args ? args : "{}");+ }+ set_val(x.d, sp, "step_details", det);+ as_emit(id, "thread.run.step.created", sp);+ set_str(x.d, x.o, "status", "requires_action");+ set_val(x.d, x.o, "required_action", ra);+ set_int(x.d, x.o, "expires_at", (int64_t)time(NULL) + RUN_EXPIRES);+ } else {+ if (rs && !strcmp(rs, "incomplete")) {+ set_str(x.d, x.o, "status", "incomplete");+ yyjson_mut_val *det = yyjson_mut_obj(x.d);+ yyjson_mut_obj_add_str(x.d, det, "reason",+ "max_completion_tokens");+ set_val(x.d, x.o, "incomplete_details", det);+ } else {+ set_str(x.d, x.o, "status", "completed");+ set_int(x.d, x.o, "completed_at", (int64_t)time(NULL));+ }+ set_val(x.d, x.o, "required_action", NULL);+ }+ }+ run_save(s, &x, 0);+ tell_status(&x);+ as_unlock();+ run_free(&x);+ yyjson_doc_free(errd);+ free(c.body);+}++static void *run_main(void *arg)+{+ char *id = arg;+ struct pass me = {.next = NULL};+ snprintf(me.id, sizeof me.id, "%s", id);+ atomic_init(&me.cancel, 0);+ pthread_mutex_lock(&live_mu);+ me.next = passes;+ passes = &me;+ pthread_mutex_unlock(&live_mu);+ srv_engine_watch = &me.cancel;+ run_pass(server, &me);+ srv_engine_watch = NULL;+ janas_buf_free(&me.sse);+ yyjson_doc_free(me.final);+ free(id);+ pthread_mutex_lock(&live_mu);+ for (struct pass **p = &passes; *p; p = &(*p)->next)+ if (*p == &me) {+ *p = me.next;+ break;+ }+ live--;+ pthread_cond_broadcast(&live_cv);+ pthread_mutex_unlock(&live_mu);+ return NULL;+}++/* A pass of run id on a thread of its own: 0, or -1. */+static int run_go(const char *id)+{+ pthread_mutex_lock(&live_mu);+ if (stopping) {+ pthread_mutex_unlock(&live_mu);+ return -1;+ }+ live++;+ pthread_mutex_unlock(&live_mu);+ char *arg = strdup(id);+ pthread_t th;+ pthread_attr_t at;+ pthread_attr_init(&at);+ pthread_attr_setdetachstate(&at, PTHREAD_CREATE_DETACHED);+ int rc = arg ? pthread_create(&th, &at, run_main, arg) : -1;+ pthread_attr_destroy(&at);+ if (rc != 0) {+ free(arg);+ pthread_mutex_lock(&live_mu);+ live--;+ pthread_cond_broadcast(&live_cv);+ pthread_mutex_unlock(&live_mu);+ return -1;+ }+ return 0;+}++void srv_runs_start(struct srv *s)+{+ server = s;+ stopping = 0;+ yyjson_doc *d = as_all(s, AS_RUN, NULL, NULL);+ size_t i, n;+ yyjson_val *v;+ yyjson_arr_foreach(yyjson_doc_get_root(d), i, n, v)+ {+ const char *st = yyjson_get_str(yyjson_obj_get(v, "status"));+ const char *id = yyjson_get_str(yyjson_obj_get(v, "id"));+ if (st && id &&+ (!strcmp(st, "queued") || !strcmp(st, "in_progress") ||+ !strcmp(st, "cancelling")))+ run_go(id);+ }+ yyjson_doc_free(d);+}++void srv_runs_stop(void)+{+ as_feeds_end();+ pthread_mutex_lock(&live_mu);+ stopping = 1;+ while (live > 0)+ pthread_cond_wait(&live_cv, &live_mu);+ pthread_mutex_unlock(&live_mu);+}++/* ---- the API ---- */++/* The run made of a body for a thread (as_lock not held): 0 with the+ reply sent, or the reason. */+static const char *run_make(struct srv_req *r, const char *thread,+ yyjson_val *root, int with_thread, int *ret)+{+ const char *aid = yyjson_get_str(yyjson_obj_get(root, "assistant_id"));+ if (!aid)+ return "'assistant_id' names the assistant to run.";+ int stream = yyjson_get_bool(yyjson_obj_get(root, "stream"));+ char *at = as_get(r->srv, AS_ASSISTANT, aid, NULL);+ yyjson_doc *ad = at ? yyjson_read(at, strlen(at), 0) : NULL;+ free(at);+ if (!ad)+ return "'assistant_id' names no assistant here.";+ yyjson_val *asst = yyjson_doc_get_root(ad);+ const char *why = NULL;+ yyjson_val *tools = yyjson_obj_get(root, "tools");+ yyjson_val *md = yyjson_obj_get(root, "metadata");+ yyjson_val *addm = yyjson_obj_get(root, "additional_messages");+ if ((why = as_tools_error(tools)) || (why = as_metadata_error(md))) {+ yyjson_doc_free(ad);+ return why;+ }+ if (addm && !yyjson_is_null(addm) &&+ (!yyjson_is_arr(addm) || yyjson_arr_size(addm) > 32)) {+ yyjson_doc_free(ad);+ return "'additional_messages' is a list of at most 32 messages.";+ }+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ struct run x = {.d = d};+ x.o = yyjson_mut_obj(d);+ x.steps = yyjson_mut_arr(d);+ x.items = yyjson_mut_arr(d);+ char id[48];+ srv_new_id(id, sizeof id, "run_");+ int64_t now = (int64_t)time(NULL);+ yyjson_mut_obj_add_strcpy(d, x.o, "id", id);+ yyjson_mut_obj_add_str(d, x.o, "object", "thread.run");+ yyjson_mut_obj_add_int(d, x.o, "created_at", now);+ yyjson_mut_obj_add_strcpy(d, x.o, "thread_id", thread);+ yyjson_mut_obj_add_strcpy(d, x.o, "assistant_id", aid);+ yyjson_mut_obj_add_str(d, x.o, "status", "queued");+ yyjson_mut_obj_add_null(d, x.o, "required_action");+ yyjson_mut_obj_add_null(d, x.o, "last_error");+ yyjson_mut_obj_add_null(d, x.o, "expires_at");+ yyjson_mut_obj_add_null(d, x.o, "started_at");+ yyjson_mut_obj_add_null(d, x.o, "cancelled_at");+ yyjson_mut_obj_add_null(d, x.o, "failed_at");+ yyjson_mut_obj_add_null(d, x.o, "completed_at");+ yyjson_mut_obj_add_null(d, x.o, "incomplete_details");+ yyjson_mut_obj_add_strcpy(d, x.o, "model", r->srv->cfg.model_id);+ /* the instructions: the run's, else the assistant's, and more */+ const char *ins = yyjson_get_str(yyjson_obj_get(root, "instructions"));+ if (!ins)+ ins = yyjson_get_str(yyjson_obj_get(asst, "instructions"));+ const char *more =+ yyjson_get_str(yyjson_obj_get(root, "additional_instructions"));+ struct janas_buf b = {0};+ if (ins)+ janas_buf_puts(&b, ins);+ if (more) {+ if (b.n)+ janas_buf_put(&b, "\n\n", 2);+ janas_buf_puts(&b, more);+ }+ janas_buf_put(&b, "", 1);+ yyjson_mut_obj_add_strcpy(d, x.o, "instructions", b.oom ? "" : b.p);+ janas_buf_free(&b);+ yyjson_mut_obj_add_val(+ d, x.o, "tools",+ yyjson_val_mut_copy(+ d, yyjson_is_arr(tools) ? tools : yyjson_obj_get(asst, "tools")));+ yyjson_mut_obj_add_val(d, x.o, "metadata",+ yyjson_is_obj(md) ? yyjson_val_mut_copy(d, md)+ : yyjson_mut_obj(d));+ yyjson_mut_obj_add_null(d, x.o, "usage");+ /* the sampling: the run's, else the assistant's */+ static const char *const OPT[] = {"temperature", "top_p", "response_format",+ "reasoning_effort"};+ for (int i = 0; i < 4; i++) {+ yyjson_val *v = yyjson_obj_get(root, OPT[i]);+ if (!v || yyjson_is_null(v))+ v = yyjson_obj_get(asst, OPT[i]);+ yyjson_mut_obj_add_val(+ d, x.o, OPT[i], v ? yyjson_val_mut_copy(d, v) : yyjson_mut_null(d));+ }+ static const char *const ASIS[] = {+ "max_prompt_tokens", "max_completion_tokens", "truncation_strategy",+ "tool_choice", "parallel_tool_calls"};+ for (int i = 0; i < 5; i++) {+ yyjson_val *v = yyjson_obj_get(root, ASIS[i]);+ if (v)+ yyjson_mut_obj_add_val(d, x.o, ASIS[i], yyjson_val_mut_copy(d, v));+ else if (i == 2) {+ yyjson_mut_val *t = yyjson_mut_obj_add_obj(d, x.o, ASIS[i]);+ yyjson_mut_obj_add_str(d, t, "type", "auto");+ yyjson_mut_obj_add_null(d, t, "last_messages");+ } else if (i == 3)+ yyjson_mut_obj_add_str(d, x.o, ASIS[i], "auto");+ else if (i == 4)+ yyjson_mut_obj_add_bool(d, x.o, ASIS[i], true);+ else+ yyjson_mut_obj_add_null(d, x.o, ASIS[i]);+ }+ yyjson_doc_free(ad);+ char other[64];+ as_lock();+ char *tt = as_get(r->srv, AS_THREAD, thread, NULL);+ free(tt);+ if (!tt)+ why = "The thread is not here.";+ else if (as_active_run(r->srv, thread, other, sizeof other))+ why = "The thread already has a run going.";+ /* the additional messages, into the thread first */+ size_t i, n;+ yyjson_val *m;+ yyjson_arr_foreach(addm, i, n, m)+ {+ if (why)+ break;+ const char *role = yyjson_get_str(yyjson_obj_get(m, "role"));+ if (!role || (strcmp(role, "user") && strcmp(role, "assistant"))) {+ why = "A message's role is user or assistant.";+ break;+ }+ char *t = as_message_text(yyjson_obj_get(m, "content"), &why);+ yyjson_val *att = yyjson_obj_get(m, "attachments");+ if (t && !(why = as_attach(r->srv, thread, att)))+ as_message_append(r->srv, thread,+ as_message(d, thread, role, t, att,+ yyjson_obj_get(m, "metadata"), NULL,+ NULL));+ free(t);+ }+ /* a stream's feed open before the run can go */+ struct as_feed *feed = !why && stream ? as_feed_open(id) : NULL;+ if (!why && stream && !feed)+ why = "out of memory";+ if (!why && run_save(r->srv, &x, 1) != 0)+ why = "cannot keep the run";+ as_unlock();+ if (!why && feed) {+ if (with_thread) {+ char *tt = as_get(r->srv, AS_THREAD, thread, NULL);+ yyjson_doc *td = tt ? yyjson_read(tt, strlen(tt), 0) : NULL;+ free(tt);+ yyjson_mut_doc *d2 = yyjson_mut_doc_new(NULL);+ if (td)+ as_emit(id, "thread.created",+ yyjson_val_mut_copy(d2, yyjson_doc_get_root(td)));+ yyjson_mut_doc_free(d2);+ yyjson_doc_free(td);+ }+ as_emit(id, "thread.run.created", x.o);+ as_emit(id, "thread.run.queued", x.o);+ run_go(id);+ *ret = as_feed_reply(r, feed);+ } else if (!why) {+ *ret = reply_val(r, x.o);+ run_go(id);+ } else {+ as_feed_close(feed);+ }+ run_free(&x);+ return why;+}++int srv_runs_create(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ int ret = 0;+ const char *why = !yyjson_is_obj(root)+ ? "The body is a JSON object."+ : run_make(r, r->param[0], root, 0, &ret);+ yyjson_doc_free(doc);+ if (why && !strcmp(why, "The thread is not here."))+ return not_found(r, "thread", r->param[0]);+ return why ? bad(r, why) : ret;+}++int srv_runs_create_thread(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ int ret = 0;+ const char *why = NULL;+ char thread[48];+ if (!yyjson_is_obj(root))+ why = "The body is a JSON object.";+ else if (!yyjson_get_str(yyjson_obj_get(root, "assistant_id")))+ why = "'assistant_id' names the assistant to run.";+ else if (!(why = as_thread_make(r->srv, yyjson_obj_get(root, "thread"),+ thread, sizeof thread)))+ why = run_make(r, thread, root, 1, &ret);+ yyjson_doc_free(doc);+ return why ? bad(r, why) : ret;+}++/* The run of the request's thread, loaded (as_lock held). */+static int run_of(struct srv_req *r, struct run *x)+{+ if (run_load(r->srv, r->param[1], x) != 0)+ return -1;+ if (!yyjson_mut_equals_str(yyjson_mut_obj_get(x->o, "thread_id"),+ r->param[0])) {+ run_free(x);+ return -1;+ }+ return 0;+}++int srv_runs_get(struct srv_req *r)+{+ struct run x;+ as_lock();+ int rc = run_of(r, &x);+ if (rc == 0) {+ const char *before = run_status(&x);+ int was = before && !strcmp(before, "requires_action");+ run_expire(&x);+ if (was && strcmp(run_status(&x), "requires_action"))+ run_save(r->srv, &x, 0);+ }+ as_unlock();+ if (rc != 0)+ return not_found(r, "run", r->param[1]);+ int ret = reply_val(r, x.o);+ run_free(&x);+ return ret;+}++int srv_runs_list(struct srv_req *r)+{+ char *tt = as_get(r->srv, AS_THREAD, r->param[0], NULL);+ free(tt);+ if (!tt)+ return not_found(r, "thread", r->param[0]);+ yyjson_doc *d = as_all(r->srv, AS_RUN, "thread_id", r->param[0]);+ int ret = as_list_reply(r, yyjson_doc_get_root(d));+ yyjson_doc_free(d);+ return ret;+}++int srv_runs_modify(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_val *md = yyjson_obj_get(root, "metadata");+ const char *why = !yyjson_is_obj(root) ? "The body is a JSON object."+ : as_metadata_error(md);+ struct run x;+ as_lock();+ int rc = run_of(r, &x);+ if (rc == 0 && !why && md) {+ set_val(x.d, x.o, "metadata",+ yyjson_is_obj(md) ? yyjson_val_mut_copy(x.d, md)+ : yyjson_mut_obj(x.d));+ run_save(r->srv, &x, 0);+ }+ as_unlock();+ yyjson_doc_free(doc);+ if (rc != 0)+ return not_found(r, "run", r->param[1]);+ int ret = why ? bad(r, why) : reply_val(r, x.o);+ run_free(&x);+ return ret;+}++int srv_runs_cancel(struct srv_req *r)+{+ struct run x;+ as_lock();+ int rc = run_of(r, &x);+ const char *why = NULL;+ if (rc == 0) {+ const char *st = run_status(&x);+ if (st && !strcmp(st, "in_progress")) {+ set_str(x.d, x.o, "status", "cancelling"); /* the pass sees it */+ pthread_mutex_lock(&live_mu); /* and its model stops */+ for (struct pass *p = passes; p; p = p->next)+ if (!strcmp(p->id, r->param[1]))+ atomic_store(&p->cancel, 1);+ pthread_mutex_unlock(&live_mu);+ } else if (st &&+ (!strcmp(st, "queued") || !strcmp(st, "requires_action")))+ cancelled(&x);+ else+ why = "The run has ended: there is nothing to cancel.";+ if (!why) {+ run_save(r->srv, &x, 0);+ tell_status(&x);+ }+ }+ as_unlock();+ if (rc != 0)+ return not_found(r, "run", r->param[1]);+ int ret = why ? bad(r, why) : reply_val(r, x.o);+ run_free(&x);+ return ret;+}++int srv_runs_submit(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_val *outs = yyjson_obj_get(root, "tool_outputs");+ /* a stream's feed open before the run goes on */+ struct as_feed *feed = yyjson_get_bool(yyjson_obj_get(root, "stream"))+ ? as_feed_open(r->param[1])+ : NULL;+ struct run x;+ as_lock();+ int rc = run_of(r, &x);+ const char *why = NULL;+ if (rc == 0) {+ run_expire(&x);+ const char *st = run_status(&x);+ yyjson_mut_val *want = yyjson_mut_obj_get(+ yyjson_mut_obj_get(yyjson_mut_obj_get(x.o, "required_action"),+ "submit_tool_outputs"),+ "tool_calls");+ if (!st || strcmp(st, "requires_action"))+ why = "The run is not waiting for tool outputs.";+ else if (!yyjson_is_arr(outs) ||+ yyjson_arr_size(outs) != yyjson_mut_arr_size(want))+ why = "'tool_outputs' gives an output for every call the run "+ "waits for.";+ /* each call answered, once */+ size_t i, n;+ yyjson_mut_val *w;+ yyjson_mut_arr_foreach(want, i, n, w)+ {+ if (why)+ break;+ const char *cid = yyjson_mut_get_str(yyjson_mut_obj_get(w, "id"));+ int hits = 0;+ size_t k, m;+ yyjson_val *o;+ yyjson_arr_foreach(outs, k, m, o)+ {+ hits += yyjson_equals_str(yyjson_obj_get(o, "tool_call_id"),+ cid ? cid : "");+ }+ if (hits != 1)+ why = "Every call the run waits for gets one output, by its "+ "tool_call_id.";+ }+ if (!why) {+ /* the outputs into the waiting step and into the input */+ yyjson_mut_val *sp = yyjson_mut_arr_get_last(x.steps);+ yyjson_mut_val *tc = yyjson_mut_obj_get(+ yyjson_mut_obj_get(sp, "step_details"), "tool_calls");+ size_t k, m;+ yyjson_val *o;+ yyjson_arr_foreach(outs, k, m, o)+ {+ const char *cid =+ yyjson_get_str(yyjson_obj_get(o, "tool_call_id"));+ const char *out = yyjson_get_str(yyjson_obj_get(o, "output"));+ yyjson_mut_val *c1;+ size_t q, qn;+ yyjson_mut_arr_foreach(tc, q, qn, c1)+ {+ if (yyjson_mut_equals_str(yyjson_mut_obj_get(c1, "id"),+ cid))+ set_str(x.d, yyjson_mut_obj_get(c1, "function"),+ "output", out ? out : "");+ }+ yyjson_mut_val *item = yyjson_mut_arr_add_obj(x.d, x.items);+ yyjson_mut_obj_add_str(x.d, item, "type",+ "function_call_output");+ yyjson_mut_obj_add_strcpy(x.d, item, "call_id", cid);+ yyjson_mut_obj_add_strcpy(x.d, item, "output", out ? out : "");+ }+ set_str(x.d, sp, "status", "completed");+ set_int(x.d, sp, "completed_at", (int64_t)time(NULL));+ set_str(x.d, x.o, "status", "queued");+ set_val(x.d, x.o, "required_action", NULL);+ run_save(r->srv, &x, 0);+ }+ }+ as_unlock();+ yyjson_doc_free(doc);+ if (rc != 0) {+ as_feed_close(feed);+ return not_found(r, "run", r->param[1]);+ }+ int ret;+ if (why) {+ as_feed_close(feed);+ ret = bad(r, why);+ } else if (feed) {+ tell_status(&x); /* queued again */+ run_go(r->param[1]);+ ret = as_feed_reply(r, feed);+ } else {+ ret = reply_val(r, x.o);+ run_go(r->param[1]);+ }+ run_free(&x);+ return ret;+}++/* The run's steps (as_lock not held): its document, or NULL. */+static yyjson_doc *steps_of(struct srv_req *r)+{+ struct run x;+ as_lock();+ int rc = run_of(r, &x);+ as_unlock();+ if (rc != 0)+ return NULL;+ char *t = yyjson_mut_val_write(x.steps, 0, NULL);+ run_free(&x);+ yyjson_doc *d = t ? yyjson_read(t, strlen(t), 0) : NULL;+ free(t);+ return d;+}++int srv_run_steps_list(struct srv_req *r)+{+ yyjson_doc *d = steps_of(r);+ if (!d)+ return not_found(r, "run", r->param[1]);+ int ret = as_list_reply(r, yyjson_doc_get_root(d));+ yyjson_doc_free(d);+ return ret;+}++int srv_run_steps_get(struct srv_req *r)+{+ yyjson_doc *d = steps_of(r);+ if (!d)+ return not_found(r, "run", r->param[1]);+ size_t i, n;+ yyjson_val *s;+ int ret = -1;+ yyjson_arr_foreach(yyjson_doc_get_root(d), i, n, s)+ {+ if (yyjson_equals_str(yyjson_obj_get(s, "id"), r->param[2])) {+ char *t = yyjson_val_write(s, 0, NULL);+ ret = srv_reply_json(r, 200, t, t ? strlen(t) : 0);+ }+ }+ yyjson_doc_free(d);+ return ret < 0 ? not_found(r, "run step", r->param[2]) : ret;+}diff --git a/src/server/runs_stream.c b/src/server/runs_stream.cnew file mode 100644index 0000000..bc701b5--- /dev/null+++ b/src/server/runs_stream.c@@ -0,0 +1,129 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * runs_stream.c - the events of a run of the Assistants API, for the+ * clients that asked for them (stream: true): a feed per client, opened+ * before the run is started so nothing is missed, filled by runs.c as the+ * run goes (thread.run.*, thread.run.step.*, thread.message.*, the text as+ * the model writes it) and ended with done when the run ends or waits for+ * tool outputs.+ */+#include "assistants.h"++#include <pthread.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <time.h>++#include "llm/json.h"++struct as_feed {+ char run[64];+ struct janas_buf b; /* the events not yet sent */+ size_t off;+ int ended;+ struct as_feed *next;+};++static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;+static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;+static struct as_feed *feeds;++struct as_feed *as_feed_open(const char *run)+{+ struct as_feed *f = calloc(1, sizeof(*f));+ if (!f)+ return NULL;+ snprintf(f->run, sizeof f->run, "%s", run);+ pthread_mutex_lock(&mu);+ f->next = feeds;+ feeds = f;+ pthread_mutex_unlock(&mu);+ return f;+}++static void put(const char *run, const char *text, size_t n, int end)+{+ pthread_mutex_lock(&mu);+ for (struct as_feed *f = feeds; f; f = f->next)+ if (!f->ended && (!run || !strcmp(f->run, run))) {+ janas_buf_put(&f->b, text, n);+ f->ended = end;+ }+ pthread_cond_broadcast(&cv);+ pthread_mutex_unlock(&mu);+}++void as_emit(const char *run, const char *event, yyjson_mut_val *v)+{+ size_t n = 0;+ char *json = v ? yyjson_mut_val_write(v, 0, &n) : NULL;+ if (!json)+ return;+ size_t cap = strlen(event) + n + 32;+ char *t = malloc(cap);+ if (t) {+ int k = snprintf(t, cap, "event: %s\ndata: %s\n\n", event, json);+ put(run, t, (size_t)k, 0);+ }+ free(t);+ free(json);+}++void as_emit_done(const char *run)+{+ static const char done[] = "event: done\ndata: [DONE]\n\n";+ put(run, done, sizeof done - 1, 1);+}++void as_feeds_end(void)+{+ as_emit_done(NULL);+}++static long next(void *ctx, char *buf, size_t cap)+{+ struct as_feed *f = ctx;+ pthread_mutex_lock(&mu);+ if (f->off == f->b.n && !f->ended) {+ struct timespec ts;+ clock_gettime(CLOCK_REALTIME, &ts);+ ts.tv_nsec += 200 * 1000000L;+ if (ts.tv_nsec >= 1000000000L)+ ts.tv_sec++, ts.tv_nsec -= 1000000000L;+ pthread_cond_timedwait(&cv, &mu, &ts);+ }+ long k = 0;+ if (f->off < f->b.n) {+ size_t n = f->b.n - f->off < cap ? f->b.n - f->off : cap;+ memcpy(buf, f->b.p + f->off, n);+ f->off += n;+ k = (long)n;+ } else if (f->ended) {+ k = -1;+ }+ pthread_mutex_unlock(&mu);+ return k;+}++void as_feed_close(void *ctx)+{+ struct as_feed *f = ctx;+ if (!f)+ return;+ pthread_mutex_lock(&mu);+ for (struct as_feed **p = &feeds; *p; p = &(*p)->next)+ if (*p == f) {+ *p = f->next;+ break;+ }+ pthread_mutex_unlock(&mu);+ janas_buf_free(&f->b);+ free(f);+}++int as_feed_reply(struct srv_req *r, struct as_feed *f)+{+ return srv_reply_stream(r, next, as_feed_close, f);+}diff --git a/src/server/server.h b/src/server/server.hindex 8c750ab..a4c6b4c 100644--- a/src/server/server.h+++ b/src/server/server.h@@ -15,12 +15,16 @@#define JANAS_SERVER_H#include <pthread.h>+#include <stdatomic.h>#include <stddef.h>#include <stdint.h>+#include "common/mcp_policy.h"#include "janas/llm.h"#include "store.h"+#include <yyjson.h>+struct MHD_Connection;struct srv_config {@@ -41,23 +45,36 @@ struct srv_config {for the model's own default */int reasoning_budget; /* the longest reasoning when a request does notsay, in tokens: -1 no limit (default), 0 none */- const char *embed_path; /* the embedding model, or NULL */- const char *embed_id; /* its name for clients */- const char *store_dir; /* where stored documents outlive the server;- NULL: memory only (--no-store) */- int keep; /* conversations kept computed besides the- current one (default 8) */- uint64_t keep_bytes; /* memory for them; 0: automatic */- uint64_t keep_disk; /* disk for them, below the memory; 0: none- (default 8 GiB) */- int no_mcp; /* refuse the tools of type mcp (Responses):- no connection goes out on a request's word */- uint32_t max_input; /* longest prompt taken, tokens; 0: the context */- uint32_t warn_input; /* prompts longer are taken, and said on stderr */- uint32_t fim_prefix; /* fill in the middle: the tokens before the- cursor read at most (0: all) */- uint32_t fim_suffix; /* the lines after it (0: all) */- uint32_t fim_max; /* the tokens it writes at most (0: as asked) */+ const char *embed_path; /* the embedding model, or NULL */+ const char *embed_id; /* its name for clients */+ const char *speech_path; /* the speech-to-text model, or NULL */+ const char *vad_path; /* its voice activity detector, or NULL */+ const char *speaker_path; /* its speaker embeddings, or NULL */+ const char *speech_id; /* its name for clients */+ const char *store_dir; /* where stored documents outlive the server;+ NULL: memory only (--no-store) */+ int keep; /* conversations kept computed besides the+ current one (default 8) */+ uint64_t keep_bytes; /* memory for them; 0: automatic */+ uint64_t keep_disk; /* disk for them, below the memory; 0: none+ (default 8 GiB) */+ int no_web; /* refuse the web_search tool (Responses) */+ int code_interpreter; /* run code (code_interpreter, containers) */+ const char *code_python; /* the python3 of the cage, NULL: the system's */+ const char *tls_cert, *tls_key; /* HTTPS (tls.c); NULL: HTTP */+ int no_mcp; /* refuse the tools of type mcp (Responses):+ no connection goes out on a request's word */+ struct janas_net_policy mcp_policy; /* where they may: public addresses,+ and those --mcp-allow names */+ const char **allow_origin; /* pages of other origins let in+ (--allow-origin; "*": any) */+ int n_allow_origin;+ uint32_t max_input; /* longest prompt taken, tokens; 0: the context */+ uint32_t warn_input; /* prompts longer are taken, and said on stderr */+ uint32_t fim_prefix; /* fill in the middle: the tokens before the+ cursor read at most (0: all) */+ uint32_t fim_suffix; /* the lines after it (0: all) */+ uint32_t fim_max; /* the tokens it writes at most (0: as asked) */};/* Stop strings a request may give: OpenAI takes four, editors' extensions@@ -76,14 +93,40 @@ void srv_reasoning_default(const struct srv_config *cfg,struct janas_llm_chat_params *p);/* A message of a chat request, with the tool calls of an assistant's. */+/* A picture of a message (images.c): its reference as the request gave it+ (a data: or http(s) URL, or a file id), and its bytes once fetched. */+/* a data: URL's bytes (base64) of at most max: 0, or -1 with the reason */+int srv_data_url(const char *url, size_t len, size_t max, unsigned char **data,+ size_t *n_data, char *err, size_t el);++struct srv_img {+ char *ref;+ int is_file;+ unsigned char *data;+ size_t n;+};+struct srv_msg {int32_t role; /* JANAS_LLM_ROLE_... */char *text;size_t n;int n_calls;char **call_name, **call_args;+ struct srv_img *imgs; /* a user message's pictures, before its text */+ int n_imgs;};+/* images.c: a part that is a picture (chat's image_url, the Responses'+ input_image) added to imgs: 1, 0 when the part is not a picture, -1 with+ the reason; the pictures of a job's messages fetched (0, or -1 with the+ reason); a list freed */+int srv_image_part(yyjson_val *part, const char *type, struct srv_img **imgs,+ int *n_imgs, char *err, size_t el);+struct srv_job;+struct srv;+int srv_images_fetch(struct srv *s, struct srv_job *j, char *err, size_t el);+void srv_imgs_free(struct srv_img *imgs, int n);+/* A prompt of a completion: text, or token ids (the engine turns theminto text, which is where an echo finds it). */struct srv_prompt {@@ -183,14 +226,22 @@ struct srv_job {struct janas_llm_chat_stats st; /* of the first reply */uint32_t prompt_tokens; /* of every prompt, once */uint32_t completion_tokens, reasoning_tokens; /* of all of them */- uint32_t queue_pos; /* requests ahead of it, while it waits */- int refs; /* the handler and the engine: freed by the last one */+ uint32_t queue_pos; /* requests ahead of it, while it waits */+ int low; /* a batch's: every other request goes first */+ const _Atomic int *watch; /* its submitter's (srv_engine_watch): 1 stops+ it as cancel does */+ int refs; /* the handler and the engine: freed by the last one */struct srv_job *next;};struct srv_engine *srv_engine_start(const struct srv_config *cfg, char *err,size_t err_len);void srv_engine_stop(struct srv_engine *e);+/* At stop, before the connections close: the clients' requests, running+ and waiting, end at their next token, and no new one is taken, so no+ connection waits for a whole reply that no one will read; the batches'+ and the evals' go on (their workers stop them where they resume). */+void srv_engine_cancel_clients(struct srv_engine *e);/* Whether an embedding model is open, and the length of its vectors. */int32_t srv_engine_embed_dim(const struct srv_engine *e);/* The model's description (janas_llm_describe), for /v1/models. */@@ -204,8 +255,13 @@ struct srv_job *srv_job_new(void);void srv_job_release(struct srv_job *j);/* The choices the job's replies go into: 0, or -1 on memory. */int srv_job_choices(struct srv_job *j, int n, int keep);-/* Queues the job; -1 when the queue is full. */+/* Queues the job; -1 when the queue is full. A job submitted on a thread+ that set srv_engine_low (the batches') waits behind every other. */int srv_engine_submit(struct srv_engine *e, struct srv_job *j);+extern _Thread_local int srv_engine_low;+/* A flag the jobs submitted on this thread watch: set to 1 elsewhere, they+ end at their next token (a run of the Assistants API cancelled). */+extern _Thread_local const _Atomic int *srv_engine_watch;/* How much of choice k's answer may be handed out now: while the replyruns, the end that could still turn out to be the start of a stop stringis held back. Call with j->mu held. */@@ -216,21 +272,43 @@ struct srv {struct srv_config cfg;struct srv_engine *engine;struct srv_store *store; /* completions, responses, conversations */+ struct srv_files *files; /* uploaded files (files_store.h) */struct MHD_Daemon *mhd;+ janas_llm_speech *speech; /* --speech-model, or NULL */+ pthread_mutex_t speech_mu; /* one transcription at a time */};struct sockaddr;+/* tls.c: the certificate and key read (0, or -1 with the reason), the+ listener of HTTPS on addr, and its end */+int srv_tls_setup(const char *cert, const char *key, char *err, size_t err_len);+int srv_tls_start(struct srv *s, const struct sockaddr *addr, char *err,+ size_t err_len);+void srv_tls_stop(void);/* Listening on addr (the port inside it), and no longer. */int srv_http_start(struct srv *s, const struct sockaddr *addr, char *err,size_t err_len);void srv_http_stop(struct srv *s);/* One request, as the handlers see it. */+/* An answer kept instead of sent: a request the server makes of itself+ (a batch's lines) */+struct srv_capture {+ unsigned status;+ char *body; /* malloc'd, NUL-terminated */+ size_t n;+ /* when set, a stream's bytes go to it as they come instead of into+ body (a run of the Assistants API reading /responses streamed) */+ void (*on_data)(void *arg, const char *p, size_t n);+ void *arg;+};+struct srv_req {struct srv *srv;- struct MHD_Connection *conn;- const char *method, *path; /* path without /v1 */- char param[2][256]; /* the {…} segments of the route, in order */+ struct MHD_Connection *conn; /* NULL with cap */+ struct srv_capture *cap; /* the answer goes here when set */+ const char *method, *path; /* path without /v1 */+ char param[3][256]; /* the {…} segments of the route, in order */int n_param;const char *body;size_t n_body;@@ -239,6 +317,8 @@ struct srv_req {/* A handler answers through the functions below and returns their result(an MHD_Result). */typedef int (*srv_handler)(struct srv_req *r);+/* The handler of method and path (without /v1), or NULL. */+srv_handler srv_route_find(const char *method, const char *path);/* Answers: JSON text (taken over, freed here), an OpenAI error, or astream of server-sent events. */@@ -256,6 +336,11 @@ int srv_reply_limit(struct srv_req *r, const struct srv_job *j);end; done(ctx) is called once the connection is over. */typedef long (*srv_stream_next)(void *ctx, char *buf, size_t cap);typedef void (*srv_stream_done)(void *ctx);+/* n bytes (taken over, freed here) of that Content-Type as the answer. */+int srv_reply_bytes(struct srv_req *r, unsigned status, char *body, size_t n,+ const char *type);+/* n bytes of an open file (taken over, closed here) as the answer. */+int srv_reply_fd(struct srv_req *r, int fd, uint64_t n, const char *type);int srv_reply_stream(struct srv_req *r, srv_stream_next next,srv_stream_done done, void *ctx);@@ -264,6 +349,11 @@ int srv_models_list(struct srv_req *r);int srv_models_get(struct srv_req *r);int srv_models_delete(struct srv_req *r);int srv_embeddings_create(struct srv_req *r);+/* audio.c: speech to text with the speech model (--speech-model) */+int srv_audio_transcriptions(struct srv_req *r);+int srv_audio_translations(struct srv_req *r);+int srv_speech_start(struct srv *s, char *err, size_t err_len);+void srv_speech_stop(struct srv *s);int srv_moderations_create(struct srv_req *r);int srv_chat_create(struct srv_req *r);int srv_completion_create(struct srv_req *r);@@ -274,6 +364,106 @@ int srv_stored_get(struct srv_req *r);int srv_stored_update(struct srv_req *r);int srv_stored_delete(struct srv_req *r);int srv_stored_messages(struct srv_req *r);+/* files.c: the Files API */+int srv_files_create(struct srv_req *r);+int srv_files_list_req(struct srv_req *r);+int srv_files_retrieve(struct srv_req *r);+int srv_files_delete_req(struct srv_req *r);+int srv_files_content(struct srv_req *r);+/* uploads.c: the Uploads API; a part's body may pass --max-body up to+ SRV_UPLOAD_PART_MAX and a little for its envelope */+#define SRV_UPLOAD_PART_MAX ((size_t)64 << 20)+int srv_uploads_create(struct srv_req *r);+int srv_uploads_part(struct srv_req *r);+int srv_uploads_complete(struct srv_req *r);+int srv_uploads_cancel(struct srv_req *r);+void srv_uploads_shutdown(void);+/* batches.c: the Batch API, and the thread that runs the batches (started+ once the store, the files and the engine are there) */+int srv_batches_create(struct srv_req *r);+int srv_batches_get(struct srv_req *r);+int srv_batches_cancel(struct srv_req *r);+int srv_batches_list(struct srv_req *r);+int srv_batches_start(struct srv *s);+void srv_batches_stop(void);+/* evals.c: the Evals API, and the thread that runs the evals' runs */+int srv_evals_create(struct srv_req *r);+int srv_evals_list(struct srv_req *r);+int srv_evals_get(struct srv_req *r);+int srv_evals_update(struct srv_req *r);+int srv_evals_delete(struct srv_req *r);+int srv_eval_runs_list(struct srv_req *r);+int srv_eval_runs_create(struct srv_req *r);+int srv_eval_runs_get(struct srv_req *r);+int srv_eval_runs_cancel(struct srv_req *r);+int srv_eval_runs_delete(struct srv_req *r);+int srv_eval_items_list(struct srv_req *r);+int srv_eval_items_get(struct srv_req *r);+int srv_evals_start(struct srv *s);+void srv_evals_stop(void);+/* containers.c: the Containers API (containers.h), and the sessions of+ code_interpreter, ended at stop */+int srv_containers_create(struct srv_req *r);+int srv_containers_list(struct srv_req *r);+int srv_containers_get(struct srv_req *r);+int srv_containers_delete(struct srv_req *r);+int srv_container_files_create(struct srv_req *r);+int srv_container_files_list(struct srv_req *r);+int srv_container_files_get(struct srv_req *r);+int srv_container_files_delete(struct srv_req *r);+int srv_container_files_content(struct srv_req *r);+int srv_containers_start(struct srv *s);+void srv_containers_stop(void);+/* assistants.c, runs.c: the Assistants API (assistants.h) */+int srv_assistants_create(struct srv_req *r);+int srv_assistants_list(struct srv_req *r);+int srv_assistants_get(struct srv_req *r);+int srv_assistants_modify(struct srv_req *r);+int srv_assistants_delete(struct srv_req *r);+int srv_threads_create(struct srv_req *r);+int srv_threads_get(struct srv_req *r);+int srv_threads_modify(struct srv_req *r);+int srv_threads_delete(struct srv_req *r);+int srv_messages_create(struct srv_req *r);+int srv_messages_list(struct srv_req *r);+int srv_messages_get(struct srv_req *r);+int srv_messages_modify(struct srv_req *r);+int srv_messages_delete(struct srv_req *r);+int srv_runs_create(struct srv_req *r);+int srv_runs_create_thread(struct srv_req *r);+int srv_runs_list(struct srv_req *r);+int srv_runs_get(struct srv_req *r);+int srv_runs_modify(struct srv_req *r);+int srv_runs_cancel(struct srv_req *r);+int srv_runs_submit(struct srv_req *r);+int srv_run_steps_list(struct srv_req *r);+int srv_run_steps_get(struct srv_req *r);+/* vectors_api.c: the Vector stores API */+int srv_vs_create(struct srv_req *r);+int srv_vs_list(struct srv_req *r);+int srv_vs_get(struct srv_req *r);+int srv_vs_modify(struct srv_req *r);+int srv_vs_delete(struct srv_req *r);+int srv_vs_file_create(struct srv_req *r);+int srv_vs_files_list(struct srv_req *r);+int srv_vs_file_get(struct srv_req *r);+int srv_vs_file_delete(struct srv_req *r);+int srv_vs_file_update(struct srv_req *r);+int srv_vs_file_content(struct srv_req *r);+int srv_vs_search(struct srv_req *r);+int srv_vs_batch_create(struct srv_req *r);+int srv_vs_batch_get(struct srv_req *r);+int srv_vs_batch_cancel(struct srv_req *r);+int srv_vs_batch_files(struct srv_req *r);+/* A file attached to store vs (cs, attrs: the request's JSON or NULL): 0,+ -1 refused (why says), -2 the disk. */+struct yyjson_val;+int srv_vs_attach(struct srv *srv, const char *vs, const char *file_id,+ struct yyjson_val *cs, struct yyjson_val *attrs,+ const char **why);+/* A page of the files of store vs (ids not NULL: of these alone). */+int srv_vs_files_page(struct srv_req *r, const char *vs, const char *const *ids,+ size_t n_ids);/* responses.c, conversations.c: OpenAI's Responses API */int srv_responses_create(struct srv_req *r);int srv_responses_get(struct srv_req *r);diff --git a/src/server/store.c b/src/server/store.cindex e141378..129c6bf 100644--- a/src/server/store.c+++ b/src/server/store.c@@ -21,6 +21,7 @@struct entry {char *kind, *id, *json, *aux;int64_t created;+ size_t size; /* of json and aux */uint64_t seq; /* the order it was made in, across restarts */struct entry *prev, *next;};@@ -31,10 +32,17 @@ struct srv_store {size_t cap;struct srv_disk *disk; /* or NULL: memory only */uint64_t seq;- int warned; /* a write failed, and it was said */+ size_t bytes; /* of every document's texts */+ int warned; /* a write failed, and it was said */};static void trim(struct srv_store *st, const char *kind);+static void trim_bytes(struct srv_store *st, const struct entry *keep);++static size_t doc_size(const char *json, const char *aux)+{+ return (json ? strlen(json) : 0) + (aux ? strlen(aux) : 0);+}static char *str_dup(const char *s){@@ -89,8 +97,21 @@ static void entry_free(struct entry *e)free(e);}+static void link_entry(struct srv_store *st, struct entry *e)+{+ e->prev = st->tail;+ e->next = NULL;+ if (st->tail)+ st->tail->next = e;+ else+ st->head = e;+ st->tail = e;+ st->bytes += e->size;+}+static void unlink_entry(struct srv_store *st, struct entry *e){+ st->bytes -= e->size;if (e->prev)e->prev->next = e->next;else@@ -147,6 +168,7 @@ static int take(void *arg, const char *kind, const char *id, uint64_t seq,}e->json = json;e->aux = aux;+ e->size = doc_size(json, aux);e->seq = seq;e->created = created;l->e[l->n++] = e;@@ -180,12 +202,7 @@ struct srv_store *srv_store_open(size_t cap, const char *dir, char *err,qsort(l.e, l.n, sizeof(*l.e), by_seq);for (size_t i = 0; i < l.n; i++) {struct entry *e = l.e[i];- e->prev = st->tail;- if (st->tail)- st->tail->next = e;- else- st->head = e;- st->tail = e;+ link_entry(st, e);if (e->seq > st->seq)st->seq = e->seq;}@@ -205,6 +222,7 @@ struct srv_store *srv_store_open(size_t cap, const char *dir, char *err,trim(st, kinds[k]);free(kinds[k]);}+ trim_bytes(st, NULL);if (skipped)fprintf(stderr,"janas-server: %d unreadable files in %s left as they are\n",@@ -243,6 +261,20 @@ static void trim(struct srv_store *st, const char *kind)}}+/* Past SRV_STORE_MAX bytes, the oldest go but keep. Under st->mu. */+static void trim_bytes(struct srv_store *st, const struct entry *keep)+{+ for (struct entry *x = st->head; x && st->bytes > SRV_STORE_MAX;) {+ struct entry *nx = x->next;+ if (x != keep) {+ unlink_entry(st, x);+ unsave(st, x);+ entry_free(x);+ }+ x = nx;+ }+}+static struct entry *find(struct srv_store *st, const char *kind,const char *id){@@ -255,6 +287,8 @@ static struct entry *find(struct srv_store *st, const char *kind,int srv_store_put(struct srv_store *st, const char *kind, const char *id,int64_t created, const char *json, const char *aux){+ if (doc_size(json, aux) > SRV_STORE_DOC_MAX)+ return -3;struct entry *e = calloc(1, sizeof(*e));if (!e)return -1;@@ -263,6 +297,7 @@ int srv_store_put(struct srv_store *st, const char *kind, const char *id,e->json = str_dup(json);e->aux = str_dup(aux);e->created = created;+ e->size = doc_size(json, aux);if (!e->kind || !e->id || !e->json || (aux && !e->aux)) {entry_free(e);return -1;@@ -274,14 +309,10 @@ int srv_store_put(struct srv_store *st, const char *kind, const char *id,entry_free(old);}e->seq = ++st->seq;- e->prev = st->tail;- if (st->tail)- st->tail->next = e;- else- st->head = e;- st->tail = e;+ link_entry(st, e);save(st, e);trim(st, kind);+ trim_bytes(st, e);pthread_mutex_unlock(&st->mu);return 0;}@@ -315,6 +346,7 @@ int srv_store_update(struct srv_store *st, const char *kind, const char *id,const char *json, const char *aux){char *j = str_dup(json), *a = str_dup(aux);+ int rc = -1;if ((json && !j) || (aux && !a)) {free(j);free(a);@@ -322,7 +354,10 @@ int srv_store_update(struct srv_store *st, const char *kind, const char *id,}pthread_mutex_lock(&st->mu);struct entry *e = find(st, kind, id);- if (e) {+ if (e && doc_size(j ? j : e->json, a ? a : e->aux) > SRV_STORE_DOC_MAX)+ rc = -3;+ else if (e) {+ st->bytes -= e->size;if (j) {free(e->json);e->json = j;@@ -333,12 +368,16 @@ int srv_store_update(struct srv_store *st, const char *kind, const char *id,e->aux = a;a = NULL;}+ e->size = doc_size(e->json, e->aux);+ st->bytes += e->size;save(st, e);+ trim_bytes(st, e);+ rc = 0;}pthread_mutex_unlock(&st->mu);free(j);free(a);- return e ? 0 : -1;+ return rc;}int srv_store_del(struct srv_store *st, const char *kind, const char *id)diff --git a/src/server/store.h b/src/server/store.hindex ec9d499..7d5c9e2 100644--- a/src/server/store.h+++ b/src/server/store.h@@ -16,6 +16,11 @@struct srv_store;+/* A document (its two texts) at most, and all of them in memory at most:+ past the second the oldest go, whatever their kind. */+#define SRV_STORE_DOC_MAX ((size_t)64 << 20)+#define SRV_STORE_MAX ((size_t)1 << 30)+/* cap: documents of each kind at most, the oldest dropped (0: 1000). Inmemory only. */struct srv_store *srv_store_new(size_t cap);@@ -29,14 +34,15 @@ void srv_store_free(struct srv_store *st);size_t srv_store_count(struct srv_store *st);/* Keeps a copy (replacing one of the same kind and id); aux may be NULL.- 0, or -1 on memory. */+ 0, -1 on memory, -3 larger than SRV_STORE_DOC_MAX. */int srv_store_put(struct srv_store *st, const char *kind, const char *id,int64_t created, const char *json, const char *aux);/* Copies of the document (json, aux: either may be NULL, and *aux is NULLwhen there is none): 0, -1 not found, -2 memory. */int srv_store_get(struct srv_store *st, const char *kind, const char *id,char **json, char **aux);-/* New texts for a document (NULL: unchanged): 0, -1 not found, -2 memory. */+/* New texts for a document (NULL: unchanged): 0, -1 not found, -2 memory,+ -3 larger than SRV_STORE_DOC_MAX. */int srv_store_update(struct srv_store *st, const char *kind, const char *id,const char *json, const char *aux);/* 0, or -1 not found. */diff --git a/src/server/store_disk.c b/src/server/store_disk.cindex 9d1b43e..82fdbae 100644--- a/src/server/store_disk.c+++ b/src/server/store_disk.c@@ -15,6 +15,7 @@*/#define _GNU_SOURCE#include "store_disk.h"+#include "store.h" /* SRV_STORE_DOC_MAX */#include <dirent.h>#include <errno.h>@@ -199,6 +200,8 @@ static int load_one(const char *path, const char *kind, const char *id,int ok = fscanf(f, "JANAS-STORE 1 %" SCNu64 " %" SCNd64 " %lld %lld", &seq,&created, &nj, &na) == 4 &&fgetc(f) == '\n' && nj >= 0 && na >= -1 &&+ (unsigned long long)nj + (unsigned long long)(na > 0 ? na : 0) <=+ SRV_STORE_DOC_MAX &&(json = read_n(f, nj)) && (na < 0 || (aux = read_n(f, na))) &&fgetc(f) == EOF && strlen(json) == (size_t)nj &&(!aux || strlen(aux) == (size_t)na);diff --git a/src/server/tls.c b/src/server/tls.cnew file mode 100644index 0000000..0daa556--- /dev/null+++ b/src/server/tls.c@@ -0,0 +1,335 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * tls.c - HTTPS for janas-server (--tls-cert, --tls-key), on the Mbed TLS+ * the MCP client already carries. libmicrohttpd would want GnuTLS for it;+ * instead the server listens itself, a thread for each connection does the+ * handshake and then carries the bytes between the TLS session and one end+ * of a pair of sockets, whose other end libmicrohttpd is handed+ * (MHD_add_connection) as if a client had connected in clear. Everything+ * above - the requests, the streams, the origins - is the same.+ *+ * Mbed TLS has no threading of its own here: every call to it takes the+ * process's one lock (janas_tls_lock), the waits never inside it.+ */+#include "server.h"++#include <errno.h>+#include <fcntl.h>+#include <microhttpd.h>+#include <poll.h>+#include <pthread.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <netinet/in.h>+#include <sys/socket.h>+#include <unistd.h>++#include <mbedtls/error.h>+#include <mbedtls/pk.h>+#include <mbedtls/ssl.h>+#include <mbedtls/x509_crt.h>++#include "common/mcp_tls.h"+#include "common/sock.h"++#define HANDSHAKE_MS 30000++#ifndef _WIN32 /* not served there yet: socketpair */+static mbedtls_ssl_config conf;+static mbedtls_x509_crt chain;+static mbedtls_pk_context key;+static int listen_fd = -1;+static pthread_t acceptor;+static volatile int stopping;+static _Atomic int active; /* connections being served */+static struct srv *server;++static void why(int rc, char *err, size_t err_len, const char *what)+{+ char m[160];+ mbedtls_strerror(rc, m, sizeof m);+ snprintf(err, err_len, "%s: %s", what, m);+}++/* 0 when the key is the certificate's: mbedtls_pk_check_pair, or, as it+ refuses RSA pairs that are one here, their public halves the same */+static int same_key(const mbedtls_pk_context *pub,+ const mbedtls_pk_context *prv)+{+ if (mbedtls_pk_check_pair(pub, prv) == 0)+ return 0;+ static unsigned char a[4096], b[4096];+ int na = mbedtls_pk_write_pubkey_der(pub, a, sizeof a);+ int nb = mbedtls_pk_write_pubkey_der(prv, b, sizeof b);+ /* written at the end of the buffers */+ return na > 0 && na == nb &&+ memcmp(a + sizeof a - na, b + sizeof b - nb, (size_t)na) == 0+ ? 0+ : -1;+}+#endif++int srv_tls_setup(const char *cert, const char *keyfile, char *err,+ size_t err_len)+{+#ifdef _WIN32+ (void)cert;+ (void)keyfile;+ snprintf(err, err_len, "HTTPS is not served on Windows yet");+ return -1;+#else+ janas_tls_lock();+ int rc = janas_tls_crypto() == 0 ? 0 : -1;+ if (rc) {+ janas_tls_unlock();+ snprintf(err, err_len, "the crypto library would not start");+ return -1;+ }+ mbedtls_x509_crt_init(&chain);+ mbedtls_pk_init(&key);+ mbedtls_ssl_config_init(&conf);+ if (access(cert, R_OK) != 0 || access(keyfile, R_OK) != 0) {+ rc = -1;+ snprintf(err, err_len, "%s: %s", access(cert, R_OK) ? cert : keyfile,+ strerror(errno));+ } else if ((rc = mbedtls_x509_crt_parse_file(&chain, cert)) != 0)+ snprintf(err, err_len, "%s: not a certificate in PEM", cert);+ else if ((rc = mbedtls_pk_parse_keyfile(&key, keyfile, NULL)) != 0)+ snprintf(err, err_len,+ "%s: not a private key in PEM (or one with a password)",+ keyfile);+ else if ((rc = same_key(&chain.pk, &key)) != 0)+ snprintf(err, err_len, "%s is not the key of %s", keyfile, cert);+ else if ((rc = mbedtls_ssl_config_defaults(+ &conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM,+ MBEDTLS_SSL_PRESET_DEFAULT)) != 0)+ why(rc, err, err_len, "TLS");+ else if ((rc = mbedtls_ssl_conf_own_cert(&conf, &chain, &key)) != 0)+ why(rc, err, err_len, "the certificate and its key");+ janas_tls_unlock();+ return rc ? -1 : 0;+#endif+}++#ifndef _WIN32+static int bio_send(void *ctx, const unsigned char *p, size_t n)+{+ ssize_t w = send(*(int *)ctx, p, n, MSG_NOSIGNAL);+ if (w >= 0)+ return (int)w;+ return errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR+ ? MBEDTLS_ERR_SSL_WANT_WRITE+ : MBEDTLS_ERR_SSL_INTERNAL_ERROR;+}++static int bio_recv(void *ctx, unsigned char *p, size_t n)+{+ ssize_t r = recv(*(int *)ctx, p, n, 0);+ if (r >= 0)+ return (int)r;+ return errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR+ ? MBEDTLS_ERR_SSL_WANT_READ+ : MBEDTLS_ERR_SSL_INTERNAL_ERROR;+}++struct conn {+ int fd; /* the client's, non-blocking */+ struct sockaddr_storage peer;+ socklen_t peer_len;+};++/* All of n bytes to a socket in clear: 0, or -1. */+static int put_all(int fd, const unsigned char *p, size_t n)+{+ while (n) {+ ssize_t w = send(fd, p, n, MSG_NOSIGNAL);+ if (w < 0 && (errno == EAGAIN || errno == EINTR)) {+ struct pollfd pf = {.fd = fd, .events = POLLOUT};+ poll(&pf, 1, 1000);+ continue;+ }+ if (w <= 0)+ return -1;+ p += w;+ n -= (size_t)w;+ }+ return 0;+}++static void *serve(void *arg)+{+ struct conn *c = arg;+ active++;+ mbedtls_ssl_context ssl;+ int sp[2] = {-1, -1};+ janas_tls_lock();+ mbedtls_ssl_init(&ssl);+ int rc = mbedtls_ssl_setup(&ssl, &conf);+ mbedtls_ssl_set_bio(&ssl, &c->fd, bio_send, bio_recv, NULL);+ janas_tls_unlock();+ /* the handshake, the waits outside the lock */+ int waited = 0;+ while (rc == 0 && !stopping) {+ janas_tls_lock();+ rc = mbedtls_ssl_handshake(&ssl);+ janas_tls_unlock();+ if (rc != MBEDTLS_ERR_SSL_WANT_READ && rc != MBEDTLS_ERR_SSL_WANT_WRITE)+ break;+ struct pollfd pf = {+ .fd = c->fd,+ .events = rc == MBEDTLS_ERR_SSL_WANT_READ ? POLLIN : POLLOUT};+ if (poll(&pf, 1, 500) == 0 && (waited += 500) >= HANDSHAKE_MS)+ break;+ rc = 0;+ }+ if (rc != 0 || stopping ||+ socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sp) != 0 ||+ MHD_add_connection(server->mhd, sp[0], (struct sockaddr *)&c->peer,+ c->peer_len) != MHD_YES)+ goto done;+ sp[0] = -1; /* libmicrohttpd's now */+ /* the bytes both ways until either side closes */+ unsigned char buf[16384];+ for (;;) {+ janas_tls_lock();+ int pending = (int)mbedtls_ssl_get_bytes_avail(&ssl);+ janas_tls_unlock();+ struct pollfd pf[2] = {{.fd = c->fd, .events = POLLIN},+ {.fd = sp[1], .events = POLLIN}};+ if (!pending && poll(pf, 2, 1000) < 0 && errno != EINTR)+ break;+ if (stopping)+ break;+ if (pending || (pf[0].revents & (POLLIN | POLLHUP | POLLERR))) {+ janas_tls_lock();+ int r = mbedtls_ssl_read(&ssl, buf, sizeof buf);+ janas_tls_unlock();+ if (r > 0) {+ if (put_all(sp[1], buf, (size_t)r) != 0)+ break;+ } else if (r != MBEDTLS_ERR_SSL_WANT_READ &&+ r != MBEDTLS_ERR_SSL_WANT_WRITE &&+ r != MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET) {+ break; /* closed, or an error */+ }+ }+ if (pf[1].revents & (POLLIN | POLLHUP | POLLERR)) {+ ssize_t r = recv(sp[1], buf, sizeof buf, 0);+ if (r <= 0 && !(r < 0 && (errno == EAGAIN || errno == EINTR)))+ break;+ for (ssize_t done = 0; r > 0 && done < r;) {+ janas_tls_lock();+ int w = mbedtls_ssl_write(&ssl, buf + done, (size_t)(r - done));+ janas_tls_unlock();+ if (w > 0)+ done += w;+ else if (w == MBEDTLS_ERR_SSL_WANT_WRITE ||+ w == MBEDTLS_ERR_SSL_WANT_READ) {+ struct pollfd po = {.fd = c->fd, .events = POLLOUT};+ poll(&po, 1, 1000);+ } else {+ goto done;+ }+ }+ }+ }+done:+ janas_tls_lock();+ mbedtls_ssl_close_notify(&ssl);+ mbedtls_ssl_free(&ssl);+ janas_tls_unlock();+ if (sp[0] >= 0)+ close(sp[0]);+ if (sp[1] >= 0)+ close(sp[1]);+ close(c->fd);+ free(c);+ active--;+ return NULL;+}++static void *accept_main(void *arg)+{+ (void)arg;+ while (!stopping) {+ struct pollfd pf = {.fd = listen_fd, .events = POLLIN};+ if (poll(&pf, 1, 500) <= 0)+ continue;+ struct conn *c = calloc(1, sizeof(*c));+ if (!c)+ continue;+ c->peer_len = sizeof c->peer;+ c->fd = accept(listen_fd, (struct sockaddr *)&c->peer, &c->peer_len);+ if (c->fd < 0) {+ free(c);+ continue;+ }+ fcntl(c->fd, F_SETFL, fcntl(c->fd, F_GETFL) | O_NONBLOCK);+ pthread_t t;+ pthread_attr_t at;+ pthread_attr_init(&at);+ pthread_attr_setdetachstate(&at, PTHREAD_CREATE_DETACHED);+ if (pthread_create(&t, &at, serve, c) != 0) {+ close(c->fd);+ free(c);+ }+ pthread_attr_destroy(&at);+ }+ return NULL;+}+#endif++int srv_tls_start(struct srv *s, const struct sockaddr *addr, char *err,+ size_t err_len)+{+#ifdef _WIN32+ (void)s;+ (void)addr;+ snprintf(err, err_len, "HTTPS is not served on Windows yet");+ return -1;+#else+ server = s;+ socklen_t len = addr->sa_family == AF_INET6+ ? (socklen_t)sizeof(struct sockaddr_in6)+ : (socklen_t)sizeof(struct sockaddr_in);+ listen_fd = socket(addr->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0);+ int one = 1;+ if (listen_fd >= 0)+ setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);+ if (listen_fd < 0 || bind(listen_fd, addr, len) != 0 ||+ listen(listen_fd, 64) != 0) {+ snprintf(err, err_len, "cannot listen on %s:%u: %s", s->cfg.host,+ (unsigned)s->cfg.port, strerror(errno));+ if (listen_fd >= 0)+ close(listen_fd);+ listen_fd = -1;+ return -1;+ }+ stopping = 0;+ if (pthread_create(&acceptor, NULL, accept_main, NULL) != 0) {+ snprintf(err, err_len, "cannot start the TLS listener");+ close(listen_fd);+ listen_fd = -1;+ return -1;+ }+ return 0;+#endif+}++void srv_tls_stop(void)+{+#ifndef _WIN32+ if (listen_fd < 0)+ return;+ stopping = 1;+ pthread_join(acceptor, NULL);+ /* the connections end within a second of their polls: before+ libmicrohttpd stops under them */+ for (int i = 0; active > 0 && i < 50; i++)+ usleep(100000);+ close(listen_fd);+ listen_fd = -1;+#endif+}diff --git a/src/server/uploads.c b/src/server/uploads.cnew file mode 100644index 0000000..eb48078--- /dev/null+++ b/src/server/uploads.c@@ -0,0 +1,439 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * uploads.c - OpenAI's Uploads API: a file larger than one request sent in+ * parts (each at most 64 MiB), joined into a File when the client+ * completes the upload, checked against the size it announced and the MD5+ * it may give. An upload lasts an hour, as at OpenAI. Its parts wait on+ * disk beside the files (.uploads in their directory); what is pending is+ * known only to the server running, so a restart drops it and the parts+ * left over are removed at start.+ */+#include "server.h"++#include <dirent.h>+#include <errno.h>+#include <fcntl.h>+#include <microhttpd.h>+#include <pthread.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include <time.h>+#include <unistd.h>+#include <yyjson.h>++#include "files.h"+#include "files_store.h"+#include "multipart.h"+#include "request.h"+#include "store_disk.h"++#define UPLOAD_MAX ((uint64_t)8 << 30) /* bytes of an upload, as OpenAI */+#define UPLOAD_LIFE 3600 /* seconds */+#define UPLOADS_KEPT 256 /* uploads remembered at most */++enum { PENDING, COMPLETED, CANCELLED, EXPIRED };+static const char *const STATUS[] = {"pending", "completed", "cancelled",+ "expired"};++struct upload {+ char id[48];+ char filename[256];+ char purpose[16];+ uint64_t bytes, got;+ int64_t created_at, expires_at, file_after;+ int status;+ char (*part)[48]; /* the parts' ids, in the order they came */+ uint64_t *part_bytes;+ size_t np;+ struct srv_file file; /* once completed */+};++static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;+static struct upload *ups;+static size_t n_ups;+static char parts_dir[4200];++/* The directory of the parts; what an earlier run left in it goes. */+static int ensure_dir(struct srv *srv)+{+ if (parts_dir[0])+ return 0;+ snprintf(parts_dir, sizeof parts_dir, "%s/.uploads",+ srv_files_dir(srv->files));+ if (mkdir(parts_dir, 0700) != 0 && errno != EEXIST) {+ parts_dir[0] = 0;+ return -1;+ }+ DIR *d = opendir(parts_dir);+ struct dirent *de;+ while (d && (de = readdir(d))) {+ char p[4500];+ if (de->d_name[0] != '.' && snprintf(p, sizeof p, "%s/%s", parts_dir,+ de->d_name) < (int)sizeof p)+ unlink(p);+ }+ if (d)+ closedir(d);+ return 0;+}++static void part_path(char *out, size_t cap, const char *id)+{+ snprintf(out, cap, "%s/%s", parts_dir, id);+}++static void drop_parts(struct upload *u)+{+ for (size_t i = 0; i < u->np; i++) {+ char p[4500];+ part_path(p, sizeof p, u->part[i]);+ unlink(p);+ }+ free(u->part);+ free(u->part_bytes);+ u->part = NULL;+ u->part_bytes = NULL;+ u->np = 0;+}++/* Under mu: past its hour, a pending upload expires. */+static void age(struct upload *u)+{+ if (u->status == PENDING && (int64_t)time(NULL) >= u->expires_at) {+ u->status = EXPIRED;+ drop_parts(u);+ }+}++static struct upload *find(const char *id)+{+ for (size_t i = 0; i < n_ups; i++)+ if (strcmp(ups[i].id, id) == 0) {+ age(&ups[i]);+ return &ups[i];+ }+ return NULL;+}++static int reply_upload(struct srv_req *r, const struct upload *u)+{+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, o, "id", u->id);+ yyjson_mut_obj_add_str(d, o, "object", "upload");+ yyjson_mut_obj_add_uint(d, o, "bytes", u->bytes);+ yyjson_mut_obj_add_int(d, o, "created_at", u->created_at);+ yyjson_mut_obj_add_strcpy(d, o, "filename", u->filename);+ yyjson_mut_obj_add_strcpy(d, o, "purpose", u->purpose);+ yyjson_mut_obj_add_str(d, o, "status", STATUS[u->status]);+ yyjson_mut_obj_add_int(d, o, "expires_at", u->expires_at);+ if (u->status == COMPLETED)+ yyjson_mut_obj_add_val(d, o, "file", srv_file_json(d, &u->file));+ else+ yyjson_mut_obj_add_null(d, o, "file");+ yyjson_mut_doc_set_root(d, o);+ size_t n;+ char *out = yyjson_mut_write(d, 0, &n);+ yyjson_mut_doc_free(d);+ return srv_reply_json(r, 200, out, n);+}++static int bad(struct srv_req *r, const char *msg)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_value",+ "%s", msg);+}++static int no_upload(struct srv_req *r)+{+ return srv_reply_error(r, 404, "invalid_request_error", "not_found",+ "No such Upload: %s", r->param[0]);+}++static int not_pending(struct srv_req *r, const struct upload *u)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_state",+ "The upload %s is %s, not pending.", u->id,+ STATUS[u->status]);+}++int srv_uploads_create(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ const char *filename = yyjson_get_str(yyjson_obj_get(root, "filename"));+ const char *purpose = yyjson_get_str(yyjson_obj_get(root, "purpose"));+ yyjson_val *by = yyjson_obj_get(root, "bytes");+ yyjson_val *ea = yyjson_obj_get(root, "expires_after");+ static const char *const purposes[] = {+ "assistants", "batch", "fine-tune", "vision", "user_data", "evals"};+ int pok = 0;+ for (size_t i = 0; purpose && i < 6; i++)+ pok |= !strcmp(purpose, purposes[i]);+ int64_t file_after = 0;+ const char *why = NULL;+ if (!yyjson_is_obj(root))+ why = "The body is a JSON object: filename, purpose, bytes, "+ "mime_type.";+ else if (!filename || !*filename)+ why = "'filename' is required.";+ else if (!pok)+ why = "'purpose' is assistants, batch, fine-tune, vision, user_data "+ "or evals.";+ else if (!yyjson_is_uint(by) &&+ !(yyjson_is_int(by) && yyjson_get_sint(by) >= 0))+ why = "'bytes' is the file's size, a whole number.";+ else if (yyjson_get_uint(by) > UPLOAD_MAX)+ why = "An upload is at most 8 GiB.";+ else if (!yyjson_get_str(yyjson_obj_get(root, "mime_type")))+ why = "'mime_type' is required.";+ else if (ea) {+ yyjson_val *an = yyjson_obj_get(ea, "anchor"),+ *se = yyjson_obj_get(ea, "seconds");+ if (!yyjson_equals_str(an, "created_at") || !yyjson_is_int(se) ||+ yyjson_get_sint(se) < 3600 || yyjson_get_sint(se) > 2592000)+ why = "'expires_after' is anchor created_at and seconds from "+ "3600 to 2592000.";+ else+ file_after = yyjson_get_sint(se);+ }+ if (!why && !ea && !strcmp(purpose, "batch"))+ file_after = 2592000;+ if (why) {+ yyjson_doc_free(doc);+ return bad(r, why);+ }+ struct upload u = {0};+ srv_new_id(u.id, sizeof u.id, "upload_");+ snprintf(u.filename, sizeof u.filename, "%s", filename);+ snprintf(u.purpose, sizeof u.purpose, "%s", purpose);+ u.bytes = yyjson_get_uint(by);+ u.created_at = (int64_t)time(NULL);+ u.expires_at = u.created_at + UPLOAD_LIFE;+ u.file_after = file_after;+ u.status = PENDING;+ yyjson_doc_free(doc);+ pthread_mutex_lock(&mu);+ int ok = ensure_dir(r->srv) == 0;+ if (ok && n_ups == UPLOADS_KEPT) {+ /* the oldest that is no longer pending makes room */+ size_t k = 0;+ while (k < n_ups && (age(&ups[k]), ups[k].status == PENDING))+ k++;+ if (k == n_ups)+ ok = 0;+ else {+ drop_parts(&ups[k]);+ memmove(&ups[k], &ups[k + 1], (n_ups - k - 1) * sizeof(ups[0]));+ n_ups--;+ }+ }+ struct upload *t = ok ? realloc(ups, (n_ups + 1) * sizeof(*t)) : NULL;+ if (t) {+ ups = t;+ ups[n_ups++] = u;+ }+ pthread_mutex_unlock(&mu);+ if (!t)+ return srv_reply_error(r, 429, "invalid_request_error",+ "too_many_uploads",+ "%d uploads are pending: complete or cancel "+ "some.",+ UPLOADS_KEPT);+ return reply_upload(r, &u);+}++int srv_uploads_part(struct srv_req *r)+{+ struct srv_part parts[4];+ char err[200];+ const char *ct =+ MHD_lookup_connection_value(r->conn, MHD_HEADER_KIND, "Content-Type");+ int n =+ srv_multipart_parse(ct, r->body, r->n_body, parts, 4, err, sizeof err);+ if (n < 0)+ return bad(r, err);+ const struct srv_part *data = srv_multipart_get(parts, n, "data");+ if (!data)+ return bad(r, "'data' is required: the part's bytes.");+ if (data->n > SRV_UPLOAD_PART_MAX)+ return bad(r, "A part is at most 64 MiB.");+ char id[48], path[4500];+ srv_new_id(id, sizeof id, "part_");+ pthread_mutex_lock(&mu);+ struct upload *u = find(r->param[0]);+ int ret = -1;+ if (!u)+ ret = no_upload(r);+ else if (u->status != PENDING)+ ret = not_pending(r, u);+ else if (u->got + data->n > u->bytes)+ ret = bad(r, "The parts would pass the bytes the upload was created "+ "with.");+ int64_t now = (int64_t)time(NULL);+ if (ret == -1) {+ part_path(path, sizeof path, id);+ int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);+ size_t done = 0;+ while (fd >= 0 && done < data->n) {+ ssize_t w = write(fd, data->data + done, data->n - done);+ if (w <= 0)+ break;+ done += (size_t)w;+ }+ void *a = realloc(u->part, (u->np + 1) * sizeof(*u->part));+ if (a)+ u->part = a;+ void *b = realloc(u->part_bytes, (u->np + 1) * sizeof(uint64_t));+ if (b)+ u->part_bytes = b;+ if (fd < 0 || close(fd) != 0 || done != data->n || !a || !b) {+ unlink(path);+ ret = srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the part");+ } else {+ snprintf(u->part[u->np], sizeof u->part[0], "%s", id);+ u->part_bytes[u->np++] = data->n;+ u->got += data->n;+ }+ }+ pthread_mutex_unlock(&mu);+ if (ret != -1)+ return ret;+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, o, "id", id);+ yyjson_mut_obj_add_str(d, o, "object", "upload.part");+ yyjson_mut_obj_add_int(d, o, "created_at", now);+ yyjson_mut_obj_add_strcpy(d, o, "upload_id", r->param[0]);+ yyjson_mut_doc_set_root(d, o);+ size_t len;+ char *out = yyjson_mut_write(d, 0, &len);+ yyjson_mut_doc_free(d);+ return srv_reply_json(r, 200, out, len);+}++int srv_uploads_complete(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_val *ids = yyjson_obj_get(root, "part_ids");+ const char *md5 = yyjson_get_str(yyjson_obj_get(root, "md5"));+ if (!yyjson_is_arr(ids)) {+ yyjson_doc_free(doc);+ return bad(r, "'part_ids' is the parts' ids, in order.");+ }+ pthread_mutex_lock(&mu);+ struct upload *u = find(r->param[0]);+ int ret = -1;+ size_t n = yyjson_arr_size(ids);+ char **paths = n ? calloc(n, sizeof(char *)) : NULL;+ if (!u)+ ret = no_upload(r);+ else if (u->status != PENDING)+ ret = not_pending(r, u);+ else if (n == 0 || !paths)+ ret = bad(r, "'part_ids' names no part.");+ /* each id one of this upload's parts, once */+ size_t idx, max;+ yyjson_val *v;+ char *used = u && ret == -1 ? calloc(u->np ? u->np : 1, 1) : NULL;+ if (ret == -1 && !used)+ ret = srv_reply_error(r, 500, "server_error", NULL, "out of memory");+ yyjson_arr_foreach(ids, idx, max, v)+ {+ if (ret != -1)+ break;+ const char *pid = yyjson_get_str(v);+ size_t k = 0;+ while (pid && k < u->np && strcmp(u->part[k], pid) != 0)+ k++;+ if (!pid || k == u->np || used[k]) {+ ret = bad(r, "'part_ids' names a part this upload does not have, "+ "or one twice.");+ break;+ }+ used[k] = 1;+ paths[idx] = malloc(4500);+ if (!paths[idx]) {+ ret =+ srv_reply_error(r, 500, "server_error", NULL, "out of memory");+ break;+ }+ part_path(paths[idx], 4500, pid);+ }+ free(used);+ /* joined outside the lock: the upload is marked so meanwhile */+ struct upload copy;+ if (ret == -1) {+ u->status = COMPLETED; /* nothing else may touch it now */+ copy = *u;+ }+ pthread_mutex_unlock(&mu);+ if (ret == -1) {+ char err[200];+ struct srv_file f;+ int rc = srv_files_add_parts(r->srv->files, copy.filename, copy.purpose,+ (const char *const *)paths, n, copy.bytes,+ md5, copy.file_after, &f, err, sizeof err);+ pthread_mutex_lock(&mu);+ u = find(copy.id);+ if (u) {+ if (rc == 0) {+ u->file = f;+ drop_parts(u);+ copy = *u;+ } else {+ u->status = PENDING; /* the client may try again */+ }+ }+ pthread_mutex_unlock(&mu);+ if (rc == 0)+ ret = reply_upload(r, ©);+ else if (rc == -3 || rc == -2)+ ret = srv_reply_error(+ r, rc == -2 ? 413 : 400, "invalid_request_error",+ rc == -2 ? "files_limit_reached" : "invalid_value", "%s", err);+ else+ ret = srv_reply_error(r, 500, "server_error", NULL, "%s", err);+ }+ for (size_t i = 0; paths && i < n; i++)+ free(paths[i]);+ free(paths);+ yyjson_doc_free(doc);+ return ret;+}++int srv_uploads_cancel(struct srv_req *r)+{+ pthread_mutex_lock(&mu);+ struct upload *u = find(r->param[0]);+ struct upload copy;+ int ret = -1;+ if (!u)+ ret = no_upload(r);+ else if (u->status != PENDING)+ ret = not_pending(r, u);+ else {+ u->status = CANCELLED;+ drop_parts(u);+ copy = *u;+ }+ pthread_mutex_unlock(&mu);+ return ret != -1 ? ret : reply_upload(r, ©);+}++void srv_uploads_shutdown(void)+{+ pthread_mutex_lock(&mu);+ for (size_t i = 0; i < n_ups; i++)+ drop_parts(&ups[i]);+ free(ups);+ ups = NULL;+ n_ups = 0;+ parts_dir[0] = 0;+ pthread_mutex_unlock(&mu);+}diff --git a/src/server/vectors.c b/src/server/vectors.cnew file mode 100644index 0000000..541fa82--- /dev/null+++ b/src/server/vectors.c@@ -0,0 +1,900 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vectors.c - the vector stores' data, indexing and search (see vectors.h).+ *+ * On disk, beside the uploaded files (.vectors in their directory), a+ * store is a directory: meta.json the store as OpenAI writes it, files/+ * one JSON object for each file attached (its status, attributes, chunking+ * strategy), and for each file indexed <file>.chunks, its chunks as JSON+ * lines, and <file>.vec, their embeddings (a 4-byte dimension, then the+ * floats). A file is cut at whitespace into chunks of about the tokens the+ * strategy says (four bytes a token), each overlapping the one before.+ *+ * A thread indexes the files waiting, one at a time and behind every+ * client's request (srv_engine_low): their chunks' embeddings come from+ * the same handler as /v1/embeddings, called from inside. Without an+ * embedding model the chunks are kept alone, and the search reads the+ * words (BM25) instead of the meaning.+ */+#include "vectors.h"++#include <ctype.h>+#include <dirent.h>+#include <errno.h>+#include <math.h>+#include <pthread.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <strings.h>+#include <sys/stat.h>+#include <time.h>+#include <unistd.h>++#include "files_store.h"+#include "llm/json.h"+#include "server.h"+#include "store_disk.h"++#define EMBED_BATCH 16+#define TEXT_MAX ((size_t)32 << 20) /* a file indexed at most */++static struct srv *server;+static char root[4200];+static pthread_t worker;+static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;+static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;+static int running, stopping, kicked;++void srv_vectors_dir(char *out, size_t cap, const char *vs)+{+ if (vs)+ snprintf(out, cap, "%s/%s", root, vs);+ else+ snprintf(out, cap, "%s", root);+}++/* ---- small file helpers ---- */++static char *read_text(const char *path, size_t *n)+{+ FILE *f = fopen(path, "rb");+ if (!f)+ return NULL;+ char *b = NULL;+ long len = fseek(f, 0, SEEK_END) ? -1 : ftell(f);+ if (len >= 0 && !fseek(f, 0, SEEK_SET) && (b = malloc((size_t)len + 1)) &&+ fread(b, 1, (size_t)len, f) != (size_t)len) {+ free(b);+ b = NULL;+ }+ fclose(f);+ if (b) {+ b[len] = 0;+ if (n)+ *n = (size_t)len;+ }+ return b;+}++int srv_vectors_write(const char *path, const char *text, size_t n)+{+ char tmp[4400];+ snprintf(tmp, sizeof tmp, "%s.tmp", path);+ FILE *f = fopen(tmp, "wb");+ if (!f)+ return -1;+ int ok = fwrite(text, 1, n, f) == n;+ ok = !fclose(f) && ok;+ if (!ok || rename(tmp, path) != 0) {+ unlink(tmp);+ return -1;+ }+ return 0;+}++/* ---- chunks ---- */++struct chunks {+ char **t;+ size_t n, cap;+};++static int chunk_add(struct chunks *c, const char *s, size_t n)+{+ if (c->n == c->cap) {+ size_t cap = c->cap ? 2 * c->cap : 64;+ char **t = realloc(c->t, cap * sizeof(*t));+ if (!t)+ return -1;+ c->t = t;+ c->cap = cap;+ }+ char *x = malloc(n + 1);+ if (!x)+ return -1;+ memcpy(x, s, n);+ x[n] = 0;+ c->t[c->n++] = x;+ return 0;+}++static void chunks_free(struct chunks *c)+{+ for (size_t i = 0; i < c->n; i++)+ free(c->t[i]);+ free(c->t);+ memset(c, 0, sizeof(*c));+}++/* The text cut into chunks of about size bytes, each beginning overlap+ bytes before the last one ended, at whitespace and at the start of a+ UTF-8 character. */+static int cut(const char *t, size_t n, size_t size, size_t overlap,+ struct chunks *out)+{+ size_t at = 0;+ while (at < n) {+ size_t end = at + size < n ? at + size : n;+ if (end < n) { /* back to a space, not further than half */+ size_t e = end;+ while (e > at + size / 2 && !isspace((unsigned char)t[e]))+ e--;+ if (e > at + size / 2)+ end = e;+ while (end < n && ((unsigned char)t[end] & 0xc0) == 0x80)+ end++;+ }+ if (chunk_add(out, t + at, end - at) != 0)+ return -1;+ if (end >= n)+ break;+ size_t next = end > overlap ? end - overlap : end;+ if (next <= at)+ next = end;+ while (next < end && !isspace((unsigned char)t[next]))+ next++; /* begin at a word */+ while (next < end && ((unsigned char)t[next] & 0xc0) == 0x80)+ next++;+ at = next;+ }+ return 0;+}++/* Text a file holds: no NUL bytes, mostly valid UTF-8. */+static int is_text(const char *t, size_t n)+{+ if (memchr(t, 0, n))+ return 0;+ size_t bad = 0;+ for (size_t i = 0; i < n;) {+ unsigned char c = (unsigned char)t[i];+ size_t k = c < 0x80 ? 1+ : (c >> 5) == 6 ? 2+ : (c >> 4) == 14 ? 3+ : (c >> 3) == 30 ? 4+ : 0;+ if (!k || i + k > n) {+ bad++;+ i++;+ continue;+ }+ for (size_t j = 1; j < k; j++)+ if (((unsigned char)t[i + j] & 0xc0) != 0x80) {+ bad++;+ k = 1;+ break;+ }+ i += k;+ }+ return bad * 100 <= n; /* a byte in a hundred at most */+}++/* ---- embeddings, through the server's own handler ---- */++/* The embeddings of n texts (prefix before each, may be ""), into a+ malloc'd [n][*dim]: 0, 1 when there is no embedding model, -1 on an+ error. */+static int embed(const char *const *texts, size_t n, const char *prefix,+ float **out, int *dim)+{+ *out = NULL;+ srv_handler h = srv_route_find("POST", "/embeddings");+ if (!h)+ return -1;+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d), *in = yyjson_mut_arr(d);+ yyjson_mut_doc_set_root(d, o);+ yyjson_mut_obj_add_str(d, o, "model", "embedding");+ for (size_t i = 0; i < n; i++) {+ size_t pn = strlen(prefix), tn = strlen(texts[i]);+ char *s = malloc(pn + tn + 1);+ if (s) {+ memcpy(s, prefix, pn);+ memcpy(s + pn, texts[i], tn + 1);+ yyjson_mut_arr_add_strcpy(d, in, s);+ free(s);+ }+ }+ yyjson_mut_obj_add_val(d, o, "input", in);+ size_t bn = 0;+ char *body = yyjson_mut_write(d, 0, &bn);+ yyjson_mut_doc_free(d);+ struct srv_capture cap = {0};+ struct srv_req r = {.srv = server,+ .cap = &cap,+ .method = "POST",+ .path = "/embeddings",+ .body = body ? body : "{}",+ .n_body = body ? bn : 2};+ h(&r);+ free(body);+ int rc = -1;+ if (cap.status == 200 && cap.body) {+ yyjson_doc *ad = yyjson_read(cap.body, cap.n, 0);+ yyjson_val *data = yyjson_obj_get(yyjson_doc_get_root(ad), "data");+ size_t k = 0, idx, mx;+ yyjson_val *it;+ int dm = 0;+ float *v = NULL;+ yyjson_arr_foreach(data, idx, mx, it)+ {+ yyjson_val *e = yyjson_obj_get(it, "embedding");+ if (!dm) {+ dm = (int)yyjson_arr_size(e);+ v = dm ? malloc(n * (size_t)dm * sizeof(float)) : NULL;+ if (!v)+ break;+ }+ if ((int)yyjson_arr_size(e) != dm || k >= n)+ break;+ size_t j, m;+ yyjson_val *x;+ yyjson_arr_foreach(e, j, m, x) v[k * (size_t)dm + j] =+ (float)yyjson_get_num(x);+ k++;+ }+ yyjson_doc_free(ad);+ if (v && k == n) {+ *out = v;+ *dim = dm;+ rc = 0;+ } else {+ free(v);+ }+ } else if (cap.body && strstr(cap.body, "embedding model")) {+ rc = 1; /* none: the search goes by the words */+ }+ free(cap.body);+ return rc;+}++/* What makes a query read as one by an instructed embedding model+ (Qwen3-Embedding); the chunks go as they are. */+#define QUERY_PREFIX \+ "Instruct: Given a search query, retrieve relevant passages that " \+ "answer the query\nQuery: "++/* ---- indexing ---- */++static void file_path(char *out, size_t cap, const char *vs, const char *id,+ const char *suf)+{+ snprintf(out, cap, "%s/%s/%s%s", root, vs, id, suf);+}++static void meta_path(char *out, size_t cap, const char *vs, const char *id)+{+ snprintf(out, cap, "%s/%s/files/%s.json", root, vs, id);+}++/* A file attached's JSON with status and the rest changed: 0, or -1. */+int srv_vectors_set_status(const char *vs, const char *id, const char *status,+ const char *err_code, const char *err_msg,+ long long usage)+{+ char p[4500];+ meta_path(p, sizeof p, vs, id);+ pthread_mutex_lock(&mu);+ size_t n = 0;+ char *t = read_text(p, &n);+ yyjson_doc *d = t ? yyjson_read(t, n, 0) : NULL;+ free(t);+ int rc = -1;+ if (d) {+ yyjson_mut_doc *m = yyjson_doc_mut_copy(d, NULL);+ yyjson_mut_val *o = yyjson_mut_doc_get_root(m);+ yyjson_mut_obj_remove_key(o, "status");+ yyjson_mut_obj_add_strcpy(m, o, "status", status);+ yyjson_mut_obj_remove_key(o, "last_error");+ if (err_code) {+ yyjson_mut_val *e = yyjson_mut_obj_add_obj(m, o, "last_error");+ yyjson_mut_obj_add_strcpy(m, e, "code", err_code);+ yyjson_mut_obj_add_strcpy(m, e, "message", err_msg);+ } else {+ yyjson_mut_obj_add_null(m, o, "last_error");+ }+ if (usage >= 0) {+ yyjson_mut_obj_remove_key(o, "usage_bytes");+ yyjson_mut_obj_add_int(m, o, "usage_bytes", usage);+ }+ size_t wn;+ char *w = yyjson_mut_write(m, 0, &wn);+ rc = w ? srv_vectors_write(p, w, wn) : -1;+ free(w);+ yyjson_mut_doc_free(m);+ yyjson_doc_free(d);+ }+ pthread_mutex_unlock(&mu);+ return rc;+}++static const char *status_of(const char *vs, const char *id, char *buf,+ size_t cap)+{+ char p[4500];+ meta_path(p, sizeof p, vs, id);+ pthread_mutex_lock(&mu);+ size_t n = 0;+ char *t = read_text(p, &n);+ pthread_mutex_unlock(&mu);+ yyjson_doc *d = t ? yyjson_read(t, n, 0) : NULL;+ free(t);+ const char *s =+ yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(d), "status"));+ snprintf(buf, cap, "%s", s ? s : "");+ yyjson_doc_free(d);+ return buf;+}++/* The chunking of a file attached: its static strategy's sizes. */+static void sizes(const char *vs, const char *id, size_t *size, size_t *over)+{+ *size = 800, *over = 400;+ char p[4500];+ meta_path(p, sizeof p, vs, id);+ pthread_mutex_lock(&mu);+ size_t n = 0;+ char *t = read_text(p, &n);+ pthread_mutex_unlock(&mu);+ yyjson_doc *d = t ? yyjson_read(t, n, 0) : NULL;+ free(t);+ yyjson_val *st = yyjson_obj_get(+ yyjson_obj_get(yyjson_doc_get_root(d), "chunking_strategy"), "static");+ if (st) {+ *size = (size_t)yyjson_get_uint(+ yyjson_obj_get(st, "max_chunk_size_tokens"));+ *over =+ (size_t)yyjson_get_uint(yyjson_obj_get(st, "chunk_overlap_tokens"));+ }+ yyjson_doc_free(d);+ if (*size < 100)+ *size = 800;+ if (*over > *size / 2)+ *over = *size / 2;+ *size *= 4; /* tokens to bytes, about */+ *over *= 4;+}++static void index_file(const char *vs, const char *id)+{+ char buf[32];+ struct srv_file f;+ if (srv_files_get(server->files, id, &f) != 0) {+ srv_vectors_set_status(vs, id, "failed", "invalid_file",+ "The file is not there any more.", 0);+ return;+ }+ if (f.bytes > TEXT_MAX) {+ srv_vectors_set_status(vs, id, "failed", "invalid_file",+ "The file is larger than 32 MiB.", 0);+ return;+ }+ int fd = srv_files_open_content(server->files, id);+ char *t = fd >= 0 ? malloc(f.bytes + 1) : NULL;+ size_t got = 0;+ while (t && got < f.bytes) {+ ssize_t r = read(fd, t + got, f.bytes - got);+ if (r <= 0)+ break;+ got += (size_t)r;+ }+ if (fd >= 0)+ close(fd);+ if (!t || got != f.bytes) {+ free(t);+ srv_vectors_set_status(vs, id, "failed", "server_error",+ "The file could not be read.", 0);+ return;+ }+ t[got] = 0;+ if (!is_text(t, got)) {+ free(t);+ srv_vectors_set_status(vs, id, "failed", "unsupported_file",+ "Only text files are read (text, Markdown, "+ "code, JSON, CSV).",+ 0);+ return;+ }+ size_t size, over;+ sizes(vs, id, &size, &over);+ struct chunks c = {0};+ int ok = cut(t, got, size, over, &c) == 0;+ free(t);+ /* the chunks as JSON lines */+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ struct janas_buf lines = {0};+ for (size_t i = 0; ok && i < c.n; i++) {+ yyjson_mut_val *s = yyjson_mut_str(d, c.t[i]);+ size_t ln;+ char *l = yyjson_mut_val_write(s, 0, &ln);+ if (l) {+ janas_buf_put(&lines, l, ln);+ janas_buf_put(&lines, "\n", 1);+ }+ free(l);+ }+ yyjson_mut_doc_free(d);+ char cp[4500], vp[4500];+ file_path(cp, sizeof cp, vs, id, ".chunks");+ file_path(vp, sizeof vp, vs, id, ".vec");+ ok = ok && !lines.oom &&+ srv_vectors_write(cp, lines.p ? lines.p : "", lines.n) == 0;+ janas_buf_free(&lines);+ /* their embeddings, a few at a time, stopping if the file is taken+ away meanwhile */+ float *all = NULL;+ int dim = 0, none = 0;+ for (size_t i = 0; ok && !none && i < c.n; i += EMBED_BATCH) {+ if (strcmp(status_of(vs, id, buf, sizeof buf), "in_progress") != 0 ||+ stopping) {+ ok = 0;+ break;+ }+ size_t k = c.n - i < EMBED_BATCH ? c.n - i : EMBED_BATCH;+ float *v;+ int dm;+ int rc = embed((const char *const *)c.t + i, k, "", &v, &dm);+ if (rc == 1) {+ none = 1; /* no embedding model: the words alone */+ break;+ }+ if (rc != 0 || (dim && dm != dim)) {+ ok = 0;+ break;+ }+ float *a = realloc(all, (i + k) * (size_t)dm * sizeof(float));+ if (!a) {+ free(v);+ ok = 0;+ break;+ }+ all = a;+ memcpy(all + i * (size_t)dm, v, k * (size_t)dm * sizeof(float));+ dim = dm;+ free(v);+ }+ if (ok && !none && c.n) {+ size_t bytes = 4 + c.n * (size_t)dim * sizeof(float);+ char *vb = malloc(bytes);+ if (vb) {+ int32_t d32 = dim;+ memcpy(vb, &d32, 4);+ memcpy(vb + 4, all, bytes - 4);+ ok = srv_vectors_write(vp, vb, bytes) == 0;+ free(vb);+ } else {+ ok = 0;+ }+ } else if (none) {+ unlink(vp);+ }+ free(all);+ size_t usage = 0;+ for (size_t i = 0; i < c.n; i++)+ usage += strlen(c.t[i]);+ chunks_free(&c);+ if (stopping && !ok)+ return; /* left in progress: indexed again at the next start */+ if (strcmp(status_of(vs, id, buf, sizeof buf), "in_progress") != 0)+ return; /* cancelled or detached meanwhile */+ if (ok)+ srv_vectors_set_status(vs, id, "completed", NULL, NULL,+ (long long)usage);+ else+ srv_vectors_set_status(vs, id, "failed", "server_error",+ "The chunks could not be embedded.", 0);+}++/* The oldest file waiting, of any store: 1 with vs and id, or 0. */+static int next_file(char *vs, size_t vcap, char *id, size_t icap)+{+ DIR *d = opendir(root);+ struct dirent *e;+ long long best = -1;+ while (d && (e = readdir(d))) {+ if (!srv_disk_name_ok(e->d_name))+ continue;+ char fd[4500];+ snprintf(fd, sizeof fd, "%s/%s/files", root, e->d_name);+ DIR *f = opendir(fd);+ struct dirent *g;+ while (f && (g = readdir(f))) {+ size_t n = strlen(g->d_name);+ if (n < 6 || strcmp(g->d_name + n - 5, ".json") != 0)+ continue;+ char p[4800];+ snprintf(p, sizeof p, "%s/%s", fd, g->d_name);+ size_t len;+ char *t = read_text(p, &len);+ yyjson_doc *jd = t ? yyjson_read(t, len, 0) : NULL;+ free(t);+ yyjson_val *o = yyjson_doc_get_root(jd);+ const char *st = yyjson_get_str(yyjson_obj_get(o, "status"));+ long long at = yyjson_get_sint(yyjson_obj_get(o, "created_at"));+ size_t vn = strlen(e->d_name);+ if (st && !strcmp(st, "in_progress") && (best < 0 || at < best) &&+ n - 5 < icap && vn < vcap) {+ best = at;+ memcpy(vs, e->d_name, vn + 1);+ memcpy(id, g->d_name, n - 5);+ id[n - 5] = 0;+ }+ yyjson_doc_free(jd);+ }+ if (f)+ closedir(f);+ }+ if (d)+ closedir(d);+ return best >= 0;+}++static void *worker_main(void *arg)+{+ (void)arg;+ srv_engine_low = 1; /* behind every client's request */+ char vs[64], id[64];+ for (;;) {+ pthread_mutex_lock(&mu);+ int stop = stopping;+ kicked = 0;+ pthread_mutex_unlock(&mu);+ if (stop)+ break;+ if (next_file(vs, sizeof vs, id, sizeof id)) {+ index_file(vs, id);+ continue;+ }+ pthread_mutex_lock(&mu); /* nothing waiting: until woken */+ while (!stopping && !kicked)+ pthread_cond_wait(&cv, &mu);+ pthread_mutex_unlock(&mu);+ }+ return NULL;+}++void srv_vectors_lock(void)+{+ pthread_mutex_lock(&mu);+}++void srv_vectors_unlock(void)+{+ pthread_mutex_unlock(&mu);+}++void srv_vectors_wake(void)+{+ pthread_mutex_lock(&mu);+ kicked = 1;+ pthread_cond_broadcast(&cv);+ pthread_mutex_unlock(&mu);+}++int srv_vectors_start(struct srv *s)+{+ server = s;+ snprintf(root, sizeof root, "%s/.vectors", srv_files_dir(s->files));+ if (mkdir(root, 0700) != 0 && errno != EEXIST)+ return -1;+ stopping = 0;+ if (pthread_create(&worker, NULL, worker_main, NULL) != 0)+ return -1;+ running = 1;+ return 0;+}++void srv_vectors_stop(void)+{+ if (!running)+ return;+ pthread_mutex_lock(&mu);+ stopping = 1;+ pthread_cond_broadcast(&cv);+ pthread_mutex_unlock(&mu);+ pthread_join(worker, NULL);+ running = 0;+}++/* ---- search ---- */++/* Whether a file's attributes pass a filter (comparison or compound). */+static int passes(yyjson_val *attrs, yyjson_val *f)+{+ if (!f || yyjson_is_null(f))+ return 1;+ const char *type = yyjson_get_str(yyjson_obj_get(f, "type"));+ if (!type)+ return 1;+ if (!strcmp(type, "and") || !strcmp(type, "or")) {+ int and_ = !strcmp(type, "and");+ size_t i, m;+ yyjson_val *x;+ yyjson_arr_foreach(yyjson_obj_get(f, "filters"), i, m, x)+ {+ int p = passes(attrs, x);+ if (and_ && !p)+ return 0;+ if (!and_ && p)+ return 1;+ }+ return and_;+ }+ const char *key = yyjson_get_str(yyjson_obj_get(f, "key"));+ yyjson_val *want = yyjson_obj_get(f, "value");+ yyjson_val *have = key ? yyjson_obj_get(attrs, key) : NULL;+ if (!strcmp(type, "in") || !strcmp(type, "nin")) {+ int in = 0;+ size_t i, m;+ yyjson_val *x;+ yyjson_arr_foreach(want, i, m, x) in |= have && yyjson_equals(x, have);+ return !strcmp(type, "in") ? in : !in;+ }+ if (!have)+ return !strcmp(type, "ne");+ if (!strcmp(type, "eq"))+ return yyjson_equals(have, want);+ if (!strcmp(type, "ne"))+ return !yyjson_equals(have, want);+ if (yyjson_is_num(have) && yyjson_is_num(want)) {+ double a = yyjson_get_num(have), b = yyjson_get_num(want);+ return !strcmp(type, "gt") ? a > b+ : !strcmp(type, "gte") ? a >= b+ : !strcmp(type, "lt") ? a < b+ : !strcmp(type, "lte") ? a <= b+ : 0;+ }+ if (yyjson_is_str(have) && yyjson_is_str(want)) {+ int c = strcmp(yyjson_get_str(have), yyjson_get_str(want));+ return !strcmp(type, "gt") ? c > 0+ : !strcmp(type, "gte") ? c >= 0+ : !strcmp(type, "lt") ? c < 0+ : !strcmp(type, "lte") ? c <= 0+ : 0;+ }+ return 0;+}++/* Words of a text, lower case ASCII letters and digits, and every byte of+ a UTF-8 character: for BM25. */+static int next_word(const char **p, char *w, size_t cap)+{+ const unsigned char *s = (const unsigned char *)*p;+ while (*s && !(isalnum(*s) || *s >= 0x80))+ s++;+ if (!*s)+ return 0;+ size_t n = 0;+ while (*s && (isalnum(*s) || *s >= 0x80)) {+ if (n + 1 < cap)+ w[n++] = (char)tolower(*s);+ s++;+ }+ w[n] = 0;+ *p = (const char *)s;+ return 1;+}++/* Words too common to tell chunks apart (no idf: a store is read once a+ query), English and Italian */+static int stopword(const char *w)+{+ static const char *const S[] = {+ "the", "and", "for", "with", "how", "what", "who", "which",+ "that", "this", "are", "was", "were", "from", "into", "about",+ "does", "can", "you", "your", "its", "his", "her", "their",+ "not", "but", "have", "has", "had", "will", "would", "should",+ "could", "there", "where", "when", "why", "all", "any", "some",+ "one", "two", "also", "per", "che", "chi", "con", "come",+ "dove", "quando", "del", "della", "dei", "delle", "degli", "nel",+ "nella", "una", "uno", "sono", "non", "gli", "le", "il",+ "la", "di", "da", "in", "su", "tra", "fra", "anche"};+ for (size_t i = 0; i < sizeof(S) / sizeof(S[0]); i++)+ if (!strcmp(w, S[i]))+ return 1;+ return 0;+}++/* BM25's term frequencies against the chunk's length */+static double bm25(const char *query, const char *chunk, double avg_len)+{+ double len = 0, score = 0;+ const char *p = chunk;+ char w[64];+ while (next_word(&p, w, sizeof w))+ len++;+ const char *q = query;+ char qw[64];+ while (next_word(&q, qw, sizeof qw)) {+ if (strlen(qw) < 3 || stopword(qw))+ continue;+ double tf = 0;+ p = chunk;+ while (next_word(&p, w, sizeof w))+ tf += !strcmp(w, qw);+ double k1 = 1.2, b = 0.75;+ score += tf * (k1 + 1) / (tf + k1 * (1 - b + b * len / avg_len));+ }+ return score;+}++static int by_score(const void *a, const void *b)+{+ const struct srv_vhit *x = a, *y = b;+ return x->score < y->score ? 1 : x->score > y->score ? -1 : 0;+}++void srv_vhits_free(struct srv_vhit *h, int n)+{+ for (int i = 0; h && i < n; i++) {+ free(h[i].text);+ free(h[i].attributes);+ }+ free(h);+}++int srv_vectors_search(const char *vs, const char *const *queries, int nq,+ yyjson_val *filters, int max, double threshold,+ struct srv_vhit **hits, char *err, size_t err_len)+{+ *hits = NULL;+ char dir[4500], meta[4600];+ if (!srv_disk_name_ok(vs)) {+ snprintf(err, err_len, "no vector store %s", vs);+ return -1;+ }+ srv_vectors_dir(dir, sizeof dir, vs);+ snprintf(meta, sizeof meta, "%s/meta.json", dir);+ if (access(meta, F_OK) != 0) {+ snprintf(err, err_len, "no vector store %s", vs);+ return -1;+ }+ /* the queries' embeddings, when the model is there */+ float *qv = NULL;+ int qdim = 0;+ if (embed(queries, (size_t)nq, QUERY_PREFIX, &qv, &qdim) != 0)+ qv = NULL;+ struct srv_vhit *all = NULL;+ size_t n = 0, cap = 0;+ char fdir[4600];+ snprintf(fdir, sizeof fdir, "%s/files", dir);+ DIR *d = opendir(fdir);+ struct dirent *e;+ while (d && (e = readdir(d))) {+ size_t nl = strlen(e->d_name);+ if (nl < 6 || strcmp(e->d_name + nl - 5, ".json") != 0 || nl - 5 >= 48)+ continue;+ char id[48];+ memcpy(id, e->d_name, nl - 5);+ id[nl - 5] = 0;+ char p[5000];+ snprintf(p, sizeof p, "%s/%.255s", fdir, e->d_name);+ size_t len;+ pthread_mutex_lock(&mu);+ char *t = read_text(p, &len);+ pthread_mutex_unlock(&mu);+ yyjson_doc *jd = t ? yyjson_read(t, len, 0) : NULL;+ free(t);+ yyjson_val *o = yyjson_doc_get_root(jd);+ const char *st = yyjson_get_str(yyjson_obj_get(o, "status"));+ yyjson_val *attrs = yyjson_obj_get(o, "attributes");+ if (!st || strcmp(st, "completed") != 0 || !passes(attrs, filters)) {+ yyjson_doc_free(jd);+ continue;+ }+ struct srv_file f;+ const char *fname =+ srv_files_get(server->files, id, &f) == 0 ? f.filename : id;+ char *attr_text = attrs ? yyjson_val_write(attrs, 0, NULL) : NULL;+ yyjson_doc_free(jd);+ /* the chunks, and their vectors if any */+ char cp[4500], vp[4500];+ file_path(cp, sizeof cp, vs, id, ".chunks");+ file_path(vp, sizeof vp, vs, id, ".vec");+ size_t cn = 0, vn = 0;+ char *ct = read_text(cp, &cn), *vt = qv ? read_text(vp, &vn) : NULL;+ int32_t dim = 0;+ if (vt && vn >= 4)+ memcpy(&dim, vt, 4);+ int use_vec = vt && dim == qdim && dim > 0;+ /* chunk lengths for BM25's average */+ double avg = 0;+ long count = 0;+ for (char *l = ct; l && *l;) {+ char *nlp = strchr(l, '\n');+ avg += nlp ? (double)(nlp - l) / 6 : 0;+ count++;+ l = nlp ? nlp + 1 : l + strlen(l);+ }+ avg = count ? avg / (double)count : 1;+ long k = 0;+ for (char *l = ct; l && *l; k++) {+ char *nlp = strchr(l, '\n');+ if (nlp)+ *nlp = 0;+ yyjson_doc *cd = yyjson_read(l, strlen(l), 0);+ const char *chunk = yyjson_get_str(yyjson_doc_get_root(cd));+ double s = 0;+ if (chunk && use_vec &&+ (size_t)(k + 1) * (size_t)dim * 4 + 4 <= vn) {+ const float *v = (const float *)(void *)(vt + 4) + k * dim;+ for (int q = 0; q < nq; q++) { /* the best of the queries */+ double dot = 0, a = 0, b = 0;+ for (int j = 0; j < dim; j++) {+ dot += (double)v[j] * qv[(size_t)q * qdim + j];+ a += (double)v[j] * v[j];+ b += (double)qv[(size_t)q * qdim + j] *+ qv[(size_t)q * qdim + j];+ }+ double c = a > 0 && b > 0 ? dot / sqrt(a * b) : 0;+ if (c > s)+ s = c;+ }+ } else if (chunk) {+ for (int q = 0; q < nq; q++) {+ double b = bm25(queries[q], chunk, avg > 1 ? avg : 1);+ double c = b / (b + 4.0); /* 0 to 1 */+ if (c > s)+ s = c;+ }+ }+ if (chunk && s > threshold && s > 0) {+ if (n == cap) {+ size_t nc = cap ? 2 * cap : 64;+ struct srv_vhit *x = realloc(all, nc * sizeof(*x));+ if (!x) {+ yyjson_doc_free(cd);+ break;+ }+ all = x;+ cap = nc;+ }+ struct srv_vhit *h = &all[n++];+ memset(h, 0, sizeof(*h));+ snprintf(h->file_id, sizeof h->file_id, "%s", id);+ snprintf(h->filename, sizeof h->filename, "%s", fname);+ h->score = s > 1 ? 1 : s;+ h->text = strdup(chunk);+ h->attributes = attr_text ? strdup(attr_text) : NULL;+ }+ yyjson_doc_free(cd);+ l = nlp ? nlp + 1 : l + strlen(l);+ }+ free(ct);+ free(vt);+ free(attr_text);+ }+ if (d)+ closedir(d);+ free(qv);+ if (n)+ qsort(all, n, sizeof(*all), by_score);+ for (size_t i = (size_t)max; i < n; i++) {+ free(all[i].text);+ free(all[i].attributes);+ }+ *hits = all;+ return (int)(n < (size_t)max ? n : (size_t)max);+}diff --git a/src/server/vectors.h b/src/server/vectors.hnew file mode 100644index 0000000..1eee772--- /dev/null+++ b/src/server/vectors.h@@ -0,0 +1,54 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vectors.h - vector stores (vectors.c): files cut into overlapping chunks,+ * each with its embedding when the server has an embedding model, searched+ * by the cosine of a query's (or by BM25 over the words, without one). The+ * API is vectors_api.c's; file_search in the Responses API asks the same+ * search.+ */+#ifndef JANAS_SERVER_VECTORS_H+#define JANAS_SERVER_VECTORS_H++#include <stddef.h>+#include <yyjson.h>++struct srv;++/* The directory and the indexing thread (after the files and the engine),+ and their end. */+int srv_vectors_start(struct srv *s);+void srv_vectors_stop(void);+/* The indexing thread has work: a file was attached. */+void srv_vectors_wake(void);+/* A file written aside and renamed: 0, or -1. */+int srv_vectors_write(const char *path, const char *text, size_t n);+/* A file attached's status set (with its last error, code and message, or+ NULL; usage < 0: unchanged): 0, or -1. */+int srv_vectors_set_status(const char *vs, const char *id, const char *status,+ const char *err_code, const char *err_msg,+ long long usage);+/* The lock the files' JSON are read and written under. */+void srv_vectors_lock(void);+void srv_vectors_unlock(void);+/* The directory of a store (vs NULL: of them all). */+void srv_vectors_dir(char *out, size_t cap, const char *vs);++struct srv_vhit {+ char file_id[48];+ char filename[256];+ double score; /* 0 to 1 */+ char *text; /* the chunk; malloc'd */+ char *attributes; /* the file's, JSON text; malloc'd, or NULL */+};++/* The chunks of store vs that answer the queries best (max at most,+ above threshold, of the files filters lets through - a JSON filter, or+ NULL): how many into *hits (free with srv_vhits_free), or -1 with the+ reason in err (no such store). */+int srv_vectors_search(const char *vs, const char *const *queries, int nq,+ yyjson_val *filters, int max, double threshold,+ struct srv_vhit **hits, char *err, size_t err_len);+void srv_vhits_free(struct srv_vhit *h, int n);++#endifdiff --git a/src/server/vectors_api.c b/src/server/vectors_api.cnew file mode 100644index 0000000..e21ef90--- /dev/null+++ b/src/server/vectors_api.c@@ -0,0 +1,882 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vectors_api.c - OpenAI's Vector stores API (vectors.c holds the data):+ * stores made, listed, read, changed and deleted; files attached to them,+ * listed, read, their attributes changed, detached, their text read back;+ * the search. A store's counts and status are its files', read when asked.+ */+#include "server.h"++#include <dirent.h>+#include <errno.h>+#include <ftw.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include <time.h>+#include <unistd.h>+#include <yyjson.h>++#include "files_store.h"+#include "store_disk.h"+#include "vectors.h"++/* ---- helpers ---- */++static int bad(struct srv_req *r, const char *msg)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_value",+ "%s", msg);+}++static int not_found(struct srv_req *r, const char *what, const char *id)+{+ return srv_reply_error(r, 404, "invalid_request_error", "not_found",+ "No %s found with id '%s'.", what, id);+}++static int reply(struct srv_req *r, yyjson_mut_doc *d, yyjson_mut_val *o)+{+ yyjson_mut_doc_set_root(d, o);+ size_t n;+ char *out = yyjson_mut_write(d, 0, &n);+ yyjson_mut_doc_free(d);+ return srv_reply_json(r, 200, out, n);+}++static char *slurp(const char *path, size_t *n)+{+ FILE *f = fopen(path, "rb");+ if (!f)+ return NULL;+ char *b = NULL;+ long len = fseek(f, 0, SEEK_END) ? -1 : ftell(f);+ if (len >= 0 && !fseek(f, 0, SEEK_SET) && (b = malloc((size_t)len + 1)) &&+ fread(b, 1, (size_t)len, f) != (size_t)len) {+ free(b);+ b = NULL;+ }+ fclose(f);+ if (b) {+ b[len] = 0;+ *n = (size_t)len;+ }+ return b;+}++static yyjson_doc *read_json(const char *path)+{+ size_t n = 0;+ srv_vectors_lock();+ char *t = slurp(path, &n);+ srv_vectors_unlock();+ yyjson_doc *d = t ? yyjson_read(t, n, 0) : NULL;+ free(t);+ return d;+}++static int write_json(const char *path, yyjson_mut_doc *d, yyjson_mut_val *o)+{+ size_t n;+ char *t = yyjson_mut_val_write(o, 0, &n);+ (void)d;+ srv_vectors_lock();+ int rc = t ? srv_vectors_write(path, t, n) : -1;+ srv_vectors_unlock();+ free(t);+ return rc;+}++static int store_exists(const char *vs)+{+ char p[4500];+ if (!srv_disk_name_ok(vs))+ return 0;+ srv_vectors_dir(p, sizeof p, vs);+ strcat(p, "/meta.json");+ return access(p, F_OK) == 0;+}++/* A chunking strategy as a request gives it into a static one; NULL when+ it is not one (why says). */+static yyjson_mut_val *chunking(yyjson_mut_doc *d, yyjson_val *cs,+ const char **why)+{+ long size = 800, over = 400;+ const char *type = yyjson_get_str(yyjson_obj_get(cs, "type"));+ if (cs && type && !strcmp(type, "static")) {+ yyjson_val *st = yyjson_obj_get(cs, "static");+ yyjson_val *ms = yyjson_obj_get(st, "max_chunk_size_tokens");+ yyjson_val *co = yyjson_obj_get(st, "chunk_overlap_tokens");+ if (!yyjson_is_int(ms) || !yyjson_is_int(co) ||+ yyjson_get_sint(ms) < 100 || yyjson_get_sint(ms) > 4096 ||+ yyjson_get_sint(co) < 0 ||+ yyjson_get_sint(co) > yyjson_get_sint(ms) / 2) {+ *why = "A static chunking strategy has max_chunk_size_tokens "+ "from 100 to 4096 and chunk_overlap_tokens at most half of "+ "it.";+ return NULL;+ }+ size = (long)yyjson_get_sint(ms);+ over = (long)yyjson_get_sint(co);+ } else if (cs && !yyjson_is_null(cs) && (!type || strcmp(type, "auto"))) {+ *why = "'chunking_strategy' is auto or static.";+ return NULL;+ }+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_str(d, o, "type", "static");+ yyjson_mut_val *s = yyjson_mut_obj_add_obj(d, o, "static");+ yyjson_mut_obj_add_int(d, s, "max_chunk_size_tokens", size);+ yyjson_mut_obj_add_int(d, s, "chunk_overlap_tokens", over);+ return o;+}++/* Attributes: up to 16 keys of strings up to 512, numbers or booleans. */+static int attributes_ok(yyjson_val *a)+{+ if (!a || yyjson_is_null(a))+ return 1;+ if (!yyjson_is_obj(a) || yyjson_obj_size(a) > 16)+ return 0;+ size_t i, m;+ yyjson_val *k, *v;+ yyjson_obj_foreach(a, i, m, k, v)+ {+ if (yyjson_get_len(k) > 64 ||+ !(yyjson_is_num(v) || yyjson_is_bool(v) ||+ (yyjson_is_str(v) && yyjson_get_len(v) <= 512)))+ return 0;+ }+ return 1;+}++/* A file attached: its JSON written, the indexer woken. 0, -1 no such+ file (why), -2 the disk. */+static int attach(const char *vs, const char *file_id, yyjson_val *cs,+ yyjson_val *attrs, struct srv *srv, const char **why)+{+ struct srv_file f;+ if (srv_files_get(srv->files, file_id, &f) != 0) {+ *why = "a file_id names no file uploaded here";+ return -1;+ }+ if (!attributes_ok(attrs)) {+ *why = "'attributes' holds at most 16 keys of strings (512 at "+ "most), numbers or booleans";+ return -1;+ }+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *cv = chunking(d, cs, why);+ if (!cv) {+ yyjson_mut_doc_free(d);+ return -1;+ }+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, o, "id", file_id);+ yyjson_mut_obj_add_str(d, o, "object", "vector_store.file");+ yyjson_mut_obj_add_int(d, o, "usage_bytes", 0);+ yyjson_mut_obj_add_int(d, o, "created_at", (int64_t)time(NULL));+ yyjson_mut_obj_add_strcpy(d, o, "vector_store_id", vs);+ yyjson_mut_obj_add_str(d, o, "status", "in_progress");+ yyjson_mut_obj_add_null(d, o, "last_error");+ yyjson_mut_obj_add_val(d, o, "chunking_strategy", cv);+ if (attrs && yyjson_is_obj(attrs))+ yyjson_mut_obj_add_val(d, o, "attributes",+ yyjson_val_mut_copy(d, attrs));+ else+ yyjson_mut_obj_add_obj(d, o, "attributes");+ char p[4600];+ srv_vectors_dir(p, sizeof p, vs);+ snprintf(p + strlen(p), sizeof p - strlen(p), "/files/%s.json", file_id);+ int rc = write_json(p, d, o) == 0 ? 0 : -2;+ yyjson_mut_doc_free(d);+ srv_vectors_wake();+ return rc;+}++int srv_vs_attach(struct srv *srv, const char *vs, const char *file_id,+ yyjson_val *cs, yyjson_val *attrs, const char **why)+{+ return attach(vs, file_id, cs, attrs, srv, why);+}++/* A store's object with its counts and status, made from its files. */+static yyjson_mut_val *store_object(yyjson_mut_doc *d, const char *vs)+{+ char dir[4500], p[4700];+ srv_vectors_dir(dir, sizeof dir, vs);+ snprintf(p, sizeof p, "%s/meta.json", dir);+ yyjson_doc *md = read_json(p);+ if (!md)+ return NULL;+ yyjson_mut_val *o = yyjson_val_mut_copy(d, yyjson_doc_get_root(md));+ yyjson_doc_free(md);+ long c[4] = {0, 0, 0, 0}; /* in_progress, completed, failed, cancelled */+ long long usage = 0;+ snprintf(p, sizeof p, "%s/files", dir);+ DIR *fd = opendir(p);+ struct dirent *e;+ while (fd && (e = readdir(fd))) {+ size_t n = strlen(e->d_name);+ if (n < 6 || strcmp(e->d_name + n - 5, ".json") != 0)+ continue;+ char fp[5000];+ snprintf(fp, sizeof fp, "%s/%.255s", p, e->d_name);+ yyjson_doc *fdoc = read_json(fp);+ yyjson_val *fo = yyjson_doc_get_root(fdoc);+ const char *st = yyjson_get_str(yyjson_obj_get(fo, "status"));+ static const char *const S[] = {"in_progress", "completed", "failed",+ "cancelled"};+ for (int i = 0; st && i < 4; i++)+ if (!strcmp(st, S[i]))+ c[i]++;+ usage += yyjson_get_sint(yyjson_obj_get(fo, "usage_bytes"));+ yyjson_doc_free(fdoc);+ }+ if (fd)+ closedir(fd);+ yyjson_mut_obj_remove_key(o, "usage_bytes");+ yyjson_mut_obj_add_int(d, o, "usage_bytes", usage);+ yyjson_mut_val *fc = yyjson_mut_obj(d);+ yyjson_mut_obj_add_int(d, fc, "in_progress", c[0]);+ yyjson_mut_obj_add_int(d, fc, "completed", c[1]);+ yyjson_mut_obj_add_int(d, fc, "failed", c[2]);+ yyjson_mut_obj_add_int(d, fc, "cancelled", c[3]);+ yyjson_mut_obj_add_int(d, fc, "total", c[0] + c[1] + c[2] + c[3]);+ yyjson_mut_obj_remove_key(o, "file_counts");+ yyjson_mut_obj_add_val(d, o, "file_counts", fc);+ int64_t exp = yyjson_mut_get_sint(yyjson_mut_obj_get(o, "expires_at"));+ const char *status = exp && (int64_t)time(NULL) >= exp ? "expired"+ : c[0] ? "in_progress"+ : "completed";+ yyjson_mut_obj_remove_key(o, "status");+ yyjson_mut_obj_add_str(d, o, "status", status);+ return o;+}++/* expires_after {anchor: last_active_at, days 1-365} into the object, with+ expires_at from last_active_at. 0, or -1 (why). */+static int set_expiry(yyjson_mut_doc *d, yyjson_mut_val *o, yyjson_val *ea,+ const char **why)+{+ yyjson_mut_obj_remove_key(o, "expires_after");+ yyjson_mut_obj_remove_key(o, "expires_at");+ if (!ea || yyjson_is_null(ea)) {+ yyjson_mut_obj_add_null(d, o, "expires_after");+ yyjson_mut_obj_add_null(d, o, "expires_at");+ return 0;+ }+ yyjson_val *days = yyjson_obj_get(ea, "days");+ if (!yyjson_equals_str(yyjson_obj_get(ea, "anchor"), "last_active_at") ||+ !yyjson_is_int(days) || yyjson_get_sint(days) < 1 ||+ yyjson_get_sint(days) > 365) {+ *why = "'expires_after' is anchor last_active_at and days from 1 to "+ "365.";+ return -1;+ }+ yyjson_mut_obj_add_val(d, o, "expires_after", yyjson_val_mut_copy(d, ea));+ int64_t last = yyjson_mut_get_sint(yyjson_mut_obj_get(o, "last_active_at"));+ yyjson_mut_obj_add_int(d, o, "expires_at",+ last + yyjson_get_sint(days) * 86400);+ return 0;+}++static int metadata_ok(yyjson_val *m)+{+ if (!m || yyjson_is_null(m))+ return 1;+ if (!yyjson_is_obj(m) || yyjson_obj_size(m) > 16)+ return 0;+ size_t i, n;+ yyjson_val *k, *v;+ yyjson_obj_foreach(m, i, n, k, v) if (yyjson_get_len(k) > 64 ||+ !yyjson_is_str(v) ||+ yyjson_get_len(v) > 512) return 0;+ return 1;+}++/* ---- stores ---- */++int srv_vs_create(struct srv_req *r)+{+ yyjson_doc *doc = r->n_body ? yyjson_read(r->body, r->n_body, 0) : NULL;+ yyjson_val *root = yyjson_doc_get_root(doc);+ const char *why = NULL;+ yyjson_val *ids = yyjson_obj_get(root, "file_ids");+ yyjson_val *name = yyjson_obj_get(root, "name");+ yyjson_val *desc = yyjson_obj_get(root, "description");+ yyjson_val *md = yyjson_obj_get(root, "metadata");+ if (r->n_body && !yyjson_is_obj(root))+ why = "The body is a JSON object.";+ else if (ids && (!yyjson_is_arr(ids) || yyjson_arr_size(ids) > 500))+ why = "'file_ids' is a list of up to 500 ids.";+ else if (!metadata_ok(md))+ why = "'metadata' holds at most 16 strings.";+ /* every file there before anything is made */+ size_t idx, max;+ yyjson_val *v;+ yyjson_arr_foreach(ids, idx, max, v)+ {+ struct srv_file f;+ if (!why && (!yyjson_is_str(v) ||+ srv_files_get(r->srv->files, yyjson_get_str(v), &f) != 0))+ why = "A file_id names no file uploaded here.";+ }+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ if (!why) /* a strategy that is not one: said here, not per file */+ chunking(d, yyjson_obj_get(root, "chunking_strategy"), &why);+ char id[48];+ srv_new_id(id, sizeof id, "vs_");+ int64_t now = (int64_t)time(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, o, "id", id);+ yyjson_mut_obj_add_str(d, o, "object", "vector_store");+ yyjson_mut_obj_add_int(d, o, "created_at", now);+ if (yyjson_is_str(name))+ yyjson_mut_obj_add_strcpy(d, o, "name", yyjson_get_str(name));+ else+ yyjson_mut_obj_add_null(d, o, "name");+ if (yyjson_is_str(desc))+ yyjson_mut_obj_add_strcpy(d, o, "description", yyjson_get_str(desc));+ yyjson_mut_obj_add_int(d, o, "last_active_at", now);+ if (!why)+ set_expiry(d, o, yyjson_obj_get(root, "expires_after"), &why);+ if (md && yyjson_is_obj(md))+ yyjson_mut_obj_add_val(d, o, "metadata", yyjson_val_mut_copy(d, md));+ else+ yyjson_mut_obj_add_obj(d, o, "metadata");+ if (why) {+ yyjson_mut_doc_free(d);+ yyjson_doc_free(doc);+ return bad(r, why);+ }+ char dir[4500], p[4700];+ srv_vectors_dir(dir, sizeof dir, id);+ snprintf(p, sizeof p, "%s/files", dir);+ int ok = mkdir(dir, 0700) == 0 && mkdir(p, 0700) == 0;+ snprintf(p, sizeof p, "%s/meta.json", dir);+ ok = ok && write_json(p, d, o) == 0;+ yyjson_arr_foreach(ids, idx, max, v) if (ok) ok =+ attach(id, yyjson_get_str(v), yyjson_obj_get(root, "chunking_strategy"),+ NULL, r->srv, &why) == 0;+ yyjson_mut_doc_free(d);+ yyjson_doc_free(doc);+ if (!ok)+ return srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the vector store");+ d = yyjson_mut_doc_new(NULL);+ return reply(r, d, store_object(d, id));+}++int srv_vs_get(struct srv_req *r)+{+ if (!store_exists(r->param[0]))+ return not_found(r, "vector store", r->param[0]);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = store_object(d, r->param[0]);+ if (!o) {+ yyjson_mut_doc_free(d);+ return not_found(r, "vector store", r->param[0]);+ }+ return reply(r, d, o);+}++int srv_vs_modify(struct srv_req *r)+{+ if (!store_exists(r->param[0]))+ return not_found(r, "vector store", r->param[0]);+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ const char *why = NULL;+ char p[4700];+ srv_vectors_dir(p, sizeof p, r->param[0]);+ strcat(p, "/meta.json");+ yyjson_doc *md = read_json(p);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o =+ md ? yyjson_val_mut_copy(d, yyjson_doc_get_root(md)) : NULL;+ yyjson_doc_free(md);+ yyjson_val *name = yyjson_obj_get(root, "name");+ yyjson_val *meta = yyjson_obj_get(root, "metadata");+ if (!yyjson_is_obj(root) || !o)+ why = "The body is a JSON object.";+ else if (!metadata_ok(meta))+ why = "'metadata' holds at most 16 strings.";+ if (!why && name) {+ yyjson_mut_obj_remove_key(o, "name");+ if (yyjson_is_str(name))+ yyjson_mut_obj_add_strcpy(d, o, "name", yyjson_get_str(name));+ else+ yyjson_mut_obj_add_null(d, o, "name");+ }+ if (!why && meta) {+ yyjson_mut_obj_remove_key(o, "metadata");+ yyjson_mut_obj_add_val(d, o, "metadata",+ yyjson_is_obj(meta)+ ? yyjson_val_mut_copy(d, meta)+ : yyjson_mut_obj(d));+ }+ if (!why && yyjson_obj_get(root, "expires_after"))+ set_expiry(d, o, yyjson_obj_get(root, "expires_after"), &why);+ int ok = !why && write_json(p, d, o) == 0;+ yyjson_mut_doc_free(d);+ yyjson_doc_free(doc);+ if (why)+ return bad(r, why);+ if (!ok)+ return srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the vector store");+ d = yyjson_mut_doc_new(NULL);+ return reply(r, d, store_object(d, r->param[0]));+}++static int rm_one(const char *p, const struct stat *s, int f, struct FTW *w)+{+ (void)s;+ (void)f;+ (void)w;+ return remove(p);+}++int srv_vs_delete(struct srv_req *r)+{+ if (!store_exists(r->param[0]))+ return not_found(r, "vector store", r->param[0]);+ char dir[4500];+ srv_vectors_dir(dir, sizeof dir, r->param[0]);+ srv_vectors_lock();+ nftw(dir, rm_one, 16, FTW_DEPTH | FTW_PHYS);+ srv_vectors_unlock();+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, o, "id", r->param[0]);+ yyjson_mut_obj_add_str(d, o, "object", "vector_store.deleted");+ yyjson_mut_obj_add_bool(d, o, "deleted", true);+ return reply(r, d, o);+}++/* ---- lists ---- */++struct entry {+ char id[48];+ int64_t at;+};++static int by_at(const void *a, const void *b)+{+ const struct entry *x = a, *y = b;+ if (x->at != y->at)+ return x->at < y->at ? -1 : 1;+ return strcmp(x->id, y->id);+}++/* A page of a list (limit, order, after, before) of entries in creation+ order, each made into an object by make. */+static int page(struct srv_req *r, struct entry *e, size_t n,+ yyjson_mut_val *(*make)(yyjson_mut_doc *, const char *,+ const void *),+ const void *ctx)+{+ const char *l = srv_query(r, "limit"), *o = srv_query(r, "order");+ const char *after = srv_query(r, "after"), *before = srv_query(r, "before");+ long limit = 20;+ if (l) {+ char *end;+ limit = strtol(l, &end, 10);+ if (*end || limit < 1 || limit > 100) {+ free(e);+ return bad(r, "'limit' is a number from 1 to 100.");+ }+ }+ int asc = o && !strcmp(o, "asc");+ if (n)+ qsort(e, n, sizeof(*e), by_at);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *root = yyjson_mut_obj(d), *data = yyjson_mut_arr(d);+ long shown = 0;+ int more = 0, past = after == NULL;+ char first[48] = "", last[48] = "";+ for (size_t k = 0; k < n; k++) {+ const struct entry *x = &e[asc ? k : n - 1 - k];+ if (!past) {+ past = !strcmp(x->id, after);+ continue;+ }+ if (before && !strcmp(x->id, before))+ break;+ if (shown == limit) {+ more = 1;+ break;+ }+ yyjson_mut_val *v = make(d, x->id, ctx);+ if (!v)+ continue;+ yyjson_mut_arr_append(data, v);+ if (!shown)+ snprintf(first, sizeof first, "%s", x->id);+ snprintf(last, sizeof last, "%s", x->id);+ shown++;+ }+ free(e);+ yyjson_mut_obj_add_str(d, root, "object", "list");+ yyjson_mut_obj_add_val(d, root, "data", data);+ if (shown) {+ yyjson_mut_obj_add_strcpy(d, root, "first_id", first);+ yyjson_mut_obj_add_strcpy(d, root, "last_id", last);+ } else {+ yyjson_mut_obj_add_null(d, root, "first_id");+ yyjson_mut_obj_add_null(d, root, "last_id");+ }+ yyjson_mut_obj_add_bool(d, root, "has_more", more);+ return reply(r, d, root);+}++static yyjson_mut_val *make_store(yyjson_mut_doc *d, const char *id,+ const void *ctx)+{+ (void)ctx;+ return store_object(d, id);+}++int srv_vs_list(struct srv_req *r)+{+ char dir[4500];+ srv_vectors_dir(dir, sizeof dir, NULL);+ struct entry *e = NULL;+ size_t n = 0, cap = 0;+ DIR *dd = opendir(dir);+ struct dirent *x;+ while (dd && (x = readdir(dd))) {+ if (!srv_disk_name_ok(x->d_name) || strlen(x->d_name) >= 48 ||+ !store_exists(x->d_name))+ continue;+ if (n == cap) {+ cap = cap ? 2 * cap : 32;+ struct entry *t = realloc(e, cap * sizeof(*t));+ if (!t)+ break;+ e = t;+ }+ char p[4800];+ snprintf(p, sizeof p, "%s/%.47s/meta.json", dir, x->d_name);+ yyjson_doc *md = read_json(p);+ memcpy(e[n].id, x->d_name, strlen(x->d_name) + 1); /* < 48 */+ e[n].at = yyjson_get_sint(+ yyjson_obj_get(yyjson_doc_get_root(md), "created_at"));+ yyjson_doc_free(md);+ n++;+ }+ if (dd)+ closedir(dd);+ return page(r, e, n, make_store, NULL);+}++/* ---- files of a store ---- */++static void vfile_path(char *out, size_t cap, const char *vs, const char *id)+{+ srv_vectors_dir(out, cap, vs);+ snprintf(out + strlen(out), cap - strlen(out), "/files/%s.json", id);+}++static yyjson_mut_val *vfile_object(yyjson_mut_doc *d, const char *vs,+ const char *id)+{+ char p[4800];+ if (!srv_disk_name_ok(id))+ return NULL;+ vfile_path(p, sizeof p, vs, id);+ yyjson_doc *fd = read_json(p);+ if (!fd)+ return NULL;+ yyjson_mut_val *o = yyjson_val_mut_copy(d, yyjson_doc_get_root(fd));+ yyjson_doc_free(fd);+ return o;+}++int srv_vs_file_create(struct srv_req *r)+{+ if (!store_exists(r->param[0]))+ return not_found(r, "vector store", r->param[0]);+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ const char *fs = yyjson_get_str(yyjson_obj_get(root, "file_id"));+ char fid_buf[64];+ snprintf(fid_buf, sizeof fid_buf, "%s", fs ? fs : "");+ const char *fid = fs ? fid_buf : NULL; /* kept past the body */+ const char *why = "'file_id' is required.";+ int rc = fid ? attach(r->param[0], fid,+ yyjson_obj_get(root, "chunking_strategy"),+ yyjson_obj_get(root, "attributes"), r->srv, &why)+ : -1;+ yyjson_doc_free(doc);+ if (rc == -1)+ return bad(r, why);+ if (rc != 0)+ return srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the file");+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ return reply(r, d, vfile_object(d, r->param[0], fid));+}++int srv_vs_file_get(struct srv_req *r)+{+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = store_exists(r->param[0])+ ? vfile_object(d, r->param[0], r->param[1])+ : NULL;+ if (!o) {+ yyjson_mut_doc_free(d);+ return not_found(r, "vector store file", r->param[1]);+ }+ return reply(r, d, o);+}++int srv_vs_file_update(struct srv_req *r)+{+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *attrs = yyjson_obj_get(yyjson_doc_get_root(doc), "attributes");+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = store_exists(r->param[0])+ ? vfile_object(d, r->param[0], r->param[1])+ : NULL;+ int ret;+ if (!o) {+ yyjson_mut_doc_free(d);+ ret = not_found(r, "vector store file", r->param[1]);+ } else if (!attrs || !attributes_ok(attrs)) {+ yyjson_mut_doc_free(d);+ ret = bad(r, "'attributes' holds at most 16 keys of strings (512 at "+ "most), numbers or booleans.");+ } else {+ yyjson_mut_obj_remove_key(o, "attributes");+ yyjson_mut_obj_add_val(d, o, "attributes",+ yyjson_is_obj(attrs)+ ? yyjson_val_mut_copy(d, attrs)+ : yyjson_mut_obj(d));+ char p[4800];+ vfile_path(p, sizeof p, r->param[0], r->param[1]);+ if (write_json(p, d, o) != 0) {+ yyjson_mut_doc_free(d);+ ret = srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the file");+ } else {+ ret = reply(r, d, o);+ }+ }+ yyjson_doc_free(doc);+ return ret;+}++int srv_vs_file_delete(struct srv_req *r)+{+ char p[4800];+ if (!store_exists(r->param[0]) || !srv_disk_name_ok(r->param[1]))+ return not_found(r, "vector store file", r->param[1]);+ vfile_path(p, sizeof p, r->param[0], r->param[1]);+ if (access(p, F_OK) != 0)+ return not_found(r, "vector store file", r->param[1]);+ srv_vectors_lock();+ unlink(p);+ srv_vectors_dir(p, sizeof p, r->param[0]);+ size_t at = strlen(p);+ snprintf(p + at, sizeof p - at, "/%s.chunks", r->param[1]);+ unlink(p);+ snprintf(p + at, sizeof p - at, "/%s.vec", r->param[1]);+ unlink(p);+ srv_vectors_unlock();+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, o, "id", r->param[1]);+ yyjson_mut_obj_add_str(d, o, "object", "vector_store.file.deleted");+ yyjson_mut_obj_add_bool(d, o, "deleted", true);+ return reply(r, d, o);+}++struct vfile_ctx {+ const char *vs;+ const char *status; /* filter: NULL any */+};++static yyjson_mut_val *make_vfile(yyjson_mut_doc *d, const char *id,+ const void *ctx)+{+ const struct vfile_ctx *c = ctx;+ yyjson_mut_val *o = vfile_object(d, c->vs, id);+ const char *st =+ o ? yyjson_mut_get_str(yyjson_mut_obj_get(o, "status")) : NULL;+ return o && (!c->status || (st && !strcmp(st, c->status))) ? o : NULL;+}++/* The files of a store, or (ids not NULL) those of a batch of them. */+int srv_vs_files_page(struct srv_req *r, const char *vs, const char *const *ids,+ size_t n_ids)+{+ char dir[4600];+ srv_vectors_dir(dir, sizeof dir, vs);+ strcat(dir, "/files");+ struct entry *e = NULL;+ size_t n = 0, cap = 0;+ DIR *dd = opendir(dir);+ struct dirent *x;+ while (dd && (x = readdir(dd))) {+ size_t nl = strlen(x->d_name);+ if (nl < 6 || nl - 5 >= 48 || strcmp(x->d_name + nl - 5, ".json"))+ continue;+ char id[48];+ memcpy(id, x->d_name, nl - 5);+ id[nl - 5] = 0;+ int in = !ids;+ for (size_t k = 0; ids && k < n_ids && !in; k++)+ in = !strcmp(ids[k], id);+ if (!in)+ continue;+ if (n == cap) {+ cap = cap ? 2 * cap : 32;+ struct entry *t = realloc(e, cap * sizeof(*t));+ if (!t)+ break;+ e = t;+ }+ char p[4900];+ snprintf(p, sizeof p, "%s/%s", dir, x->d_name);+ yyjson_doc *fd = read_json(p);+ snprintf(e[n].id, sizeof e[n].id, "%s", id);+ e[n].at = yyjson_get_sint(+ yyjson_obj_get(yyjson_doc_get_root(fd), "created_at"));+ yyjson_doc_free(fd);+ n++;+ }+ if (dd)+ closedir(dd);+ const char *f = srv_query(r, "filter");+ if (f && strcmp(f, "in_progress") && strcmp(f, "completed") &&+ strcmp(f, "failed") && strcmp(f, "cancelled")) {+ free(e);+ return bad(r, "'filter' is in_progress, completed, failed or "+ "cancelled.");+ }+ struct vfile_ctx c = {vs, f};+ return page(r, e, n, make_vfile, &c);+}++int srv_vs_files_list(struct srv_req *r)+{+ if (!store_exists(r->param[0]))+ return not_found(r, "vector store", r->param[0]);+ return srv_vs_files_page(r, r->param[0], NULL, 0);+}++int srv_vs_file_content(struct srv_req *r)+{+ char p[4800];+ if (!store_exists(r->param[0]) || !srv_disk_name_ok(r->param[1]))+ return not_found(r, "vector store file", r->param[1]);+ vfile_path(p, sizeof p, r->param[0], r->param[1]);+ if (access(p, F_OK) != 0)+ return not_found(r, "vector store file", r->param[1]);+ /* the text the file was read as: its chunks without their overlap is+ not kept, so the file itself, when it is text */+ struct srv_file f;+ int fd = srv_files_get(r->srv->files, r->param[1], &f) == 0+ ? srv_files_open_content(r->srv->files, r->param[1])+ : -1;+ char *t =+ fd >= 0 && f.bytes <= ((size_t)32 << 20) ? malloc(f.bytes + 1) : NULL;+ size_t got = 0;+ while (t && got < f.bytes) {+ ssize_t k = read(fd, t + got, f.bytes - got);+ if (k <= 0)+ break;+ got += (size_t)k;+ }+ if (fd >= 0)+ close(fd);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d), *data = yyjson_mut_arr(d);+ yyjson_mut_obj_add_str(d, o, "object", "vector_store.file_content.page");+ if (t) {+ yyjson_mut_val *c = yyjson_mut_arr_add_obj(d, data);+ yyjson_mut_obj_add_str(d, c, "type", "text");+ yyjson_mut_obj_add_strncpy(d, c, "text", t, got);+ }+ free(t);+ yyjson_mut_obj_add_val(d, o, "data", data);+ yyjson_mut_obj_add_bool(d, o, "has_more", false);+ yyjson_mut_obj_add_null(d, o, "next_page");+ return reply(r, d, o);+}++/* ---- search ---- */++int srv_vs_search(struct srv_req *r)+{+ if (!store_exists(r->param[0]))+ return not_found(r, "vector store", r->param[0]);+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_val *q = yyjson_obj_get(root, "query");+ yyjson_val *mx = yyjson_obj_get(root, "max_num_results");+ yyjson_val *th = yyjson_obj_get(yyjson_obj_get(root, "ranking_options"),+ "score_threshold");+ const char *qs[16];+ int nq = 0;+ if (yyjson_is_str(q) && yyjson_get_len(q))+ qs[nq++] = yyjson_get_str(q);+ size_t idx, m;+ yyjson_val *v;+ if (yyjson_is_arr(q))+ yyjson_arr_foreach(q, idx, m, v) if (nq < 16 && yyjson_is_str(v))+ qs[nq++] = yyjson_get_str(v);+ long max = mx ? (long)yyjson_get_sint(mx) : 10;+ double thr = th ? yyjson_get_num(th) : 0;+ const char *why = NULL;+ if (!nq)+ why = "'query' is a string or a list of them.";+ else if (mx && (!yyjson_is_int(mx) || max < 1 || max > 50))+ why = "'max_num_results' is from 1 to 50.";+ else if (th && (!yyjson_is_num(th) || thr < 0 || thr > 1))+ why = "'score_threshold' is from 0 to 1.";+ if (why) {+ yyjson_doc_free(doc);+ return bad(r, why);+ }+ struct srv_vhit *hits;+ char err[200];+ int n =+ srv_vectors_search(r->param[0], qs, nq, yyjson_obj_get(root, "filters"),+ (int)max, thr, &hits, err, sizeof err);+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d), *data = yyjson_mut_arr(d);+ yyjson_mut_obj_add_str(d, o, "object", "vector_store.search_results.page");+ yyjson_mut_val *sq = yyjson_mut_obj_add_arr(d, o, "search_query");+ for (int i = 0; i < nq; i++)+ yyjson_mut_arr_add_strcpy(d, sq, qs[i]);+ for (int i = 0; i < n; i++) {+ yyjson_mut_val *it = yyjson_mut_arr_add_obj(d, data);+ yyjson_mut_obj_add_strcpy(d, it, "file_id", hits[i].file_id);+ yyjson_mut_obj_add_strcpy(d, it, "filename", hits[i].filename);+ yyjson_mut_obj_add_real(d, it, "score", hits[i].score);+ yyjson_doc *ad =+ hits[i].attributes+ ? yyjson_read(hits[i].attributes, strlen(hits[i].attributes), 0)+ : NULL;+ yyjson_mut_obj_add_val(+ d, it, "attributes",+ ad ? yyjson_val_mut_copy(d, yyjson_doc_get_root(ad))+ : yyjson_mut_obj(d));+ yyjson_doc_free(ad);+ yyjson_mut_val *c = yyjson_mut_obj_add_arr(d, it, "content");+ yyjson_mut_val *t = yyjson_mut_arr_add_obj(d, c);+ yyjson_mut_obj_add_str(d, t, "type", "text");+ yyjson_mut_obj_add_strcpy(d, t, "text", hits[i].text);+ }+ srv_vhits_free(hits, n > 0 ? n : 0);+ yyjson_doc_free(doc);+ yyjson_mut_obj_add_val(d, o, "data", data);+ yyjson_mut_obj_add_bool(d, o, "has_more", false);+ yyjson_mut_obj_add_null(d, o, "next_page");+ return reply(r, d, o);+}diff --git a/src/server/vectors_batches.c b/src/server/vectors_batches.cnew file mode 100644index 0000000..f6be5ad--- /dev/null+++ b/src/server/vectors_batches.c@@ -0,0 +1,284 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * vectors_batches.c - a batch of files attached to a vector store at once+ * (OpenAI's file_batches): kept as the list of its files in the store's+ * batches/ directory, its status and counts read from theirs when asked;+ * cancelled, the files still waiting are cancelled.+ */+#include "server.h"++#include <errno.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include <time.h>+#include <unistd.h>+#include <yyjson.h>++#include "files_store.h"+#include "store_disk.h"+#include "vectors.h"++#define BATCH_FILES 2000++static int bad(struct srv_req *r, const char *msg)+{+ return srv_reply_error(r, 400, "invalid_request_error", "invalid_value",+ "%s", msg);+}++static int not_found(struct srv_req *r, const char *what, const char *id)+{+ return srv_reply_error(r, 404, "invalid_request_error", "not_found",+ "No %s found with id '%s'.", what, id);+}++static void batch_path(char *out, size_t cap, const char *vs, const char *id)+{+ srv_vectors_dir(out, cap, vs);+ size_t n = strlen(out);+ snprintf(out + n, cap - n, "/batches/%s.json", id);+}++static yyjson_doc *read_batch(const char *vs, const char *id)+{+ if (!srv_disk_name_ok(vs) || !srv_disk_name_ok(id))+ return NULL;+ char p[4800];+ batch_path(p, sizeof p, vs, id);+ FILE *f = fopen(p, "rb");+ if (!f)+ return NULL;+ char *t = NULL;+ long n = fseek(f, 0, SEEK_END) ? -1 : ftell(f);+ if (n >= 0 && !fseek(f, 0, SEEK_SET) && (t = malloc((size_t)n + 1)) &&+ fread(t, 1, (size_t)n, f) != (size_t)n) {+ free(t);+ t = NULL;+ }+ fclose(f);+ yyjson_doc *d = t ? yyjson_read(t, (size_t)n, 0) : NULL;+ free(t);+ return d;+}++/* A file's status in the store ("" when detached). */+static void file_status(const char *vs, const char *id, char *out, size_t cap)+{+ char p[4800];+ srv_vectors_dir(p, sizeof p, vs);+ size_t n = strlen(p);+ snprintf(p + n, sizeof p - n, "/files/%s.json", id);+ out[0] = 0;+ srv_vectors_lock();+ FILE *f = fopen(p, "rb");+ char buf[4096];+ size_t len = f ? fread(buf, 1, sizeof buf - 1, f) : 0;+ if (f)+ fclose(f);+ srv_vectors_unlock();+ buf[len] = 0;+ yyjson_doc *d = len ? yyjson_read(buf, len, 0) : NULL;+ const char *s =+ yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(d), "status"));+ snprintf(out, cap, "%s", s ? s : "");+ yyjson_doc_free(d);+}++/* The batch object, its counts and status from its files'. */+static int reply_batch(struct srv_req *r, const char *vs, const char *id)+{+ yyjson_doc *bd = read_batch(vs, id);+ yyjson_val *b = yyjson_doc_get_root(bd);+ if (!b)+ return not_found(r, "vector store file batch", id);+ long c[4] = {0, 0, 0, 0};+ static const char *const S[] = {"in_progress", "completed", "failed",+ "cancelled"};+ size_t idx, mx;+ yyjson_val *v;+ yyjson_arr_foreach(yyjson_obj_get(b, "file_ids"), idx, mx, v)+ {+ char st[32];+ file_status(vs, yyjson_get_str(v), st, sizeof st);+ for (int i = 0; i < 4; i++)+ if (!strcmp(st, S[i]))+ c[i]++;+ }+ int cancelled = yyjson_get_bool(yyjson_obj_get(b, "cancelled"));+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d);+ yyjson_mut_obj_add_strcpy(d, o, "id", id);+ yyjson_mut_obj_add_str(d, o, "object", "vector_store.files_batch");+ yyjson_mut_obj_add_int(d, o, "created_at",+ yyjson_get_sint(yyjson_obj_get(b, "created_at")));+ yyjson_mut_obj_add_strcpy(d, o, "vector_store_id", vs);+ yyjson_mut_obj_add_str(d, o, "status",+ cancelled ? "cancelled"+ : c[0] ? "in_progress"+ : c[1] ? "completed"+ : "failed");+ yyjson_mut_val *fc = yyjson_mut_obj_add_obj(d, o, "file_counts");+ yyjson_mut_obj_add_int(d, fc, "in_progress", c[0]);+ yyjson_mut_obj_add_int(d, fc, "completed", c[1]);+ yyjson_mut_obj_add_int(d, fc, "failed", c[2]);+ yyjson_mut_obj_add_int(d, fc, "cancelled", c[3]);+ yyjson_mut_obj_add_int(+ d, fc, "total",+ (int64_t)yyjson_arr_size(yyjson_obj_get(b, "file_ids")));+ yyjson_doc_free(bd);+ yyjson_mut_doc_set_root(d, o);+ size_t n;+ char *out = yyjson_mut_write(d, 0, &n);+ yyjson_mut_doc_free(d);+ return srv_reply_json(r, 200, out, n);+}++static int store_ok(const char *vs)+{+ char p[4700];+ if (!srv_disk_name_ok(vs))+ return 0;+ srv_vectors_dir(p, sizeof p, vs);+ size_t n = strlen(p);+ snprintf(p + n, sizeof p - n, "/meta.json");+ return access(p, F_OK) == 0;+}++int srv_vs_batch_create(struct srv_req *r)+{+ const char *vs = r->param[0];+ if (!store_ok(vs))+ return not_found(r, "vector store", vs);+ yyjson_doc *doc = yyjson_read(r->body, r->n_body, 0);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_val *ids = yyjson_obj_get(root, "file_ids");+ yyjson_val *files = yyjson_obj_get(root, "files");+ yyjson_val *cs = yyjson_obj_get(root, "chunking_strategy");+ yyjson_val *attrs = yyjson_obj_get(root, "attributes");+ const char *why = NULL;+ if ((ids != NULL) == (files != NULL))+ why = "Give either 'file_ids' or 'files'.";+ else if (yyjson_arr_size(ids ? ids : files) < 1 ||+ yyjson_arr_size(ids ? ids : files) > BATCH_FILES)+ why = "A batch holds from 1 to 2,000 files.";+ /* every file there before any is attached */+ size_t idx, mx;+ yyjson_val *v;+ yyjson_arr_foreach(ids ? ids : files, idx, mx, v)+ {+ const char *fid = ids ? yyjson_get_str(v)+ : yyjson_get_str(yyjson_obj_get(v, "file_id"));+ struct srv_file f;+ if (!why && (!fid || srv_files_get(r->srv->files, fid, &f) != 0))+ why = "A file_id names no file uploaded here.";+ }+ char id[48], p[4800];+ srv_new_id(id, sizeof id, "vsfb_");+ yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *o = yyjson_mut_obj(d), *list = yyjson_mut_arr(d);+ yyjson_arr_foreach(ids ? ids : files, idx, mx, v)+ {+ if (why)+ break;+ const char *fid = ids ? yyjson_get_str(v)+ : yyjson_get_str(yyjson_obj_get(v, "file_id"));+ if (srv_vs_attach(r->srv, vs, fid,+ ids ? cs : yyjson_obj_get(v, "chunking_strategy"),+ ids ? attrs : yyjson_obj_get(v, "attributes"),+ &why) != 0 &&+ !why)+ why = "A file could not be attached.";+ yyjson_mut_arr_add_strcpy(d, list, fid);+ }+ int ok = !why;+ if (ok) {+ yyjson_mut_obj_add_int(d, o, "created_at", (int64_t)time(NULL));+ yyjson_mut_obj_add_val(d, o, "file_ids", list);+ yyjson_mut_obj_add_bool(d, o, "cancelled", false);+ srv_vectors_dir(p, sizeof p, vs);+ strcat(p, "/batches");+ mkdir(p, 0700);+ batch_path(p, sizeof p, vs, id);+ size_t n;+ char *t = yyjson_mut_val_write(o, 0, &n);+ srv_vectors_lock();+ ok = t && srv_vectors_write(p, t, n) == 0;+ srv_vectors_unlock();+ free(t);+ }+ yyjson_mut_doc_free(d);+ yyjson_doc_free(doc);+ if (why)+ return bad(r, why);+ if (!ok)+ return srv_reply_error(r, 500, "server_error", NULL,+ "cannot keep the batch");+ return reply_batch(r, vs, id);+}++int srv_vs_batch_get(struct srv_req *r)+{+ if (!store_ok(r->param[0]))+ return not_found(r, "vector store", r->param[0]);+ return reply_batch(r, r->param[0], r->param[1]);+}++int srv_vs_batch_cancel(struct srv_req *r)+{+ const char *vs = r->param[0], *id = r->param[1];+ if (!store_ok(vs))+ return not_found(r, "vector store", vs);+ yyjson_doc *bd = read_batch(vs, id);+ if (!bd)+ return not_found(r, "vector store file batch", id);+ yyjson_mut_doc *d = yyjson_doc_mut_copy(bd, NULL);+ yyjson_mut_val *o = yyjson_mut_doc_get_root(d);+ yyjson_mut_obj_remove_key(o, "cancelled");+ yyjson_mut_obj_add_bool(d, o, "cancelled", true);+ size_t idx, mx;+ yyjson_val *v;+ yyjson_arr_foreach(yyjson_obj_get(yyjson_doc_get_root(bd), "file_ids"), idx,+ mx, v)+ {+ char st[32];+ file_status(vs, yyjson_get_str(v), st, sizeof st);+ if (!strcmp(st, "in_progress"))+ srv_vectors_set_status(vs, yyjson_get_str(v), "cancelled", NULL,+ NULL, -1);+ }+ char p[4800];+ batch_path(p, sizeof p, vs, id);+ size_t n;+ char *t = yyjson_mut_val_write(o, 0, &n);+ srv_vectors_lock();+ if (t)+ srv_vectors_write(p, t, n);+ srv_vectors_unlock();+ free(t);+ yyjson_mut_doc_free(d);+ yyjson_doc_free(bd);+ return reply_batch(r, vs, id);+}++int srv_vs_batch_files(struct srv_req *r)+{+ if (!store_ok(r->param[0]))+ return not_found(r, "vector store", r->param[0]);+ yyjson_doc *bd = read_batch(r->param[0], r->param[1]);+ if (!bd)+ return not_found(r, "vector store file batch", r->param[1]);+ yyjson_val *ids = yyjson_obj_get(yyjson_doc_get_root(bd), "file_ids");+ size_t n = yyjson_arr_size(ids), idx, mx;+ const char **list = calloc(n ? n : 1, sizeof(*list));+ yyjson_val *v;+ yyjson_arr_foreach(ids, idx, mx, v) if (list) list[idx] = yyjson_get_str(v);+ int ret =+ list ? srv_vs_files_page(r, r->param[0], list, n)+ : srv_reply_error(r, 500, "server_error", NULL, "out of memory");+ free(list);+ yyjson_doc_free(bd);+ return ret;+}diff --git a/src/server/web_search.c b/src/server/web_search.cnew file mode 100644index 0000000..5d35c96--- /dev/null+++ b/src/server/web_search.c@@ -0,0 +1,145 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * web_search.c - the search behind the web_search tool of the Responses+ * API (see web_search.h): Wikipedia's, the open source of the project's+ * services, in one request of its API - the pages that match, best first,+ * with their first sentences and their addresses.+ */+#include "web_search.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <yyjson.h>++#include "services/common/https_get.h"++/* the query as a URL's part */+static void encode(char *out, size_t cap, const char *s)+{+ static const char hex[] = "0123456789ABCDEF";+ size_t w = 0;+ for (; *s && w + 4 < cap; s++) {+ unsigned char c = (unsigned char)*s;+ if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||+ (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' ||+ c == '~') {+ out[w++] = (char)c;+ } else {+ out[w++] = '%';+ out[w++] = hex[c >> 4];+ out[w++] = hex[c & 15];+ }+ }+ out[w] = 0;+}++static int by_index(const void *a, const void *b)+{+ const struct srv_web_hit *x = a, *y = b;+ return x->index - y->index;+}++/* A language as a Wikipedia's code: a code (it, en, zh-yue), or the name+ of one of the languages models write most; NULL when neither. */+static const char *code_of(const char *lang)+{+ static const char *const names[][2] = {+ {"english", "en"}, {"italian", "it"}, {"italiano", "it"},+ {"french", "fr"}, {"german", "de"}, {"spanish", "es"},+ {"portuguese", "pt"}, {"dutch", "nl"}, {"russian", "ru"},+ {"chinese", "zh"}, {"japanese", "ja"}, {"korean", "ko"},+ {"arabic", "ar"}, {"polish", "pl"}, {"swedish", "sv"},+ {"turkish", "tr"}, {"ukrainian", "uk"}, {"sardinian", "sc"},+ };+ static _Thread_local char buf[16];+ if (!lang)+ return NULL;+ size_t n = strlen(lang), i;+ if (n >= sizeof buf)+ return NULL;+ for (i = 0; i < n; i++)+ buf[i] =+ (char)(lang[i] >= 'A' && lang[i] <= 'Z' ? lang[i] + 32 : lang[i]);+ buf[n] = 0;+ for (i = 0; i < sizeof(names) / sizeof(names[0]); i++)+ if (!strcmp(buf, names[i][0]))+ return names[i][1];+ /* aa, aaa, aa-bbb: letters, at most one dash */+ size_t head = strspn(buf, "abcdefghijklmnopqrstuvwxyz");+ if ((head == 2 || head == 3) &&+ (buf[head] == 0 ||+ (buf[head] == '-' && strlen(buf + head + 1) >= 2 &&+ strspn(buf + head + 1, "abcdefghijklmnopqrstuvwxyz") ==+ strlen(buf + head + 1))))+ return buf;+ return NULL;+}++static int search_in(const char *query, const char *lg,+ struct srv_web_hit *hits, int max, char *err,+ size_t err_len);++int srv_web_search(const char *query, const char *lang,+ struct srv_web_hit *hits, int max, char *err, size_t err_len)+{+ const char *lg = code_of(lang);+ int n = lg && strcmp(lg, "en") != 0+ ? search_in(query, lg, hits, max, err, err_len)+ : -1;+ if (n <= 0) /* none asked, or nothing there: the English one */+ n = search_in(query, "en", hits, max, err, err_len);+ return n;+}++static int search_in(const char *query, const char *lg,+ struct srv_web_hit *hits, int max, char *err,+ size_t err_len)+{+ char q[1024], url[2048];+ encode(q, sizeof q, query);+ snprintf(url, sizeof url,+ "https://%s.wikipedia.org/w/api.php?action=query&format=json&"+ "formatversion=2&generator=search&gsrsearch=%s&gsrlimit=%d&"+ "prop=extracts%%7Cinfo&exintro=1&explaintext=1&exsentences=6&"+ "exlimit=max&inprop=url&redirects=1",+ lg, q, max);+ const char *hdr[] = {"User-Agent: janas-server/" JANAS_VERSION+ " (https://github.com/prabanta-dev/janas)"};+ struct janas_buf body = {0};+ int st = janas_https_get(url, hdr, 1, &body, err, err_len);+ if (st != 200) {+ if (st > 0)+ snprintf(err, err_len, "Wikipedia answered HTTP %d", st);+ janas_buf_free(&body);+ return -1;+ }+ yyjson_doc *d = yyjson_read(body.p, body.n, 0);+ janas_buf_free(&body);+ yyjson_val *pages = yyjson_obj_get(+ yyjson_obj_get(yyjson_doc_get_root(d), "query"), "pages");+ int n = 0;+ size_t idx, mx;+ yyjson_val *p;+ yyjson_arr_foreach(pages, idx, mx, p)+ {+ if (n == max)+ break;+ const char *title = yyjson_get_str(yyjson_obj_get(p, "title"));+ const char *u = yyjson_get_str(yyjson_obj_get(p, "fullurl"));+ const char *x = yyjson_get_str(yyjson_obj_get(p, "extract"));+ if (!title || !u)+ continue;+ struct srv_web_hit *h = &hits[n++];+ snprintf(h->title, sizeof h->title, "%s", title);+ snprintf(h->url, sizeof h->url, "%s", u);+ snprintf(h->text, sizeof h->text, "%s", x ? x : "");+ yyjson_val *ix = yyjson_obj_get(p, "index");+ h->index = yyjson_is_int(ix) ? (int)yyjson_get_int(ix) : 1000 + n;+ }+ yyjson_doc_free(d);+ if (n)+ qsort(hits, (size_t)n, sizeof(*hits), by_index);+ return n;+}diff --git a/src/server/web_search.h b/src/server/web_search.hnew file mode 100644index 0000000..93ab38d--- /dev/null+++ b/src/server/web_search.h@@ -0,0 +1,26 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * web_search.h - the search behind the Responses API's web_search tool+ * (web_search.c): Wikipedia, in the language asked.+ */+#ifndef JANAS_SERVER_WEB_SEARCH_H+#define JANAS_SERVER_WEB_SEARCH_H++#include <stddef.h>++struct srv_web_hit {+ char title[256];+ char url[512];+ char text[2048]; /* the page's first sentences */+ int index; /* its rank */+};++/* The pages of Wikipedia (lang: "en", "it"...; NULL or not a code: "en")+ that match query, best first, at most max: how many, or -1 with the+ reason in err. */+int srv_web_search(const char *query, const char *lang,+ struct srv_web_hit *hits, int max, char *err,+ size_t err_len);++#endifdiff --git a/src/services/common/https_get.c b/src/services/common/https_get.cindex 7d78b8a..a1f4a28 100644--- a/src/services/common/https_get.c+++ b/src/services/common/https_get.c@@ -24,22 +24,46 @@ int janas_https_get(const char *url, const char *const *headers,err_len);}+static int get(const char *url, const char *const *headers, size_t n_headers,+ int timeout_ms, const struct janas_net_policy *policy,+ struct janas_buf *out, char *err, size_t err_len);+int janas_https_get_ms(const char *url, const char *const *headers,size_t n_headers, int timeout_ms, struct janas_buf *out,char *err, size_t err_len){- char where[4096], first[256] = "";+ return get(url, headers, n_headers, timeout_ms, NULL, out, err, err_len);+}++int janas_https_get_policy(const char *url,+ const struct janas_net_policy *policy,+ int timeout_ms, struct janas_buf *out, char *err,+ size_t err_len)+{+ return get(url, NULL, 0, timeout_ms, policy, out, err, err_len);+}++static int get(const char *url, const char *const *headers, size_t n_headers,+ int timeout_ms, const struct janas_net_policy *policy,+ struct janas_buf *out, char *err, size_t err_len)+{+ char where[4096];snprintf(where, sizeof where, "%s", url);const char *kept[16];+ struct janas_url first;for (int hop = 0; hop <= MAX_REDIRECTS; hop++) {struct janas_url u;if (janas_url_parse(where, &u, err, err_len) != 0)return -1;+ u.policy = policy; /* on every hop: a redirect too is checked */if (!hop)- snprintf(first, sizeof first, "%s", u.host);- /* sent to another host, a redirect loses the credentials (as curl- does): a token for one server is not for the next */- if (hop && strcasecmp(first, u.host) != 0 && headers) {+ first = u;+ /* sent to another origin, a redirect loses the credentials (as curl+ does): a token for one server is not for the next, nor for the+ same one in clear */+ if (hop && headers &&+ (strcasecmp(first.host, u.host) != 0 || first.tls != u.tls ||+ strcmp(first.port, u.port) != 0)) {size_t k = 0;for (size_t i = 0; i < n_headers && k < 16; i++)if (strncasecmp(headers[i], "Authorization:", 14) != 0)diff --git a/src/services/common/https_get.h b/src/services/common/https_get.hindex 898960d..fdfa443 100644--- a/src/services/common/https_get.h+++ b/src/services/common/https_get.h@@ -10,6 +10,8 @@#include "llm/json.h"+struct janas_net_policy;+/* GET of url (https), following redirects, the body into out (with a 0after it, not counted). The HTTP status (200...), or -1 with the reasonin err. */@@ -21,5 +23,12 @@ int janas_https_get(const char *url, const char *const *headers,int janas_https_get_ms(const char *url, const char *const *headers,size_t n_headers, int timeout_ms, struct janas_buf *out,char *err, size_t err_len);+/* The same for a URL someone else gave (http too): every address it and+ its redirects resolve to checked against policy (common/mcp_policy.h),+ the connection made only to one that passed. */+int janas_https_get_policy(const char *url,+ const struct janas_net_policy *policy,+ int timeout_ms, struct janas_buf *out, char *err,+ size_t err_len);#endifdiff --git a/tests/audio_clips.h b/tests/audio_clips.hnew file mode 100644index 0000000..62bbdfe--- /dev/null+++ b/tests/audio_clips.h@@ -0,0 +1,6068 @@+/* audio_clips.h - generated by gen_suite_clips.py: do not edit (no+ clang-format). Small clips of every format Janas reads, encoded by+ FFmpeg, and FFmpeg's decoding of each, its channels averaged, every+ 4th sample in 1/32768. */++#include <stddef.h>++static const unsigned char CLIP0[17268] = {+ 102,76,97,67,0,0,0,34,4,0,4,0,0,1,76,0,11,145,3,232,+ 2,240,0,0,12,128,142,214,38,168,21,220,42,78,188,182,182,98,7,51,+ 170,198,4,0,0,44,12,0,0,0,76,97,118,102,54,49,46,55,46,49,+ 48,51,1,0,0,0,20,0,0,0,101,110,99,111,100,101,114,61,76,97,+ 118,102,54,49,46,55,46,49,48,51,129,0,32,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,255,248,165,24,0,253,76,253,187,252,86,252,101,253,+ 39,255,172,0,7,254,222,230,182,7,191,60,85,219,26,66,51,205,15,64,+ 255,132,138,121,100,187,130,222,154,84,70,160,4,188,200,79,87,128,105,194,+ 56,20,64,125,202,168,225,17,0,241,172,210,78,52,182,106,174,169,196,55,+ 139,35,11,240,16,242,225,22,86,138,105,234,255,41,119,135,20,147,66,43,+ 13,4,81,167,54,200,36,74,162,91,21,68,188,110,38,18,172,20,209,75,+ 98,145,205,33,219,35,79,150,21,92,105,225,237,36,151,157,114,17,131,49,+ 187,230,150,182,245,92,233,99,137,21,222,236,67,243,54,22,66,93,95,62,+ 6,164,119,161,246,100,116,51,181,100,12,133,36,14,64,251,87,203,193,48,+ 194,196,112,163,75,81,131,24,181,87,27,52,90,138,69,26,17,146,214,184,+ 23,2,248,50,3,136,172,243,0,73,65,211,38,143,144,241,78,109,244,72,+ 217,70,159,233,167,113,211,121,71,216,101,186,252,38,65,153,201,251,17,153,+ 139,81,53,26,188,168,68,183,85,91,223,61,91,229,208,202,251,140,69,13,+ 89,187,189,37,182,123,26,224,1,234,181,242,9,199,31,75,251,20,45,80,+ 18,128,68,180,114,190,43,100,73,144,221,98,123,113,237,21,65,92,109,25,+ 170,82,99,170,215,72,100,250,207,35,231,74,241,51,1,210,171,90,53,109,+ 207,102,67,106,183,90,52,228,227,53,5,236,201,81,246,234,98,17,13,150,+ 82,59,229,169,173,65,29,90,127,225,118,222,236,149,253,84,251,10,42,50,+ 242,25,61,174,149,216,213,206,108,227,30,199,207,137,6,212,163,217,148,61,+ 76,195,0,136,184,133,3,164,132,9,3,20,30,73,140,107,106,195,217,249,+ 136,248,69,148,20,24,71,115,200,130,150,6,139,22,62,243,23,139,21,162,+ 31,238,121,89,51,222,77,53,3,163,86,125,235,6,230,16,76,203,150,221,+ 65,27,2,40,164,28,11,91,171,78,198,25,138,131,214,151,106,73,214,138,+ 153,56,208,115,238,102,143,73,106,44,4,2,162,201,168,136,191,154,49,217,+ 200,41,90,208,34,204,144,11,19,178,109,92,12,241,76,89,113,145,218,53,+ 135,200,70,65,113,49,255,12,40,16,108,103,86,227,72,13,65,137,133,232,+ 62,21,181,58,74,94,191,37,105,160,195,85,105,32,207,162,165,160,113,39,+ 209,236,166,82,206,209,169,43,113,176,197,249,223,41,60,229,19,55,232,190,+ 212,234,172,99,191,100,59,204,131,70,253,32,247,244,85,116,198,157,101,142,+ 31,180,210,139,154,167,196,72,168,146,245,80,214,175,156,129,201,148,52,163,+ 73,125,206,29,6,35,252,143,145,148,176,178,30,226,94,11,128,46,17,73,+ 86,233,105,170,200,163,162,34,220,233,108,48,123,207,232,240,92,30,143,24,+ 145,77,94,179,24,221,64,140,71,19,32,158,136,80,210,132,45,2,27,82,+ 211,217,135,201,209,148,28,121,19,58,52,112,225,34,21,21,28,172,107,128,+ 88,75,108,166,100,56,168,175,4,87,138,186,63,231,242,128,254,27,244,223,+ 136,109,224,83,11,61,199,232,28,238,186,43,66,10,245,105,124,51,246,117,+ 115,132,124,162,153,37,87,31,244,16,98,21,18,120,228,253,210,140,161,164,+ 152,42,92,156,94,83,62,97,212,213,65,25,92,194,158,180,56,24,51,154,+ 85,16,192,209,231,125,53,189,208,123,119,32,125,142,80,27,233,185,138,86,+ 58,13,108,178,51,126,192,138,0,218,121,148,125,145,103,71,72,109,252,43,+ 251,196,76,73,243,190,201,172,44,45,178,162,146,169,43,249,122,34,116,0,+ 123,71,97,69,146,98,22,183,173,69,120,113,22,218,137,52,214,86,149,195,+ 230,136,19,209,207,196,84,146,85,176,179,12,200,68,106,188,253,81,174,131,+ 106,61,193,149,217,200,170,247,123,162,223,218,225,224,121,196,71,154,149,49,+ 101,166,13,81,114,175,244,19,10,147,236,191,228,23,68,253,126,170,66,32,+ 167,112,117,38,124,158,9,62,166,108,203,154,130,118,51,202,19,148,92,93,+ 144,128,128,40,93,143,8,80,248,107,108,179,237,183,184,75,100,140,20,10,+ 135,167,185,15,170,229,36,40,129,61,152,236,169,205,19,247,221,97,126,223,+ 73,16,200,35,200,33,195,255,88,249,154,69,42,204,76,46,95,65,72,16,+ 65,15,136,200,152,31,110,78,150,219,90,18,198,154,243,116,56,148,107,31,+ 153,162,242,77,55,238,243,132,141,41,18,254,235,228,189,185,140,245,123,91,+ 140,247,14,255,237,111,66,210,113,133,252,247,74,196,53,64,164,102,109,132,+ 204,4,215,89,213,50,214,53,194,71,129,203,228,30,134,240,117,36,135,79,+ 192,138,144,72,115,76,52,212,130,37,233,112,190,21,46,8,160,150,136,151,+ 157,95,117,205,154,106,144,88,38,5,197,184,86,230,203,63,15,169,96,65,+ 107,74,45,85,203,100,101,68,223,130,6,32,58,24,85,8,235,166,157,21,+ 33,26,221,133,32,198,175,181,60,237,152,23,159,125,75,56,95,43,200,23,+ 108,78,10,104,115,128,182,186,200,241,252,140,149,181,43,237,132,206,30,224,+ 66,178,125,221,247,222,244,28,46,159,145,205,219,52,74,29,236,240,227,74,+ 133,72,225,221,24,57,43,42,88,219,158,17,40,209,38,140,61,136,149,202,+ 180,245,231,6,213,33,124,20,52,180,242,90,117,120,111,116,152,197,79,236,+ 147,151,209,78,196,53,31,242,67,152,187,11,231,130,172,64,211,158,110,155,+ 233,3,38,82,184,50,92,62,83,215,212,80,164,171,130,250,53,49,49,125,+ 170,75,221,135,166,42,112,196,145,120,199,17,35,191,85,113,181,249,65,184,+ 72,201,127,33,45,15,97,49,77,50,242,73,167,188,187,233,78,164,149,247,+ 21,213,84,247,130,83,166,134,139,66,99,16,202,133,78,45,91,105,73,198,+ 138,117,63,42,244,26,5,55,147,17,30,181,12,71,186,152,233,20,81,41,+ 27,26,9,30,236,58,11,118,55,116,252,216,10,88,123,10,109,109,83,154,+ 86,130,232,153,204,176,81,181,235,80,78,93,163,177,60,205,190,46,199,148,+ 195,216,4,110,176,173,65,255,6,94,70,42,11,3,133,199,242,194,14,28,+ 161,249,187,112,78,98,121,105,29,36,182,204,205,144,131,193,226,181,174,251,+ 67,9,21,95,56,65,196,194,232,70,40,88,53,106,138,208,225,90,102,57,+ 153,116,62,242,171,166,61,173,218,28,26,161,244,150,163,174,66,149,102,185,+ 174,77,196,169,16,102,16,134,33,220,82,59,10,131,212,226,110,169,27,3,+ 105,188,146,38,202,212,133,24,113,20,66,100,86,21,108,81,88,170,184,155,+ 194,93,144,186,195,37,76,147,247,74,250,39,79,188,62,221,11,225,231,93,+ 6,254,127,1,173,243,211,63,214,255,207,64,41,127,218,128,104,192,65,63,+ 248,249,173,125,175,161,24,22,193,218,136,39,81,136,60,0,228,61,208,121,+ 23,179,51,113,117,233,204,120,172,230,118,91,154,241,192,138,48,221,94,177,+ 15,165,44,175,247,50,102,182,45,65,142,172,130,19,32,166,242,111,14,86,+ 59,202,13,132,189,19,247,200,90,248,184,205,129,147,171,162,66,193,148,221,+ 176,159,29,91,91,103,22,251,130,166,119,223,112,175,77,89,147,253,8,246,+ 224,166,176,95,11,181,153,229,173,204,147,18,110,5,47,104,59,76,57,90,+ 119,101,221,48,224,100,82,155,55,94,171,72,236,163,46,189,24,57,25,60,+ 34,144,158,174,4,128,241,7,98,197,4,58,111,134,90,85,18,91,50,248,+ 75,61,56,47,55,100,220,70,44,104,255,119,190,210,150,30,91,69,237,210,+ 83,61,235,162,143,104,19,208,151,87,4,231,138,113,37,239,126,250,13,106,+ 134,99,229,49,111,31,13,44,81,4,228,134,98,208,133,244,198,106,99,153,+ 122,186,77,11,179,34,28,153,138,253,118,132,193,152,74,101,76,190,115,251,+ 151,213,3,213,12,170,76,35,181,154,170,151,16,71,210,242,90,172,109,163,+ 164,78,87,189,35,175,201,115,56,161,3,202,209,67,94,118,120,103,249,135,+ 131,147,19,144,52,25,120,177,83,39,7,108,38,132,104,94,200,187,134,17,+ 194,52,39,99,102,99,133,203,194,135,146,76,114,142,72,138,66,253,110,118,+ 153,1,220,33,79,26,48,9,181,219,168,142,11,65,72,73,195,221,112,10,+ 160,51,33,66,106,185,83,153,214,185,150,35,12,51,62,145,200,83,69,223,+ 216,172,237,53,83,211,189,153,13,151,25,63,246,33,48,210,245,85,148,151,+ 218,171,124,14,34,221,181,80,238,74,139,152,233,247,105,39,182,197,189,178,+ 186,14,212,100,225,94,74,111,177,206,171,244,133,68,107,20,15,47,91,54,+ 69,113,197,45,223,105,182,96,228,105,86,213,37,96,149,215,149,27,198,29,+ 149,17,126,64,82,157,72,61,77,226,64,187,170,116,196,220,53,152,180,134,+ 169,138,28,166,225,116,216,88,21,17,111,221,187,246,135,79,41,218,78,156,+ 59,119,150,11,77,17,137,252,80,225,171,86,32,191,201,164,3,62,94,159,+ 106,231,133,161,184,109,38,35,161,86,1,70,209,1,40,1,192,190,27,248,+ 71,75,238,137,152,240,135,107,153,44,94,95,95,116,245,79,193,30,23,24,+ 176,24,9,194,35,170,69,143,241,94,212,220,236,40,12,158,48,111,74,204,+ 217,27,25,231,28,38,19,209,32,240,179,19,203,54,147,59,84,101,79,243,+ 23,172,97,184,65,101,172,216,84,206,253,62,91,204,16,227,86,121,141,191,+ 243,81,24,44,187,152,132,174,55,181,86,251,54,198,108,24,1,131,48,145,+ 139,213,165,121,183,184,225,253,34,144,40,114,249,233,14,54,194,40,143,22,+ 123,51,254,13,194,34,217,45,165,51,204,151,103,55,79,69,92,14,217,108,+ 180,171,108,228,199,85,230,199,149,22,248,166,2,79,28,128,133,199,45,142,+ 37,229,169,79,202,33,100,71,144,249,60,39,170,208,167,109,196,192,129,186,+ 233,146,188,28,157,205,252,234,188,253,148,70,206,25,60,148,44,79,175,44,+ 233,9,244,26,75,137,19,136,51,77,22,189,146,132,70,98,38,21,54,134,+ 223,56,114,214,89,187,83,106,115,29,148,119,199,123,149,223,35,101,223,80,+ 86,158,42,92,233,213,13,97,67,108,90,54,83,250,109,229,38,178,200,71,+ 201,213,55,50,93,252,168,115,39,67,102,187,61,135,122,126,0,207,132,54,+ 232,106,89,55,35,227,14,101,229,220,158,152,239,147,98,62,193,136,153,223,+ 184,102,244,212,219,126,110,153,217,54,82,214,57,135,95,216,110,244,139,226,+ 119,157,221,164,47,114,138,144,237,38,224,119,17,114,14,41,246,14,253,7,+ 151,212,185,55,171,205,51,226,135,14,59,34,7,21,135,141,236,137,51,7,+ 17,187,82,231,160,204,85,146,2,16,18,135,62,1,56,248,145,240,153,0,+ 153,130,99,234,50,226,255,123,27,60,237,78,42,103,242,140,241,251,240,68,+ 69,164,213,194,98,111,165,240,223,14,25,143,114,205,10,17,165,101,250,170,+ 87,89,2,107,90,4,151,171,95,180,94,137,17,65,48,164,71,164,144,169,+ 117,255,226,197,34,177,99,175,54,131,154,96,66,225,23,96,36,165,173,57,+ 21,58,195,33,83,140,10,40,248,242,240,68,105,148,78,0,14,212,36,69,+ 156,128,89,145,197,181,248,240,234,148,213,128,186,236,178,126,147,147,48,129,+ 19,98,79,48,33,78,252,154,119,194,98,135,206,140,245,74,0,64,14,141,+ 51,46,68,172,227,33,192,70,146,209,12,63,175,65,145,232,135,165,84,149,+ 29,170,134,94,242,131,223,234,105,185,187,66,147,195,237,135,217,137,167,243,+ 127,43,243,157,139,207,236,84,51,211,61,201,140,119,43,6,109,24,140,192,+ 189,129,80,179,26,20,250,153,82,55,124,44,244,78,227,71,194,10,187,13,+ 228,42,16,190,202,178,107,252,198,56,141,42,50,195,31,134,215,52,10,3,+ 4,47,39,255,65,238,243,226,138,248,236,78,159,248,77,75,70,65,101,195,+ 204,138,66,80,112,34,82,141,138,176,205,153,27,202,233,122,170,213,89,243,+ 78,188,178,62,185,77,66,194,170,217,201,179,4,131,195,43,86,219,254,140,+ 44,64,98,33,103,44,46,31,160,144,253,1,157,129,216,21,32,64,37,31,+ 123,138,26,144,154,151,208,133,201,204,87,110,192,40,117,254,27,134,45,46,+ 1,17,141,120,249,68,56,86,207,254,75,88,127,160,64,74,30,76,122,146,+ 80,37,33,78,185,248,90,182,89,11,234,166,56,150,52,148,205,25,150,248,+ 67,132,156,197,152,238,73,153,144,116,135,168,185,208,22,222,142,184,102,227,+ 10,178,113,115,67,26,142,167,76,165,140,87,4,123,242,13,149,24,113,161,+ 217,132,126,64,254,43,137,102,125,141,54,202,95,242,236,8,96,187,214,122,+ 156,67,168,43,51,61,172,239,237,13,222,38,152,229,180,57,167,241,3,87,+ 120,144,75,199,43,42,4,149,134,43,207,35,168,125,138,52,140,51,69,161,+ 208,36,21,203,175,165,132,199,159,109,201,86,89,102,141,146,111,145,58,134,+ 164,229,132,252,128,43,202,107,234,44,125,187,12,76,211,14,110,206,61,157,+ 34,11,94,210,56,107,130,255,248,165,24,1,250,76,249,150,249,191,249,129,+ 249,26,249,15,248,230,248,102,230,35,170,191,42,66,131,211,240,89,159,43,+ 192,123,196,121,60,82,189,43,84,25,248,9,114,143,240,82,191,144,241,29,+ 105,87,103,108,100,160,196,96,163,207,207,151,193,129,47,40,34,50,145,167,+ 86,209,60,45,137,49,173,63,42,194,50,8,42,139,141,194,9,145,150,87,+ 122,101,35,196,196,131,245,49,52,4,109,43,199,49,28,131,72,201,214,174,+ 84,167,250,200,166,241,150,231,74,29,64,228,228,37,238,174,222,114,78,250,+ 243,223,83,175,145,171,170,102,191,152,83,108,192,206,176,56,133,113,233,236,+ 56,111,70,146,8,100,0,134,1,235,178,213,109,23,217,155,228,122,243,144,+ 56,57,32,12,173,43,54,38,125,240,140,200,105,64,68,243,85,114,68,128,+ 199,111,120,56,160,210,34,199,160,209,228,146,215,6,13,60,37,222,28,154,+ 223,119,73,249,33,223,140,35,231,93,118,161,74,61,234,74,228,249,119,180,+ 190,94,51,111,239,225,35,133,177,156,148,38,122,148,230,147,37,86,104,250,+ 252,151,174,196,28,47,163,55,117,105,165,244,131,203,251,28,216,149,209,123,+ 158,209,189,80,133,169,249,252,217,26,108,204,20,186,18,146,201,133,67,110,+ 152,207,116,140,30,2,64,126,46,168,171,22,158,28,188,62,103,2,103,77,+ 11,229,112,169,74,155,70,244,165,21,86,144,108,179,172,157,218,155,97,117,+ 193,179,133,34,44,50,41,70,109,164,189,4,166,18,242,86,9,134,4,13,+ 236,2,54,188,65,88,141,203,145,127,165,78,32,198,254,71,52,52,228,247,+ 241,145,118,74,32,18,169,31,155,226,239,213,25,96,214,185,162,47,37,228,+ 60,172,79,152,107,78,118,169,146,12,179,155,204,138,177,46,116,20,11,158,+ 86,32,163,155,34,110,115,125,176,222,181,109,220,195,185,134,31,214,85,231,+ 104,34,57,60,140,24,12,192,45,149,228,40,50,195,235,1,44,132,158,47,+ 23,94,176,73,142,232,143,129,212,67,214,120,97,105,238,67,48,82,166,76,+ 217,7,238,124,234,200,64,162,27,23,161,183,21,56,90,230,36,228,225,129,+ 107,72,117,155,193,140,8,169,43,133,180,33,10,216,159,100,161,43,34,254,+ 200,79,208,223,251,94,145,113,64,206,64,134,32,7,29,8,100,139,220,215,+ 0,219,43,46,185,37,139,58,7,82,103,160,248,162,81,23,171,116,221,252,+ 86,206,168,201,212,136,138,235,84,48,63,125,194,7,19,63,104,151,115,59,+ 1,66,244,135,159,3,99,81,105,242,110,188,134,119,142,77,130,154,213,141,+ 210,7,240,49,218,251,237,43,93,226,255,30,33,79,197,18,130,52,158,132,+ 169,118,77,68,38,70,174,16,210,70,37,84,31,206,137,97,144,243,158,159,+ 50,213,227,38,42,231,21,106,165,243,216,80,17,121,29,249,83,94,65,148,+ 234,174,184,149,88,22,61,35,20,0,239,41,187,162,25,116,211,85,62,0,+ 9,255,4,21,96,198,170,4,231,162,197,146,141,70,230,130,101,211,159,192,+ 135,134,66,173,185,132,252,166,200,124,231,191,232,216,108,172,125,206,102,180,+ 28,197,189,18,164,162,181,37,74,152,59,183,108,124,105,121,62,59,221,38,+ 214,20,190,45,177,173,14,74,245,205,17,157,140,215,189,105,210,31,252,132,+ 9,169,44,249,30,46,65,235,12,86,110,197,91,59,38,10,156,139,232,137,+ 23,243,151,204,185,211,180,97,76,132,164,30,133,89,225,142,53,108,232,66,+ 230,121,108,45,123,96,183,156,43,105,167,143,179,73,210,20,162,251,191,97,+ 145,212,244,237,159,173,237,140,254,175,108,242,155,245,192,193,112,139,214,40,+ 45,127,71,100,138,206,155,114,13,210,31,50,73,95,146,250,124,232,108,206,+ 146,6,41,57,177,66,4,25,146,144,21,24,116,122,110,29,194,227,40,95,+ 84,212,147,169,206,141,145,117,48,90,99,93,139,145,141,153,139,60,191,12,+ 28,74,124,136,151,151,30,225,167,173,194,165,114,106,28,172,175,185,27,221,+ 186,224,73,17,225,226,70,181,85,67,68,240,60,33,27,212,165,81,235,241,+ 28,156,37,112,40,77,178,72,199,146,192,228,58,40,178,136,206,188,171,204,+ 68,41,156,130,210,59,47,101,196,83,94,36,54,241,139,210,149,90,46,46,+ 144,177,126,215,125,36,185,77,252,205,26,34,187,44,197,38,67,74,18,79,+ 67,169,121,126,242,237,76,89,164,126,53,119,184,36,205,66,73,23,207,213,+ 210,243,170,107,56,174,226,239,94,52,199,42,27,187,150,205,16,98,173,97,+ 212,118,49,123,39,121,4,122,24,49,86,196,181,128,36,18,163,254,237,60,+ 217,253,159,220,117,107,231,71,164,195,89,226,243,240,138,94,51,100,164,32,+ 223,246,94,100,192,59,40,215,8,108,47,79,80,201,149,118,20,207,100,54,+ 51,78,80,80,232,92,224,211,76,238,201,131,36,78,108,195,92,242,194,175,+ 150,72,138,105,113,255,222,231,164,113,185,113,6,169,106,225,205,107,46,134,+ 53,116,88,45,19,212,196,156,107,138,236,109,186,201,12,219,94,209,8,69,+ 37,26,69,23,139,190,159,249,72,76,28,59,142,232,45,88,132,40,65,201,+ 239,13,137,70,205,34,153,30,244,114,12,103,6,8,48,103,242,103,33,152,+ 95,77,106,10,206,8,244,97,216,176,42,160,155,62,11,81,43,106,58,251,+ 161,34,150,179,200,160,24,193,30,35,141,210,253,50,65,51,190,175,41,121,+ 36,233,156,19,30,126,187,207,145,116,134,55,142,171,229,97,246,206,81,195,+ 75,154,66,246,79,56,63,51,118,207,91,180,73,134,31,144,251,2,17,202,+ 221,190,139,200,167,10,203,160,18,159,86,136,79,129,24,21,125,79,248,169,+ 190,66,172,251,165,234,216,77,85,58,31,125,170,18,98,39,227,205,170,162,+ 141,31,39,145,162,229,213,34,89,22,44,40,216,154,11,128,247,227,206,111,+ 15,215,102,142,32,136,22,75,72,120,35,32,161,39,98,245,113,169,107,82,+ 62,72,43,2,167,222,74,237,225,153,53,194,235,57,155,211,244,217,100,122,+ 112,82,155,159,208,67,176,112,226,17,41,232,170,182,205,95,26,195,114,1,+ 3,69,58,56,72,127,178,248,81,190,199,229,188,195,91,198,71,90,174,3,+ 223,103,50,202,208,145,135,31,32,97,233,55,59,220,149,32,129,255,124,158,+ 76,102,76,173,95,250,158,135,56,5,8,156,208,175,237,100,10,172,192,25,+ 115,86,108,171,243,71,248,142,58,168,122,135,43,66,95,78,150,183,139,140,+ 94,123,50,112,151,175,92,253,179,228,5,251,98,44,240,110,81,189,29,10,+ 65,147,151,223,35,222,186,134,144,17,45,71,173,220,54,185,71,72,126,149,+ 253,21,251,33,251,237,255,213,252,167,248,7,204,72,137,116,32,159,215,167,+ 160,39,127,147,4,137,9,69,218,9,73,16,9,122,134,233,24,177,231,89,+ 53,74,191,41,93,23,0,61,37,4,9,80,63,249,134,37,122,214,132,28,+ 239,26,155,124,123,182,191,49,55,8,206,102,91,13,206,118,81,142,198,79,+ 18,199,81,144,191,183,75,7,220,175,162,24,171,122,209,90,130,181,207,57,+ 184,68,85,18,139,107,84,210,160,130,195,80,55,34,100,141,168,71,65,201,+ 241,156,69,120,113,139,239,19,114,119,133,65,97,56,209,35,48,169,86,46,+ 143,184,236,122,165,208,185,44,189,86,185,27,115,46,56,108,86,255,92,110,+ 186,166,15,84,197,77,62,102,90,8,190,37,20,208,156,232,182,41,224,244,+ 50,11,234,143,139,227,115,178,75,97,227,60,49,171,216,32,44,42,135,130,+ 106,1,80,200,70,173,163,225,227,212,227,181,107,149,194,145,38,250,82,20,+ 153,140,59,9,252,19,109,72,13,33,248,146,223,186,92,205,125,140,213,246,+ 33,58,6,236,168,238,56,6,144,207,237,232,30,109,137,233,93,255,30,213,+ 46,249,169,83,206,30,225,112,13,118,184,9,152,16,26,232,151,191,21,129,+ 109,240,211,167,16,241,129,89,154,37,74,140,170,241,79,78,103,126,132,49,+ 239,250,173,26,113,97,3,23,120,237,104,141,25,105,243,61,32,181,68,4,+ 210,105,153,64,38,184,92,199,135,132,148,157,245,121,198,38,208,43,185,255,+ 124,47,19,61,17,103,40,43,117,200,138,121,55,223,242,32,234,161,6,250,+ 63,140,99,117,164,43,19,91,135,32,149,75,157,161,46,98,6,71,181,164,+ 175,130,169,4,206,79,72,167,130,229,174,36,114,140,138,57,28,239,236,10,+ 218,208,16,219,150,157,177,157,82,124,41,182,177,109,149,215,43,108,222,217,+ 189,39,83,70,245,74,79,165,16,218,171,217,121,121,88,140,201,101,250,250,+ 255,12,2,128,156,40,239,149,138,9,201,200,12,25,33,238,161,210,34,127,+ 189,205,254,150,159,126,27,28,207,234,127,82,106,80,33,22,193,126,151,214,+ 157,191,194,56,154,241,150,209,60,208,38,45,235,37,89,171,36,181,213,244,+ 168,12,227,176,130,115,41,162,4,19,165,105,201,38,20,204,231,32,233,49,+ 171,109,50,141,121,14,45,34,229,77,146,166,47,21,183,30,184,225,101,121,+ 116,106,45,36,168,119,255,231,33,145,144,16,181,173,25,192,78,125,99,146,+ 222,203,130,185,17,222,151,39,27,189,210,236,175,130,15,86,105,129,219,175,+ 73,100,25,76,168,167,250,238,18,67,20,231,149,22,38,30,209,57,115,171,+ 1,62,106,116,172,37,205,60,54,68,65,84,43,42,176,104,47,52,253,15,+ 106,58,206,178,146,54,232,105,102,185,135,99,172,207,41,245,162,47,114,186,+ 104,187,203,1,252,140,134,180,203,198,130,43,179,255,208,175,200,69,49,179,+ 224,137,39,59,120,127,178,132,11,195,242,171,138,188,199,120,60,240,183,254,+ 252,27,13,241,121,251,29,224,122,147,149,115,85,38,108,148,155,20,228,241,+ 64,88,9,226,155,90,24,167,131,241,193,231,60,255,216,249,145,39,73,159,+ 133,82,3,174,34,211,244,222,35,199,145,231,35,46,31,92,224,165,85,243,+ 143,217,104,108,86,137,246,30,10,149,221,90,178,237,72,72,122,34,247,32,+ 21,171,151,51,157,11,114,39,99,132,160,83,73,217,133,157,24,42,117,216,+ 101,102,251,59,168,247,87,42,68,190,73,118,234,137,240,134,181,172,77,26,+ 71,53,40,137,50,237,82,217,52,196,94,67,162,36,208,168,193,111,230,86,+ 99,219,26,84,53,73,231,104,99,108,91,71,13,105,109,137,154,98,246,156,+ 116,250,211,18,5,145,116,254,46,50,138,229,102,91,253,206,52,201,135,39,+ 244,214,191,193,154,232,167,22,205,52,70,75,66,69,160,130,59,241,65,2,+ 51,71,205,131,67,39,96,167,85,89,207,242,243,8,148,124,233,45,174,240,+ 203,113,11,40,12,59,10,231,22,168,253,167,157,18,102,176,112,99,111,4,+ 53,61,136,77,40,45,149,79,46,75,82,17,149,243,177,171,9,123,242,60,+ 99,64,157,232,37,9,69,113,158,194,204,117,237,54,63,116,109,155,18,204,+ 117,148,207,180,89,59,86,110,192,88,72,111,183,159,210,71,249,131,29,16,+ 49,29,147,208,8,184,53,222,168,206,215,50,160,91,234,8,25,26,127,71,+ 231,247,33,77,158,195,175,213,75,153,209,100,14,35,13,97,81,188,218,175,+ 137,1,76,9,141,109,55,84,112,224,4,119,52,227,74,109,101,100,22,34,+ 0,88,216,18,206,162,199,48,67,203,70,20,45,139,82,64,251,16,154,39,+ 39,215,16,188,102,68,244,162,217,111,238,74,9,99,31,91,98,29,33,83,+ 213,148,248,122,50,192,51,145,75,66,194,0,25,112,153,205,247,250,79,215,+ 143,150,82,165,222,67,178,73,42,183,24,192,187,204,26,15,237,45,160,232,+ 28,63,10,163,155,180,112,252,193,82,16,153,208,145,34,7,132,34,138,146,+ 136,18,185,54,72,136,25,190,77,66,134,44,247,24,127,74,179,220,139,218,+ 164,21,122,40,98,249,90,59,211,91,75,158,170,229,138,227,225,18,52,244,+ 163,44,107,172,132,146,218,153,206,245,45,0,189,80,5,109,167,190,170,86,+ 157,46,123,197,147,155,92,72,205,254,240,217,125,38,240,116,122,83,43,35,+ 196,94,202,82,52,188,202,18,170,19,17,30,215,179,105,137,228,89,85,167,+ 59,112,137,100,88,228,133,77,98,12,99,214,227,191,42,104,205,17,188,229,+ 145,12,37,92,246,64,109,92,157,223,222,161,100,79,155,111,171,45,161,78,+ 184,7,185,147,247,155,89,233,210,112,184,54,48,118,228,206,240,71,39,244,+ 37,47,77,21,170,144,19,103,68,89,122,211,225,103,15,149,233,97,64,168,+ 232,80,182,89,80,185,110,194,86,121,175,50,243,107,142,91,200,45,111,37,+ 123,156,164,57,145,85,94,234,134,54,118,141,8,74,182,79,150,129,139,52,+ 25,50,69,149,193,4,127,53,155,188,43,228,41,184,32,201,204,9,108,255,+ 248,165,24,2,243,78,241,93,237,89,234,114,235,94,236,8,238,76,244,208,+ 255,198,230,173,32,215,248,48,51,99,25,222,93,101,196,144,248,154,133,66,+ 187,80,169,86,104,34,47,60,27,95,217,194,86,175,47,153,28,23,77,198,+ 172,92,206,234,20,38,0,224,206,225,232,142,52,129,135,204,141,240,190,200,+ 209,132,35,35,251,13,195,26,182,118,63,87,42,124,66,222,119,113,123,99,+ 157,39,82,81,96,197,43,131,100,54,68,26,212,23,96,60,112,45,1,206,+ 49,62,158,17,26,142,28,244,65,176,64,144,253,161,100,250,72,32,88,130,+ 188,61,205,205,68,37,145,37,139,4,107,163,203,16,235,76,41,52,144,48,+ 20,82,12,239,68,93,83,229,187,234,117,152,167,233,76,238,226,90,194,176,+ 32,214,234,33,65,103,141,108,195,22,29,95,200,140,140,12,34,79,254,214,+ 209,143,190,77,255,14,136,248,181,22,164,35,237,202,60,154,202,43,242,150,+ 121,156,157,43,32,112,39,103,170,160,124,96,118,166,186,90,193,101,248,17,+ 4,54,0,103,112,69,113,19,66,230,163,248,160,101,75,108,29,99,120,68,+ 228,122,197,92,24,128,169,130,34,116,47,156,161,214,251,167,118,17,84,101,+ 252,206,121,151,95,151,210,94,222,91,170,28,38,130,3,18,129,31,174,81,+ 28,147,129,34,159,93,14,46,78,142,75,181,91,238,169,87,244,17,253,14,+ 83,151,170,147,201,27,219,135,94,218,44,74,216,186,173,205,203,222,84,1,+ 90,13,53,10,7,228,212,8,252,41,67,91,200,225,250,92,215,160,157,12,+ 236,181,28,51,72,108,246,204,100,18,7,24,34,246,53,145,125,164,147,170,+ 102,100,80,94,4,157,143,243,18,124,68,27,232,168,44,107,126,45,130,80,+ 88,210,4,72,204,188,120,40,61,134,146,9,18,165,202,98,225,121,65,208,+ 2,99,29,248,177,244,188,208,115,222,24,155,201,136,71,7,6,32,58,164,+ 47,216,25,30,142,246,151,232,68,52,155,130,18,3,40,25,210,112,78,68,+ 200,151,18,40,58,112,14,116,83,180,136,96,161,83,152,72,101,125,85,224,+ 114,7,228,17,184,17,195,6,226,70,53,5,22,18,163,105,91,183,192,158,+ 136,185,225,12,229,65,74,226,212,78,109,92,78,204,231,48,87,27,242,50,+ 147,144,114,99,99,114,2,144,34,253,129,185,41,100,18,190,72,216,250,186,+ 9,216,218,103,18,122,103,34,91,12,84,26,74,235,111,226,179,29,246,144,+ 235,194,144,46,224,85,128,185,155,211,57,161,113,124,124,69,97,166,200,29,+ 200,61,48,142,216,218,79,63,132,70,78,132,202,177,15,75,67,134,164,127,+ 40,177,28,20,9,19,121,17,57,209,68,206,133,132,73,174,52,229,171,70,+ 93,184,197,187,39,118,168,142,170,248,141,147,63,52,45,132,104,167,77,1,+ 126,167,87,204,109,44,218,148,149,204,60,52,29,16,123,38,158,150,224,186,+ 224,73,44,71,184,229,194,154,26,55,81,244,21,96,142,139,84,80,144,112,+ 158,74,140,169,85,21,74,11,223,88,12,93,193,203,124,250,4,65,37,136,+ 74,245,138,20,48,193,18,27,168,134,147,194,230,186,43,226,242,42,178,158,+ 88,194,69,215,14,178,136,225,209,216,179,180,13,144,99,22,140,213,181,102,+ 40,138,121,72,136,24,57,242,233,167,224,44,230,19,46,80,147,232,120,109,+ 132,31,252,228,141,195,177,15,200,164,19,101,50,55,7,61,130,61,115,146,+ 40,180,110,113,16,1,3,33,224,4,55,42,69,194,146,109,104,167,238,84,+ 2,40,60,37,116,153,15,178,244,229,145,0,152,35,160,72,143,195,18,209,+ 82,143,180,55,188,243,59,24,36,97,20,127,136,64,147,96,167,233,136,215,+ 115,8,121,136,105,205,105,81,47,181,46,105,39,36,167,148,124,249,106,91,+ 138,40,96,156,35,135,72,115,156,112,208,59,76,67,58,28,84,220,192,58,+ 224,159,214,67,34,58,46,64,147,253,144,71,165,46,158,29,214,142,76,213,+ 142,101,249,137,14,215,64,154,76,82,49,6,32,203,28,59,237,136,8,69,+ 121,192,63,194,0,218,47,198,234,73,232,142,43,139,118,186,143,126,86,103,+ 90,141,58,226,43,100,20,79,68,212,174,154,159,31,186,239,98,80,155,38,+ 56,130,121,100,54,115,95,207,112,72,60,54,167,207,87,234,144,210,6,172,+ 84,196,4,139,218,76,94,3,112,95,153,182,33,174,228,52,253,147,88,151,+ 182,136,116,114,156,33,67,184,237,53,3,152,246,49,95,194,201,130,239,164,+ 196,203,197,27,75,169,54,153,172,112,238,154,83,54,78,196,108,82,114,162,+ 47,14,168,202,45,169,2,133,172,244,163,20,126,169,189,87,147,85,95,237,+ 184,245,235,250,43,9,77,157,221,194,7,195,160,248,16,84,145,108,76,155,+ 175,81,111,237,226,54,151,52,144,237,12,195,216,254,8,1,112,43,78,1,+ 100,169,159,139,66,165,10,44,80,92,68,89,99,136,233,215,87,196,116,200,+ 136,169,97,24,84,109,121,90,186,208,61,31,56,77,151,115,44,253,165,233,+ 188,57,99,131,96,45,26,117,158,219,247,103,91,59,105,159,166,32,106,142,+ 17,156,84,48,82,236,85,6,117,143,188,74,10,66,108,73,25,192,146,58,+ 70,72,155,25,228,25,200,210,44,204,51,205,192,67,119,83,21,229,203,251,+ 145,50,58,67,198,76,77,204,207,78,223,94,136,240,147,114,41,166,127,34,+ 202,95,5,225,78,80,121,117,125,194,191,49,105,10,242,188,160,36,214,169,+ 117,86,252,179,239,19,228,160,70,124,50,248,6,162,81,226,27,107,99,168,+ 166,30,102,168,69,159,227,244,181,31,203,243,53,53,119,16,196,158,166,149,+ 160,229,247,238,95,88,83,146,175,141,63,62,155,144,109,245,30,9,220,82,+ 165,22,73,121,191,148,53,245,231,82,175,186,15,82,78,140,237,209,50,35,+ 13,154,210,73,14,145,108,36,229,225,110,131,241,102,92,202,74,228,97,54,+ 89,163,220,34,27,37,224,198,23,161,10,230,21,173,178,120,133,98,190,31,+ 56,226,95,231,50,41,137,119,125,201,41,37,46,63,116,47,192,148,192,96,+ 27,236,4,37,101,221,198,222,91,48,172,211,35,137,146,15,122,221,235,102,+ 169,223,101,33,204,195,189,187,229,50,33,106,206,33,82,10,125,102,229,136,+ 54,15,18,245,252,56,9,194,163,162,102,130,88,98,44,97,200,96,251,63,+ 252,223,90,220,213,255,26,86,133,33,237,139,63,231,161,96,126,135,76,64,+ 171,56,117,26,135,121,26,42,1,92,52,223,92,119,39,111,157,228,147,245,+ 215,98,36,64,88,192,132,139,20,108,252,125,201,13,52,160,160,128,171,114,+ 233,102,213,115,117,51,57,236,95,93,72,118,131,10,196,181,66,211,35,180,+ 130,79,201,245,223,153,146,92,148,165,4,19,39,109,234,148,109,206,243,227,+ 28,40,191,72,35,179,176,80,78,224,169,73,128,214,214,66,49,75,40,181,+ 123,121,127,11,75,52,107,179,112,87,109,162,156,38,199,151,34,173,184,27,+ 94,169,92,129,154,175,35,128,191,82,55,26,176,107,239,237,126,126,84,34,+ 242,156,21,21,78,154,38,25,73,118,41,82,68,45,152,118,57,32,18,27,+ 127,124,162,255,29,161,5,170,61,128,122,236,4,177,49,110,223,52,252,235,+ 219,173,151,36,170,196,108,67,150,16,117,103,160,174,8,96,170,153,185,124,+ 41,125,38,150,246,51,246,252,28,99,118,56,90,235,201,184,161,114,52,66,+ 133,183,12,242,192,233,226,79,149,127,190,88,33,78,123,17,168,10,160,90,+ 237,113,136,248,227,140,105,213,205,100,243,72,156,133,145,103,8,159,121,151,+ 89,232,182,186,96,175,232,162,138,248,194,215,27,22,106,74,200,224,200,98,+ 86,20,18,245,158,247,141,216,244,151,237,85,86,54,55,50,89,72,236,144,+ 167,74,252,175,91,130,253,214,150,108,132,223,197,6,239,22,138,114,62,39,+ 69,243,172,199,232,151,101,71,225,118,165,71,31,119,110,120,138,207,172,182,+ 218,121,222,112,26,97,204,171,100,156,114,244,114,248,210,96,85,23,13,120,+ 114,32,207,226,42,182,170,197,80,76,62,62,78,192,169,6,148,96,133,185,+ 16,55,37,17,195,38,199,202,35,116,89,27,148,170,41,84,24,174,161,36,+ 47,1,150,102,135,225,67,40,130,6,115,148,243,59,121,91,127,64,253,112,+ 52,187,130,160,135,227,215,187,122,88,131,12,245,172,219,180,19,157,34,27,+ 28,85,100,50,50,129,56,41,72,164,18,19,69,56,66,62,99,247,56,111,+ 53,106,181,23,107,189,128,192,254,78,28,151,30,72,148,8,154,208,119,15,+ 194,59,42,170,200,148,50,37,75,44,99,21,229,25,216,17,47,223,234,121,+ 200,18,46,60,79,69,197,28,117,122,148,26,162,80,246,84,109,85,181,70,+ 125,41,83,169,90,87,170,34,167,54,143,219,215,12,126,189,29,134,113,117,+ 19,71,32,155,62,121,105,208,33,234,8,3,230,145,190,37,85,77,36,113,+ 196,133,51,80,211,162,5,91,171,76,165,253,136,156,210,163,246,94,21,162,+ 93,180,25,101,248,130,146,107,160,158,252,109,193,229,69,142,5,51,86,143,+ 209,148,233,13,73,72,246,157,119,81,88,93,25,2,33,211,248,43,9,55,+ 160,79,202,124,80,179,59,146,73,17,149,38,216,241,174,41,129,187,211,153,+ 58,59,141,214,163,179,88,76,59,25,90,32,129,20,19,85,140,173,229,140,+ 22,145,40,85,187,187,38,208,128,80,31,43,74,39,235,13,36,16,27,180,+ 237,171,217,58,88,217,147,22,49,8,83,89,132,180,225,54,252,157,93,105,+ 53,155,224,134,190,14,239,114,139,42,248,61,7,170,39,49,79,76,80,217,+ 72,236,98,200,49,53,5,48,186,156,236,92,120,21,49,61,222,3,134,206,+ 169,120,198,188,57,181,24,151,212,31,230,82,197,73,30,196,8,218,130,192,+ 74,71,132,180,175,98,0,36,225,73,112,128,234,209,146,191,245,18,202,107,+ 2,118,132,38,129,68,85,141,168,119,162,140,212,132,194,55,6,180,41,147,+ 49,163,17,129,68,157,177,120,64,206,39,230,184,125,147,126,229,74,28,163,+ 86,79,183,204,31,180,198,37,2,66,235,65,75,12,45,42,27,192,12,57,+ 10,15,232,225,129,30,80,37,114,0,200,68,22,148,252,37,64,151,191,136,+ 128,86,200,40,32,111,193,109,108,252,48,82,234,241,11,32,158,217,201,243,+ 245,153,191,153,198,211,138,28,72,181,63,144,168,47,145,20,113,144,215,101,+ 58,202,66,84,205,55,81,41,204,120,227,101,237,46,67,110,171,173,56,249,+ 103,101,119,79,203,123,243,151,228,144,116,252,71,19,82,57,98,217,150,168,+ 245,216,136,153,59,242,24,70,204,61,200,102,253,254,102,101,54,128,250,125,+ 150,150,158,55,32,182,153,111,76,248,238,75,23,45,221,185,250,211,121,125,+ 216,4,0,133,75,152,103,105,190,210,46,75,244,15,169,68,83,183,22,136,+ 101,139,199,210,134,73,214,54,77,49,33,237,186,138,214,200,228,186,217,62,+ 10,213,130,180,37,50,255,203,103,107,86,208,56,46,39,90,238,117,83,21,+ 122,246,78,132,206,143,155,239,224,156,125,20,226,201,133,180,97,185,14,6,+ 104,140,72,85,34,131,208,37,22,135,192,120,48,8,7,112,214,179,128,51,+ 15,73,238,167,21,173,173,132,24,66,146,162,28,151,14,210,189,150,56,101,+ 81,254,106,157,203,196,155,39,105,174,85,136,242,64,101,241,248,90,21,52,+ 42,53,233,230,202,38,71,60,198,102,226,52,232,98,48,138,44,195,183,153,+ 48,164,205,18,131,80,35,149,195,104,82,11,130,16,94,59,182,135,86,67,+ 227,80,101,115,184,183,106,253,77,86,56,195,62,197,120,149,31,233,210,61,+ 188,159,193,52,73,172,231,203,176,115,186,93,55,36,188,229,20,107,229,63,+ 146,135,36,121,122,76,212,50,231,85,164,78,47,168,16,154,149,24,150,29,+ 74,35,137,228,95,31,65,232,226,23,7,160,138,80,26,136,66,249,228,118,+ 67,88,177,46,91,235,156,243,164,145,57,66,108,122,151,42,62,93,251,255,+ 119,139,139,5,160,66,185,72,83,109,211,255,39,118,232,237,40,209,172,148,+ 154,149,198,152,77,164,236,85,75,35,137,1,21,238,128,150,127,72,138,65,+ 117,152,78,26,2,240,178,28,71,112,156,32,6,214,49,157,66,57,166,236,+ 109,107,93,175,21,41,90,91,155,165,24,83,228,128,209,89,255,248,101,24,+ 3,127,18,78,20,243,21,66,21,113,21,142,21,37,20,212,20,94,19,151,+ 230,189,142,198,248,8,131,173,48,211,174,62,35,50,125,116,128,240,91,150,+ 12,253,86,217,83,79,14,133,39,208,183,117,166,3,58,129,42,161,167,112,+ 5,39,61,170,125,131,130,192,195,5,55,147,205,195,47,45,46,204,197,93,+ 217,232,66,125,70,215,55,250,169,84,56,241,215,249,222,77,203,251,17,4,+ 135,214,203,37,59,55,127,49,246,223,19,170,244,222,130,52,141,174,225,17,+ 246,210,118,34,109,116,248,118,48,113,237,169,133,162,22,54,61,90,220,101,+ 217,137,4,245,21,135,57,30,101,132,22,226,151,134,211,2,39,43,149,114,+ 226,103,38,91,164,60,141,34,103,158,7,148,119,137,127,129,87,124,47,118,+ 223,117,119,53,221,118,161,31,123,158,31,196,253,118,255,9,68,11,165,17,+ 175,158,90,104,184,193,157,150,241,196,193,103,215,41,40,23,111,232,79,87,+ 152,210,232,53,67,43,52,247,225,178,16,89,103,195,96,45,234,78,102,53,+ 108,243,106,222,87,165,109,207,201,65,23,98,231,160,177,213,219,146,75,228,+ 183,181,126,46,72,197,160,95,155,133,161,121,132,50,248,101,188,110,5,144,+ 102,80,140,97,235,254,73,27,54,94,52,23,16,67,195,240,143,86,19,36,+ 36,183,232,132,192,98,226,71,103,19,5,45,89,161,165,168,78,151,105,34,+ 113,63,57,164,155,12,224,139,+};+static const int REF0[800] = {+ -373,168,-250,-607,-642,-88,-468,-235,-83,219,-283,224,422,495,+ 560,1070,1088,1104,326,-888,-1497,-2091,-4144,-4716,-3704,-688,916,1866,+ 1645,1380,609,1013,2131,3169,3728,3977,3168,1114,-434,-1952,-3295,-5966,+ -6093,-2314,507,3183,3135,426,-1213,-2473,-1974,-1500,466,2880,4412,5102,+ 4566,3366,816,232,50,-1273,-5654,-6474,-1254,718,2254,770,-1354,-4818,+ -5176,-1262,1828,4288,4420,3165,3068,3058,2686,2054,1402,1558,-2065,-5064,+ -7019,-1546,854,3854,966,-1954,-7088,-6886,-1123,2354,4920,3662,2650,2074,+ 3080,2994,3242,1875,500,-2695,-6492,-6246,-1082,1624,4431,426,-3119,-7288,+ -6906,-579,3810,5834,2806,1449,740,3182,5014,3396,1322,-836,-2456,-4835,+ -6615,-1254,1506,4518,152,-3888,-8364,-6014,175,3980,5604,2053,952,1010,+ 3908,5534,4424,1564,-1368,-2913,-5578,-5626,-1162,818,3260,-186,-3254,-6184,+ -7283,-238,3338,5927,2281,268,944,4406,6938,4216,1890,-1760,-2487,-4359,+ -5038,-2136,533,3370,-444,-2362,-6351,-6118,-1968,3074,5437,2598,796,1615,+ 3564,5542,4261,2334,-1024,-2351,-4680,-4810,-2332,151,3216,494,-1642,-5630,+ -5722,-2528,2888,4818,2740,1361,1398,3060,3820,3846,2723,682,-1326,-4018,+ -5052,-4096,-1096,1634,975,24,-3914,-5421,-4042,-172,3526,3830,2658,1624,+ 2060,2233,3090,1750,1218,583,464,-152,-1384,-1822,-2170,-2082,-2248,-4278,+ -5085,-3740,-710,2075,2311,2184,1306,1053,820,1752,1960,2807,2858,2955,+ 2322,1207,218,-648,-1008,-1103,-1704,-2628,-3138,-2296,-1598,-478,168,724,+ 976,1201,1200,1208,1247,1147,1041,870,465,205,34,182,116,184,+ 99,-32,-770,-54,-412,0,-266,694,286,3090,629,2041,675,1028,+ -1485,-2322,-2944,-3083,-1736,-1358,492,308,830,75,247,734,1426,1986,+ 1996,1946,338,-447,-2832,-4419,-7740,-5108,-2449,3819,3083,2033,180,-1740,+ -1178,-300,1752,1838,2409,2472,2888,3024,2183,-49,-2234,-5350,-8154,-4152,+ -399,5201,3168,649,-2832,-5230,-4492,-1498,2419,4430,4726,2374,3640,2452,+ 1711,1444,-748,-3032,-7916,-5182,-27,6811,4418,-10,-3740,-5932,-4580,294,+ 3785,5804,3802,2675,2825,2556,2040,1360,-1520,-6108,-7348,-2574,3192,6462,+ 2736,-2242,-3792,-4406,-3280,403,2031,5296,2560,2516,3477,2480,1785,-1055,+ -1734,-7642,-7165,973,6410,4344,-222,-3187,-3762,-2764,-1990,2292,1988,3136,+ 3636,2456,4277,904,64,-550,-6126,-7938,-3920,2006,7702,2152,-2587,-3358,+ -2122,-1842,-644,1267,3305,2288,3147,3748,1512,1161,-1402,-3580,-7409,-5798,+ 2726,7103,3438,-1974,-4183,-1832,-1235,-1817,602,585,3784,3922,3282,2634,+ 459,-294,-2350,-7426,-6626,327,6168,4266,-773,-3472,-1773,-584,-1406,-432,+ 543,3602,3189,3516,2390,-275,-556,-2193,-6743,-6904,626,5332,4368,-127,+ -3331,-777,292,-1074,-924,66,4110,4085,3375,1494,-1130,-834,-3688,-6048,+ -5770,-24,6374,2750,90,-2533,-638,2156,-1754,-1782,1101,4286,4842,1965,+ -1015,-1968,-1963,-5231,-5868,-530,3766,3045,829,-730,417,1530,610,-2254,+ -2021,2675,6339,3408,259,-2432,-3098,-4435,-5986,-632,2394,2172,1582,-626,+ 498,796,290,-865,-2845,2033,6064,3851,1630,-2308,-1554,-3545,-5132,-3778,+ -752,4680,1296,292,-602,126,1724,-1538,-1654,1248,4002,3940,1828,-625,+ -2074,-2261,-3963,-3783,-1626,2505,3205,747,1138,-1170,951,-1214,-1793,180,+ 2025,5512,1664,-1009,-1252,-2358,-1586,-3978,-1192,1036,2209,2565,-20,162,+ -1264,-1018,-668,-456,1440,3772,1758,7,-972,-433,56,-2490,-2478,-1394,+ 1145,2808,938,489,-678,-1328,-1396,-1580,104,2002,1864,1722,950,734,+ 724,-1266,-1546,-3224,-1409,716,270,1562,-696,230,91,-1520,-181,45,+ 1718,3185,1466,2578,-738,-1938,-2712,-4178,-1978,-606,900,1048,930,1092,+ 986,404,-34,-347,584,2164,1689,1442,-530,-2594,-3017,-2798,-1033,-196,+ 392,1710,1338,1872,730,-152,-334,-1180,490,1284,1891,1386,-56,-1350,+ -2560,-1972,-1669,-794,-99,994,1158,1316,992,4,-482,334,1230,1630,+ 1561,1106,-550,-2126,-2906,-2839,-2281,-1110,262,1347,1805,2218,1738,915,+ 560,872,934,923,356,-752,-2502,-2892,-2894,-2360,-1142,196,1508,1974,+ 2800,2466,1580,876,498,430,230,-142,-1160,-2516,-3091,-2658,-2310,-1017,+ 240,1580,2214,3072,2524,1954,808,621,470,144,-374,-1570,-2549,-3121,+ -2638,-2234,-1070,360,1609,2476,3192,2762,2118,1043,608,384,-148,-707,+ -1864,-2826,-3230,-2625,-2156,-854,394,1792,2738,3258,2868,2055,1114,597,+ 248,-258,-1022,-2158,-2806,-3124,-2436,-2042,-578,706,1958,2952,3290,2709,+ 1794,900,343,-60,-654,-1486,-2462,-2931,-2869,-2286,-1536,-109,1052,2232,+ 3054,3448,+};+static const unsigned char CLIP1[2240] = {+ 73,68,51,4,0,0,0,0,0,34,84,83,83,69,0,0,0,14,0,0,+ 3,76,97,118,102,54,49,46,55,46,49,48,51,0,0,0,0,0,0,0,+ 0,0,0,0,255,243,88,192,0,0,0,0,0,0,0,0,0,73,110,102,+ 111,0,0,0,15,0,0,0,14,0,0,8,148,0,37,37,37,37,37,37,+ 37,54,54,54,54,54,54,54,71,71,71,71,71,71,71,88,88,88,88,88,+ 88,88,104,104,104,104,104,104,104,121,121,121,121,121,121,121,138,138,138,138,+ 138,138,138,155,155,155,155,155,155,155,155,172,172,172,172,172,172,172,188,188,+ 188,188,188,188,188,205,205,205,205,205,205,205,222,222,222,222,222,222,222,239,+ 239,239,239,239,239,239,255,255,255,255,255,255,255,0,0,0,0,76,97,118,+ 99,54,49,46,49,57,0,0,0,0,0,0,0,0,0,0,0,0,36,4,+ 64,0,0,0,0,0,0,8,148,192,146,158,234,0,0,0,0,0,0,0,+ 0,0,0,0,255,243,72,196,0,24,208,190,128,5,76,24,0,1,169,70,+ 14,4,128,104,34,40,28,192,0,6,40,111,202,128,208,116,100,26,8,135,+ 142,82,139,22,56,45,221,226,0,8,41,215,115,247,0,64,64,16,4,1,+ 48,124,252,184,32,8,2,0,128,38,15,131,224,249,254,160,76,31,196,224,+ 128,32,8,2,0,152,62,15,131,255,254,39,7,193,192,64,16,193,0,64,+ 16,57,131,255,235,63,119,252,160,33,255,255,202,2,10,4,21,143,171,38,+ 78,150,10,166,44,72,20,93,112,1,102,171,23,209,78,7,159,28,12,76,+ 188,92,35,217,56,8,68,157,255,243,72,196,31,36,67,218,192,1,152,144,+ 0,64,33,243,17,156,28,242,73,140,67,130,80,230,162,30,37,139,113,199,+ 93,55,49,173,229,38,91,171,161,155,58,9,151,20,211,232,23,144,77,55,+ 77,60,192,180,145,163,167,31,78,79,152,164,104,138,117,53,70,151,93,141,+ 141,211,46,162,157,214,231,205,138,9,158,213,214,239,161,218,203,169,217,116,+ 27,73,145,162,143,255,250,23,246,102,116,153,4,221,151,212,189,5,178,79,+ 245,33,79,255,244,54,107,255,230,203,62,163,207,135,168,160,11,248,57,171,+ 238,207,166,228,105,157,17,162,38,151,182,22,255,243,72,196,17,31,186,174,+ 196,203,152,104,0,125,114,209,112,182,176,229,120,184,29,69,48,192,4,24,+ 95,12,70,104,23,132,200,186,22,6,99,148,225,155,187,164,196,246,166,145,+ 173,84,80,82,110,229,150,117,158,169,36,152,246,250,137,200,25,144,157,54,+ 50,62,164,157,145,173,205,16,65,53,81,102,58,182,69,39,117,178,206,37,+ 77,122,41,233,182,189,181,45,213,181,85,46,141,55,67,253,111,171,247,70,+ 113,22,55,220,191,229,152,68,180,245,126,33,113,215,82,188,0,0,163,238,+ 224,139,175,195,78,177,59,76,121,66,24,175,19,181,72,90,255,243,72,196,+ 21,33,122,78,213,149,143,104,0,149,68,253,120,54,85,103,105,78,51,12,+ 1,242,72,6,153,44,98,5,194,2,102,235,53,32,148,198,147,162,16,249,+ 137,9,211,100,25,68,231,56,105,43,52,82,107,118,48,76,201,102,136,153,+ 32,113,7,99,37,209,37,74,8,26,86,132,197,148,130,234,93,69,53,59,+ 42,199,212,155,156,101,32,165,35,82,141,92,238,130,153,6,109,170,173,169,+ 50,62,245,86,163,142,206,4,52,26,249,32,197,189,32,233,187,246,127,255,+ 255,215,132,7,18,0,9,224,185,174,139,83,103,188,112,186,66,60,75,164,+ 255,243,72,196,18,33,242,198,210,247,216,64,1,185,171,238,172,180,148,22,+ 46,103,250,149,83,43,217,127,59,176,54,5,64,68,234,152,26,42,135,142,+ 183,82,78,18,199,252,11,17,60,242,74,219,118,205,99,127,255,251,211,46,+ 181,117,38,39,133,248,249,75,238,35,105,110,150,175,159,104,152,153,189,101,+ 163,159,191,185,254,34,126,248,190,95,230,134,66,69,43,33,87,252,85,15,+ 128,220,80,248,39,224,177,85,68,72,220,234,252,116,231,144,82,203,57,79,+ 31,213,161,62,212,121,21,120,239,191,237,212,32,16,44,192,82,67,45,212,+ 141,225,244,189,255,243,72,196,13,32,131,182,190,62,194,4,220,11,12,20,+ 181,4,97,181,185,77,213,226,210,62,45,133,170,193,208,68,48,122,47,139,+ 3,99,175,185,162,142,114,140,255,229,136,49,10,174,224,238,255,133,105,90,+ 255,187,230,14,82,135,171,88,132,31,78,215,80,205,214,206,107,97,158,101,+ 46,101,43,204,99,59,34,169,80,197,67,57,92,182,244,167,230,187,116,114,+ 133,67,54,99,244,67,125,81,203,49,189,12,230,63,255,243,43,77,55,86,+ 205,43,85,140,4,20,74,66,64,209,212,135,105,232,140,36,178,113,216,228,+ 146,203,106,87,47,230,60,237,255,243,72,196,14,32,155,190,198,94,194,68,+ 211,56,113,6,147,44,238,31,148,117,63,80,180,80,136,148,254,195,31,208,+ 12,220,88,205,132,139,142,85,78,242,220,187,183,215,122,137,12,193,88,54,+ 106,174,89,212,147,144,87,101,72,97,79,50,76,236,229,148,215,40,146,144,+ 202,236,87,84,187,51,171,174,107,202,202,200,107,233,250,82,79,233,237,190,+ 25,212,8,251,69,24,174,231,76,236,82,175,78,68,172,147,55,175,245,110,+ 223,174,204,219,57,204,37,133,14,240,142,19,143,53,234,235,1,10,79,245,+ 92,98,88,13,224,22,161,10,135,39,182,196,255,243,72,196,14,30,76,22,+ 168,10,120,83,60,109,202,243,85,223,174,61,119,111,156,125,99,231,63,25,+ 5,20,171,59,209,81,79,202,179,74,255,250,242,243,223,219,248,255,62,72,+ 188,151,47,167,2,224,199,49,140,148,121,46,123,255,132,231,176,168,111,223,+ 57,194,23,115,156,253,65,71,101,32,135,171,159,64,155,112,168,126,187,19,+ 213,24,245,11,65,29,202,182,245,0,173,177,0,32,72,226,49,90,52,10,+ 134,234,105,10,50,108,17,188,128,194,61,167,85,252,172,10,44,2,110,114,+ 196,42,123,154,86,33,15,11,167,78,167,250,125,63,255,183,255,243,72,196,+ 23,26,60,22,188,0,24,147,92,110,141,87,84,165,17,168,158,127,79,219,+ 223,255,244,233,255,233,219,147,167,125,106,238,102,158,179,18,103,41,36,189,+ 175,49,94,168,85,5,69,50,127,248,179,59,56,198,212,184,163,131,174,52,+ 233,54,40,215,170,133,184,98,23,134,19,62,125,68,67,105,39,55,146,82,+ 23,52,203,251,160,103,196,251,61,188,70,120,137,119,135,135,107,101,182,0,+ 35,82,152,147,162,250,47,32,40,19,177,64,240,213,57,34,76,34,210,168,+ 230,217,120,170,96,133,13,148,193,64,6,66,97,140,48,106,75,1,22,61,+ 255,243,72,196,49,28,233,227,15,30,214,68,122,62,1,98,28,82,209,65,+ 26,44,33,145,34,166,56,199,0,76,221,171,67,81,139,233,110,156,112,37,+ 252,117,186,242,186,50,91,32,24,226,58,16,128,97,24,140,112,225,199,87,+ 165,191,111,175,223,253,189,24,250,152,231,8,18,81,107,62,175,245,87,69,+ 113,118,28,40,205,201,165,50,213,37,121,245,84,98,39,25,42,37,15,154,+ 118,225,134,70,140,137,213,61,216,220,48,210,214,16,226,166,142,233,83,253,+ 206,21,128,40,82,239,110,29,15,37,149,171,58,130,124,189,65,10,170,77,+ 97,247,227,137,255,243,72,196,64,27,241,18,202,86,195,6,228,17,1,64,+ 161,39,177,110,59,173,209,87,34,12,174,18,18,172,194,1,163,193,95,127,+ 22,101,181,157,229,135,21,119,215,207,84,248,118,137,234,135,130,175,252,241,+ 86,191,248,106,85,102,52,219,150,34,254,212,151,46,213,22,12,176,137,162,+ 221,112,158,53,114,86,42,143,0,57,95,18,88,46,129,18,161,108,229,134,+ 87,209,57,120,250,53,71,43,94,132,246,161,72,117,146,148,189,85,1,140,+ 155,217,179,100,204,97,122,84,135,58,28,5,9,52,75,122,6,140,17,60,+ 236,17,164,20,29,17,185,202,255,243,72,196,83,28,249,54,132,30,195,6,+ 116,31,118,251,16,84,203,118,83,150,183,162,245,180,121,36,109,177,192,85,+ 26,44,169,36,86,218,0,170,125,124,244,94,116,74,10,224,40,178,241,64,+ 42,68,131,5,53,43,63,63,60,29,48,192,89,211,44,165,98,191,56,68,+ 144,83,169,60,91,142,162,220,182,55,73,106,135,48,155,220,6,241,122,211,+ 236,169,78,153,152,144,229,21,222,189,125,111,27,82,98,171,0,154,128,137,+ 59,179,51,26,210,218,170,169,70,102,61,85,91,255,255,253,181,47,206,170,+ 168,81,35,225,175,149,146,88,42,118,68,74,255,243,72,196,98,27,89,214,+ 68,10,203,198,148,118,175,255,250,191,231,131,191,195,191,242,85,50,57,58,+ 74,132,156,224,252,212,69,16,131,145,80,154,52,14,98,64,238,63,9,35,+ 144,242,39,16,199,130,89,49,17,242,212,200,104,77,172,126,55,90,102,8,+ 88,188,78,174,101,168,96,109,135,222,117,166,65,3,8,114,51,255,255,98,+ 152,178,199,67,101,12,12,16,48,76,48,254,161,97,102,252,42,42,42,42,+ 99,254,42,42,42,144,40,168,168,171,60,36,44,44,44,109,76,65,77,69,+ 51,46,49,48,48,85,85,85,85,85,85,85,85,85,85,85,255,243,72,196,+ 119,24,177,129,8,0,96,135,100,85,85,85,85,85,85,85,85,85,85,85,+ 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,+ 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,+ 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,+ 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,+ 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,+ 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,+};+static const int REF1[1600] = {+ -799,-379,-93,-544,-1133,-710,-815,-303,-165,-265,6,752,825,710,+ 1086,1805,2244,2392,882,-1402,-2326,-4534,-8044,-9858,-7515,-1163,2111,3725,+ 3627,2619,981,2219,4583,5774,7552,8232,6108,2010,-1144,-3851,-6702,-11752,+ -12788,-5741,-478,4248,5206,1935,-713,-1966,590,2669,5228,6522,6689,8036,+ 6747,5433,284,-681,-2671,-6395,-14705,-15865,-6002,816,4562,3841,1302,-1962,+ -2139,683,3526,4917,5404,4501,5804,6884,5013,2034,-440,-788,-8481,-13871,+ -16825,-6001,1378,7919,3918,-131,-4242,-2844,1706,4270,6337,4762,4417,4678,+ 7160,5360,4431,558,-2232,-8180,-15519,-16073,-4911,1893,8661,1408,-685,-4672,+ -2677,2579,6632,6587,2922,2646,3176,7376,8478,4344,-1126,-4454,-7580,-11621,+ -16928,-5455,382,8889,1419,-2538,-5909,-2155,2772,5866,6089,3358,2545,5247,+ 9267,8739,4265,-614,-4373,-7508,-13614,-15712,-7704,-308,7909,2962,-1297,-4478,+ -4059,1974,5892,5914,3323,1553,5396,9877,11116,5025,-232,-5203,-6834,-12012,+ -15587,-9436,-1289,7700,2002,153,-4110,-2696,4,6714,5932,4367,2540,5879,+ 8991,9265,4556,-20,-4101,-5585,-12840,-15539,-10901,-2294,5894,3546,710,-3904,+ -1785,918,6571,4787,4353,2751,5047,7387,7132,3592,1471,-362,-3354,-11067,+ -15217,-13176,-4920,3106,3793,3974,35,-1627,-2118,844,2360,4723,4114,5694,+ 5349,4103,2500,584,-168,144,-1753,-4774,-7206,-5420,-5081,-3108,-1844,-1129,+ -440,667,920,1131,1610,1685,2384,2811,2612,3028,2262,2582,2717,2203,+ 1706,360,-639,-1720,-1878,-2318,-2150,-1829,-1766,-996,-594,911,1744,2018,+ 2148,1958,1834,1487,1262,1378,489,5,-697,-1315,-1315,-1446,-1259,-1304,+ -555,-206,-472,381,-535,263,569,2537,2240,6708,1133,2949,-455,219,+ -3622,-5486,-6950,-6666,-4441,-3559,738,704,2358,915,1141,1780,3484,4533,+ 4373,3812,1004,-1336,-5816,-9277,-14715,-11934,-5952,2927,4308,2038,-212,-3209,+ -67,3200,7317,8415,7628,7159,5071,5066,2414,-1081,-6022,-11709,-17602,-10322,+ -3070,7073,5220,2084,-2751,-4403,871,2964,7547,6647,6102,4039,6681,6303,+ 2767,1459,-5494,-9856,-19072,-14392,-3724,9015,6675,1187,-3512,-4156,2080,5704,+ 7724,7162,4464,5546,6923,8157,3040,-1327,-7788,-15532,-17487,-6946,1200,8633,+ 3576,-4738,-3878,-1278,5114,8842,5716,4395,2526,5451,9854,7011,1621,-6609,+ -9224,-18699,-16234,-558,7833,3663,-1758,-6599,-2828,5503,7306,8388,1363,957,+ 5708,7827,10319,2690,-3445,-6734,-14824,-17407,-8017,1700,8996,724,-5637,-2051,+ 1021,6793,7778,2564,1058,2318,7223,10519,4736,-1422,-6496,-11061,-14513,-11250,+ 2206,8136,799,-4202,-5828,-1154,6494,7353,6502,378,1506,7897,9639,6348,+ 356,-5008,-9400,-16039,-12928,-350,7525,2197,-4180,-5437,218,5753,6148,5942,+ 127,2481,6138,9027,8038,-1008,-5501,-7269,-15744,-12980,-1159,5513,2036,-4559,+ -5990,1026,6590,7068,5075,1172,4168,7724,8262,5877,-2442,-5423,-10409,-14858,+ -10481,-243,7790,-623,-5129,-4119,2694,8909,6523,3616,2321,5663,7764,6209,+ 1585,-4745,-8094,-14717,-14036,-1536,5383,1347,-3738,-4356,2480,7205,7484,4118,+ 1661,6594,7114,6956,2475,-2275,-7372,-14659,-14800,-4444,3694,2027,-2084,-5182,+ 457,4841,5098,7735,3359,4882,8073,7347,4623,-1907,-5456,-10522,-14284,-10531,+ -3851,5270,-712,-2982,-2533,1719,8472,6021,5295,3922,5632,7368,4926,455,+ -4844,-7452,-11235,-10925,-5517,1835,1290,-1626,-677,-858,5873,4730,5208,4750,+ 5919,7523,4371,277,-1345,-5114,-8111,-10876,-5917,-1156,838,455,-2727,-123,+ 379,3104,5057,5680,5557,5889,3075,991,-1660,-401,-2469,-7536,-7778,-5627,+ -1083,1344,-1018,1165,-207,1226,2651,3206,3146,3636,3099,3330,3315,1534,+ 1116,-4481,-6055,-9588,-5818,-2002,-809,2395,-497,969,1253,1946,3824,3063,+ 4051,5524,3675,4179,-1561,-3752,-6709,-10248,-5928,-3208,-215,311,-124,1111,+ 1203,3390,3784,4864,4718,5309,3852,1767,-1262,-4735,-7024,-6173,-4422,-3091,+ -1726,-347,827,1137,1753,2777,3280,3719,4085,4106,4224,2818,21,-3276,+ -5607,-6093,-5793,-4584,-3109,-1019,-205,1527,2364,2886,3269,4969,5073,4702,+ 4007,1765,-1183,-4892,-6681,-7328,-6561,-4290,-1926,-66,899,2638,3302,3674,+ 4858,5411,5350,4628,2474,-928,-4722,-6405,-7182,-6629,-4432,-2463,-184,1275,+ 2748,3113,3969,4873,5138,5183,4454,2200,-889,-4820,-6970,-6641,-5995,-4297,+ -2479,-373,966,3337,3335,4316,4398,5042,5170,4031,1644,-1595,-4863,-6322,+ -6764,-6128,-4464,-1928,-152,1552,3464,3971,4695,4909,4935,4678,3492,1034,+ -2050,-5468,-6856,-6761,-6044,-4176,-1879,265,2086,3545,4510,4916,4976,5082,+ 4572,3262,420,-2957,-5416,-6916,-6632,-6069,-3702,-1420,634,2734,4119,4572,+ 4603,4682,4619,4038,2448,-429,-3639,-5826,-6759,-6504,-5444,-2997,-891,1384,+ 3181,4624,4667,4924,4944,4705,3699,1794,-876,-3893,-5816,-6746,-6720,-5570,+ -3547,-1339,999,2812,4119,4758,5119,5193,4899,3756,1990,-370,-2997,-4841,+ -5809,-6203,-5250,-4100,-2221,-145,1513,2814,3722,4340,4815,4763,4118,2754,+ 890,-963,-2951,-4115,-4886,-4949,-4174,-2855,-1352,83,1283,2672,3396,3765,+ 3873,3269,2672,1324,253,-886,-1918,-2801,-3217,-3020,-2899,-2321,-1084,29,+ 788,1712,2161,2308,2212,2145,1564,979,276,-426,-818,-1120,-2081,-1806,+ -1649,-1369,-856,-464,-190,361,680,1114,1147,1137,835,1004,536,27,+ -495,-776,-690,-758,-540,-781,-887,-1184,-439,-600,-359,146,2,655,+ 1016,-12,1121,1333,931,386,206,277,-265,-471,-223,-933,-1141,-593,+ -540,-299,-224,179,430,-114,606,887,678,177,444,305,-61,-95,+ -367,-497,-50,16,40,-477,-150,-476,-161,50,595,91,489,822,+ 555,-29,-424,-84,-155,-39,542,-1106,-120,-1020,-373,-365,172,-64,+ -233,208,554,-210,27,-64,805,665,-392,392,532,253,-940,-578,+ -837,-166,974,-789,-728,968,-387,-558,875,-530,-627,1707,-474,-185,+ -316,916,-501,-767,-337,253,-235,-347,629,710,230,-1128,1356,-28,+ 78,209,-1130,372,71,-936,557,-1328,14,-152,278,851,275,-685,+ 715,1484,-960,147,720,-2247,1844,-635,-618,813,-152,-868,-618,-510,+ 1935,-862,-1667,3052,260,-426,1062,-1130,1084,396,-987,-1553,1728,-852,+ -917,-1298,452,54,-2110,1335,-637,1552,-666,2480,-2467,-393,2933,-881,+ -1470,-348,-556,1542,-243,-1637,1214,-1376,1043,599,-1480,-2150,4303,-1071,+ -1264,-431,407,-1265,1364,-1024,706,-224,-3583,3292,-646,1618,-721,-820,+ 2073,-295,335,-2290,306,192,1479,-1243,667,-1785,1830,-956,-1428,3596,+ -2086,-1360,2425,-2872,291,576,267,-393,-2123,1236,-592,-226,1541,-1116,+ 1363,-1205,-503,1256,1727,-518,177,525,87,-485,782,671,-1041,1456,+ -217,645,625,-579,-2311,-960,208,1268,438,283,-373,336,155,-580,+ -118,-75,1007,-66,-1098,-367,933,-696,1225,43,257,32,330,-781,+ 636,115,201,789,-392,-505,-1133,922,-849,1520,-98,673,726,320,+ -511,1006,-22,91,-944,223,-1049,-248,120,-643,-715,49,-275,2674,+ 1834,2474,1521,2608,2966,494,-633,-902,-4522,-5139,-9326,-7992,-1776,3090,+ 5434,4351,2390,4894,5325,7400,7033,5938,1706,-316,-3481,-7045,-16337,-13514,+ -2973,4957,6362,1407,-851,-1582,3723,6523,8347,8011,7445,4440,-497,-3371,+ -4798,-8520,-16262,-12733,-1572,5387,5128,-1430,-576,-20,4611,5879,7223,6339,+ 7027,3862,2186,-2564,-3653,-9076,-17334,-13536,382,6883,6722,-2834,-2121,-1039,+ 6238,7218,7740,3918,5319,3156,4599,-1050,-1982,-8782,-16460,-16434,-34,7460,+ 7753,-4054,-4419,-3001,6490,8491,8648,3254,4402,3444,5008,1910,-2013,-5526,+ -15898,-18396,-6739,6009,9026,561,-5158,-3724,1990,7366,8975,6444,3971,3867,+ 3683,4575,19,-3039,-10337,-18795,-13014,1656,7665,7009,-3534,-3800,-2492,5032,+ 8666,8329,3468,2969,3932,5029,4289,-983,-3494,-13624,-18759,-11060,4246,7306,+ 3793,-4907,-4343,-11,6529,9000,6780,4089,3236,4752,5969,4316,-2600,-5161,+ -12426,-17082,-11872,2025,6644,4540,-2574,-3299,-902,4009,8257,6971,5585,2799,+ 3817,3998,3975,-499,-4612,-8757,-14721,-13860,-3697,2973,3965,1851,-624,504,+ 963,4276,5048,5409,4323,3531,4132,2634,-819,-3685,-3435,-4873,-6220,-5556,+ -4105,-3931,-2883,-541,94,1156,1815,2465,2554,3102,3350,4202,4033,3023,+ 2301,778,-702,-289,-840,-1908,-3001,-2948,-2672,-2242,-1744,-1026,-656,487,+ 1525,2671,2179,1785,1231,905,541,-148,-730,-41,-118,-584,-1174,-1093,+ -506,-508,205,429,-100,458,292,321,-248,-260,-568,-116,-796,-1046,+ -235,-93,477,391,136,-104,8,-94,-329,-79,548,646,486,267,+ -415,-376,-674,-612,-355,-220,-156,-54,514,705,-69,-97,21,256,+ -55,231,188,53,-8,-220,-557,-506,-315,-32,-232,51,-47,87,+ 669,575,234,-230,-194,-81,93,122,10,-211,-393,-425,-454,-217,+ -149,227,166,252,356,636,457,209,-5,-142,-84,-78,-33,-112,+ -171,-339,-272,-120,16,70,71,10,25,103,227,236,126,-28,+ -55,-11,12,-59,-214,-185,-114,-61,-111,-2,204,194,122,111,+ 194,182,140,-56,-188,-238,-261,-299,-367,-358,-234,-153,20,49,+ 142,242,220,267,317,450,336,131,46,-20,-81,-202,-276,-374,+ -341,-220,-126,-45,12,79,159,213,392,391,265,175,116,54,+ -45,-124,-216,-203,-186,-243,-322,-231,-99,-26,17,85,160,199,+ 159,134,123,82,+};+static const unsigned char CLIP2[4432] = {+ 73,68,51,4,0,0,0,0,0,34,84,83,83,69,0,0,0,14,0,0,+ 3,76,97,118,102,54,49,46,55,46,49,48,51,0,0,0,0,0,0,0,+ 0,0,0,0,255,251,112,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 73,110,102,111,0,0,0,15,0,0,0,13,0,0,17,36,0,36,36,36,+ 36,36,36,36,54,54,54,54,54,54,54,54,73,73,73,73,73,73,73,73,+ 91,91,91,91,91,91,91,109,109,109,109,109,109,109,109,128,128,128,128,128,+ 128,128,128,146,146,146,146,146,146,146,164,164,164,164,164,164,164,164,182,182,+ 182,182,182,182,182,182,201,201,201,201,201,201,201,219,219,219,219,219,219,219,+ 219,237,237,237,237,237,237,237,237,255,255,255,255,255,255,255,0,0,0,0,+ 76,97,118,99,54,49,46,49,57,0,0,0,0,0,0,0,0,0,0,0,+ 0,36,4,146,0,0,0,0,0,0,17,36,158,6,59,255,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,251,112,+ 68,0,0,1,139,0,84,213,4,96,4,61,66,170,23,160,140,0,137,120,+ 191,86,25,148,128,9,40,14,171,115,48,96,1,1,9,20,130,66,114,64,+ 193,112,124,249,115,239,96,32,31,203,131,128,128,32,80,16,62,15,135,255,+ 4,63,255,224,255,46,255,255,7,193,252,8,8,2,24,62,15,190,129,1,+ 205,253,18,64,0,64,50,0,99,24,199,140,113,140,111,119,226,34,33,123,+ 187,241,16,88,24,24,24,24,24,124,70,15,131,224,248,124,184,62,15,131,+ 224,224,32,16,4,14,120,144,16,168,16,7,207,255,4,60,191,255,191,160,+ 49,193,240,249,40,96,224,193,62,130,142,17,50,104,240,231,192,0,103,218,+ 192,52,82,202,212,172,232,221,100,146,120,100,228,34,43,27,168,193,68,102,+ 135,96,206,201,24,201,102,201,6,128,41,171,188,141,228,204,98,105,196,243,+ 55,89,44,247,59,175,123,243,45,201,239,255,239,207,63,149,201,219,132,220,+ 143,131,105,148,8,109,54,50,48,0,4,50,177,31,202,36,38,3,137,34,+ 152,127,45,58,142,93,123,147,22,226,245,0,143,40,14,176,155,156,0,68,+ 182,250,28,74,64,195,0,197,23,81,57,46,87,87,246,130,92,178,96,101,+ 198,62,38,214,217,95,138,241,187,109,203,59,255,170,137,38,239,182,181,35,+ 144,1,133,80,185,161,232,139,81,67,255,251,114,68,8,0,2,44,42,96,+ 239,49,0,14,68,133,74,201,236,32,0,72,132,171,101,236,24,110,129,18,+ 149,171,181,135,140,241,4,136,240,83,206,7,194,80,151,2,17,164,144,130,+ 57,130,8,32,17,4,196,233,119,183,72,209,250,118,211,42,197,12,34,234,+ 175,254,230,104,99,209,5,141,220,119,155,152,130,235,171,120,178,186,255,196,+ 8,248,36,88,1,173,53,227,138,215,107,4,38,123,250,132,196,113,67,229,+ 249,213,70,254,203,98,244,147,138,129,250,17,46,122,7,98,57,3,183,204,+ 62,132,29,1,1,245,94,159,75,109,90,59,245,255,82,201,87,45,105,81,+ 221,15,161,163,43,156,211,167,128,0,4,56,80,0,19,15,128,35,41,144,+ 32,47,177,164,176,172,135,59,42,200,239,71,211,91,39,162,27,180,215,143,+ 38,51,227,155,205,91,3,227,47,120,108,12,17,35,85,41,199,206,91,21,+ 45,250,169,115,154,27,27,108,94,144,64,202,18,9,250,13,112,2,7,164,+ 72,4,62,0,113,92,19,32,61,68,137,133,177,210,204,115,154,184,41,42,+ 216,196,200,113,246,201,232,246,140,143,152,20,147,207,232,130,98,49,196,40,+ 43,189,47,239,120,172,103,223,235,79,70,213,95,118,45,118,14,12,65,86,+ 31,207,119,141,90,192,36,148,104,173,12,0,67,129,246,43,218,177,180,93,+ 192,46,85,135,255,251,112,68,9,128,2,21,46,91,225,230,28,28,71,5,+ 91,13,61,35,116,200,212,123,91,181,131,0,9,27,150,235,118,176,96,0,+ 209,105,162,183,183,58,162,196,20,157,50,159,117,200,121,69,241,49,41,142,+ 48,120,17,126,211,93,173,100,24,25,148,111,44,202,58,231,234,38,219,37,+ 38,51,152,51,31,57,180,140,0,16,170,64,132,138,128,3,124,33,64,23,+ 236,83,8,169,68,234,56,50,208,56,6,255,61,136,35,41,126,136,211,168,+ 221,141,85,30,113,94,162,16,29,83,54,12,226,188,143,55,135,194,6,68,+ 50,63,219,108,203,85,48,97,182,126,49,4,172,196,23,191,253,148,211,0,+ 24,236,96,2,209,192,5,142,241,158,138,199,11,170,138,37,253,71,24,114,+ 219,201,53,46,151,74,101,146,186,34,50,10,40,153,135,229,26,80,9,42,+ 170,244,206,171,34,139,109,55,157,236,218,200,176,28,105,206,234,3,126,254,+ 42,89,162,2,229,253,55,174,252,88,0,3,83,4,22,87,0,96,132,240,+ 161,36,69,83,38,124,88,186,50,213,21,129,46,190,217,218,150,207,197,120,+ 116,75,49,58,93,155,196,46,13,188,171,155,165,163,245,183,211,124,159,170,+ 56,167,114,155,189,59,255,45,181,219,103,248,175,179,247,255,70,210,178,244,+ 213,65,19,120,198,140,192,145,244,22,100,166,243,102,140,147,50,255,251,114,+ 68,8,0,2,47,36,214,6,101,224,0,65,132,170,96,204,188,0,8,152,+ 113,92,25,150,0,1,26,14,42,131,50,240,0,154,159,246,229,142,229,39,+ 187,217,95,183,248,119,124,37,212,14,71,139,133,227,223,106,184,110,109,151,+ 211,109,175,245,174,229,103,246,96,137,53,97,68,165,53,255,151,231,91,172,+ 96,8,183,149,47,140,58,101,210,10,20,128,178,133,154,82,87,93,184,198,+ 182,191,117,21,165,230,185,134,33,111,21,126,251,58,146,253,248,164,55,176,+ 226,251,241,63,103,123,30,38,98,226,20,106,231,227,168,228,136,253,179,26,+ 251,250,255,255,220,31,144,84,183,192,127,253,33,118,194,135,138,222,20,116,+ 168,216,20,224,33,172,228,97,168,108,217,21,147,60,247,225,136,178,185,148,+ 45,100,229,211,212,209,155,22,75,194,194,113,216,116,92,163,138,29,81,101,+ 174,8,234,202,15,69,93,202,183,243,74,22,135,157,202,142,223,180,199,126,+ 148,133,138,3,14,96,56,104,170,59,177,132,156,134,45,43,95,224,87,65,+ 202,110,51,4,89,114,97,253,67,122,175,149,94,190,59,85,179,46,149,15,+ 207,245,204,86,214,11,77,182,203,198,108,123,25,246,179,29,154,38,252,57,+ 230,17,146,23,36,227,126,47,43,66,17,98,164,0,83,76,133,86,1,49,+ 0,114,9,130,228,248,189,193,61,78,10,255,251,112,68,9,128,2,46,46,+ 92,103,60,192,12,70,3,203,157,231,152,1,200,116,175,119,167,152,112,249,+ 3,149,173,112,244,10,31,35,86,172,73,247,13,98,13,41,208,56,193,248,+ 237,5,224,160,222,52,184,218,111,159,144,198,204,200,243,249,220,244,171,239,+ 215,250,206,240,253,245,155,31,42,25,149,15,172,173,57,135,77,166,34,194,+ 117,180,147,109,192,1,110,17,224,187,56,91,75,211,56,229,77,212,223,202,+ 232,203,193,248,150,165,75,166,242,155,230,48,122,16,48,160,183,208,70,60,+ 196,238,168,113,174,237,155,239,205,177,198,15,227,56,227,239,190,176,54,0,+ 244,179,62,167,248,29,58,91,35,65,110,52,82,105,192,1,115,1,64,24,+ 70,37,203,97,125,30,147,235,105,136,213,47,19,233,150,217,74,68,140,174,+ 74,63,102,85,7,205,168,194,10,56,103,169,160,207,252,223,143,77,101,205,+ 172,255,135,63,182,35,8,212,87,96,198,149,230,242,122,240,128,11,109,37,+ 102,192,176,179,129,85,47,83,19,73,51,26,168,212,188,193,10,116,196,143,+ 218,70,109,60,153,31,179,13,83,6,0,91,38,11,38,84,81,25,228,99,+ 223,191,85,28,167,115,37,207,189,91,201,204,200,80,130,184,77,222,188,162,+ 106,160,16,11,68,0,66,124,0,203,146,164,67,148,161,233,110,220,198,236,+ 9,75,168,214,255,251,114,68,11,129,2,73,23,215,235,9,51,178,70,36,+ 171,44,97,131,77,200,120,171,97,140,36,110,201,20,15,107,117,132,141,161,+ 132,135,109,165,50,125,186,82,251,58,99,21,241,67,189,120,7,65,134,73,+ 82,138,32,199,38,173,155,44,169,35,9,122,221,116,30,114,135,134,133,2,+ 113,152,205,69,5,132,163,117,126,10,80,8,5,36,65,87,1,176,130,72,+ 153,16,172,24,158,108,37,92,90,72,17,190,52,168,226,149,16,203,20,106,+ 247,90,252,231,88,231,74,215,126,172,104,6,56,55,222,168,167,118,89,115,+ 96,241,249,39,155,215,3,113,146,212,131,7,92,12,133,205,177,254,24,250,+ 248,8,25,146,0,203,148,42,17,141,186,249,163,203,55,130,208,98,138,13,+ 93,143,239,19,137,231,128,34,252,236,96,196,102,255,26,17,160,31,34,137,+ 137,192,135,1,74,146,179,42,95,191,200,22,58,70,62,145,249,95,133,105,+ 200,56,39,38,214,45,238,219,192,176,5,72,0,145,88,1,13,142,0,119,+ 74,242,157,1,168,150,238,149,87,150,107,42,159,23,151,131,202,114,209,164,+ 165,221,90,211,98,96,73,51,109,61,97,65,16,230,202,21,130,36,164,86,+ 25,144,72,39,192,42,48,230,119,177,23,15,113,111,59,249,252,128,16,20,+ 68,2,136,52,0,176,40,201,40,170,122,31,196,25,116,92,107,99,255,251,+ 112,68,10,128,2,28,43,216,105,233,19,180,65,69,90,204,97,2,106,72,+ 36,177,99,76,12,207,217,6,21,237,116,244,21,174,165,80,93,97,39,204,+ 45,248,243,222,107,213,218,78,25,51,54,46,249,130,182,49,73,22,200,202,+ 153,148,172,6,12,172,140,223,235,154,201,29,198,16,1,36,207,34,42,241,+ 106,2,1,182,64,97,0,64,205,53,62,136,155,52,66,69,238,214,211,74,+ 17,68,174,105,29,135,20,88,73,27,138,148,235,26,73,96,232,64,2,165,+ 30,98,37,178,51,29,82,112,99,85,168,143,107,170,107,235,213,44,146,85,+ 142,40,153,77,103,111,4,255,128,154,78,128,40,94,162,66,43,233,214,22,+ 193,105,160,43,211,205,146,102,204,22,247,255,161,134,11,49,215,213,0,128,+ 143,239,66,179,58,30,85,51,45,142,236,234,109,255,175,249,37,139,134,48,+ 170,6,168,57,18,181,160,91,119,218,41,73,105,203,25,41,176,96,1,14,+ 13,144,167,112,230,201,46,59,73,107,130,153,84,180,166,115,0,247,90,146,+ 100,13,99,190,152,240,154,239,170,145,181,82,214,208,238,204,238,210,188,2,+ 28,119,46,175,255,149,209,92,168,52,60,191,40,73,167,132,181,64,1,27,+ 96,4,12,4,189,213,6,238,55,33,225,3,6,86,86,109,137,244,126,97,+ 136,252,243,97,92,150,105,64,187,75,249,255,251,114,68,17,1,1,230,30,+ 211,99,73,27,160,61,131,202,157,101,131,60,7,88,67,73,109,229,130,128,+ 239,21,169,177,164,9,117,92,141,2,12,148,136,178,194,53,139,157,218,146,+ 206,222,127,220,84,51,244,82,176,42,129,71,118,171,2,11,177,160,19,69,+ 96,3,74,97,227,22,151,174,29,78,85,47,188,11,12,96,32,147,86,131,+ 161,245,216,255,170,222,175,79,218,3,47,86,246,240,98,99,49,213,190,199,+ 252,240,170,33,254,131,101,69,10,149,9,11,185,42,230,0,0,70,16,4,+ 126,157,208,74,193,223,134,175,225,17,162,100,61,180,105,242,106,56,131,152,+ 156,105,250,26,187,48,222,88,203,157,56,212,111,224,148,32,3,50,208,152,+ 238,46,166,131,110,183,203,54,192,96,232,242,29,72,96,129,161,104,12,150,+ 5,59,50,36,138,161,215,86,216,209,107,249,58,180,143,200,45,96,116,41,+ 225,235,45,108,85,64,133,180,172,204,110,181,85,26,73,83,213,117,172,100,+ 51,255,247,165,178,24,129,78,13,71,239,141,72,61,194,167,245,181,181,35,+ 104,0,152,212,56,11,195,56,146,136,13,27,209,198,232,228,58,94,167,201,+ 243,184,172,87,221,236,31,212,155,142,69,18,100,95,239,27,170,94,89,46,+ 41,107,109,238,202,175,109,72,46,13,102,222,29,255,168,18,118,182,213,117,+ 32,76,163,49,2,255,251,112,68,35,128,1,228,30,89,234,15,24,238,60,+ 34,10,204,97,134,97,199,140,203,69,45,132,113,192,237,10,231,21,204,164,+ 72,70,177,21,67,187,48,179,37,84,122,103,10,234,237,40,189,222,6,206,+ 249,66,225,183,27,85,75,147,104,181,152,179,66,241,183,65,14,254,123,121,+ 174,84,83,130,161,86,133,53,221,255,32,68,242,0,1,129,121,84,140,25,+ 88,225,198,157,150,186,208,53,46,134,223,138,40,148,178,81,148,163,183,185,+ 134,120,0,68,49,179,102,50,184,2,219,198,200,47,59,27,36,137,124,31,+ 104,112,101,100,5,243,131,198,24,109,43,3,194,68,62,170,102,17,136,156,+ 200,70,68,96,142,176,57,51,166,5,40,199,68,138,18,136,88,84,173,49,+ 138,45,182,215,141,126,211,216,135,93,136,239,112,59,82,79,64,43,22,32,+ 141,11,139,163,38,133,222,76,212,154,213,129,192,197,12,56,1,67,252,61,+ 98,198,129,96,4,145,102,19,2,61,68,78,45,1,113,221,141,54,240,153,+ 38,87,202,177,231,33,59,239,251,177,62,129,20,233,187,248,119,52,233,237,+ 135,23,105,81,98,8,32,65,56,235,97,134,1,68,177,91,204,182,19,96,+ 54,28,114,217,196,230,81,88,85,58,203,210,97,116,78,201,147,189,184,38,+ 99,58,123,4,136,29,206,216,241,28,143,82,253,249,241,30,165,61,255,251,+ 114,68,53,130,193,235,48,83,171,6,24,226,60,100,218,1,104,195,88,71,+ 160,211,84,6,5,43,200,240,153,41,64,241,153,184,19,51,68,34,0,176,+ 184,64,227,48,50,63,192,119,246,180,20,242,235,46,222,118,186,104,4,147,+ 21,162,148,44,109,185,219,171,229,48,25,16,63,109,66,51,240,135,140,231,+ 138,90,244,229,146,65,4,43,91,19,106,151,21,134,215,101,202,144,57,232,+ 195,116,218,206,155,109,192,61,183,246,3,112,236,66,159,194,131,60,102,88,+ 117,172,87,171,73,141,137,13,174,239,243,52,132,100,149,249,156,132,146,167,+ 38,134,249,7,64,8,110,182,120,194,136,94,195,222,218,209,107,108,42,80,+ 7,94,162,113,4,32,152,121,216,32,152,151,152,136,135,107,96,73,128,45,+ 32,14,25,10,20,86,71,230,232,91,104,164,164,50,155,118,35,102,236,204,+ 248,206,164,255,247,255,243,127,45,204,130,68,54,135,117,32,16,0,104,82,+ 143,157,150,108,51,190,153,0,0,192,147,129,145,77,142,169,187,103,237,235,+ 21,191,127,255,63,255,5,146,12,84,118,211,51,99,67,55,98,194,182,106,+ 155,67,47,29,169,117,35,0,131,148,44,41,30,229,81,57,240,53,115,83,+ 53,19,12,218,214,136,0,48,11,8,34,12,5,67,33,16,204,212,158,132,+ 108,137,196,39,195,197,113,171,109,125,206,33,255,251,112,68,71,0,17,182,+ 52,99,121,33,52,126,53,38,202,201,24,41,126,71,132,69,141,231,176,97,+ 248,234,25,237,124,192,154,49,175,140,14,194,239,125,139,211,133,210,95,254,+ 202,125,244,127,52,232,36,112,1,164,51,25,146,148,217,151,155,154,151,93,+ 162,11,132,176,234,144,159,195,177,191,231,107,224,115,31,117,166,107,64,141,+ 134,129,114,67,16,202,177,49,228,102,113,203,168,222,94,185,203,144,27,237,+ 151,123,93,157,177,157,156,23,234,57,135,45,186,237,205,203,187,183,111,235,+ 5,128,7,226,1,22,117,164,145,137,212,185,246,159,82,185,43,20,207,213,+ 207,217,14,7,52,206,231,159,226,46,76,200,110,161,99,98,113,230,107,12,+ 61,137,83,136,254,120,170,143,18,1,10,26,162,105,17,14,238,172,204,169,+ 36,72,160,3,34,173,80,56,165,76,186,92,33,21,143,121,211,237,204,235,+ 79,128,3,8,3,142,130,151,154,77,60,225,183,197,249,116,174,81,63,252,+ 201,191,40,234,122,104,212,209,110,10,225,179,134,238,91,135,246,84,229,146,+ 124,227,114,71,26,77,36,195,3,1,175,56,2,5,7,97,84,19,50,45,+ 42,188,242,38,152,103,29,152,22,236,144,24,36,172,2,40,7,162,92,77,+ 197,184,236,31,196,220,180,144,45,68,112,137,248,151,154,156,49,40,36,179,+ 65,238,67,40,141,255,251,114,68,96,0,1,235,18,221,253,60,192,0,62,+ 101,91,191,167,140,0,212,133,149,111,185,134,128,18,107,166,236,55,50,240,+ 2,99,121,227,85,19,140,205,150,138,137,97,232,92,64,200,160,117,52,75,+ 171,53,125,26,205,7,186,210,49,55,47,18,75,60,146,70,203,60,165,160,+ 182,62,200,162,131,49,56,249,139,55,255,205,236,154,105,178,211,255,255,252,+ 21,192,188,12,1,72,75,6,0,22,241,51,37,202,196,111,255,255,255,241,+ 43,10,160,240,64,112,8,192,192,7,60,250,2,94,35,99,7,190,219,91,+ 108,173,198,227,76,18,1,105,1,64,128,128,12,230,128,228,156,148,76,9,+ 92,16,129,105,192,228,134,14,102,156,4,61,42,212,108,189,105,144,200,89,+ 73,154,75,211,132,196,120,154,234,51,213,70,93,26,93,28,196,20,131,53,+ 43,157,159,136,84,85,237,177,49,70,52,145,145,210,200,116,40,146,233,114,+ 165,136,211,25,173,53,188,251,66,98,101,127,103,140,51,111,249,51,86,44,+ 63,102,149,181,245,164,204,244,143,51,216,81,163,91,57,22,164,29,23,108,+ 27,255,183,179,57,99,63,255,92,215,95,255,255,255,255,255,255,255,255,255,+ 248,247,222,111,120,142,16,127,255,51,46,106,37,65,62,240,2,80,237,242,+ 193,62,240,132,6,248,13,56,119,240,90,60,37,13,191,208,108,15,135,255,+ 251,112,68,13,128,1,228,51,85,6,52,224,0,61,197,90,44,197,28,0,+ 0,0,1,164,28,0,0,32,0,0,52,131,128,0,4,72,255,230,154,53,+ 26,156,255,254,168,54,27,103,127,254,166,142,172,36,13,127,203,5,65,82,+ 161,39,255,241,16,86,37,58,188,4,3,129,0,160,64,32,64,8,48,64,+ 9,129,255,0,40,173,224,169,188,9,4,222,104,216,33,252,91,81,23,252,+ 120,184,212,209,239,252,108,172,60,105,191,254,114,28,58,135,14,255,6,130,+ 128,208,151,255,5,72,168,26,255,250,234,76,65,77,69,51,46,49,48,48,+ 170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,+ 170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,+ 170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,+ 170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,+ 170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,+ 170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,+ 170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,+ 170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,+ 170,170,170,170,170,170,170,170,170,170,170,170,+};+static const int REF2[3308] = {+ -249,-480,-418,276,-52,-630,260,36,-536,-363,-484,-663,-574,-771,+ -170,-685,-519,-452,-456,-289,177,78,-26,-129,-36,101,-123,-432,+ 17,524,190,544,-156,460,375,643,430,565,503,518,1038,898,+ 946,1145,1005,1151,1008,974,880,554,-17,-13,-673,-951,-1214,-1554,+ -1762,-1850,-1978,-2582,-3554,-3888,-3944,-4291,-4338,-3641,-3449,-3565,-1699,-545,+ 257,422,1208,2065,1949,1901,2503,1708,1089,1125,1154,940,544,670,+ 927,749,1223,1416,2032,1876,2451,3053,2938,3453,3414,3889,3929,3503,+ 3481,3071,2273,1529,1047,-219,-558,-559,-1525,-1833,-2014,-2305,-2795,-3678,+ -4484,-5608,-6284,-6030,-5716,-5547,-3394,-1658,-469,71,1496,2914,3026,3109,+ 3320,2772,944,970,670,-272,-1178,-1438,-1599,-2541,-2556,-2219,-2222,-2107,+ -1866,-798,-283,677,1034,2360,3004,4150,4111,4374,4484,5213,4715,4544,+ 3803,3707,3046,3185,2390,1252,837,1421,1416,318,485,673,-40,238,+ -678,-2323,-3690,-5373,-4478,-4774,-5700,-3302,-1428,-1318,-654,567,1818,3022,+ 2186,2567,808,413,-44,-727,-2508,-4030,-4653,-4974,-4992,-5184,-4439,-2663,+ -1131,386,1233,2501,3274,4042,4648,4723,4214,3161,3387,3396,3196,3052,+ 2827,2952,2698,2814,2661,2448,2292,2335,2406,2406,1986,1657,1496,1752,+ 1066,-513,-2045,-3566,-4198,-5721,-7290,-7632,-6312,-4484,-1619,-1256,-899,1003,+ 3170,4596,3137,2324,1269,522,-74,-1594,-4274,-5095,-6932,-6021,-6408,-6518,+ -4040,-2338,-264,1063,2362,3566,5090,4943,5699,4235,3491,3093,3209,2407,+ 1804,1512,2698,2537,2626,3307,2965,3004,3387,3756,2632,1558,1877,1086,+ 506,311,-750,-2401,-2881,-3662,-4626,-6856,-6787,-6541,-5629,-3929,-1106,-467,+ 266,1680,4147,4419,3251,1874,1529,-170,-1602,-3083,-4926,-6329,-7332,-6972,+ -7069,-6066,-4127,-1178,518,1738,3413,5023,6197,5426,4474,3735,2394,1683,+ 1840,832,462,413,1786,2995,3221,3583,3603,5201,4565,4046,3532,2945,+ 1493,1077,306,-1093,-1722,-1226,-3098,-5462,-5255,-4645,-6411,-5977,-5599,-3718,+ -1658,1316,1550,1500,2737,4855,2976,1383,183,-260,-2758,-3677,-4856,-6259,+ -7779,-7452,-6460,-5900,-4169,-1099,1776,2037,3339,5381,6285,4827,4101,3161,+ 1636,1125,1167,731,-112,870,2513,3360,3732,4548,5188,5504,5579,4917,+ 3950,2334,1689,702,-420,-1624,-2192,-2177,-3438,-4153,-5595,-5756,-6311,-5329,+ -5643,-3942,-1087,187,673,1347,2939,3354,3036,1385,717,-1659,-1140,-2747,+ -5245,-6091,-6344,-6116,-6769,-5684,-4342,-1492,1540,2840,2893,4276,5691,5592,+ 3249,2273,1851,1477,462,231,46,1062,2198,3615,4104,4304,5703,6539,+ 5498,4378,4030,3153,2094,998,-692,-1764,-1636,-1622,-2831,-4317,-4385,-4945,+ -4833,-5024,-4638,-4181,-1909,679,1145,831,1080,2835,2524,2127,-65,-1061,+ -1855,-2294,-3753,-5494,-6793,-6998,-5921,-5646,-4222,-2171,375,1665,3121,4293,+ 5053,4888,3787,3221,2144,1802,747,784,799,1602,2251,2771,3976,4419,+ 5156,5301,5232,4547,3679,2928,2347,288,-398,-907,-1176,-2263,-2632,-3052,+ -4289,-5326,-5027,-4471,-4780,-3891,-1252,83,-258,395,1945,2675,2741,1605,+ 579,-813,-1211,-1633,-3156,-4857,-6326,-5541,-5433,-5567,-4519,-2874,74,1854,+ 2839,2870,4156,4603,5063,2915,2232,2230,1483,1764,1303,1622,1783,2810,+ 3615,3609,3543,4113,4200,3548,3213,2714,2031,1392,778,162,-948,-1494,+ -1035,-2181,-3843,-4633,-4894,-4779,-5152,-4473,-3075,-1455,-1230,-342,314,1519,+ 2174,2072,999,395,464,-128,-1936,-3406,-4717,-5338,-5182,-5172,-5435,-3730,+ -1640,-269,192,1688,2923,3529,3610,3917,2771,2332,2838,2714,1952,1529,+ 2094,1826,1936,2281,2525,2051,2416,2971,1974,1652,1696,2081,1320,506,+ 673,753,265,305,606,172,150,-35,-271,-1103,-1425,-1399,-1827,-1478,+ -1799,-2373,-2235,-2135,-2242,-2335,-2260,-2148,-3016,-3939,-4151,-4558,-4696,-4744,+ -4315,-3187,-1598,-999,-292,930,1747,2753,2523,2237,2059,2155,1927,1348,+ 1032,899,810,962,1007,638,803,1571,1764,1601,1887,2033,1969,2393,+ 2862,2550,2498,2767,3023,2977,2694,2404,2438,2065,1860,1466,490,456,+ 100,-360,-468,-800,-724,-720,-1066,-1096,-882,-1323,-1486,-1633,-1820,-2013,+ -2472,-2574,-2816,-2844,-2680,-2273,-1695,-1822,-1629,-1089,-725,-407,-133,-28,+ 198,429,476,813,837,919,1030,1143,1253,1193,1187,1238,1432,1284,+ 1245,1262,1189,1214,1096,1143,1099,912,1040,850,923,718,629,489,+ 487,282,212,77,44,3,-233,-1,91,167,139,197,262,158,+ 274,292,-32,-212,-17,-27,-280,-823,45,39,-44,-24,-256,-475,+ -314,-147,0,689,-8,-397,-3,624,1369,481,437,1145,2017,3201,+ 1402,406,1301,2153,1890,1066,754,535,816,1309,370,-1089,-1451,-960,+ -1171,-2064,-2652,-3578,-2716,-2711,-2861,-2995,-3007,-2098,-1095,-1161,-1300,-918,+ -49,498,602,455,162,411,930,488,35,-22,-18,352,227,399,+ 725,769,1505,1348,1406,1764,1840,2134,1910,1908,1769,1579,2021,1281,+ 542,417,69,-292,-1079,-1779,-2724,-3520,-3927,-4484,-5452,-7044,-7608,-6622,+ -5434,-3192,-2639,-2388,209,2371,3773,2925,2411,3164,3085,2155,790,252,+ 181,-440,-1004,-1861,-2291,-1628,-627,-395,-260,359,1153,1826,1327,1605,+ 1648,1678,2033,2361,2060,1994,2758,2899,2653,3049,3119,2605,2410,2257,+ 1718,714,61,-237,-968,-2066,-2930,-3620,-5349,-6777,-7573,-7652,-6753,-4900,+ -2328,-1683,-503,1829,4097,4843,4115,3564,2733,2382,1359,-749,-2194,-2692,+ -3061,-4008,-4997,-5552,-5194,-3851,-2749,-2147,-232,1268,2288,3381,4007,4191,+ 3928,4229,4515,3647,2227,2514,3603,3652,2625,2262,2579,2743,2420,1310,+ 744,1230,1251,510,-634,-1251,-1851,-3108,-5158,-7437,-7321,-6892,-6396,-3408,+ -721,-103,1241,4542,6424,4942,3966,3994,2549,791,-866,-2320,-3192,-3826,+ -4707,-5682,-5797,-5169,-4053,-3271,-995,1445,2894,3523,4115,5783,5582,4161,+ 4125,3266,2812,2792,2486,2266,2424,3351,3090,2394,2639,2388,2044,1298,+ 1105,841,-794,-832,-1130,-2793,-5657,-7208,-7026,-7214,-6703,-4496,-1266,338,+ 2020,4644,6343,6249,5239,4148,2257,565,-1458,-2991,-3803,-4068,-4469,-4605,+ -4599,-4023,-3680,-2975,-1905,-1300,1430,3551,2522,2161,4062,5886,3909,1925,+ 2536,3580,3136,2277,2126,2889,3618,3384,2310,1423,1474,1502,508,-1322,+ -1735,-672,-986,-3657,-5998,-7346,-6679,-5961,-6431,-4180,-279,2077,3070,5215,+ 7835,7094,4514,3355,1765,-436,-2900,-3657,-3347,-3879,-3957,-3480,-2888,-2513,+ -2534,-2256,-2198,-900,1643,2299,1189,996,2889,4668,3034,1830,2146,3328,+ 3731,2625,2247,3324,4543,3555,1639,530,1293,1483,-165,-1052,-1016,-657,+ -1430,-3974,-6050,-6939,-6841,-6920,-6215,-3956,-79,1704,2403,4639,6974,6375,+ 4203,2252,1022,130,-2215,-3699,-3557,-2745,-2469,-2410,-2514,-2663,-2377,-1980,+ -1557,-1039,427,1382,1251,1424,2631,3654,3459,2739,2640,3631,3099,2184,+ 2323,3221,3158,1869,991,841,1008,487,-1178,-1434,-184,-691,-3329,-5628,+ -6937,-6891,-6123,-5793,-4000,-205,2228,3174,4915,6725,6416,4698,2540,380,+ -1427,-2025,-2833,-3693,-3256,-1934,-1665,-1453,-1445,-977,-931,-1618,-1577,-1236,+ -63,1056,667,774,2762,3974,3440,2561,3273,3866,3478,2852,3015,3035,+ 1856,808,722,-60,-1230,-792,-508,-1239,-1736,-2365,-4548,-6750,-6901,-6630,+ -5966,-3368,-198,976,3170,5498,6605,6365,4565,3222,1313,-781,-2368,-2897,+ -3488,-3248,-2579,-1578,-1318,-1157,-590,-1491,-1739,-922,-600,-153,1043,942,+ 801,3144,3938,3561,3456,3561,3500,3574,3257,2864,2049,1664,941,-449,+ -831,-988,-904,-1030,-1606,-2110,-3498,-5829,-6781,-6197,-6404,-5250,-1471,656,+ 1321,3521,5977,6601,5152,3731,2521,253,-1494,-2114,-2968,-3132,-2024,-438,+ -768,-440,592,82,-895,-1455,-1481,-1063,-557,-164,391,1629,3142,4327,+ 3984,4046,4599,4633,3389,1899,1852,1111,-95,-1134,-1313,-1662,-1107,-622,+ -1987,-3327,-4486,-5228,-6080,-6520,-6725,-3362,433,197,593,4275,6089,5118,+ 3251,2241,2149,823,-670,-2030,-2350,-1392,-609,-351,-648,646,1907,817,+ -867,-1483,-950,-1297,-1315,71,1235,2532,3531,4506,5146,5048,4356,3537,+ 2001,1047,-3,-881,-1219,-1343,-1626,-1993,-1151,-2153,-4227,-5207,-5240,-5414,+ -5799,-4878,-2102,-279,508,1884,3962,4912,3583,2404,2183,872,-221,-713,+ -1013,-1458,-954,377,347,666,1702,1696,1103,-683,-1522,-2154,-2254,-2562,+ -1073,1977,2763,3006,5152,6271,5687,4263,3013,1736,903,-50,-1795,-2215,+ -1395,-791,-2355,-3313,-3326,-4062,-4637,-5441,-5525,-3917,-1408,-708,-700,1387,+ 3525,4066,1979,948,1740,1468,-162,-1049,-427,231,302,516,336,917,+ 2344,1679,167,-894,-618,-1357,-2586,-2892,-1190,1558,1789,2184,3761,5898,+ 5832,4272,3309,2566,1699,776,-1006,-2196,-1954,-1071,-1674,-2836,-3279,-3183,+ -3703,-4428,-4431,-5239,-4126,-1258,-157,-820,820,3468,4164,2896,1443,1673,+ 1683,664,-1376,-1259,-549,1,195,212,790,1029,1610,-123,-1304,-1392,+ -1624,-1529,-1292,42,1568,2222,3707,4265,4377,3800,3172,2780,1710,341,+ -771,-171,-505,-1644,-1837,-1520,-1894,-1879,-2930,-3815,-4337,-4295,-3522,-2781,+ -2144,-1057,1185,2030,2157,2242,3119,3055,2206,439,-508,520,928,-100,+ -972,-522,371,1048,-351,-1272,-1056,-255,-1229,-2085,-2131,-595,1684,2305,+ 1975,2679,4582,5325,3554,1702,1745,1536,149,-2063,-2373,-1399,-1351,-1177,+ -2168,-2248,-1662,-1522,-2997,-3607,-3221,-2442,-1308,-678,-370,1443,2682,2946,+ 1814,2147,2487,2220,581,-106,367,-25,148,-1285,-1095,-1053,-247,-1115,+ -1450,-1216,-809,-1007,-1595,-539,227,1670,1390,2396,2845,3289,3226,2111,+ 1742,895,172,-523,-716,-1043,-544,-446,-458,-751,-43,-468,-1571,-2245,+ -2462,-2260,-2583,-2597,-1793,-121,1069,1184,1394,2123,2728,2787,1519,717,+ 516,757,126,-721,-1007,-754,-529,-1037,-1366,-1526,-1279,-1492,-1527,-1375,+ -990,-75,654,1223,1975,2389,2305,1661,2343,2005,1295,757,1046,1323,+ 747,503,29,780,711,-272,-965,-1742,-1269,-1467,-2406,-3272,-3058,-2001,+ -1282,-1453,-1063,-126,1254,996,702,498,1420,1123,523,-44,-547,523,+ 492,-209,-942,-163,244,-371,-1207,-1305,-585,-243,-187,-452,857,2046,+ 2264,1768,2152,3002,3360,2760,1353,1578,1952,1897,16,-901,-722,-623,+ -1655,-2945,-2952,-2877,-2796,-3845,-3643,-2969,-2166,-1551,-1411,-831,-232,847,+ 872,924,650,1048,1529,1361,1037,1059,1292,1144,1277,933,633,511,+ 361,328,-113,-298,-432,-531,126,388,613,846,1381,2245,1926,1684,+ 1692,2006,1589,527,-229,-617,-645,-1420,-2569,-3185,-3087,-2960,-2992,-2669,+ -2526,-1576,-1106,-549,-945,26,543,622,186,645,1425,1798,1629,1330,+ 1561,1884,1714,1165,581,1045,1087,253,-447,-364,-167,-544,-1271,-1060,+ -429,263,459,609,792,1290,1881,1923,1467,1055,1309,1223,641,-369,+ -652,-988,-1565,-2098,-2526,-2356,-2225,-1943,-1838,-1792,-1593,-1217,-924,-682,+ -488,-241,323,783,1025,967,1036,1228,1372,1348,1180,979,1163,930,+ 461,86,-99,-161,-462,-252,159,364,810,1012,1310,1205,1569,1828,+ 1684,1310,1131,1082,1003,279,-356,-1131,-1504,-2093,-2337,-2823,-2852,-2748,+ -2706,-2648,-2446,-2254,-1988,-1608,-1228,-588,-91,320,640,1029,1343,1448,+ 1772,1923,2045,2212,2340,2175,1681,1383,1107,823,782,635,571,638,+ 840,981,914,820,866,895,881,704,442,144,-166,-623,-1212,-1973,+ -2502,-2715,-2799,-2894,-3189,-2863,-2629,-2361,-2317,-2065,-1621,-995,-388,-19,+ 395,885,1309,1609,1788,1834,2186,2483,2635,2491,2321,2265,1964,1611,+ 1306,931,883,725,617,484,476,506,462,496,372,272,146,-101,+ -292,-610,-1050,-1628,-2220,-2536,-2707,-2924,-3078,-3050,-2697,-2246,-2229,-2247,+ -1982,-1431,-791,-235,6,440,924,1415,1733,1925,2077,2317,2754,2932,+ 2739,2431,2325,2216,1949,1507,1100,784,733,682,538,349,430,507,+ 445,273,-21,-151,-352,-584,-1044,-1501,-1944,-2313,-2528,-2742,-2976,-2929,+ -2770,-2559,-2458,-2336,-2085,-1813,-1310,-856,-324,121,601,1078,1417,1887,+ 2146,2436,2703,2905,3025,2953,2699,2539,2321,2111,1610,1245,980,837,+ 708,513,362,317,417,186,-40,-324,-476,-682,-959,-1464,-1903,-2302,+ -2556,-2746,-2894,-3057,-3031,-2696,-2483,-2402,-2310,-1971,-1630,-1063,-566,-175,+ 306,798,1353,1726,2037,2388,2714,3071,3159,3099,3058,2773,2512,2191,+ 1929,1587,1146,1008,799,656,408,280,229,107,-57,-277,-520,-752,+ -1038,-1398,-1857,-2262,-2528,-2649,-2711,-2871,-2962,-2750,-2504,-2257,-2165,-2001,+ -1662,-1107,-534,-207,254,723,1192,1592,1943,2308,2666,2935,3101,3134,+ 3037,2826,2567,2263,1941,1597,1260,980,682,520,287,143,1,-132,+ -302,-548,-706,-1042,-1234,-1517,-1860,-2129,-2479,-2620,-2704,-2800,-2803,-2584,+ -2418,-2162,-1985,-1714,-1523,-1121,-632,-78,328,630,1132,1526,1899,2277,+ 2610,2843,2975,3178,3184,3009,2663,2317,2037,1674,1315,1030,780,662,+ 422,222,100,-42,-203,-452,-745,-1027,-1176,-1385,-1654,-1998,-2236,-2396,+ -2474,-2562,-2541,-2449,-2331,-2180,-2032,-1900,-1665,-1413,-1067,-670,-243,180,+ 605,932,1308,1680,2024,2244,2460,2571,2614,2565,2463,2287,2035,1739,+ 1496,1211,951,811,574,371,284,130,16,-215,-418,-594,-703,-851,+ -1036,-1220,-1421,-1632,-1739,-1865,-1862,-1857,-1816,-1734,-1664,-1541,-1508,-1408,+ -1233,-986,-714,-453,-53,242,603,913,1206,1481,1607,1747,1878,1817,+ 1763,1691,1603,1403,1171,1040,913,747,603,527,380,283,209,92,+ -73,-216,-306,-436,-509,-556,-634,-699,-843,-948,-1002,-1068,-1147,-1216,+ -1213,-1222,-1251,-1185,-1093,-1044,-914,-687,-518,-281,-52,185,433,695,+ 863,1051,1118,1225,1312,1292,1279,1215,1109,1013,859,740,581,518,+ 315,276,194,72,-47,-163,-215,-238,-329,-414,-449,-421,-447,-472,+ -455,-468,-455,-514,-518,-583,-612,-650,-538,-588,-538,-414,-320,-243,+ -129,-31,78,170,331,432,574,638,676,734,765,832,752,735,+ 676,619,372,337,141,41,-6,-164,-212,-222,-284,-294,-345,-418,+ -457,-398,-444,-498,-312,-415,-281,-238,-113,-107,-85,9,82,97,+ 99,132,163,125,196,179,213,219,247,368,415,396,408,452,+ 426,357,249,110,19,-10,-104,-82,-131,-141,-156,-236,-371,-305,+ -328,-367,-504,-425,-404,-354,-374,-450,-289,-312,-222,-148,-202,-58,+ -56,112,27,60,181,-36,124,177,171,231,340,284,417,378,+ 433,503,573,495,404,370,296,115,38,-136,-219,-235,-314,-257,+ -337,-180,-357,-553,-439,-400,-347,-347,-320,-415,-333,-453,-417,-212,+ -210,-28,-26,-143,-211,-204,-179,58,439,451,448,464,444,467,+ 294,349,284,342,30,393,455,371,251,317,211,185,130,68,+ 22,134,27,-182,-141,-241,-201,-295,-200,-388,-279,-100,-307,-341,+ -391,-283,-380,-257,-258,-307,-201,-167,-127,30,51,-88,266,-128,+ 298,95,-83,167,141,-34,123,151,33,148,76,-265,206,-51,+ 74,118,-4,14,192,-106,145,21,237,-86,292,-154,-299,-85,+ -2,339,473,288,555,231,301,46,95,-71,-28,-184,-249,-78,+ -103,46,-406,-9,-83,-196,-98,-452,-259,142,-487,78,-97,-43,+ -75,-12,-188,218,109,160,187,-164,302,-225,55,-127,-108,-93,+ 10,-356,315,150,189,199,-9,293,155,-185,414,-94,-106,-57,+ 167,-273,-191,-157,-154,-275,45,-315,29,125,-239,-162,-135,32,+ -310,182,-188,-321,-111,74,-341,-62,315,-408,120,287,-84,156,+ 120,-123,104,168,-386,396,34,-117,575,-78,25,290,93,181,+ 305,-251,388,-225,111,286,-78,-19,79,-204,367,-577,238,302,+ -245,-66,72,159,-643,-115,164,-736,227,-496,-99,337,-611,611,+ -147,-377,245,49,-290,120,-534,465,-467,571,-565,24,377,-717,+ 129,51,-205,636,-414,-24,379,-423,243,-358,114,-470,401,98,+ -178,-290,-2,-171,-367,387,-220,159,179,75,280,-92,-289,442,+ 32,-47,479,-186,99,200,544,-226,130,412,-182,253,-363,471,+ -246,-97,585,-1062,616,-590,72,-85,-248,89,-392,-210,-495,-70,+ -624,84,-490,-332,215,-321,128,45,-300,738,-370,528,-871,545,+ 27,-429,853,-546,253,176,-111,34,357,-89,698,-384,365,15,+ 452,67,-592,473,521,-1039,1142,-360,-396,242,-310,-175,-76,220,+ 1,-1115,1981,-1098,-459,102,-290,786,-1249,1052,-198,-646,591,240,+ -1091,1227,-983,173,456,-745,-39,-48,134,-168,335,247,-415,-38,+ 518,-193,162,373,-309,-174,1118,-1116,353,877,-1232,300,886,-1226,+ 610,449,-448,220,603,-1223,1101,-443,-267,304,162,-754,863,-547,+ -524,1094,-1132,1261,-902,-282,1137,-926,618,-740,111,898,-1417,666,+ 481,-717,-887,947,-581,-291,780,-481,-74,828,-625,-359,331,-45,+ 361,-856,933,453,-1342,2049,-987,-247,686,-381,-186,67,678,-1604,+ 1311,183,-1210,1307,-268,-1870,1987,-1302,-370,1248,-579,148,-195,736,+ -1134,1410,355,-1705,2255,-1131,-152,1079,-1131,425,-82,451,-1097,1756,+ -1204,-62,1217,-1338,-353,2075,-2575,1152,1310,-2446,2580,-1021,-663,1867,+ -1647,1248,-867,158,675,-1223,223,532,-2247,1816,-589,-1202,1247,-780,+ 22,681,-542,-342,777,-9,-103,508,-597,-78,1321,-1456,1886,-1704,+ 1668,-1529,1188,-167,-199,678,-533,290,-185,595,-1210,562,-448,-210,+ 526,490,-1020,693,-182,-335,331,-523,279,-486,-587,1184,-703,466,+ -208,-185,-41,-661,1004,-1140,1682,-1364,1378,-995,134,-114,-345,338,+ -1253,3146,-3504,2701,-398,-841,2175,-2597,1970,-1193,-58,1109,-1648,644,+ 636,-2319,2316,-1328,-510,1767,-2170,2159,-1845,1314,-940,1061,-539,327,+ 175,-501,933,-1099,374,300,-1263,2044,-2286,916,167,-60,507,-809,+ 304,-365,1027,-1185,1087,-429,-16,929,-997,-275,147,-29,-391,441,+ 156,-21,27,-582,1070,-1610,1484,-715,-180,-132,422,-1125,938,-276,+ -298,827,-868,1216,-1261,340,401,-1545,1948,-1025,334,381,-658,1247,+ -1032,1160,-1207,53,1689,-1890,814,248,-1450,1227,-76,-600,309,-505,+ 40,-938,706,-1341,-32,48,-539,432,-653,1199,-582,-195,18,-164,+ 817,-435,60,-207,152,-134,-199,159,-721,790,-1120,1053,31,-808,+ 660,-1846,776,-257,1036,-388,134,341,498,-479,547,-636,-132,513,+ -108,-477,-250,523,-1700,317,330,-1485,277,182,791,-540,895,-2,+ -455,940,57,-474,174,-31,99,369,24,-403,165,683,-656,258,+ -84,-370,445,-331,-460,-47,-283,-112,-219,18,-747,3,-209,-223,+ -137,-64,568,1,-517,116,-36,565,253,-123,526,-175,116,583,+ -59,-274,474,-55,418,432,-293,-264,-342,130,565,658,-328,-104,+ 188,-108,-18,389,-1447,264,-610,-383,-291,-824,-527,-1018,-36,-337,+ -849,-192,-527,69,-71,160,-395,193,-130,427,113,199,296,350,+ 345,1046,1001,1058,949,880,590,1055,790,546,465,657,1051,1151,+ 1590,1171,896,769,681,734,-465,-892,-875,-866,-976,-1248,-2025,-2260,+ -2580,-3249,-3768,-4564,+};+static const unsigned char CLIP3[1671] = {+ 255,245,64,196,100,66,45,18,74,178,0,0,0,23,217,215,196,100,160,66,+ 102,40,226,174,42,147,136,172,171,9,192,195,14,44,203,143,48,219,203,61,+ 239,91,22,199,205,91,31,53,173,107,90,215,205,123,222,182,45,143,154,182,+ 62,107,90,214,181,175,154,247,185,27,111,92,197,56,52,214,180,77,33,46,+ 89,213,45,48,123,170,77,98,166,196,157,136,195,143,171,60,152,28,202,115,+ 100,141,241,44,210,25,165,134,212,207,136,139,122,233,176,141,123,10,150,27,+ 102,49,66,110,152,104,106,3,149,151,238,131,27,7,66,203,34,71,169,71,+ 253,70,31,70,172,230,181,154,59,219,116,169,155,219,25,170,120,143,157,77,+ 34,132,73,22,166,70,20,80,121,109,21,7,37,14,147,205,185,26,206,58,+ 168,32,245,49,198,87,114,57,173,117,43,192,175,241,146,50,86,115,146,168,+ 204,97,23,169,190,136,0,0,255,245,66,196,100,67,45,18,74,178,0,0,+ 0,16,35,47,93,100,144,128,230,40,122,201,40,218,170,168,138,201,172,146,+ 171,46,202,236,176,226,238,17,54,95,162,17,35,40,107,104,203,26,198,173,+ 12,46,39,51,75,128,81,77,237,193,74,173,100,26,69,188,89,211,83,252,+ 203,4,114,228,160,104,41,173,64,216,229,75,90,214,161,108,8,249,153,107,+ 66,70,38,106,99,84,110,34,109,181,108,124,214,181,172,99,98,54,11,147,+ 122,182,182,218,182,62,107,90,214,181,175,155,22,182,86,48,28,71,68,95,+ 77,170,181,137,99,103,186,145,104,132,193,182,58,82,156,243,197,203,168,200,+ 232,230,115,143,84,225,204,55,36,107,149,5,145,3,175,97,132,233,243,44,+ 131,69,134,168,109,112,167,92,143,241,160,82,153,192,74,121,189,72,202,181,+ 109,66,212,106,115,91,147,93,29,118,132,55,118,136,201,189,96,255,245,66,+ 196,100,68,45,18,82,178,0,0,0,21,21,245,21,100,144,90,104,38,162,+ 202,32,130,73,170,179,108,56,203,12,186,200,231,177,181,6,112,155,7,146,+ 128,223,116,170,84,68,145,125,72,23,130,238,44,218,250,61,239,218,168,62,+ 170,141,135,77,195,95,86,127,155,75,154,34,173,91,219,80,114,87,74,118,+ 133,138,90,87,193,166,90,34,43,206,136,197,25,215,55,32,17,83,52,230,+ 30,214,114,75,113,39,133,101,105,20,41,251,147,170,16,212,102,165,110,222,+ 71,136,186,79,50,249,107,204,161,73,129,44,58,230,98,77,194,131,45,76,+ 58,134,195,8,45,104,88,189,174,33,228,209,123,138,136,190,177,8,24,236,+ 120,199,75,99,25,224,201,89,168,217,26,172,235,210,171,114,35,120,117,105,+ 180,54,200,218,63,105,92,246,13,28,70,167,107,109,86,227,45,201,50,151,+ 146,9,92,82,195,112,255,245,66,196,100,66,44,146,74,178,0,0,0,23,+ 153,230,250,5,24,122,43,168,195,109,58,195,111,36,194,142,176,227,237,60,+ 220,47,196,211,237,192,244,82,187,12,198,212,32,144,251,106,162,154,52,128,+ 211,140,212,10,45,46,140,183,41,22,132,213,227,120,17,66,66,89,33,163,+ 61,189,124,213,193,84,37,180,23,208,147,134,237,165,109,243,250,34,111,123,+ 206,215,81,106,200,102,108,64,174,199,40,97,144,140,245,81,141,128,4,163,+ 161,148,214,157,165,34,137,6,180,102,81,167,33,30,13,44,242,105,110,219,+ 102,193,176,163,19,52,234,232,121,13,47,178,196,181,181,173,107,90,149,69,+ 103,72,179,42,252,74,224,101,205,5,159,52,128,19,248,109,131,187,154,197,+ 53,45,104,92,186,201,104,39,165,244,10,150,165,250,205,67,155,6,170,8,+ 193,107,57,165,178,38,65,77,107,90,211,181,86,134,0,255,245,66,196,100,+ 66,44,146,74,178,0,0,0,7,97,119,224,5,26,170,75,48,219,16,178,+ 236,15,184,228,46,61,27,147,59,59,114,190,220,143,49,59,205,206,235,0,+ 204,231,104,88,117,217,148,141,212,9,93,86,234,6,165,50,67,42,156,132,+ 52,237,188,202,163,195,112,177,56,45,119,115,4,33,172,82,179,5,65,227,+ 73,89,174,107,99,153,55,141,83,92,235,16,144,108,10,89,116,136,31,52,+ 202,107,90,214,181,170,124,212,58,141,226,57,93,173,59,90,214,217,173,107,+ 230,181,113,87,233,225,132,149,154,71,53,173,27,34,90,75,122,36,187,195,+ 109,97,3,50,199,180,223,131,62,73,159,83,86,27,11,171,84,118,107,108,+ 209,141,196,4,97,41,238,93,42,149,133,105,219,102,225,115,240,116,113,26,+ 126,245,77,144,84,16,71,171,137,217,52,45,105,29,29,40,239,58,44,214,+ 52,131,198,0,255,245,66,196,100,67,45,18,82,178,0,0,0,8,89,85,+ 85,76,18,210,199,182,201,203,184,203,109,44,187,12,178,203,11,42,170,152,+ 73,230,246,201,182,84,29,243,90,228,183,179,82,183,19,20,173,12,52,139,+ 63,86,32,216,50,16,179,215,102,225,152,135,118,229,129,101,68,133,133,183,+ 45,84,87,213,36,225,82,213,210,169,193,179,130,2,52,52,137,71,193,115,+ 134,99,109,0,161,182,141,140,198,145,198,38,135,172,78,11,86,12,156,69,+ 102,180,73,123,35,75,91,7,21,179,108,65,99,116,70,145,243,74,219,182,+ 102,13,84,152,173,162,75,204,20,160,130,141,136,129,175,123,214,53,178,83,+ 149,192,251,24,83,236,83,208,65,239,122,216,182,38,160,100,199,99,108,213,+ 173,107,230,193,176,111,86,198,109,172,184,132,107,107,181,173,124,218,15,20,+ 68,102,158,129,82,28,238,115,230,149,175,154,0,255,245,66,196,100,67,45,+ 18,82,178,0,0,0,20,212,83,49,196,144,82,8,38,130,139,182,202,202,+ 48,187,11,174,179,46,176,218,213,13,208,20,81,40,214,241,196,88,83,234,+ 108,28,54,225,162,162,147,8,220,191,82,37,216,245,193,50,245,100,49,84,+ 24,149,148,6,247,208,58,15,165,100,75,13,141,175,213,7,90,173,97,232,+ 74,178,166,221,87,21,195,38,193,154,32,170,51,9,161,141,152,211,81,235,+ 120,179,70,104,81,173,242,242,66,52,111,90,163,87,182,164,34,44,121,112,+ 169,180,234,152,181,237,157,140,209,174,68,10,65,145,107,44,151,101,107,61,+ 107,91,153,175,233,160,100,37,66,213,183,214,148,250,238,106,219,57,88,147,+ 125,58,197,24,182,66,107,150,250,173,99,150,150,22,91,18,52,219,86,199,+ 205,107,95,53,173,124,216,46,43,98,214,219,106,216,249,173,107,230,181,175,+ 154,0,255,245,66,196,100,66,45,18,74,178,0,0,0,19,157,188,105,202,+ 46,243,46,189,12,143,201,20,115,75,84,52,75,45,22,73,76,216,79,93,+ 243,215,79,133,85,232,224,176,202,80,252,238,236,5,174,154,194,166,227,236,+ 8,82,82,213,154,217,141,187,150,158,109,37,228,135,126,3,81,211,233,107,+ 220,230,189,232,30,107,204,5,34,166,161,239,96,164,181,173,107,229,60,246,+ 67,205,54,212,51,189,170,174,40,109,90,206,138,160,176,193,144,148,227,99,+ 107,84,210,54,150,155,21,201,108,62,161,197,198,66,210,181,141,104,79,146,+ 70,131,122,152,107,99,104,149,181,107,110,214,161,109,212,219,41,209,210,101,+ 206,17,20,74,182,57,124,171,185,72,98,94,209,171,26,181,89,173,33,29,+ 53,82,131,219,69,174,11,86,199,205,107,88,212,185,243,94,247,173,139,99,+ 230,173,143,154,214,181,172,107,230,128,0,+};+static const int REF3[2304] = {+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,-1,-2,0,1,-2,-5,0,-1,-1,5,1,+ -4,0,8,-1,-5,-8,-19,-12,-4,-13,-22,-20,-11,14,+ 24,7,-30,1,47,-49,-42,-20,-146,-94,9,-11,-102,-118,+ -87,-35,159,-71,-250,33,121,242,-254,-676,34,31,-570,-714,+ -839,-771,-947,-909,-292,22,-182,-377,-224,-58,128,505,1230,1742,+ 2014,2730,2680,2695,2010,891,-643,-2693,-3529,-4801,-6681,-8514,-10130,-7958,+ -4407,-725,2366,4891,5101,3138,1914,958,1182,2847,5843,7082,7943,8301,+ 7817,6444,4623,2013,-1290,-2907,-3832,-5063,-8308,-12267,-13104,-11007,-3778,-1182,+ 4582,4673,3594,3927,953,-16,-2164,33,-176,3188,4780,6329,7880,6727,+ 8204,5431,5358,4741,1297,1726,-1028,-1919,-4169,-11181,-16262,-18516,-10491,-5863,+ 1576,5777,6675,4635,2225,-1280,-1852,-1655,202,3820,5521,6481,6935,5715,+ 5966,5723,6033,5494,3731,2127,1605,145,716,-5837,-12022,-18839,-19757,-10169,+ -5045,2429,8712,5658,4165,600,-1591,-1979,-2268,-223,3510,5219,5891,3916,+ 3658,4972,5429,7348,7019,6486,4769,180,-2099,-4016,-9039,-13263,-19008,-16873,+ -8036,-1669,5498,10254,4603,2338,-1765,-5897,-4375,-3550,1202,5336,7960,7885,+ 4926,3286,3714,4896,8182,8429,7238,4593,62,-2143,-4116,-5758,-12344,-15112,+ -17734,-9202,-316,2833,10453,3060,2085,-2733,-5547,-3366,-3330,4551,5842,6793,+ 6505,2645,3233,3353,6606,9763,10098,9827,5439,1785,-2244,-5187,-8138,-12718,+ -17544,-16160,-10629,-1114,2215,8666,4402,-32,-1333,-5090,-2289,-1131,2912,5588,+ 5828,4820,2801,1928,2791,6718,9311,10085,9118,4167,1144,-2452,-5047,-5919,+ -10585,-14798,-15974,-12461,-2443,-912,6184,3757,496,-539,-3390,-1794,-716,703,+ 4839,4818,5084,4359,2722,4701,6530,9055,8847,8042,3986,1012,-1412,-2639,+ -4831,-10516,-16684,-17020,-13937,-4212,714,7865,5576,2530,1729,-2842,-2193,-2883,+ -44,4242,4476,6452,4440,4255,4136,5703,7110,7835,6602,3185,2504,-1167,+ -2390,-3989,-9606,-13544,-16236,-12890,-6434,-1384,4734,4393,5096,2618,-255,-1774,+ -3601,-1470,785,3314,4317,5070,4997,5697,5918,5412,5595,2327,2061,1077,+ 851,-1393,-2053,-3731,-3604,-4863,-6006,-5451,-4171,-3867,-3445,-2582,-2208,-1475,+ -1054,-249,674,1610,1926,2136,2156,2521,2908,3157,3344,3379,3185,2796,+ 2463,2411,2443,2435,2195,1618,688,-237,-997,-1373,-1527,-1657,-1809,-2118,+ -2402,-2493,-2285,-1858,-1190,-441,278,1091,1587,2087,2118,2270,2183,1926,+ 1803,1395,1112,925,926,535,374,-69,-1270,-824,-1726,-1131,-1754,-1331,+ -1342,-1180,-717,-1399,26,-793,336,557,-516,1080,2043,792,3945,2090,+ 4117,6185,1839,3876,1523,-592,1025,-4554,-3300,-6402,-8026,-6761,-7329,-3884,+ -3578,-1470,402,785,1943,1058,1237,1205,1890,3089,3933,5317,5559,5740,+ 4123,2842,-608,-2389,-6055,-8773,-11366,-15901,-13620,-6931,-3213,3944,5264,5576,+ 1069,-32,-3808,-4422,-1081,2406,6645,8487,8940,8257,5984,6316,3815,6084,+ 4083,3719,386,-1668,-5508,-10895,-18085,-18593,-11058,-4673,4911,8653,6536,5836,+ -2293,-2936,-6259,-3856,2265,5071,9596,8991,6860,5908,4349,5008,5343,6337,+ 5604,2388,593,-3357,-7967,-12774,-21141,-18332,-8747,-2369,9483,8042,5947,-518,+ -4997,-5042,-4149,1615,5510,10445,9101,8043,3963,1849,3283,5733,10007,10681,+ 6396,1734,-4483,-9474,-14080,-20968,-18807,-8774,-1887,9091,9391,5067,523,-6657,+ -3886,-4490,4116,6704,9467,7369,4044,1988,714,4825,6962,11003,9625,5311,+ -93,-6006,-8900,-14265,-19140,-15740,-7158,1124,8971,6987,2320,-3696,-7415,-4642,+ -2273,4001,6899,9668,7168,2563,4532,1488,6463,7272,10040,7684,3336,436,+ -5477,-6854,-16149,-19014,-18899,-5991,-823,9574,6419,1602,-3034,-4866,-1886,1532,+ 4808,7427,6748,3988,2608,2022,3373,8428,8637,10253,5144,1209,-3754,-6372,+ -9515,-16930,-15895,-12521,847,3256,9568,626,-3554,-5456,-4270,621,4594,8074,+ 5368,5769,1307,1516,2543,5672,9267,11664,8086,3457,-4397,-6308,-10579,-12447,+ -16095,-13312,-3248,1567,8462,4490,-922,-5976,-6113,-2497,436,7554,6621,9149,+ 4941,1019,3472,2845,7114,9780,9239,4377,-1790,-5432,-7475,-10217,-15041,-13641,+ -7742,-291,4341,6644,-1610,-5387,-7144,-1778,2879,7852,9159,8267,4487,1110,+ 1599,3578,6758,9502,7988,4933,-502,-4008,-6020,-13883,-15811,-16458,-2794,-1111,+ 8445,3361,-815,-4025,-4892,1745,3104,8954,7912,5937,4121,-692,4996,5092,+ 10502,9256,5732,327,-6153,-7285,-12568,-15030,-14426,-8033,199,4782,6605,-500,+ -5070,-5002,-1937,4081,7488,9143,6907,3675,2439,4904,6357,7486,7462,4214,+ 1516,-1956,-4534,-9734,-16595,-16358,-9993,-3079,4061,4673,1004,-2420,-3072,139,+ 2061,5368,5075,6925,4472,2898,4608,4532,8475,8880,6676,4114,-1495,-3873,+ -10798,-13501,-15726,-14546,-2854,-1531,6470,208,-791,-5118,-4079,960,3517,9208,+ 5955,5853,3433,2619,4859,6129,7920,7181,4254,1384,-3508,-5509,-9217,-11707,+ -13356,-7875,-5723,877,902,1969,-3605,-1407,456,1623,6060,3869,6815,2389,+ 5308,5365,6183,7551,4955,3963,-1821,-1661,-5541,-7840,-9914,-10349,-6908,-2404,+ 1522,895,743,-932,-2511,-1154,-1461,1717,2414,4835,5047,5924,6728,5447,+ 6129,2164,1853,-31,-1540,-396,-2930,-3468,-8600,-7642,-8517,-2923,-1750,231,+ 1441,-1017,1125,-555,852,1434,2627,3266,3088,3597,4154,3582,3954,3100,+ 3953,3311,1585,1372,-2859,-5137,-7043,-10065,-6502,-5858,-1797,-291,713,1652,+ -1069,998,-1405,1725,2596,3060,4758,3590,4841,3987,4445,3890,3717,2379,+ -344,-1984,-6437,-7026,-11092,-7751,-5635,-3648,-106,-1687,1214,-343,1681,1090,+ 1859,3127,3424,4653,5314,5353,5742,4526,4510,2407,-317,-1856,-4815,-6052,+ -7303,-6368,-4931,-3762,-1942,-1544,-1021,492,360,802,1641,2288,2961,3273,+ 3517,4130,4664,4979,4369,4281,2698,1529,-1150,-3895,-5371,-6570,-5756,-5737,+ -5054,-4045,-3196,-1579,-511,301,1881,2455,3245,3631,3809,4466,4845,5241,+ 4909,4685,3711,2334,-150,-3061,-5539,-7239,-7538,-7320,-6627,-5198,-2952,-1167,+ 274,1058,2162,3427,3862,4084,4473,4904,5336,5400,5236,4590,3693,1365,+ -1065,-4286,-6075,-7367,-7477,-6827,-6059,-3656,-2431,-500,553,1567,3035,3240,+ 3517,3592,4166,4974,5207,5249,4977,3901,2188,-307,-3541,-5662,-6885,-7325,+ -6482,-6416,-4573,-3007,-1800,-322,705,2187,3651,3710,4373,4621,4828,5407,+ 5269,5110,4363,2515,262,-2814,-5254,-6847,-7832,-7308,-7069,-5493,-3971,-2359,+ -813,701,2020,3420,4203,4742,5222,5346,5373,5277,4905,4248,2980,861,+ -1726,-4463,-6230,-7470,-7573,-7428,-6541,-4857,-2875,-1099,551,2111,3684,4766,+ 5117,5476,5709,5677,5854,5233,4469,3219,850,-1479,-4281,-5734,-6984,-7314,+ -6901,-6509,-4712,-3045,-1441,288,1883,3270,4160,4554,4804,5072,5112,5151,+ 4810,4085,2971,1041,-1264,-3681,-5546,-6693,-7116,-6954,-6389,-5160,-3336,-1900,+ -15,1785,3219,4586,4879,4963,5035,5072,5173,4762,4031,2899,1151,-840,+ -3226,-5012,-6332,-7044,-7264,-6987,-5908,-4332,-2547,-867,931,2506,3642,4400,+ 4980,5275,5538,5566,5226,4584,3446,2074,319,-1590,-3520,-4835,-5838,-6214,+ -6316,-5791,-4875,-3605,-2129,-594,687,1906,2658,3493,4075,4670,4952,5093,+ 4961,4438,3325,1977,656,-767,-2234,-3541,-4475,-5118,-5277,-5052,-4399,-3550,+ -2518,-1219,-66,1004,1840,2720,3341,3679,3799,3834,3591,3191,2582,1754,+ 887,35,-894,-1854,-2548,-3038,-3356,-3454,-3276,-2907,-2397,-1722,-888,-38,+ 734,1387,1829,2133,2335,2400,2440,2315,1966,1533,958,401,-6,-460,+ -822,-1136,-1546,-1757,-1825,-1775,-1450,-1210,-1000,-701,-567,-354,99,406,+ 772,1065,1047,1189,1320,1110,1012,973,725,431,10,-639,-710,-697,+ -684,-748,-640,-639,-660,-886,-1106,-953,-718,-490,-565,-127,172,389,+ 357,844,951,711,160,888,1177,1277,711,471,-30,320,127,-135,+ -495,-643,-405,-996,-1009,-1006,-800,-444,-394,-290,-26,-25,-73,444,+ 466,814,518,1042,700,698,511,345,-63,-23,-80,-244,-301,-349,+ -641,-383,135,77,-85,-410,-258,-577,-349,-346,-105,376,-67,246,+ 488,562,777,518,517,543,81,99,-176,-161,277,128,-235,-563,+ -743,-523,-576,-442,-497,-311,-169,-158,-82,-63,24,153,15,212,+ 205,237,455,-39,470,516,377,604,496,10,-326,-439,-567,-364,+ -143,-706,-641,-569,-600,-787,-856,-290,-80,-49,275,87,472,194,+ 419,-9,95,11,24,-69,-466,-129,-512,-655,-616,116,130,208,+ -625,-283,309,65,191,255,432,92,-51,660,11,-253,71,-25,+ -31,-454,-674,-128,94,9,-104,-107,722,733,-152,720,197,728,+ 238,642,565,543,709,179,535,129,-855,61,-461,-473,-329,120,+ -24,348,-414,-367,-832,407,544,1055,335,177,393,378,1112,267,+ 206,102,835,0,-141,-366,-91,-457,-1433,-772,-1030,-325,-839,-892,+ 59,298,-966,-366,18,-17,184,-20,88,670,667,11,-582,-365,+ -133,580,405,-16,-856,-717,-411,-916,536,-297,-190,-731,-1175,-1069,+ -688,13,635,-526,464,914,204,610,395,221,35,222,411,1241,+ 588,440,252,33,819,268,781,-407,-967,-670,-313,-657,-989,-908,+ -490,450,-259,-607,-393,-935,-840,-283,-534,-545,-242,310,898,-277,+ -327,-577,225,1057,678,110,471,314,-576,-943,-47,508,-377,-3,+ 128,684,292,-433,-148,82,428,640,-197,1127,903,1038,-485,-601,+ 84,403,426,-160,-348,396,126,-179,-793,273,-429,-383,-247,692,+ 659,655,-131,784,-787,-658,18,-210,204,-91,-591,-991,-455,986,+ 561,1401,8,499,-226,-491,-6,851,180,-163,1151,396,1156,882,+ -466,-680,-903,-295,-92,-259,328,407,-419,105,-553,447,-201,-101,+ 376,87,442,-180,-189,83,-27,6,-303,205,-138,-574,490,2,+ 24,506,152,800,707,81,606,240,-238,377,-448,-589,-343,-1374,+ -598,-653,-1614,-876,-1205,-334,715,597,482,2179,1718,1714,1886,1368,+ 2867,2905,2444,1848,673,-1646,-1684,-3233,-5065,-6865,-9585,-10295,-5417,-1277,+ 3478,6346,4335,4609,3446,3767,4022,5088,7199,8665,7922,6889,3490,-785,+ -2306,-3304,-5940,-9933,-16265,-16653,-8628,-1586,5530,6284,3707,52,-2012,-892,+ 653,3839,7128,8113,9425,8333,8211,6847,4057,550,-2486,-5290,-3506,-5259,+ -11022,-16772,-15457,-6352,-659,4864,7098,1066,-967,-1892,-596,4122,6085,5690,+ 6904,7260,6736,7246,6295,3129,1253,-697,-2276,-3330,-7175,-15481,-17690,-14852,+ -2345,4735,6192,5928,-3111,-3994,-2669,75,6469,8838,7383,7322,5173,3630,+ 5726,5324,3931,4640,-146,-3266,-3911,-9258,-16725,-19369,-16732,-1971,4785,9489,+ 9877,-1331,-5099,-4566,-4052,3999,7756,8590,7982,4918,3307,4314,4474,4718,+ 6531,3403,-995,-2637,-7629,-16293,-19900,-17919,-7477,5082,10052,10005,2318,-5529,+ -6209,-4900,765,7503,7847,10234,8566,4445,3839,3608,4042,4005,4350,1437,+ -1236,-4554,-10088,-15375,-18563,-11687,-1369,6195,8911,7653,-5,-4761,-3049,-3800,+ 2516,8140,8682,10203,7810,3574,4008,4534,5536,5901,4413,-170,-2357,-4247,+ -11552,-18521,-18462,-12846,-1964,7453,8945,5922,-1261,-5767,-5862,-3469,2118,8621,+ 10806,10125,7387,4490,2911,4576,5350,5678,4599,894,-3070,-6029,-8621,-17331,+ -17989,-14428,-3438,5931,8171,7101,2738,-2749,-3556,-1806,1022,4690,7949,7585,+ 6585,5660,4609,5155,5016,6044,4057,1612,-955,-3999,-5693,-10700,-14993,-15491,+ -9264,-3680,1236,3234,3975,1226,863,2124,2450,2693,4067,5854,4995,3858,+ 3569,3040,3651,5323,5450,2929,-637,-2654,-4001,-4768,-5500,-6223,-6254,-5057,+ -3376,-2808,-3201,-3174,-2223,-1043,71,1228,2319,3119,3574,3988,4378,4385,+ 3884,3397,3391,3542,3270,2435,1396,561,174,96,-49,-563,-1421,-2257,+ -2686,-2701,-2476,-2208,-1970,-1771,-1454,-959,-312,368,963,1445,1789,1928,+ 1826,1541,1359,1089,840,484,147,-266,-276,-169,-122,-239,-526,-775,+ -974,-946,-645,-295,-109,-32,117,106,245,261,247,193,110,-152,+ -125,-171,-370,-545,-897,-912,-778,-569,-289,89,283,329,391,242,+ 48,-57,210,156,129,105,238,362,506,528,272,-7,-193,-195,+ -497,-835,-962,-842,-525,-242,-336,-358,-212,147,520,635,453,244,+ 98,108,229,212,210,156,77,133,93,36,-50,-284,-516,-623,+ -652,-484,-324,-110,-53,-43,-51,76,360,660,779,617,304,46,+ -148,-166,-32,-41,28,121,-9,-72,-230,-407,-496,-462,-408,-297,+ -156,22,160,190,180,220,345,455,566,485,285,128,-26,-125,+ -52,-40,-60,-51,-120,-180,-274,-344,-322,-266,-161,-47,8,26,+ 74,68,24,18,54,136,197,217,195,71,-29,-63,6,16,+ -38,-94,-198,-260,-200,-78,-28,-53,-94,-68,27,162,205,213,+ 140,133,187,216,214,176,40,-81,-172,-193,-190,-236,-319,-381,+ -426,-432,-335,-237,-127,-3,92,115,167,238,279,257,295,318,+ 359,441,351,184,90,70,34,2,-83,-229,-326,-358,-392,-346,+ -264,-130,-68,1,27,54,99,159,251,293,390,430,361,282,+ 199,157,103,44,-41,-106,-142,-192,+};+static const unsigned char CLIP4[2688] = {+ 255,255,228,84,88,18,0,18,226,128,187,184,178,128,178,3,232,184,3,0,+ 0,0,0,0,98,125,88,26,160,213,62,193,95,230,152,121,54,92,201,152,+ 72,188,9,251,12,39,205,229,118,233,120,113,177,220,25,25,20,162,48,234,+ 103,134,49,5,84,22,118,171,95,187,246,3,167,230,153,49,140,168,52,51,+ 188,231,182,176,61,211,179,136,112,88,19,192,97,146,234,250,112,64,155,11,+ 117,164,166,89,168,70,87,29,194,175,117,221,167,204,3,249,27,136,41,36,+ 200,129,151,81,27,224,146,59,71,200,51,145,180,26,188,75,68,121,88,194,+ 192,136,174,212,199,8,84,141,223,118,62,109,80,152,62,186,106,32,36,17,+ 211,69,88,185,212,100,180,123,105,172,212,93,94,236,214,31,28,1,60,1,+ 14,32,216,76,239,210,88,12,182,172,223,6,112,122,24,191,164,207,44,164,+ 39,134,24,31,13,228,184,185,245,43,130,110,24,59,185,141,189,182,57,113,+ 115,57,134,129,148,6,67,144,114,1,178,230,133,204,225,156,244,8,46,151,+ 212,48,184,46,36,7,70,237,34,70,140,208,12,84,176,210,238,20,69,44,+ 200,192,105,58,92,171,57,67,82,226,141,24,45,109,244,190,133,157,241,11,+ 170,68,33,9,171,64,223,86,205,11,225,180,193,245,105,92,216,222,178,126,+ 148,163,45,70,215,183,214,175,56,103,89,116,117,37,114,170,195,107,153,19,+ 235,105,97,218,107,69,61,16,140,135,253,221,110,208,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,255,255,228,116,3,51,227,56,37,34,235,34,+ 226,91,0,128,24,1,2,48,11,179,1,80,0,0,0,0,83,32,181,232,+ 93,136,167,53,245,19,67,174,10,108,148,57,43,214,245,65,33,228,126,174,+ 157,102,200,147,146,251,19,114,228,4,70,147,143,11,179,57,86,255,76,167,+ 144,17,146,183,142,68,116,169,195,136,238,0,199,151,190,128,169,154,67,232,+ 168,111,173,0,172,103,15,155,30,230,90,148,106,51,188,223,149,213,98,94,+ 65,171,222,88,175,216,28,124,95,85,121,11,206,233,174,163,209,80,136,201,+ 196,177,145,139,13,143,17,64,207,210,219,204,74,160,55,157,124,97,89,172,+ 39,197,150,216,0,184,185,118,148,115,189,185,230,228,26,133,123,133,1,69,+ 249,211,82,170,62,75,214,134,143,83,79,22,189,10,139,20,150,25,218,219,+ 111,149,11,192,36,14,170,234,80,249,160,112,84,242,113,247,203,21,26,90,+ 75,32,93,200,183,182,82,22,58,38,169,54,187,147,186,161,107,230,181,116,+ 196,89,40,221,133,108,205,213,85,203,24,208,34,250,41,75,10,4,86,128,+ 182,101,114,125,179,123,53,154,98,243,134,243,39,164,0,226,214,255,162,138,+ 1,120,220,44,131,19,168,11,72,90,226,168,121,108,47,205,67,164,106,24,+ 136,66,129,238,203,209,180,220,57,112,16,74,182,161,174,243,133,170,180,133,+ 140,223,130,52,117,141,80,10,14,62,127,91,41,16,222,177,108,158,73,66,+ 219,83,2,195,60,180,86,205,128,193,134,220,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,228,116,+ 21,32,133,238,187,81,27,2,229,30,34,130,81,48,181,18,128,88,83,0,+ 0,0,0,0,98,189,187,171,103,26,152,7,213,142,222,55,251,163,1,192,+ 15,45,154,200,20,94,215,184,148,105,203,28,55,17,96,56,195,249,84,52,+ 58,240,76,67,86,39,170,3,251,102,189,23,121,200,221,230,97,185,223,46,+ 24,254,190,185,27,3,179,65,200,196,8,33,165,97,121,240,48,206,40,232,+ 39,98,65,23,83,171,41,20,94,118,100,114,23,83,40,199,127,43,43,120,+ 175,149,135,97,91,101,155,210,186,29,35,29,111,24,227,124,70,61,35,24,+ 75,178,72,69,225,115,150,81,25,232,192,188,171,226,5,35,205,20,184,14,+ 40,66,138,200,134,194,251,144,33,157,209,116,19,42,131,122,28,108,198,22,+ 202,142,103,6,182,22,144,180,44,60,170,117,85,179,48,227,38,125,180,70,+ 181,80,16,40,198,241,47,184,28,104,163,87,154,214,151,119,88,155,6,133,+ 112,161,102,101,72,22,215,53,124,185,108,153,184,139,142,13,130,27,114,113,+ 52,66,147,105,105,61,220,194,208,228,189,66,49,28,229,180,247,184,239,178,+ 122,126,111,250,192,168,104,4,122,94,165,203,86,182,69,205,175,96,85,45,+ 78,33,137,200,0,106,238,59,163,54,84,104,140,143,92,208,149,52,34,117,+ 166,131,243,219,220,96,176,8,101,71,93,160,1,24,102,148,62,18,251,128,+ 4,73,56,120,173,65,147,98,41,241,162,147,97,85,150,38,11,26,55,109,+ 66,10,78,51,252,116,151,62,54,137,1,58,53,0,71,142,194,226,124,211,+ 82,178,247,39,236,150,63,124,45,75,154,69,93,212,59,142,239,225,157,27,+ 11,136,237,4,156,229,29,24,68,23,182,85,168,10,226,192,59,68,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,255,255,228,68,184,226,0,30,5,94,40,226,187,56,232,176,+ 142,88,0,0,0,0,227,87,226,98,106,131,246,45,120,207,26,140,204,232,+ 202,97,165,15,66,53,115,128,13,211,61,166,98,67,103,149,13,249,247,58,+ 237,218,130,222,143,74,222,247,74,88,44,137,133,232,192,121,234,224,83,88,+ 74,73,11,96,233,15,98,113,6,96,25,9,210,65,19,92,175,113,67,235,+ 105,70,86,109,208,99,161,232,143,141,222,137,72,234,247,229,53,66,49,80,+ 190,208,233,9,5,138,72,245,118,40,198,89,47,74,213,254,44,233,201,57,+ 15,129,72,212,91,53,197,2,64,15,14,5,159,233,120,203,58,10,160,5,+ 196,185,186,203,191,42,211,61,245,24,105,2,34,39,38,9,158,57,83,19,+ 214,28,207,20,57,50,88,26,119,76,50,151,137,209,173,22,3,0,1,19,+ 107,24,22,41,177,152,116,95,241,151,226,245,133,17,230,230,117,48,228,136,+ 93,126,103,17,15,238,109,151,253,91,40,17,183,80,72,51,108,237,172,245,+ 8,170,188,17,182,78,184,237,137,67,185,237,176,196,135,64,225,79,92,6,+ 240,188,71,205,157,111,26,26,94,216,173,1,35,89,189,193,168,30,197,172,+ 13,119,13,222,89,101,195,107,230,234,223,130,242,53,12,143,147,10,41,171,+ 39,82,91,60,92,180,1,123,68,241,42,28,137,168,95,72,51,102,100,4,+ 46,3,121,184,134,25,31,153,210,198,147,197,137,61,31,223,92,162,109,242,+ 137,251,207,98,179,215,84,18,187,159,139,56,92,152,96,235,118,65,238,224,+ 155,146,245,156,26,177,163,25,230,233,42,68,223,97,56,197,165,19,173,203,+ 78,45,238,190,147,245,117,150,26,115,52,103,67,239,206,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,255,255,228,116,1,11,1,91,+ 8,48,0,49,133,8,80,224,27,27,19,190,131,85,53,2,0,0,0,0,+ 184,166,83,44,84,211,8,167,144,148,137,231,113,191,137,160,206,35,217,165,+ 19,10,228,96,50,79,172,123,238,195,243,115,229,136,162,115,86,31,251,192,+ 54,8,25,138,172,200,12,107,160,188,111,11,95,204,137,172,208,48,91,205,+ 66,179,76,238,52,57,226,139,161,144,161,103,15,14,20,187,162,250,77,85,+ 29,85,144,187,241,3,101,73,112,84,200,94,113,222,132,207,67,182,123,129,+ 96,63,9,65,2,219,154,140,245,177,229,178,8,139,42,133,116,162,44,104,+ 138,88,239,171,5,82,86,233,177,98,125,204,156,163,181,215,254,89,248,98,+ 96,101,231,190,192,171,114,30,152,218,23,193,55,243,86,129,50,184,89,25,+ 130,199,232,247,74,82,117,158,108,236,233,222,177,6,76,72,203,32,52,170,+ 11,197,159,90,159,39,224,94,254,105,169,86,4,67,78,146,30,37,121,203,+ 71,68,7,159,12,113,102,210,119,102,23,195,13,197,223,4,75,126,222,244,+ 80,50,65,50,32,216,137,217,204,234,20,67,28,193,231,163,73,189,160,209,+ 83,253,179,182,86,97,22,40,81,95,233,161,187,52,97,51,124,86,209,246,+ 5,17,115,141,150,20,123,3,246,240,141,71,84,170,85,95,227,107,101,93,+ 155,156,242,76,67,139,201,18,147,57,60,209,27,130,122,71,157,129,110,180,+ 133,175,56,184,162,212,140,203,116,114,136,64,29,57,41,206,33,231,59,219,+ 111,98,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 255,255,228,68,179,190,129,181,46,139,91,1,51,62,19,62,2,33,0,0,+ 0,0,77,172,162,226,84,45,28,90,203,157,230,67,45,159,183,26,93,249,+ 60,67,225,104,162,28,195,19,62,68,21,81,207,160,149,188,231,71,168,224,+ 244,231,223,87,33,124,1,241,236,153,130,72,122,165,158,237,18,243,77,99,+ 28,158,248,37,172,230,152,105,65,79,192,37,202,145,29,152,142,216,192,75,+ 181,29,168,91,236,18,93,159,77,161,31,157,234,240,11,37,85,131,206,134,+ 207,225,217,225,121,180,76,80,209,202,88,0,70,195,64,54,143,76,113,78,+ 153,75,200,67,168,18,118,54,42,29,150,204,33,193,23,96,126,57,254,235,+ 2,142,74,72,71,121,9,211,15,78,94,9,151,235,71,36,208,255,57,151,+ 31,47,170,182,32,251,81,129,213,162,215,40,127,38,129,134,215,168,10,240,+ 32,86,5,188,124,177,138,73,238,173,148,57,24,77,75,12,84,29,96,60,+ 46,169,10,102,197,175,29,13,84,220,227,36,120,121,22,205,232,137,165,76,+ 185,184,132,239,39,86,60,168,99,81,84,111,83,19,207,98,193,1,57,134,+ 206,223,78,57,2,108,211,241,239,45,51,97,139,231,4,133,140,59,156,142,+ 53,117,180,83,92,181,172,211,65,16,169,253,146,49,123,133,11,26,26,151,+ 82,67,100,151,11,34,224,40,35,13,73,40,55,182,104,17,0,211,186,165,+ 235,150,222,210,93,96,72,58,250,134,86,165,155,58,228,230,246,84,94,36,+ 68,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,+};+static const int REF4[576] = {+ 0,0,0,-1,0,2,-2,0,6,10,-1,-1,39,-1,+ -4,54,55,24,-24,46,34,-4,29,86,160,235,-48,-6,+ 566,-57,-74,602,596,317,-30,221,494,363,172,999,1750,2150,+ -450,-252,3358,-369,-826,3111,3105,2702,2099,-352,2965,3852,-1642,4049,+ 10855,12929,-2689,-3104,22088,-760,-5409,26606,28079,21748,3817,19068,19989,-9127,+ 25756,26086,16893,10505,-19275,-3499,-14899,-19884,-11512,-3087,4440,-20177,-16202,-7756,+ -9518,11290,37429,18359,13180,11382,-56,7990,-5998,-5934,-26278,7427,12536,-40081,+ -21371,2709,29226,-1924,9300,24145,-10529,-9315,-5198,35315,-4511,-36529,-28125,-26743,+ 6029,-6435,19714,10010,-11472,25767,14358,22575,26399,23857,17535,-6004,19505,18767,+ 14219,26323,-3488,-15437,5132,31839,16715,9092,16710,-2281,-6326,16729,16605,-1429,+ -6829,5914,-8868,-22108,-16641,-17804,-25618,-7477,-24289,-16839,-21205,-6045,5837,-1401,+ -12651,2779,-26376,22757,-427,35794,2292,-7031,-10092,21638,-23114,-10760,-40168,20413,+ 37365,13903,8850,-4709,19490,5034,-37831,-27231,1155,1711,10316,-22182,34584,-1756,+ 39296,-9981,34572,-29007,4496,-50022,-11029,-38565,-638,2114,47123,50627,10512,878,+ -14103,3387,-41569,-21342,-532,36119,-5190,8645,-30523,10694,-12929,-3105,18072,16473,+ 12398,6753,-22551,7871,-31937,-5076,16751,11831,10581,-10862,-19398,587,-9537,12933,+ 24668,11738,8130,7513,-37872,-21231,-55911,881,38669,7955,49015,-2149,49117,-18706,+ -19053,-47895,-2576,-3739,26,-5768,2565,38373,-9515,899,2088,3235,-13616,2860,+ 357,9698,-16400,-9383,36887,-4011,-40384,19841,30097,-37874,-2181,37168,-24952,-17818,+ 42746,-8792,-38021,28773,12045,-16050,1458,-13706,23748,8161,-46707,30551,20312,-38430,+ 16801,-4859,4746,28233,-60597,19210,63206,-70578,2531,48324,-43509,19503,9866,-41740,+ 42930,1186,-47913,45083,6879,-55081,40318,27485,-61726,21144,41350,-54215,13336,40525,+ -47323,4267,26132,-15784,7392,-10879,3600,4819,-19766,50355,-30211,-65670,103804,-6820,+ -96134,74283,14093,-54596,24617,4109,8778,-7677,-28755,46383,4549,-37897,30962,10012,+ -24396,6530,27189,-2696,-46106,27056,41112,-39645,6673,33721,-11371,22526,32905,-10511,+ 23178,26286,-3356,20547,13081,-2787,-14941,31983,-15499,4173,17127,18311,10353,26802,+ 40227,17617,48847,23102,40416,2356,44557,-1784,-842,18367,-5703,-9405,8559,13424,+ -24120,52467,-20918,30569,19556,16026,12993,30355,10272,5916,17975,-13732,15108,-25920,+ 7382,-17609,-29386,5978,-38331,-11153,-12250,-5526,-31330,28480,-20712,2648,31348,-14204,+ 23278,12817,-1069,7015,-945,-13994,-17121,-8924,-46538,1651,-51175,-5995,-33711,-20104,+ -19698,-17296,-21124,-7019,-24801,-16965,-13500,-40402,-3770,-15228,-39661,-1553,-9262,-13044,+ 19718,-9384,30667,2991,22667,19724,42294,-4684,-1551,12537,37318,18476,-48837,-3645,+ 907,11088,-8596,-15973,-32553,12066,27795,12446,-30238,-10346,-5063,24907,-2957,5535,+ -44453,20001,6510,16006,-34567,9043,-12218,10706,-2189,15607,-7015,-9728,-1426,17749,+ 15352,-22914,-13663,13702,12723,2198,-14755,13144,1385,24430,-3917,941,-19508,212,+ 16296,-23104,-8695,-16337,36769,-8950,4723,-18055,18943,9169,-4778,-18721,-5519,28218,+ 2731,-22433,20457,10720,2374,-31942,25158,-7340,631,-8179,-3241,7889,8992,14733,+ -34424,6904,4990,-6057,-11227,-2289,22941,-24743,24196,-13713,29699,-44078,862,-15149,+ 36013,-4676,-7587,-22329,17127,10203,19646,-25468,-17770,-4331,49152,-847,15219,-25672,+ 423,-8227,33551,-27392,-19516,7577,46414,-6918,-35871,2520,11092,30083,11828,12655,+ -32667,17060,4515,32289,6139,-8383,-39731,19629,40049,-287,-30264,-21577,13834,16241,+ 6933,-14490,+};+static const unsigned char CLIP5[5737] = {+ 79,103,103,83,0,2,0,0,0,0,0,0,0,0,159,166,57,197,0,0,+ 0,0,6,34,155,24,1,30,1,118,111,114,98,105,115,0,0,0,0,1,+ 128,62,0,0,0,0,0,0,128,187,0,0,0,0,0,0,169,1,79,103,+ 103,83,0,0,0,0,0,0,0,0,0,0,159,166,57,197,1,0,0,0,+ 181,27,192,158,14,63,255,255,255,255,255,255,255,255,255,255,255,255,197,3,+ 118,111,114,98,105,115,12,0,0,0,76,97,118,102,54,49,46,55,46,49,+ 48,51,1,0,0,0,31,0,0,0,101,110,99,111,100,101,114,61,76,97,+ 118,99,54,49,46,49,57,46,49,48,49,32,108,105,98,118,111,114,98,105,+ 115,1,5,118,111,114,98,105,115,34,66,67,86,1,0,64,0,0,24,66,+ 16,42,5,173,99,142,58,200,21,33,140,25,162,160,66,202,41,199,29,66,+ 208,33,163,36,67,136,58,198,53,199,24,99,71,185,100,138,66,201,129,208,+ 144,85,0,0,64,0,0,164,28,87,80,114,73,45,231,156,115,163,24,87,+ 204,113,232,32,231,156,115,229,32,103,204,113,9,37,231,156,115,142,57,231,+ 146,114,142,49,231,156,115,163,24,87,14,114,41,45,231,156,115,129,20,71,+ 138,113,167,24,231,156,115,164,28,71,138,113,168,24,231,156,115,109,49,183,+ 146,114,206,57,231,156,115,230,32,135,82,114,174,53,231,156,115,164,24,103,+ 14,114,11,37,231,156,115,198,32,103,204,113,235,32,231,156,115,140,53,183,+ 212,114,206,57,231,156,115,206,57,231,156,115,206,57,231,156,115,140,49,231,+ 156,115,206,57,231,156,115,110,49,231,22,115,174,57,231,156,115,206,57,231,+ 28,115,206,57,231,156,115,32,52,100,21,0,144,0,0,160,161,40,138,226,+ 40,14,16,26,178,10,0,200,0,0,16,64,113,20,71,145,20,75,177,28,+ 203,209,36,13,8,13,89,5,0,0,1,0,8,0,0,160,72,134,164,72,+ 138,165,88,142,102,105,158,38,122,162,40,154,162,42,171,178,105,202,178,44,+ 203,178,235,186,46,16,26,178,10,0,72,0,0,80,81,20,197,112,20,7,+ 8,13,89,5,0,100,0,0,8,96,40,138,163,56,142,228,88,146,165,89,+ 158,7,132,134,172,2,0,128,0,0,4,0,0,80,12,71,177,20,77,241,+ 36,207,242,60,207,243,60,207,243,60,207,243,60,207,243,60,207,243,60,207,+ 243,60,13,8,13,89,5,0,32,0,0,0,130,40,100,24,3,66,67,86,+ 1,0,64,0,0,8,33,26,25,67,157,82,18,92,10,22,66,28,17,67,+ 29,66,206,67,169,165,131,224,41,133,37,99,210,83,172,65,8,33,124,239,+ 61,247,222,123,239,129,208,144,85,0,0,16,0,0,97,20,56,136,129,199,+ 36,8,33,132,98,20,39,68,113,166,32,8,33,132,229,36,88,202,121,232,+ 36,8,221,131,16,66,184,156,123,203,185,247,222,123,32,52,100,21,0,0,+ 8,0,192,32,132,16,66,8,33,132,16,66,8,41,164,148,82,72,41,166,+ 152,98,138,41,199,28,115,204,49,199,32,131,12,50,232,160,147,78,58,201,+ 164,146,78,58,202,36,163,142,82,107,41,181,20,83,76,177,229,22,99,173,+ 181,214,156,115,175,65,41,99,140,49,198,24,99,140,49,198,24,99,140,49,+ 198,24,35,8,13,89,5,0,128,0,0,16,6,25,100,144,65,8,33,132,+ 20,82,72,41,166,152,114,204,49,199,28,3,66,67,86,1,0,128,0,0,+ 2,0,0,0,28,69,82,36,71,114,36,71,146,36,201,146,44,73,147,60,+ 203,179,60,203,179,60,77,212,68,77,21,85,213,85,109,215,246,109,95,246,+ 109,223,213,101,223,246,101,219,213,101,93,150,101,221,181,109,93,214,93,93,+ 215,117,93,215,117,93,215,117,93,215,117,93,215,117,93,215,129,208,144,85,+ 0,128,4,0,128,142,228,56,142,228,56,142,228,72,142,164,72,10,16,26,+ 178,10,0,144,1,0,16,0,128,163,56,138,227,72,142,228,88,142,37,89,+ 146,38,105,150,103,121,150,167,121,154,168,137,30,16,26,178,10,0,0,4,+ 0,16,0,0,0,0,0,128,162,40,138,163,56,142,36,89,150,166,105,158,+ 167,122,162,40,154,170,170,138,166,169,170,170,106,154,166,105,154,166,105,154,+ 166,105,154,166,105,154,166,105,154,166,105,154,166,105,154,166,105,154,166,105,+ 154,166,105,154,166,105,2,161,33,171,0,0,9,0,0,29,199,113,28,71,+ 113,28,199,113,36,71,146,36,32,52,100,21,0,32,3,0,32,0,0,67,+ 81,28,69,114,44,199,146,52,75,179,60,203,211,68,207,244,92,81,54,117,+ 83,87,109,32,52,100,21,0,0,8,0,32,0,0,0,0,0,0,199,115,+ 60,199,115,60,201,147,60,203,115,60,199,147,60,73,211,52,77,211,52,77,+ 211,52,77,211,52,77,211,52,77,211,52,77,211,52,77,211,52,77,211,52,+ 77,211,52,77,211,52,77,211,52,77,211,52,77,211,52,77,3,66,67,86,+ 2,0,100,0,0,16,147,144,74,78,177,87,70,41,198,36,180,94,42,164,+ 20,147,212,123,168,152,98,76,58,237,169,66,6,41,7,185,135,74,33,165,+ 160,211,222,50,165,144,82,12,123,167,152,66,200,24,234,161,131,144,49,133,+ 176,215,218,115,207,189,247,30,8,13,89,17,0,68,1,0,0,198,32,198,+ 16,99,200,49,38,37,131,18,49,199,36,100,82,34,231,156,148,78,74,38,+ 165,164,86,90,204,164,132,152,74,139,145,115,78,74,39,37,147,82,90,11,+ 169,101,146,74,107,37,166,2,0,0,2,28,0,0,2,44,132,66,67,86,+ 4,0,81,0,0,136,49,72,41,164,20,82,74,49,167,152,67,74,41,199,+ 148,99,72,41,229,156,114,78,57,199,152,116,16,42,231,24,116,14,74,164,+ 148,114,142,57,167,156,115,18,50,7,149,115,14,66,38,157,0,0,128,0,+ 7,0,128,0,11,161,208,144,21,1,64,156,0,0,128,144,115,138,49,8,+ 17,99,16,66,9,41,133,80,82,170,156,147,210,65,73,169,131,146,82,73,+ 169,197,146,82,140,149,115,82,58,9,41,117,18,82,42,41,197,88,82,138,+ 45,164,84,99,105,45,215,210,82,141,45,198,156,91,140,189,134,148,98,45,+ 169,213,90,90,171,185,197,88,115,139,53,247,200,57,74,157,148,214,58,41,+ 173,165,214,106,77,173,213,218,73,105,45,164,214,98,105,45,198,214,98,205,+ 41,198,156,51,41,173,133,150,98,43,169,197,216,98,203,53,181,152,115,105,+ 45,215,20,99,207,41,198,158,107,172,185,199,156,131,48,173,213,156,90,203,+ 57,197,152,123,204,177,231,152,115,15,146,115,148,58,41,173,117,82,90,75,+ 173,213,154,90,171,53,147,210,90,105,173,198,144,90,139,45,198,156,91,139,+ 49,103,82,90,44,169,197,88,90,138,49,197,152,115,139,45,215,208,90,174,+ 41,198,156,83,139,57,199,90,131,146,177,246,94,90,171,57,197,152,123,138,+ 173,231,152,115,48,54,199,158,59,74,185,150,214,122,46,173,245,94,115,46,+ 66,214,220,139,104,45,231,212,106,15,42,198,158,115,206,193,216,220,131,16,+ 173,229,156,106,236,61,197,216,123,238,57,24,219,115,240,173,214,224,91,205,+ 69,200,156,131,208,185,248,166,123,48,70,213,218,131,204,181,8,153,115,16,+ 58,232,34,116,240,201,120,148,106,46,173,229,92,90,235,61,214,26,124,205,+ 57,8,209,90,238,41,198,222,83,139,189,215,158,155,176,189,7,33,90,203,+ 61,197,216,131,138,49,248,154,115,48,58,231,98,84,173,193,199,156,131,144,+ 181,22,161,123,47,74,231,32,148,170,181,7,153,107,80,50,215,34,116,240,+ 197,232,160,139,47,0,0,96,192,1,0,32,192,132,50,80,104,200,138,0,+ 32,78,0,128,65,200,57,165,24,132,74,41,8,161,132,148,66,40,41,85,+ 140,73,200,152,131,146,49,39,165,148,82,90,8,37,181,138,49,8,153,99,+ 82,50,199,164,132,18,90,42,37,180,18,74,105,169,148,210,90,40,165,181,+ 150,90,140,41,181,22,67,41,169,133,82,90,43,165,180,150,90,170,49,181,+ 86,99,196,152,148,204,57,41,153,99,82,74,41,173,149,82,90,171,28,147,+ 146,49,40,169,131,144,74,41,41,197,82,82,139,149,115,82,50,232,168,116,+ 16,74,42,169,196,84,82,105,173,164,210,82,41,165,197,146,82,108,41,197,+ 84,91,139,181,134,82,90,44,169,196,86,82,106,49,181,84,91,139,49,215,+ 136,49,41,25,115,82,50,231,164,148,82,82,43,165,180,150,57,39,165,131,+ 142,74,230,160,164,146,82,107,165,164,20,51,230,164,116,14,74,202,32,163,+ 82,82,138,45,165,18,83,40,165,181,146,82,108,165,164,214,90,140,181,166,+ 212,90,45,37,181,86,82,106,177,148,18,91,139,49,215,22,75,77,157,148,+ 214,74,42,49,134,82,90,107,49,230,154,90,139,49,148,18,91,41,41,198,+ 146,74,108,173,197,154,91,108,57,134,82,90,44,169,196,86,74,106,177,213,+ 150,99,107,177,230,212,82,141,41,181,154,91,108,185,198,148,83,143,181,246,+ 156,90,171,53,181,84,99,107,177,230,88,91,111,181,214,156,59,41,173,133,+ 82,90,43,37,197,152,90,139,177,197,88,115,40,37,182,146,82,108,165,164,+ 24,91,108,185,182,22,99,15,161,180,88,74,106,177,164,18,99,107,49,230,+ 24,91,142,169,181,90,91,108,185,166,212,98,173,181,246,28,91,110,61,165,+ 22,107,139,177,230,210,82,141,53,215,222,99,77,57,21,0,0,48,224,0,+ 0,16,96,66,25,40,52,100,37,0,16,5,0,0,24,195,24,99,16,26,+ 165,156,115,78,74,131,148,115,206,73,201,156,131,16,66,74,153,115,16,66,+ 72,41,115,78,66,74,45,101,206,65,72,169,181,80,74,74,173,197,22,74,+ 73,169,181,22,11,0,0,40,112,0,0,8,176,65,83,98,113,128,66,67,+ 86,2,0,81,0,0,136,49,74,49,6,161,49,70,41,231,32,52,198,40,+ 197,24,132,74,41,198,156,147,80,41,197,152,115,80,50,199,156,131,80,74,+ 230,156,115,16,74,9,33,148,82,74,74,33,132,82,74,73,169,0,0,128,+ 2,7,0,128,0,27,52,37,22,7,40,52,100,69,0,16,5,0,0,24,+ 99,156,51,206,33,10,157,165,206,82,36,169,163,214,81,107,40,165,26,75,+ 140,157,198,86,123,235,185,211,26,123,109,185,55,148,74,141,169,214,142,107,+ 203,185,213,222,105,77,61,183,28,11,0,0,59,112,0,0,59,176,16,10,+ 13,89,9,0,228,1,0,16,198,40,197,152,115,206,25,133,24,115,206,57,+ 231,12,82,140,57,231,156,115,138,49,231,156,131,16,66,197,152,115,206,65,+ 8,33,115,206,57,8,161,132,146,57,231,28,132,16,74,232,156,131,80,74,+ 41,165,116,206,65,8,161,148,82,58,231,32,132,82,74,41,157,115,16,74,+ 41,165,148,2,0,128,10,28,0,0,2,108,20,217,156,96,36,168,208,144,+ 149,0,64,30,0,0,96,12,66,206,73,105,173,97,204,57,8,45,213,216,+ 48,198,28,148,148,98,139,156,131,144,82,139,185,70,204,65,72,41,198,160,+ 59,40,41,181,24,108,240,157,132,148,90,139,57,7,147,82,139,53,231,222,+ 131,72,169,181,154,131,206,61,213,86,115,207,189,247,156,98,172,53,231,222,+ 115,47,0,0,119,193,1,0,236,192,70,145,205,9,70,130,10,13,89,9,+ 0,228,1,0,16,8,41,197,152,115,206,25,165,24,115,204,57,231,140,82,+ 140,49,230,156,115,138,49,198,156,115,206,65,197,24,99,206,57,7,33,99,+ 204,57,231,32,132,144,49,230,156,115,16,66,232,156,115,14,66,8,33,116,+ 206,57,7,33,132,16,58,231,160,131,16,66,8,157,115,16,66,8,33,132,+ 2,0,128,10,28,0,0,2,108,20,217,156,96,36,168,208,144,149,0,64,+ 56,0,0,0,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,+ 132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,+ 132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,+ 132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,+ 132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,+ 132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,+ 132,16,66,232,156,115,206,57,231,156,115,206,57,231,156,115,206,57,231,156,+ 115,206,57,39,0,200,183,194,1,192,255,193,198,25,86,146,206,10,71,131,+ 11,13,89,9,0,132,3,0,0,10,65,40,165,98,16,74,41,37,146,78,+ 58,41,157,147,80,74,41,145,131,82,74,233,164,148,82,74,9,165,148,82,+ 74,8,165,148,82,74,8,29,148,82,66,41,165,148,82,74,41,165,148,82,+ 74,41,165,148,82,58,41,165,148,82,74,41,165,148,202,57,41,165,147,82,+ 74,41,165,68,206,73,41,33,148,82,74,41,165,132,82,74,41,165,148,82,+ 74,41,165,148,82,74,41,165,148,82,74,41,165,132,16,66,8,33,132,16,+ 66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,+ 66,8,33,132,16,66,8,33,132,2,0,184,27,28,0,32,18,108,156,97,+ 37,233,172,112,52,184,208,144,149,0,64,72,0,0,160,20,115,142,74,8,+ 41,148,144,82,168,152,162,142,66,41,41,164,82,74,10,17,99,206,73,234,+ 28,133,80,82,40,169,131,202,57,8,165,164,148,66,42,33,117,206,65,7,+ 37,133,144,82,9,33,149,142,58,232,40,148,80,82,42,37,148,210,57,40,+ 165,132,20,74,74,41,149,144,66,72,169,116,148,82,40,37,149,148,66,42,+ 33,149,82,74,72,37,149,16,74,10,157,164,84,74,10,169,164,84,82,8,+ 157,116,144,66,39,37,164,146,74,10,169,147,148,82,42,37,165,148,74,74,+ 37,116,82,66,42,41,165,16,66,74,169,148,16,74,72,41,165,78,82,73,+ 169,164,20,66,40,33,133,148,82,74,37,165,146,74,74,33,149,84,66,9,+ 165,164,148,82,40,161,164,84,82,74,41,165,146,82,41,0,0,224,192,1,+ 0,32,192,8,58,201,168,178,8,27,77,184,240,0,20,26,178,18,0,32,+ 3,0,64,148,116,214,105,167,73,34,8,49,69,153,39,13,41,198,32,181,+ 164,44,195,16,83,146,137,241,20,99,140,57,40,70,67,14,49,228,148,24,+ 23,74,8,161,131,98,60,38,149,67,202,80,81,185,183,212,57,5,197,22,+ 99,124,239,177,23,1,0,0,8,2,0,4,132,4,0,24,32,40,152,1,+ 0,6,7,8,35,7,2,29,1,4,14,109,0,128,129,8,153,9,12,10,+ 161,193,65,38,0,60,64,68,72,5,0,137,9,138,210,133,46,8,33,130,+ 116,17,100,241,192,133,19,55,158,184,225,132,14,109,16,0,0,0,0,0,+ 16,0,240,1,0,144,80,0,17,17,209,204,85,88,92,96,100,104,108,112,+ 116,120,124,128,132,4,0,0,0,0,0,8,0,124,0,0,36,34,64,68,+ 68,52,115,21,22,23,24,25,26,27,28,29,30,31,32,33,1,0,0,0,+ 0,0,0,0,0,64,64,64,0,0,0,0,0,32,0,0,0,64,64,79,+ 103,103,83,0,4,0,25,0,0,0,0,0,0,159,166,57,197,2,0,0,+ 0,207,70,81,201,21,99,102,108,99,105,142,132,137,126,69,93,98,106,102,+ 105,103,100,104,102,129,109,132,55,84,214,107,91,123,194,2,219,81,43,254,+ 225,165,73,199,59,255,177,142,92,108,248,220,194,47,190,111,157,53,42,103,+ 38,244,151,246,191,253,122,52,216,98,157,175,86,204,15,163,116,240,254,245,+ 20,31,12,52,155,217,124,213,130,55,49,61,0,108,76,74,108,80,52,97,+ 57,83,159,217,106,221,120,93,234,184,206,254,16,152,33,245,240,172,254,226,+ 249,255,237,73,69,8,108,55,161,23,122,185,97,91,102,45,156,0,118,0,+ 202,77,218,213,101,69,253,18,95,250,235,163,23,215,61,208,26,188,122,71,+ 106,43,138,182,13,202,252,25,202,139,109,105,2,168,136,55,62,26,16,73,+ 52,29,250,27,253,21,193,95,241,24,43,162,18,56,128,149,186,241,56,249,+ 13,78,205,21,102,125,101,250,116,253,16,64,168,208,182,72,148,253,103,218,+ 154,15,145,197,246,115,134,51,84,57,173,95,196,231,43,108,3,213,6,0,+ 121,123,178,195,1,148,43,211,163,131,225,52,72,51,184,160,125,106,17,125,+ 16,158,6,161,73,140,116,76,125,210,246,246,214,124,240,156,17,187,3,77,+ 148,193,202,241,214,218,39,247,99,39,105,109,235,17,164,229,116,190,144,144,+ 98,0,138,137,92,148,41,150,147,43,91,199,191,31,77,36,27,5,195,255,+ 160,162,48,146,70,106,154,75,246,147,198,188,151,132,29,0,100,61,201,125,+ 158,220,165,216,36,112,75,0,11,112,19,32,167,71,111,6,7,162,242,243,+ 211,168,239,230,186,182,125,122,254,225,142,172,171,233,160,205,106,238,30,202,+ 201,103,124,161,102,83,136,89,34,94,187,165,161,5,49,81,16,118,254,30,+ 81,157,58,28,149,25,167,177,55,135,201,251,240,7,195,81,212,188,84,134,+ 90,79,80,38,202,98,53,84,119,20,153,107,103,239,108,124,55,87,132,85,+ 29,204,93,12,80,23,32,247,47,220,158,47,25,251,125,105,107,172,116,244,+ 135,107,94,220,255,142,199,116,192,60,207,243,188,136,55,219,109,132,19,219,+ 79,30,150,100,16,236,185,248,237,122,27,161,120,100,102,217,228,253,218,250,+ 243,197,83,103,41,38,231,230,40,198,29,31,219,69,158,63,255,247,241,209,+ 7,163,226,183,46,233,110,200,197,199,241,41,116,23,40,183,238,120,200,170,+ 218,234,163,17,136,91,53,96,189,27,56,44,160,93,192,9,48,1,46,128,+ 214,122,150,199,137,166,55,195,60,249,125,57,54,244,252,240,245,173,55,155,+ 159,43,148,62,150,226,231,139,15,8,239,36,28,202,88,110,144,202,237,65,+ 26,233,151,65,192,237,251,183,25,137,90,40,39,92,175,15,134,190,116,78,+ 63,140,158,28,125,172,62,144,245,246,36,34,99,103,198,231,182,196,149,206,+ 45,166,171,191,16,46,214,106,185,203,76,77,98,243,172,73,254,79,20,105,+ 18,196,173,100,21,47,19,138,162,227,253,116,115,103,173,14,65,134,226,21,+ 62,1,254,186,163,25,104,39,5,140,110,55,216,95,42,120,160,125,0,4,+ 180,29,46,219,158,0,224,121,250,75,2,204,165,32,173,245,161,95,17,212,+ 139,15,14,129,45,107,81,12,64,195,100,32,223,133,72,135,207,58,160,6,+ 115,208,178,187,71,43,94,204,204,117,217,12,233,173,41,22,53,221,172,139,+ 85,187,155,92,107,77,237,11,23,247,186,229,235,108,248,201,239,139,81,225,+ 63,140,82,155,172,119,1,214,249,45,115,151,200,4,103,62,186,88,163,158,+ 154,33,44,123,70,13,175,166,183,141,251,254,48,5,62,170,227,26,224,158,+ 12,145,159,223,13,116,155,50,225,220,227,183,206,165,102,189,203,172,93,0,+ 88,111,171,167,173,40,197,199,147,223,26,217,175,238,111,225,68,46,236,107,+ 240,97,255,84,190,47,134,76,172,166,198,52,213,15,136,130,49,131,222,161,+ 190,204,98,12,35,49,53,168,179,192,214,116,171,229,21,245,200,204,166,207,+ 129,114,186,245,178,134,49,156,37,107,243,168,249,9,80,28,152,25,17,142,+ 168,222,106,42,76,61,177,72,226,96,91,232,251,221,20,134,97,237,172,119,+ 251,26,103,248,129,10,39,93,22,173,3,214,74,75,49,161,40,21,234,180,+ 215,186,171,10,0,208,108,21,153,145,228,128,49,0,34,146,84,147,131,248,+ 238,107,56,37,203,38,88,247,158,12,251,30,143,22,142,14,236,143,10,163,+ 31,208,167,179,157,127,188,232,45,248,244,93,33,175,94,119,46,44,114,145,+ 137,27,121,94,27,119,62,66,106,111,150,217,166,50,102,33,37,88,108,180,+ 74,233,181,121,68,7,102,164,70,75,44,174,231,49,202,248,227,117,210,94,+ 223,35,58,35,123,69,182,156,40,252,124,94,154,245,66,84,167,100,57,167,+ 7,122,241,171,194,0,228,200,128,235,249,84,182,19,65,184,23,188,115,20,+ 165,181,167,219,247,52,114,248,42,196,69,62,215,201,125,90,182,78,130,176,+ 61,132,147,130,240,87,244,234,218,172,188,178,178,241,241,33,102,5,231,34,+ 127,103,116,239,59,2,44,65,245,164,151,87,2,0,181,105,170,52,189,120,+ 176,122,219,162,236,202,182,70,24,159,70,241,177,146,253,16,31,246,161,154,+ 105,141,34,22,223,66,19,198,81,78,15,146,214,160,169,110,242,245,75,128,+ 8,10,136,115,14,52,249,208,209,194,60,174,143,208,38,179,108,19,118,81,+ 29,72,62,127,156,139,129,227,12,156,135,252,198,70,49,144,106,118,48,28,+ 79,217,224,69,31,0,92,211,248,215,27,255,240,186,179,161,31,179,131,53,+ 109,124,43,185,77,183,64,152,231,100,219,83,92,142,71,92,109,239,221,222,+ 151,199,110,176,214,30,60,90,114,122,122,142,122,102,79,230,225,52,231,89,+ 111,146,55,98,50,217,121,59,191,29,159,73,122,52,247,248,3,186,109,64,+ 94,254,247,229,161,231,205,142,161,186,142,143,48,39,67,31,49,252,84,0,+ 248,184,131,4,0,84,229,3,0,200,209,119,249,132,214,183,117,92,19,40,+ 59,7,241,193,90,251,213,185,53,146,67,109,163,57,228,70,255,24,72,52,+ 87,239,135,222,254,217,34,62,231,113,121,51,34,115,203,144,239,27,131,233,+ 48,171,162,127,28,189,151,40,242,148,51,205,85,159,180,242,163,209,146,251,+ 180,101,230,60,223,106,108,98,151,167,138,152,130,241,75,102,141,147,207,253,+ 223,27,206,4,85,137,189,22,160,113,11,176,156,240,144,95,12,173,92,116,+ 211,186,176,123,92,228,161,52,157,190,90,187,183,47,127,135,174,211,167,230,+ 124,250,239,122,26,110,65,142,107,120,227,85,36,255,98,189,69,15,145,179,+ 164,54,19,176,232,123,183,245,82,251,177,16,133,38,75,155,59,63,127,71,+ 231,215,121,201,152,121,153,10,121,234,16,57,218,194,154,62,191,11,107,21,+ 58,79,16,5,0,60,85,213,132,243,9,0,191,0,11,208,68,79,79,254,+ 238,237,184,116,57,52,26,108,245,47,132,117,183,182,135,36,226,12,115,112,+ 233,38,249,207,109,126,18,188,175,186,149,126,180,49,106,41,203,112,252,100,+ 208,183,210,31,23,57,24,247,82,182,80,216,47,88,116,33,201,179,181,41,+ 109,169,101,255,66,120,189,127,58,217,121,80,126,218,132,86,105,84,216,107,+ 23,186,202,126,22,243,20,81,18,23,116,67,10,159,54,207,94,2,32,47,+ 11,80,23,128,120,53,216,232,167,139,118,231,56,201,122,248,127,28,134,90,+ 210,135,144,126,204,152,254,195,72,27,237,178,106,252,25,13,169,56,52,46,+ 253,9,121,94,30,163,79,175,174,126,216,210,224,235,179,53,9,83,9,110,+ 176,52,61,183,8,217,217,132,207,98,196,202,147,208,204,209,116,139,106,182,+ 162,79,43,58,110,81,180,201,9,108,76,48,0,100,65,165,125,63,78,114,+ 241,144,0,43,11,144,54,176,27,85,213,78,137,124,110,237,114,235,195,98,+ 50,244,66,255,180,173,121,190,178,42,85,43,19,78,175,239,218,99,122,90,+ 135,89,133,255,205,31,194,220,170,173,220,202,52,103,171,188,240,86,152,57,+ 59,67,180,110,103,81,23,162,84,24,250,249,52,99,118,236,106,167,190,216,+ 240,163,184,130,172,95,83,189,23,91,200,0,0,108,57,10,182,243,253,56,+ 68,27,201,48,64,102,1,154,6,91,154,247,163,61,79,237,221,123,117,187,+ 239,231,131,74,191,196,252,62,58,108,155,27,155,61,200,135,138,43,204,81,+ 100,81,129,73,222,230,16,173,174,137,158,21,111,193,186,19,224,208,92,150,+ 149,2,173,207,80,209,150,213,44,53,49,172,73,42,144,19,75,151,245,171,+ 117,214,63,58,241,244,146,64,238,180,77,92,108,145,162,137,1,100,45,213,+ 131,61,141,6,128,164,236,107,209,161,250,142,179,196,191,62,107,27,142,239,+ 93,180,157,202,173,253,198,21,175,105,186,134,183,125,126,224,115,251,233,82,+ 218,117,250,236,191,151,218,200,52,83,223,59,15,39,166,117,128,173,241,151,+ 53,137,203,163,156,167,173,56,9,203,147,235,197,157,115,118,66,167,0,248,+ 253,34,194,18,58,239,39,66,62,45,247,231,2,9,231,20,128,36,0,154,+ 120,2,3,220,6,192,197,111,109,26,43,245,182,109,219,206,93,210,207,222,+ 101,145,194,124,250,217,200,196,96,229,254,228,79,200,184,175,61,79,247,73,+ 172,255,69,188,10,59,11,169,40,1,59,162,127,163,23,201,137,186,194,173,+ 111,125,113,34,142,177,221,16,98,198,198,139,151,192,99,241,220,242,105,204,+ 36,65,32,17,192,248,196,47,231,133,69,235,234,170,156,53,96,150,146,29,+ 200,137,241,4,102,235,185,97,225,54,64,85,153,105,252,88,226,16,98,252,+ 244,204,68,31,72,69,9,10,254,246,65,2,40,131,6,6,117,177,215,4,+ 85,103,176,157,132,199,248,147,120,51,178,87,51,177,150,150,78,167,19,97,+ 255,244,41,82,207,168,72,235,204,85,146,27,22,0,121,225,176,96,97,193,+ 226,126,241,205,120,146,169,226,92,188,234,253,40,64,4,152,204,207,63,64,+ 211,151,122,190,160,200,0,144,42,227,152,82,183,111,26,79,221,80,125,142,+ 159,113,241,116,25,10,184,78,252,94,211,243,92,47,158,94,0,+};+static const int REF5[1536] = {+ -479,162,63,-515,-694,-245,-411,-106,361,565,299,828,1067,1150,+ 1887,2559,2818,2866,1390,-1481,-2807,-4859,-8436,-10315,-7914,-1760,1959,3560,+ 3303,2534,1131,2000,4474,6066,7508,7929,6448,2140,-1114,-4760,-7294,-12588,+ -13280,-6113,-349,5094,6157,1933,-734,-2130,1206,2955,4885,5865,6504,7388,+ 7013,5213,630,-1333,-3040,-6585,-15977,-17891,-6466,262,5050,4195,1902,-1594,+ -2491,229,3435,5939,5813,5081,6652,7589,4914,2015,-124,-1441,-8552,-14937,+ -18493,-6324,782,8355,3457,-67,-4540,-3846,1437,4227,6389,4862,4298,5135,+ 7307,5456,4332,208,-2477,-8535,-16785,-17069,-5208,2108,9261,1758,-1080,-4747,+ -3153,2075,6518,6796,3210,3302,4305,8133,9390,4412,-1965,-4461,-7355,-12757,+ -17387,-6035,627,8796,1543,-2319,-5521,-2248,2809,5482,6612,3644,2500,5113,+ 9648,9691,5230,-921,-4510,-7945,-13914,-16210,-7904,-1032,8018,3185,-1138,-4476,+ -3985,1898,6075,6802,3167,2410,5541,10091,10858,4636,-791,-5070,-6511,-12180,+ -15625,-9960,-1902,7701,1973,122,-3870,-2240,376,6494,6286,4507,2088,5869,+ 9350,9578,5308,592,-3467,-6171,-12642,-15616,-11484,-3168,6892,4908,1865,-3358,+ -2120,296,6350,5073,4423,2368,5642,7812,7134,3354,1574,-908,-4287,-12196,+ -16366,-14054,-5477,2998,4189,4055,58,-1731,-2118,747,2612,5048,4523,5526,+ 5223,4048,2290,407,-333,-344,-2105,-5104,-6901,-5299,-4480,-3122,-1658,-1151,+ 171,988,2101,2606,2639,2443,2891,3189,2989,2995,2654,2869,2945,2757,+ 1291,6,-1005,-1705,-2073,-1973,-2167,-2107,-1799,-1071,-18,1349,1860,2367,+ 2338,2212,1754,1494,1539,1238,710,334,-443,-986,-1161,-969,-1128,-668,+ -493,-287,-812,646,-20,815,277,3038,1788,6322,1479,3734,418,856,+ -3985,-6009,-6509,-7044,-3980,-3133,1001,1373,2254,706,1121,2502,3290,4445,+ 4138,3706,2162,-795,-5945,-9721,-16651,-12087,-6923,3293,5400,2881,222,-3862,+ -186,3157,7847,8608,6678,6704,5983,6082,3302,-89,-5523,-12074,-18351,-10254,+ -2910,8729,6245,2382,-1395,-4429,-132,3484,8591,6461,6467,3750,7531,6839,+ 3104,650,-5964,-10581,-20444,-14292,-3677,10287,6850,662,-4528,-3844,1974,6089,+ 9659,7375,4501,3975,6865,7718,3585,355,-8479,-15782,-18757,-7981,2411,9367,+ 4165,-4912,-3608,-1665,6124,8530,5074,4019,1390,5164,9087,7152,2541,-5340,+ -8258,-19571,-16159,-438,7979,4159,-1634,-5765,-2744,4625,6694,8561,955,1342,+ 4728,8251,10851,2318,-3573,-6766,-14806,-17990,-8033,1294,9590,-80,-6262,-2235,+ 1051,7484,8236,3569,1543,2414,8242,11520,4331,-1459,-6186,-11410,-16914,-12755,+ 1437,8927,665,-6019,-6007,855,8289,6745,5178,412,2652,8552,10403,6036,+ -74,-5217,-10048,-15357,-13798,-1464,7090,1615,-4255,-7136,361,7158,6999,6140,+ -206,1354,6461,9519,7310,-341,-5736,-8107,-16023,-13946,-1056,5378,1201,-3995,+ -6347,1240,7860,8472,5533,328,4448,7470,9344,6205,-2398,-6582,-11617,-14607,+ -11690,-464,9005,-1417,-5544,-4644,2448,9372,6187,5349,1887,4263,9024,6811,+ 2443,-5744,-8939,-14424,-14951,-2730,4707,1909,-3022,-5409,2443,6030,7853,6251,+ 2422,4792,7678,8229,3598,-3116,-8380,-14413,-16091,-4507,3150,1196,-2691,-5084,+ -208,4726,6112,7440,3520,4815,8577,7192,5186,-1081,-4470,-11489,-15491,-10223,+ -2749,5718,-1859,-3971,-3373,2531,7473,5597,6032,5430,5350,6903,5085,1967,+ -4831,-8473,-11652,-11250,-5423,2670,658,-2634,-881,-1430,5737,3535,5598,6249,+ 5361,7670,4274,613,-1096,-6464,-8166,-11957,-5836,-614,972,452,-2964,-604,+ 244,2746,5895,6174,5730,6309,2995,1735,-1266,-222,-2923,-8318,-7953,-6155,+ -1898,1109,-731,1232,-159,927,2069,3835,3158,4291,2945,3416,3625,1751,+ 1315,-4602,-6350,-10167,-6046,-2449,-1007,1708,9,757,2380,2278,3846,3788,+ 3745,5908,3345,4736,-1155,-4780,-6468,-10599,-5680,-3373,67,761,-760,1311,+ 1696,3130,4063,5061,4674,5710,3527,1636,-723,-5432,-7451,-6834,-4484,-2676,+ -1921,44,-520,969,2304,2460,3507,3677,4473,4352,4152,2328,69,-3088,+ -6294,-6295,-5540,-4930,-3135,-1118,-385,1914,2930,3561,3433,4974,4866,4939,+ 4082,1958,-1391,-5010,-7102,-7305,-6734,-4217,-2357,229,1429,2919,3577,3891,+ 4960,5471,5297,4554,2617,-810,-4796,-6937,-7201,-6802,-4714,-2582,-297,927,+ 2941,3585,3843,4696,5367,5423,4519,2279,-1185,-4910,-7085,-7187,-6944,-4547,+ -2423,-164,1357,3551,3604,4631,4769,5573,5371,4280,1846,-1714,-5202,-7088,+ -7319,-6597,-4652,-2205,-97,1519,3553,4135,4831,5114,5549,4924,3767,1163,+ -2406,-5686,-7391,-7359,-6484,-4462,-2001,369,2340,4124,4732,5091,5122,5230,+ 4484,3374,630,-3043,-5681,-7344,-7081,-6569,-4102,-1612,651,2930,4211,4770,+ 4994,5099,4860,4042,2428,-413,-3818,-5991,-7133,-6976,-5897,-3376,-1101,1362,+ 3483,5007,5008,5281,5431,4979,3825,1725,-1105,-4175,-6097,-6983,-7079,-5870,+ -3713,-1358,1043,2964,4281,5025,5449,5485,5057,3973,2183,-313,-2971,-4875,+ -5997,-6436,-5725,-4337,-2221,-121,1619,2808,3900,4685,5037,4926,4191,2638,+ 822,-1176,-3021,-4476,-5282,-5266,-4405,-3105,-1346,197,1587,2810,3588,3962,+ 3973,3467,2704,1485,219,-1055,-2217,-3112,-3522,-3403,-3030,-2269,-1159,35,+ 1014,1987,2158,2480,2469,2192,1723,965,20,-427,-992,-1565,-1933,-2057,+ -1656,-1282,-754,-512,-236,275,716,1052,1158,1259,1066,899,693,-3,+ -651,-702,-741,-638,-612,-673,-1052,-1076,-698,-561,-208,203,230,961,+ 1045,223,1189,1371,877,448,282,91,-142,-607,-372,-987,-964,-527,+ -350,-54,-350,-37,222,34,660,1103,1092,493,211,346,125,-369,+ -506,-837,-74,128,-138,-381,-661,-516,-269,193,505,169,379,501,+ 590,-336,-123,-265,-146,132,795,-704,-262,-978,-479,-564,-309,-349,+ 168,630,959,21,44,-409,1090,730,-301,524,409,306,-631,-452,+ -205,556,1260,-869,-468,946,124,-636,1116,-1412,-566,1369,-100,68,+ -568,10,-900,-870,-34,551,-450,-766,-455,19,186,-748,2238,328,+ -551,-122,-1379,-97,-428,-714,1101,-413,855,-770,487,192,220,1140,+ 2468,2904,-1223,-602,-1061,-3464,605,-970,-878,1070,1142,-786,-1087,-1749,+ 1142,-1039,119,4878,1625,-1227,-497,-1546,1174,1750,-935,-1914,2315,-676,+ 576,-321,668,-156,-2424,949,-304,454,-1421,1942,-901,811,3538,-1278,+ -2200,-1029,-584,2627,655,-2602,578,-1807,1179,1106,-2660,-3465,2136,-920,+ -988,901,1238,-1701,-205,-1934,1923,194,-2708,3690,-1307,607,-713,606,+ 2779,706,2198,-1728,854,-725,1224,-1422,1335,-1676,1589,-2670,-1959,2414,+ -2917,-1895,2550,-1864,910,1109,-8,-431,-2106,1166,-643,-91,1932,-268,+ 1547,-787,60,1318,2105,-692,-1233,421,-631,-693,2980,2269,-365,1563,+ -1559,82,1584,528,-1922,-336,-269,1388,1627,433,757,715,-948,-327,+ -705,508,582,24,-2166,-386,516,-1726,376,-359,994,-89,214,-556,+ -209,254,-25,-238,413,-251,-813,114,-869,661,1034,996,671,1260,+ -349,668,465,281,-1452,-1079,-1689,-179,-1242,-726,325,529,207,1872,+ 1584,2341,189,1535,1723,986,-1300,-2129,-4163,-7111,-10557,-8221,-2065,3450,+ 5024,4490,2690,4428,5202,7323,7110,5965,1784,-623,-3863,-8981,-17489,-14661,+ -1798,5721,6560,538,-1020,-1397,4448,7737,9571,7048,7695,4574,-1218,-3733,+ -5054,-8772,-18407,-12729,-761,4212,5061,-1516,-2183,297,4666,6025,7421,7729,+ 7193,2942,1988,-2310,-3163,-9669,-19497,-14190,-228,6999,7206,-2865,-2506,-1706,+ 6017,7140,8423,4216,6002,3534,5072,-785,-2445,-10065,-17667,-17535,-199,7928,+ 8538,-4998,-4328,-3183,6745,9229,8174,3217,5242,4767,5671,2205,-1767,-5665,+ -16926,-19780,-7976,7187,9219,720,-5028,-4198,2391,7918,9171,6654,3959,4834,+ 4574,3811,-222,-4097,-11317,-19606,-13555,1569,8346,7399,-4479,-4791,-2242,4642,+ 8818,8499,3522,3511,4665,5302,4163,-1438,-4525,-14769,-19733,-12069,4408,7911,+ 4287,-5805,-4690,-776,6786,9656,7625,5035,3606,4850,5572,4784,-2704,-5425,+ -13538,-17853,-12602,2350,7088,3813,-3175,-4168,-954,3858,8184,7295,5070,3037,+ 4230,5029,3387,-774,-4410,-8705,-15837,-15316,-4813,2100,5040,2675,767,811,+ 959,4324,4726,5430,5060,4295,4289,1836,-1433,-3637,-3282,-4779,-6342,-5924,+ -4456,-4264,-2864,-736,1062,1771,2278,2505,2720,2966,3567,3858,3931,3411,+ 1951,572,-240,-306,-865,-1690,-2875,-3066,-2804,-1979,-1388,-979,-309,441,+ 1572,2231,2004,1399,1295,796,298,-180,-457,-346,-298,-533,-818,-924,+ -661,-522,-272,-259,-136,202,213,250,6,-93,-195,-477,-752,-664,+ -339,191,434,458,288,-22,22,15,47,141,434,516,190,-86,+ -214,-576,-947,-893,-440,-303,-400,-52,505,619,333,130,194,224,+ 156,97,117,87,12,-213,-493,-613,-498,-229,-112,-106,6,395,+ 795,702,325,-96,-175,34,-9,54,-138,-262,-451,-468,-474,-307,+ -74,136,173,204,407,551,447,173,-44,-134,-14,-27,6,-111,+ -235,-359,-248,-96,27,13,34,19,41,103,209,250,131,-50,+ -128,-67,-29,-108,-278,-204,-54,-12,10,114,+};+static const unsigned char CLIP6[9207] = {+ 79,103,103,83,0,2,0,0,0,0,0,0,0,0,67,19,2,242,0,0,+ 0,0,233,123,159,239,1,51,127,70,76,65,67,1,0,0,1,102,76,97,+ 67,0,0,0,34,4,0,4,0,0,0,0,0,16,152,3,232,2,240,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,79,+ 103,103,83,0,0,0,0,0,0,0,0,0,0,67,19,2,242,1,0,0,+ 0,203,26,110,33,1,54,132,0,0,50,12,0,0,0,76,97,118,102,54,+ 49,46,55,46,49,48,51,1,0,0,0,26,0,0,0,101,110,99,111,100,+ 101,114,61,76,97,118,99,54,49,46,49,57,46,49,48,49,32,102,108,97,+ 99,79,103,103,83,0,4,128,12,0,0,0,0,0,0,67,19,2,242,2,+ 0,0,0,181,16,52,145,37,255,255,255,255,255,255,255,255,255,255,255,156,+ 255,255,255,255,255,255,255,255,255,255,255,87,255,255,255,255,255,255,255,255,+ 255,255,247,255,77,255,248,165,24,0,253,76,253,187,252,86,252,101,253,39,+ 255,172,0,7,254,222,230,182,7,191,60,85,219,26,66,51,205,15,64,255,+ 132,138,121,100,187,130,222,154,84,70,160,4,188,200,79,87,128,105,194,56,+ 20,64,125,202,168,225,17,0,241,172,210,78,52,182,106,174,169,196,55,139,+ 35,11,240,16,242,225,22,86,138,105,234,255,41,119,135,20,147,66,43,13,+ 4,81,167,54,200,36,74,162,91,21,68,188,110,38,18,172,20,209,75,98,+ 145,205,33,219,35,79,150,21,92,105,225,237,36,151,157,114,17,131,49,187,+ 230,150,182,245,92,233,99,137,21,222,236,67,243,54,22,66,93,95,62,6,+ 164,119,161,246,100,116,51,181,100,12,133,36,14,64,251,87,203,193,48,194,+ 196,112,163,75,81,131,24,181,87,27,52,90,138,69,26,17,146,214,184,23,+ 2,248,50,3,136,172,243,0,73,65,211,38,143,144,241,78,109,244,72,217,+ 70,159,233,167,113,211,121,71,216,101,186,252,38,65,153,201,251,17,153,139,+ 81,53,26,188,168,68,183,85,91,223,61,91,229,208,202,251,140,69,13,89,+ 187,189,37,182,123,26,224,1,234,181,242,9,199,31,75,251,20,45,80,18,+ 128,68,180,114,190,43,100,73,144,221,98,123,113,237,21,65,92,109,25,170,+ 82,99,170,215,72,100,250,207,35,231,74,241,51,1,210,171,90,53,109,207,+ 102,67,106,183,90,52,228,227,53,5,236,201,81,246,234,98,17,13,150,82,+ 59,229,169,173,65,29,90,127,225,118,222,236,149,253,84,251,10,42,50,242,+ 25,61,174,149,216,213,206,108,227,30,199,207,137,6,212,163,217,148,61,76,+ 195,0,136,184,133,3,164,132,9,3,20,30,73,140,107,106,195,217,249,136,+ 248,69,148,20,24,71,115,200,130,150,6,139,22,62,243,23,139,21,162,31,+ 238,121,89,51,222,77,53,3,163,86,125,235,6,230,16,76,203,150,221,65,+ 27,2,40,164,28,11,91,171,78,198,25,138,131,214,151,106,73,214,138,153,+ 56,208,115,238,102,143,73,106,44,4,2,162,201,168,136,191,154,49,217,200,+ 41,90,208,34,204,144,11,19,178,109,92,12,241,76,89,113,145,218,53,135,+ 200,70,65,113,49,255,12,40,16,108,103,86,227,72,13,65,137,133,232,62,+ 21,181,58,74,94,191,37,105,160,195,85,105,32,207,162,165,160,113,39,209,+ 236,166,82,206,209,169,43,113,176,197,249,223,41,60,229,19,55,232,190,212,+ 234,172,99,191,100,59,204,131,70,253,32,247,244,85,116,198,157,101,142,31,+ 180,210,139,154,167,196,72,168,146,245,80,214,175,156,129,201,148,52,163,73,+ 125,206,29,6,35,252,143,145,148,176,178,30,226,94,11,128,46,17,73,86,+ 233,105,170,200,163,162,34,220,233,108,48,123,207,232,240,92,30,143,24,145,+ 77,94,179,24,221,64,140,71,19,32,158,136,80,210,132,45,2,27,82,211,+ 217,135,201,209,148,28,121,19,58,52,112,225,34,21,21,28,172,107,128,88,+ 75,108,166,100,56,168,175,4,87,138,186,63,231,242,128,254,27,244,223,136,+ 109,224,83,11,61,199,232,28,238,186,43,66,10,245,105,124,51,246,117,115,+ 132,124,162,153,37,87,31,244,16,98,21,18,120,228,253,210,140,161,164,152,+ 42,92,156,94,83,62,97,212,213,65,25,92,194,158,180,56,24,51,154,85,+ 16,192,209,231,125,53,189,208,123,119,32,125,142,80,27,233,185,138,86,58,+ 13,108,178,51,126,192,138,0,218,121,148,125,145,103,71,72,109,252,43,251,+ 196,76,73,243,190,201,172,44,45,178,162,146,169,43,249,122,34,116,0,123,+ 71,97,69,146,98,22,183,173,69,120,113,22,218,137,52,214,86,149,195,230,+ 136,19,209,207,196,84,146,85,176,179,12,200,68,106,188,253,81,174,131,106,+ 61,193,149,217,200,170,247,123,162,223,218,225,224,121,196,71,154,149,49,101,+ 166,13,81,114,175,244,19,10,147,236,191,228,23,68,253,126,170,66,32,167,+ 112,117,38,124,158,9,62,166,108,203,154,130,118,51,202,19,148,92,93,144,+ 128,128,40,93,143,8,80,248,107,108,179,237,183,184,75,100,140,20,10,135,+ 167,185,15,170,229,36,40,129,61,152,236,169,205,19,247,221,97,126,223,73,+ 16,200,35,200,33,195,255,88,249,154,69,42,204,76,46,95,65,72,16,65,+ 15,136,200,152,31,110,78,150,219,90,18,198,154,243,116,56,148,107,31,153,+ 162,242,77,55,238,243,132,141,41,18,254,235,228,189,185,140,245,123,91,140,+ 247,14,255,237,111,66,210,113,133,252,247,74,196,53,64,164,102,109,132,204,+ 4,215,89,213,50,214,53,194,71,129,203,228,30,134,240,117,36,135,79,192,+ 138,144,72,115,76,52,212,130,37,233,112,190,21,46,8,160,150,136,151,157,+ 95,117,205,154,106,144,88,38,5,197,184,86,230,203,63,15,169,96,65,107,+ 74,45,85,203,100,101,68,223,130,6,32,58,24,85,8,235,166,157,21,33,+ 26,221,133,32,198,175,181,60,237,152,23,159,125,75,56,95,43,200,23,108,+ 78,10,104,115,128,182,186,200,241,252,140,149,181,43,237,132,206,30,224,66,+ 178,125,221,247,222,244,28,46,159,145,205,219,52,74,29,236,240,227,74,133,+ 72,225,221,24,57,43,42,88,219,158,17,40,209,38,140,61,136,149,202,180,+ 245,231,6,213,33,124,20,52,180,242,90,117,120,111,116,152,197,79,236,147,+ 151,209,78,196,53,31,242,67,152,187,11,231,130,172,64,211,158,110,155,233,+ 3,38,82,184,50,92,62,83,215,212,80,164,171,130,250,53,49,49,125,170,+ 75,221,135,166,42,112,196,145,120,199,17,35,191,85,113,181,249,65,184,72,+ 201,127,33,45,15,97,49,77,50,242,73,167,188,187,233,78,164,149,247,21,+ 213,84,247,130,83,166,134,139,66,99,16,202,133,78,45,91,105,73,198,138,+ 117,63,42,244,26,5,55,147,17,30,181,12,71,186,152,233,20,81,41,27,+ 26,9,30,236,58,11,118,55,116,252,216,10,88,123,10,109,109,83,154,86,+ 130,232,153,204,176,81,181,235,80,78,93,163,177,60,205,190,46,199,148,195,+ 216,4,110,176,173,65,255,6,94,70,42,11,3,133,199,242,194,14,28,161,+ 249,187,112,78,98,121,105,29,36,182,204,205,144,131,193,226,181,174,251,67,+ 9,21,95,56,65,196,194,232,70,40,88,53,106,138,208,225,90,102,57,153,+ 116,62,242,171,166,61,173,218,28,26,161,244,150,163,174,66,149,102,185,174,+ 77,196,169,16,102,16,134,33,220,82,59,10,131,212,226,110,169,27,3,105,+ 188,146,38,202,212,133,24,113,20,66,100,86,21,108,81,88,170,184,155,194,+ 93,144,186,195,37,76,147,247,74,250,39,79,188,62,221,11,225,231,93,6,+ 254,127,1,173,243,211,63,214,255,207,64,41,127,218,128,104,192,65,63,248,+ 249,173,125,175,161,24,22,193,218,136,39,81,136,60,0,228,61,208,121,23,+ 179,51,113,117,233,204,120,172,230,118,91,154,241,192,138,48,221,94,177,15,+ 165,44,175,247,50,102,182,45,65,142,172,130,19,32,166,242,111,14,86,59,+ 202,13,132,189,19,247,200,90,248,184,205,129,147,171,162,66,193,148,221,176,+ 159,29,91,91,103,22,251,130,166,119,223,112,175,77,89,147,253,8,246,224,+ 166,176,95,11,181,153,229,173,204,147,18,110,5,47,104,59,76,57,90,119,+ 101,221,48,224,100,82,155,55,94,171,72,236,163,46,189,24,57,25,60,34,+ 144,158,174,4,128,241,7,98,197,4,58,111,134,90,85,18,91,50,248,75,+ 61,56,47,55,100,220,70,44,104,255,119,190,210,150,30,91,69,237,210,83,+ 61,235,162,143,104,19,208,151,87,4,231,138,113,37,239,126,250,13,106,134,+ 99,229,49,111,31,13,44,81,4,228,134,98,208,133,244,198,106,99,153,122,+ 186,77,11,179,34,28,153,138,253,118,132,193,152,74,101,76,190,115,251,151,+ 213,3,213,12,170,76,35,181,154,170,151,16,71,210,242,90,172,109,163,164,+ 78,87,189,35,175,201,115,56,161,3,202,209,67,94,118,120,103,249,135,131,+ 147,19,144,52,25,120,177,83,39,7,108,38,132,104,94,200,187,134,17,194,+ 52,39,99,102,99,133,203,194,135,146,76,114,142,72,138,66,253,110,118,153,+ 1,220,33,79,26,48,9,181,219,168,142,11,65,72,73,195,221,112,10,160,+ 51,33,66,106,185,83,153,214,185,150,35,12,51,62,145,200,83,69,223,216,+ 172,237,53,83,211,189,153,13,151,25,63,246,33,48,210,245,85,148,151,218,+ 171,124,14,34,221,181,80,238,74,139,152,233,247,105,39,182,197,189,178,186,+ 14,212,100,225,94,74,111,177,206,171,244,133,68,107,20,15,47,91,54,69,+ 113,197,45,223,105,182,96,228,105,86,213,37,96,149,215,149,27,198,29,149,+ 17,126,64,82,157,72,61,77,226,64,187,170,116,196,220,53,152,180,134,169,+ 138,28,166,225,116,216,88,21,17,111,221,187,246,135,79,41,218,78,156,59,+ 119,150,11,77,17,137,252,80,225,171,86,32,191,201,164,3,62,94,159,106,+ 231,133,161,184,109,38,35,161,86,1,70,209,1,40,1,192,190,27,248,71,+ 75,238,137,152,240,135,107,153,44,94,95,95,116,245,79,193,30,23,24,176,+ 24,9,194,35,170,69,143,241,94,212,220,236,40,12,158,48,111,74,204,217,+ 27,25,231,28,38,19,209,32,240,179,19,203,54,147,59,84,101,79,243,23,+ 172,97,184,65,101,172,216,84,206,253,62,91,204,16,227,86,121,141,191,243,+ 81,24,44,187,152,132,174,55,181,86,251,54,198,108,24,1,131,48,145,139,+ 213,165,121,183,184,225,253,34,144,40,114,249,233,14,54,194,40,143,22,123,+ 51,254,13,194,34,217,45,165,51,204,151,103,55,79,69,92,14,217,108,180,+ 171,108,228,199,85,230,199,149,22,248,166,2,79,28,128,133,199,45,142,37,+ 229,169,79,202,33,100,71,144,249,60,39,170,208,167,109,196,192,129,186,233,+ 146,188,28,157,205,252,234,188,253,148,70,206,25,60,148,44,79,175,44,233,+ 9,244,26,75,137,19,136,51,77,22,189,146,132,70,98,38,21,54,134,223,+ 56,114,214,89,187,83,106,115,29,148,119,199,123,149,223,35,101,223,80,86,+ 158,42,92,233,213,13,97,67,108,90,54,83,250,109,229,38,178,200,71,201,+ 213,55,50,93,252,168,115,39,67,102,187,61,135,122,126,0,207,132,54,232,+ 106,89,55,35,227,14,101,229,220,158,152,239,147,98,62,193,136,153,223,184,+ 102,244,212,219,126,110,153,217,54,82,214,57,135,95,216,110,244,139,226,119,+ 157,221,164,47,114,138,144,237,38,224,119,17,114,14,41,246,14,253,7,151,+ 212,185,55,171,205,51,226,135,14,59,34,7,21,135,141,236,137,51,7,17,+ 187,82,231,160,204,85,146,2,16,18,135,62,1,56,248,145,240,153,0,153,+ 130,99,234,50,226,255,123,27,60,237,78,42,103,242,140,241,251,240,68,69,+ 164,213,194,98,111,165,240,223,14,25,143,114,205,10,17,165,101,250,170,87,+ 89,2,107,90,4,151,171,95,180,94,137,17,65,48,164,71,164,144,169,117,+ 255,226,197,34,177,99,175,54,131,154,96,66,225,23,96,36,165,173,57,21,+ 58,195,33,83,140,10,40,248,242,240,68,105,148,78,0,14,212,36,69,156,+ 128,89,145,197,181,248,240,234,148,213,128,186,236,178,126,147,147,48,129,19,+ 98,79,48,33,78,252,154,119,194,98,135,206,140,245,74,0,64,14,141,51,+ 46,68,172,227,33,192,70,146,209,12,63,175,65,145,232,135,165,84,149,29,+ 170,134,94,242,131,223,234,105,185,187,66,147,195,237,135,217,137,167,243,127,+ 43,243,157,139,207,236,84,51,211,61,201,140,119,43,6,109,24,140,192,189,+ 129,80,179,26,20,250,153,82,55,124,44,244,78,227,71,194,10,187,13,228,+ 42,16,190,202,178,107,252,198,56,141,42,50,195,31,134,215,52,10,3,4,+ 47,39,255,65,238,243,226,138,248,236,78,159,248,77,75,70,65,101,195,204,+ 138,66,80,112,34,82,141,138,176,205,153,27,202,233,122,170,213,89,243,78,+ 188,178,62,185,77,66,194,170,217,201,179,4,131,195,43,86,219,254,140,44,+ 64,98,33,103,44,46,31,160,144,253,1,157,129,216,21,32,64,37,31,123,+ 138,26,144,154,151,208,133,201,204,87,110,192,40,117,254,27,134,45,46,1,+ 17,141,120,249,68,56,86,207,254,75,88,127,160,64,74,30,76,122,146,80,+ 37,33,78,185,248,90,182,89,11,234,166,56,150,52,148,205,25,150,248,67,+ 132,156,197,152,238,73,153,144,116,135,168,185,208,22,222,142,184,102,227,10,+ 178,113,115,67,26,142,167,76,165,140,87,4,123,242,13,149,24,113,161,217,+ 132,126,64,254,43,137,102,125,141,54,202,95,242,236,8,96,187,214,122,156,+ 67,168,43,51,61,172,239,237,13,222,38,152,229,180,57,167,241,3,87,120,+ 144,75,199,43,42,4,149,134,43,207,35,168,125,138,52,140,51,69,161,208,+ 36,21,203,175,165,132,199,159,109,201,86,89,102,141,146,111,145,58,134,164,+ 229,132,252,128,43,202,107,234,44,125,187,12,76,211,14,110,206,61,157,34,+ 11,94,210,56,107,130,255,248,165,24,1,250,76,249,150,249,191,249,129,249,+ 26,249,15,248,230,248,102,230,35,170,191,42,66,131,211,240,89,159,43,192,+ 123,196,121,60,82,189,43,84,25,248,9,114,143,240,82,191,144,241,29,105,+ 87,103,108,100,160,196,96,163,207,207,151,193,129,47,40,34,50,145,167,86,+ 209,60,45,137,49,173,63,42,194,50,8,42,139,141,194,9,145,150,87,122,+ 101,35,196,196,131,245,49,52,4,109,43,199,49,28,131,72,201,214,174,84,+ 167,250,200,166,241,150,231,74,29,64,228,228,37,238,174,222,114,78,250,243,+ 223,83,175,145,171,170,102,191,152,83,108,192,206,176,56,133,113,233,236,56,+ 111,70,146,8,100,0,134,1,235,178,213,109,23,217,155,228,122,243,144,56,+ 57,32,12,173,43,54,38,125,240,140,200,105,64,68,243,85,114,68,128,199,+ 111,120,56,160,210,34,199,160,209,228,146,215,6,13,60,37,222,28,154,223,+ 119,73,249,33,223,140,35,231,93,118,161,74,61,234,74,228,249,119,180,190,+ 94,51,111,239,225,35,133,177,156,148,38,122,148,230,147,37,86,104,250,252,+ 151,174,196,28,47,163,55,117,105,165,244,131,203,251,28,216,149,209,123,158,+ 209,189,80,133,169,249,252,217,26,108,204,20,186,18,146,201,133,67,110,152,+ 207,116,140,30,2,64,126,46,168,171,22,158,28,188,62,103,2,103,77,11,+ 229,112,169,74,155,70,244,165,21,86,144,108,179,172,157,218,155,97,117,193,+ 179,133,34,44,50,41,70,109,164,189,4,166,18,242,86,9,134,4,13,236,+ 2,54,188,65,88,141,203,145,127,165,78,32,198,254,71,52,52,228,247,241,+ 145,118,74,32,18,169,31,155,226,239,213,25,96,214,185,162,47,37,228,60,+ 172,79,152,107,78,118,169,146,12,179,155,204,138,177,46,116,20,11,158,86,+ 32,163,155,34,110,115,125,176,222,181,109,220,195,185,134,31,214,85,231,104,+ 34,57,60,140,24,12,192,45,149,228,40,50,195,235,1,44,132,158,47,23,+ 94,176,73,142,232,143,129,212,67,214,120,97,105,238,67,48,82,166,76,217,+ 7,238,124,234,200,64,162,27,23,161,183,21,56,90,230,36,228,225,129,107,+ 72,117,155,193,140,8,169,43,133,180,33,10,216,159,100,161,43,34,254,200,+ 79,208,223,251,94,145,113,64,206,64,134,32,7,29,8,100,139,220,215,0,+ 219,43,46,185,37,139,58,7,82,103,160,248,162,81,23,171,116,221,252,86,+ 206,168,201,212,136,138,235,84,48,63,125,194,7,19,63,104,151,115,59,1,+ 66,244,135,159,3,99,81,105,242,110,188,134,119,142,77,130,154,213,141,210,+ 7,240,49,218,251,237,43,93,226,255,30,33,79,197,18,130,52,158,132,169,+ 118,77,68,38,70,174,16,210,70,37,84,31,206,137,97,144,243,158,159,50,+ 213,227,38,42,231,21,106,165,243,216,80,17,121,29,249,83,94,65,148,234,+ 174,184,149,88,22,61,35,20,0,239,41,187,162,25,116,211,85,62,0,9,+ 255,4,21,96,198,170,4,231,162,197,146,141,70,230,130,101,211,159,192,135,+ 134,66,173,185,132,252,166,200,124,231,191,232,216,108,172,125,206,102,180,28,+ 197,189,18,164,162,181,37,74,152,59,183,108,124,105,121,62,59,221,38,214,+ 20,190,45,177,173,14,74,245,205,17,157,140,215,189,105,210,31,252,132,9,+ 169,44,249,30,46,65,235,12,86,110,197,91,59,38,10,156,139,232,137,23,+ 243,151,204,185,211,180,97,76,132,164,30,133,89,225,142,53,108,232,66,230,+ 121,108,45,123,96,183,156,43,105,167,143,179,73,210,20,162,251,191,97,145,+ 212,244,237,159,173,237,140,254,175,108,242,155,245,192,193,112,139,214,40,45,+ 127,71,100,138,206,155,114,13,210,31,50,73,95,146,250,124,232,108,206,146,+ 6,41,57,177,66,4,25,146,144,21,24,116,122,110,29,194,227,40,95,84,+ 212,147,169,206,141,145,117,48,90,99,93,139,145,141,153,139,60,191,12,28,+ 74,124,136,151,151,30,225,167,173,194,165,114,106,28,172,175,185,27,221,186,+ 224,73,17,225,226,70,181,85,67,68,240,60,33,27,212,165,81,235,241,28,+ 156,37,112,40,77,178,72,199,146,192,228,58,40,178,136,206,188,171,204,68,+ 41,156,130,210,59,47,101,196,83,94,36,54,241,139,210,149,90,46,46,144,+ 177,126,215,125,36,185,77,252,205,26,34,187,44,197,38,67,74,18,79,67,+ 169,121,126,242,237,76,89,164,126,53,119,184,36,205,66,73,23,207,213,210,+ 243,170,107,56,174,226,239,94,52,199,42,27,187,150,205,16,98,173,97,212,+ 118,49,123,39,121,4,122,24,49,86,196,181,128,36,18,163,254,237,60,217,+ 253,159,220,117,107,231,71,164,195,89,226,243,240,138,94,51,100,164,32,223,+ 246,94,100,192,59,40,215,8,108,47,79,80,201,149,118,20,207,100,54,51,+ 78,80,80,232,92,224,211,76,238,201,131,36,78,108,195,92,242,194,175,150,+ 72,138,105,113,255,222,231,164,113,185,113,6,169,106,225,205,107,46,134,53,+ 116,88,45,19,212,196,156,107,138,236,109,186,201,12,219,94,209,8,69,37,+ 26,69,23,139,190,159,249,72,76,28,59,142,232,45,88,132,40,65,201,239,+ 13,137,70,205,34,153,30,244,114,12,103,6,8,48,103,242,103,33,152,95,+ 77,106,10,206,8,244,97,216,176,42,160,155,62,11,81,43,106,58,251,161,+ 34,150,179,200,160,24,193,30,35,141,210,253,50,65,51,190,175,41,121,36,+ 233,156,19,30,126,187,207,145,116,134,55,142,171,229,97,246,206,81,195,75,+ 154,66,246,79,56,63,51,118,207,91,180,73,134,31,144,251,2,17,202,221,+ 190,139,200,167,10,203,160,18,159,86,136,79,129,24,21,125,79,248,169,190,+ 66,172,251,165,234,216,77,85,58,31,125,170,18,98,39,227,205,170,162,141,+ 31,39,145,162,229,213,34,89,22,44,40,216,154,11,128,247,227,206,111,15,+ 215,102,142,32,136,22,75,72,120,35,32,161,39,98,245,113,169,107,82,62,+ 72,43,2,167,222,74,237,225,153,53,194,235,57,155,211,244,217,100,122,112,+ 82,155,159,208,67,176,112,226,17,41,232,170,182,205,95,26,195,114,1,3,+ 69,58,56,72,127,178,248,81,190,199,229,188,195,91,198,71,90,174,3,223,+ 103,50,202,208,145,135,31,32,97,233,55,59,220,149,32,129,255,124,158,76,+ 102,76,173,95,250,158,135,56,5,8,156,208,175,237,100,10,172,192,25,115,+ 86,108,171,243,71,248,142,58,168,122,135,43,66,95,78,150,183,139,140,94,+ 123,50,112,151,175,92,253,179,228,5,251,98,44,240,110,81,189,29,10,65,+ 147,151,223,35,222,186,134,144,17,45,71,173,220,54,185,71,72,126,149,253,+ 21,251,33,251,237,255,213,252,167,248,7,204,72,137,116,32,159,215,167,160,+ 39,127,147,4,137,9,69,218,9,73,16,9,122,134,233,24,177,231,89,53,+ 74,191,41,93,23,0,61,37,4,9,80,63,249,134,37,122,214,132,28,239,+ 26,155,124,123,182,191,49,55,8,206,102,91,13,206,118,81,142,198,79,18,+ 199,81,144,191,183,75,7,220,175,162,24,171,122,209,90,130,181,207,57,184,+ 68,85,18,139,107,84,210,160,130,195,80,55,34,100,141,168,71,65,201,241,+ 156,69,120,113,139,239,19,114,119,133,65,97,56,209,35,48,169,86,46,143,+ 184,236,122,165,208,185,44,189,86,185,27,115,46,56,108,86,255,92,110,186,+ 166,15,84,197,77,62,102,90,8,190,37,20,208,156,232,182,41,224,244,50,+ 11,234,143,139,227,115,178,75,97,227,60,49,171,216,32,44,42,135,130,106,+ 1,80,200,70,173,163,225,227,212,227,181,107,149,194,145,38,250,82,20,153,+ 140,59,9,252,19,109,72,13,33,248,146,223,186,92,205,125,140,213,246,33,+ 58,6,236,168,238,56,6,144,207,237,232,30,109,137,233,93,255,30,213,46,+ 249,169,83,206,30,225,112,13,118,184,9,152,16,26,232,151,191,21,129,109,+ 240,211,167,16,241,129,89,154,37,74,140,170,241,79,78,103,126,132,49,239,+ 250,173,26,113,97,3,23,120,237,104,141,25,105,243,61,32,181,68,4,210,+ 105,153,64,38,184,92,199,135,132,148,157,245,121,198,38,208,43,185,255,124,+ 47,19,61,17,103,40,43,117,200,138,121,55,223,242,32,234,161,6,250,63,+ 140,99,117,164,43,19,91,135,32,149,75,157,161,46,98,6,71,181,164,175,+ 130,169,4,206,79,72,167,130,229,174,36,114,140,138,57,28,239,236,10,218,+ 208,16,219,150,157,177,157,82,124,41,182,177,109,149,215,43,108,222,217,189,+ 39,83,70,245,74,79,165,16,218,171,217,121,121,88,140,201,101,250,250,255,+ 12,2,128,156,40,239,149,138,9,201,200,12,25,33,238,161,210,34,127,189,+ 205,254,150,159,126,27,28,207,234,127,82,106,80,33,22,193,126,151,214,157,+ 191,194,56,154,241,150,209,60,208,38,45,235,37,89,171,36,181,213,244,168,+ 12,227,176,130,115,41,162,4,19,165,105,201,38,20,204,231,32,233,49,171,+ 109,50,141,121,14,45,34,229,77,146,166,47,21,183,30,184,225,101,121,116,+ 106,45,36,168,119,255,231,33,145,144,16,181,173,25,192,78,125,99,146,222,+ 203,130,185,17,222,151,39,27,189,210,236,175,130,15,86,105,129,219,175,73,+ 100,25,76,168,167,250,238,18,67,20,231,149,22,38,30,209,57,115,171,1,+ 62,106,116,172,37,205,60,54,68,65,84,43,42,176,104,47,52,253,15,106,+ 58,206,178,146,54,232,105,102,185,135,99,172,207,41,245,162,47,114,186,104,+ 187,203,1,252,140,134,180,203,198,130,43,179,255,208,175,200,69,49,179,224,+ 137,39,59,120,127,178,132,11,195,242,171,138,188,199,120,60,240,183,254,252,+ 27,13,241,121,251,29,224,122,147,149,115,85,38,108,148,155,20,228,241,64,+ 88,9,226,155,90,24,167,131,241,193,231,60,255,216,249,145,39,73,159,133,+ 82,3,174,34,211,244,222,35,199,145,231,35,46,31,92,224,165,85,243,143,+ 217,104,108,86,137,246,30,10,149,221,90,178,237,72,72,122,34,247,32,21,+ 171,151,51,157,11,114,39,99,132,160,83,73,217,133,157,24,42,117,216,101,+ 102,251,59,168,247,87,42,68,190,73,118,234,137,240,134,181,172,77,26,71,+ 53,40,137,50,237,82,217,52,196,94,67,162,36,208,168,193,111,230,86,99,+ 219,26,84,53,73,231,104,99,108,91,71,13,105,109,137,154,98,246,156,116,+ 250,211,18,5,145,116,254,46,50,138,229,102,91,253,206,52,201,135,39,244,+ 214,191,193,154,232,167,22,205,52,70,75,66,69,160,130,59,241,65,2,51,+ 71,205,131,67,39,96,167,85,89,207,242,243,8,148,124,233,45,174,240,203,+ 113,11,40,12,59,10,231,22,168,253,167,157,18,102,176,112,99,111,4,53,+ 61,136,77,40,45,149,79,46,75,82,17,149,243,177,171,9,123,242,60,99,+ 64,157,232,37,9,69,113,158,194,204,117,237,54,63,116,109,155,18,204,117,+ 148,207,180,89,59,86,110,192,88,72,111,183,159,210,71,249,131,29,16,49,+ 29,147,208,8,184,53,222,168,206,215,50,160,91,234,8,25,26,127,71,231,+ 247,33,77,158,195,175,213,75,153,209,100,14,35,13,97,81,188,218,175,137,+ 1,76,9,141,109,55,84,112,224,4,119,52,227,74,109,101,100,22,34,0,+ 88,216,18,206,162,199,48,67,203,70,20,45,139,82,64,251,16,154,39,39,+ 215,16,188,102,68,244,162,217,111,238,74,9,99,31,91,98,29,33,83,213,+ 148,248,122,50,192,51,145,75,66,194,0,25,112,153,205,247,250,79,215,143,+ 150,82,165,222,67,178,73,42,183,24,192,187,204,26,15,237,45,160,232,28,+ 63,10,163,155,180,112,252,193,82,16,153,208,145,34,7,132,34,138,146,136,+ 18,185,54,72,136,25,190,77,66,134,44,247,24,127,74,179,220,139,218,164,+ 21,122,40,98,249,90,59,211,91,75,158,170,229,138,227,225,18,52,244,163,+ 44,107,172,132,146,218,153,206,245,45,0,189,80,5,109,167,190,170,86,157,+ 46,123,197,147,155,92,72,205,254,240,217,125,38,240,116,122,83,43,35,196,+ 94,202,82,52,188,202,18,170,19,17,30,215,179,105,137,228,89,85,167,59,+ 112,137,100,88,228,133,77,98,12,99,214,227,191,42,104,205,17,188,229,145,+ 12,37,92,246,64,109,92,157,223,222,161,100,79,155,111,171,45,161,78,184,+ 7,185,147,247,155,89,233,210,112,184,54,48,118,228,206,240,71,39,244,37,+ 47,77,21,170,144,19,103,68,89,122,211,225,103,15,149,233,97,64,168,232,+ 80,182,89,80,185,110,194,86,121,175,50,243,107,142,91,200,45,111,37,123,+ 156,164,57,145,85,94,234,134,54,118,141,8,74,182,79,150,129,139,52,25,+ 50,69,149,193,4,127,53,155,188,43,228,41,184,32,201,204,9,108,255,248,+ 165,24,2,243,78,241,93,237,89,234,114,235,94,236,8,238,76,244,208,255,+ 198,230,173,32,215,248,48,51,99,25,222,93,101,196,144,248,154,133,66,187,+ 80,169,86,104,34,47,60,27,95,217,194,86,175,47,153,28,23,77,198,172,+ 92,206,234,20,38,0,224,206,225,232,142,52,129,135,204,141,240,190,200,209,+ 132,35,35,251,13,195,26,182,118,63,87,42,124,66,222,119,113,123,99,157,+ 39,82,81,96,197,43,131,100,54,68,26,212,23,96,60,112,45,1,206,49,+ 62,158,17,26,142,28,244,65,176,64,144,253,161,100,250,72,32,88,130,188,+ 61,205,205,68,37,145,37,139,4,107,163,203,16,235,76,41,52,144,48,20,+ 82,12,239,68,93,83,229,187,234,117,152,167,233,76,238,226,90,194,176,32,+ 214,234,33,65,103,141,108,195,22,29,95,200,140,140,12,34,79,254,214,209,+ 143,190,77,255,14,136,248,181,22,164,35,237,202,60,154,202,43,242,150,121,+ 156,157,43,32,112,39,103,170,160,124,96,118,166,186,90,193,101,248,17,4,+ 54,0,103,112,69,113,19,66,230,163,248,160,101,75,108,29,99,120,68,228,+ 122,197,92,24,128,169,130,34,116,47,156,161,214,251,167,118,17,84,101,252,+ 206,121,151,95,151,210,94,222,91,170,28,38,130,3,18,129,31,174,81,28,+ 147,129,34,159,93,14,46,78,142,75,181,91,238,169,87,244,17,253,14,83,+ 151,170,147,201,27,219,135,94,218,44,74,216,186,173,205,203,222,84,1,90,+ 13,53,10,7,228,212,8,252,41,67,91,200,225,250,92,215,160,157,12,236,+ 181,28,51,72,108,246,204,100,18,7,24,34,246,53,145,125,164,147,170,102,+ 100,80,94,4,157,143,243,18,124,68,27,232,168,44,107,126,45,130,80,88,+ 210,4,72,204,188,120,40,61,134,146,9,18,165,202,98,225,121,65,208,2,+ 99,29,248,177,244,188,208,115,222,24,155,201,136,71,7,6,32,58,164,47,+ 216,25,30,142,246,151,232,68,52,155,130,18,3,40,25,210,112,78,68,200,+ 151,18,40,58,112,14,116,83,180,136,96,161,83,152,72,101,125,85,224,114,+ 7,228,17,184,17,195,6,226,70,53,5,22,18,163,105,91,183,192,158,136,+ 185,225,12,229,65,74,226,212,78,109,92,78,204,231,48,87,27,242,50,147,+ 144,114,99,99,114,2,144,34,253,129,185,41,100,18,190,72,216,250,186,9,+ 216,218,103,18,122,103,34,91,12,84,26,74,235,111,226,179,29,246,144,235,+ 194,144,46,224,85,128,185,155,211,57,161,113,124,124,69,97,166,200,29,200,+ 61,48,142,216,218,79,63,132,70,78,132,202,177,15,75,67,134,164,127,40,+ 177,28,20,9,19,121,17,57,209,68,206,133,132,73,174,52,229,171,70,93,+ 184,197,187,39,118,168,142,170,248,141,147,63,52,45,132,104,167,77,1,126,+ 167,87,204,109,44,218,148,149,204,60,52,29,16,123,38,158,150,224,186,224,+ 73,44,71,184,229,194,154,26,55,81,244,21,96,142,139,84,80,144,112,158,+ 74,140,169,85,21,74,11,223,88,12,93,193,203,124,250,4,65,37,136,74,+ 245,138,20,48,193,18,27,168,134,147,194,230,186,43,226,242,42,178,158,88,+ 194,69,215,14,178,136,225,209,216,179,180,13,144,99,22,140,213,181,102,40,+ 138,121,72,136,24,57,242,233,167,224,44,230,19,46,80,147,232,120,109,132,+ 31,252,228,141,195,177,15,200,164,19,101,50,55,7,61,130,61,115,146,40,+ 180,110,113,16,1,3,33,224,4,55,42,69,194,146,109,104,167,238,84,2,+ 40,60,37,116,153,15,178,244,229,145,0,152,35,160,72,143,195,18,209,82,+ 143,180,55,188,243,59,24,36,97,20,127,136,64,147,96,167,233,136,215,115,+ 8,121,136,105,205,105,81,47,181,46,105,39,36,167,148,124,249,106,91,138,+ 40,96,156,35,135,72,115,156,112,208,59,76,67,58,28,84,220,192,58,224,+ 159,214,67,34,58,46,64,147,253,144,71,165,46,158,29,214,142,76,213,142,+ 101,249,137,14,215,64,154,76,82,49,6,32,203,28,59,237,136,8,69,121,+ 192,63,194,0,218,47,198,234,73,232,142,43,139,118,186,143,126,86,103,90,+ 141,58,226,43,100,20,79,68,212,174,154,159,31,186,239,98,80,155,38,56,+ 130,121,100,54,115,95,207,112,72,60,54,167,207,87,234,144,210,6,172,84,+ 196,4,139,218,76,94,3,112,95,153,182,33,174,228,52,253,147,88,151,182,+ 136,116,114,156,33,67,184,237,53,3,152,246,49,95,194,201,130,239,164,196,+ 203,197,27,75,169,54,153,172,112,238,154,83,54,78,196,108,82,114,162,47,+ 14,168,202,45,169,2,133,172,244,163,20,126,169,189,87,147,85,95,237,184,+ 245,235,250,43,9,77,157,221,194,7,195,160,248,16,84,145,108,76,155,175,+ 81,111,237,226,54,151,52,144,237,12,195,216,254,8,1,112,43,78,1,100,+ 169,159,139,66,165,10,44,80,92,68,89,99,136,233,215,87,196,116,200,136,+ 169,97,24,84,109,121,90,186,208,61,31,56,77,151,115,44,253,165,233,188,+ 57,99,131,96,45,26,117,158,219,247,103,91,59,105,159,166,32,106,142,17,+ 156,84,48,82,236,85,6,117,143,188,74,10,66,108,73,25,192,146,58,70,+ 72,155,25,228,25,200,210,44,204,51,205,192,67,119,83,21,229,203,251,145,+ 50,58,67,198,76,77,204,207,78,223,94,136,240,147,114,41,166,127,34,202,+ 95,5,225,78,80,121,117,125,194,191,49,105,10,242,188,160,36,214,169,117,+ 86,252,179,239,19,228,160,70,124,50,248,6,162,81,226,27,107,99,168,166,+ 30,102,168,69,159,227,244,181,31,203,243,53,53,119,16,196,158,166,149,160,+ 229,247,238,95,88,83,146,175,141,63,62,155,144,109,245,30,9,220,82,165,+ 22,73,121,191,148,53,245,231,82,175,186,15,82,78,140,237,209,50,35,13,+ 154,210,73,14,145,108,36,229,225,110,131,241,102,92,202,74,228,97,54,89,+ 163,220,34,27,37,224,198,23,161,10,230,21,173,178,120,133,98,190,31,56,+ 226,95,231,50,41,137,119,125,201,41,37,46,63,116,47,192,148,192,96,27,+ 236,4,37,101,221,198,222,91,48,172,211,35,137,146,15,122,221,235,102,169,+ 223,101,33,204,195,189,187,229,50,33,106,206,33,82,10,125,102,229,136,54,+ 15,18,245,252,56,9,194,163,162,102,130,88,98,44,97,200,96,251,63,252,+ 223,90,220,213,255,26,86,133,33,237,139,63,231,161,96,126,135,76,64,171,+ 56,117,26,135,121,26,42,1,92,52,223,92,119,39,111,157,228,147,245,215,+ 98,36,64,88,192,132,139,20,108,252,125,201,13,52,160,160,128,171,114,233,+ 102,213,115,117,51,57,236,95,93,72,118,131,10,196,181,66,211,35,180,130,+ 79,201,245,223,153,146,92,148,165,4,19,39,109,234,148,109,206,243,227,28,+ 40,191,72,35,179,176,80,78,224,169,73,128,214,214,66,49,75,40,181,123,+ 121,127,11,75,52,107,179,112,87,109,162,156,38,199,151,34,173,184,27,94,+ 169,92,129,154,175,35,128,191,82,55,26,176,107,239,237,126,126,84,34,242,+ 156,21,21,78,154,38,25,73,118,41,82,68,45,152,118,57,32,18,27,127,+ 124,162,255,29,161,5,170,61,128,122,236,4,177,49,110,223,52,252,235,219,+ 173,151,36,170,196,108,67,150,16,117,103,160,174,8,96,170,153,185,124,41,+ 125,38,150,246,51,246,252,28,99,118,56,90,235,201,184,161,114,52,66,133,+ 183,12,242,192,233,226,79,149,127,190,88,33,78,123,17,168,10,160,90,237,+ 113,136,248,227,140,105,213,205,100,243,72,156,133,145,103,8,159,121,151,89,+ 232,182,186,96,175,232,162,138,248,194,215,27,22,106,74,200,224,200,98,86,+ 20,18,245,158,247,141,216,244,151,237,85,86,54,55,50,89,72,236,144,167,+ 74,252,175,91,130,253,214,150,108,132,223,197,6,239,22,138,114,62,39,69,+ 243,172,199,232,151,101,71,225,118,165,71,31,119,110,120,138,207,172,182,218,+ 121,222,112,26,97,204,171,100,156,114,244,114,248,210,96,85,23,13,120,114,+ 32,207,226,42,182,170,197,80,76,62,62,78,192,169,6,148,96,133,185,16,+ 55,37,17,195,38,199,202,35,116,89,27,148,170,41,84,24,174,161,36,47,+ 1,150,102,135,225,67,40,130,6,115,148,243,59,121,91,127,64,253,112,52,+ 187,130,160,135,227,215,187,122,88,131,12,245,172,219,180,19,157,34,27,28,+ 85,100,50,50,129,56,41,72,164,18,19,69,56,66,62,99,247,56,111,53,+ 106,181,23,107,189,128,192,254,78,28,151,30,72,148,8,154,208,119,15,194,+ 59,42,170,200,148,50,37,75,44,99,21,229,25,216,17,47,223,234,121,200,+ 18,46,60,79,69,197,28,117,122,148,26,162,80,246,84,109,85,181,70,125,+ 41,83,169,90,87,170,34,167,54,143,219,215,12,126,189,29,134,113,117,19,+ 71,32,155,62,121,105,208,33,234,8,3,230,145,190,37,85,77,36,113,196,+ 133,51,80,211,162,5,91,171,76,165,253,136,156,210,163,246,94,21,162,93,+ 180,25,101,248,130,146,107,160,158,252,109,193,229,69,142,5,51,86,143,209,+ 148,233,13,73,72,246,157,119,81,88,93,25,2,33,211,248,43,9,55,160,+ 79,202,124,80,179,59,146,73,17,149,38,216,241,174,41,129,187,211,153,58,+ 59,141,214,163,179,88,76,59,25,90,32,129,20,19,85,140,173,229,140,22,+ 145,40,85,187,187,38,208,128,80,31,43,74,39,235,13,36,16,27,180,237,+ 171,217,58,88,217,147,22,49,8,83,89,132,180,225,54,252,157,93,105,53,+ 155,224,134,190,14,239,114,139,42,248,61,7,170,39,49,79,76,80,217,72,+ 236,98,200,49,53,5,48,186,156,236,92,120,21,49,61,222,3,134,206,169,+ 120,198,188,57,181,24,151,212,31,230,82,197,73,30,196,8,218,130,192,74,+ 71,132,180,175,98,0,36,225,73,112,128,234,209,146,191,245,18,202,107,2,+ 118,132,38,129,68,85,141,168,119,162,140,212,132,194,55,6,180,41,147,49,+ 163,17,129,68,157,177,120,64,206,39,230,184,125,147,126,229,74,28,163,86,+ 79,183,204,31,180,198,37,2,66,235,65,75,12,45,42,27,192,12,57,10,+ 15,232,225,129,30,80,37,114,0,200,68,22,148,252,37,64,151,191,136,128,+ 86,200,40,32,111,193,109,108,252,48,82,234,241,11,32,158,217,201,243,245,+ 153,191,153,198,211,138,28,72,181,63,144,168,47,145,20,113,144,215,101,58,+ 202,66,84,205,55,81,41,204,120,227,101,237,46,67,110,171,173,56,249,103,+ 101,119,79,203,123,243,151,228,144,116,252,71,19,82,57,98,217,150,168,245,+ 216,136,153,59,242,24,70,204,61,200,102,253,254,102,101,54,128,250,125,150,+ 150,158,55,32,182,153,111,76,248,238,75,23,45,221,185,250,211,121,125,216,+ 4,0,133,75,152,103,105,190,210,46,75,244,15,169,68,83,183,22,136,101,+ 139,199,210,134,73,214,54,77,49,33,237,186,138,214,200,228,186,217,62,10,+ 213,130,180,37,50,255,203,103,107,86,208,56,46,39,90,238,117,83,21,122,+ 246,78,132,206,143,155,239,224,156,125,20,226,201,133,180,97,185,14,6,104,+ 140,72,85,34,131,208,37,22,135,192,120,48,8,7,112,214,179,128,51,15,+ 73,238,167,21,173,173,132,24,66,146,162,28,151,14,210,189,150,56,101,81,+ 254,106,157,203,196,155,39,105,174,85,136,242,64,101,241,248,90,21,52,42,+ 53,233,230,202,38,71,60,198,102,226,52,232,98,48,138,44,195,183,153,48,+ 164,205,18,131,80,35,149,195,104,82,11,130,16,94,59,182,135,86,67,227,+ 80,101,115,184,183,106,253,77,86,56,195,62,197,120,149,31,233,210,61,188,+ 159,193,52,73,172,231,203,176,115,186,93,55,36,188,229,20,107,229,63,146,+ 135,36,121,122,76,212,50,231,85,164,78,47,168,16,154,149,24,150,29,74,+ 35,137,228,95,31,65,232,226,23,7,160,138,80,26,136,66,249,228,118,67,+ 88,177,46,91,235,156,243,164,145,57,66,108,122,151,42,62,93,251,255,119,+ 139,139,5,160,66,185,72,83,109,211,255,39,118,232,237,40,209,172,148,154,+ 149,198,152,77,164,236,85,75,35,137,1,21,238,128,150,127,72,138,65,117,+ 152,78,26,2,240,178,28,71,112,156,32,6,214,49,157,66,57,166,236,109,+ 107,93,175,21,41,90,91,155,165,24,83,228,128,209,89,255,248,101,24,3,+ 127,18,78,20,243,21,66,21,113,21,142,21,37,20,212,20,94,19,151,230,+ 189,142,198,248,8,131,173,48,211,174,62,35,50,125,116,128,240,91,150,12,+ 253,86,217,83,79,14,133,39,208,183,117,166,3,58,129,42,161,167,112,5,+ 39,61,170,125,131,130,192,195,5,55,147,205,195,47,45,46,204,197,93,217,+ 232,66,125,70,215,55,250,169,84,56,241,215,249,222,77,203,251,17,4,135,+ 214,203,37,59,55,127,49,246,223,19,170,244,222,130,52,141,174,225,17,246,+ 210,118,34,109,116,248,118,48,113,237,169,133,162,22,54,61,90,220,101,217,+ 137,4,245,21,135,57,30,101,132,22,226,151,134,211,2,39,43,149,114,226,+ 103,38,91,164,60,141,34,103,158,7,148,119,137,127,129,87,124,47,118,223,+ 117,119,53,221,118,161,31,123,158,31,196,253,118,255,9,68,11,165,17,175,+ 158,90,104,184,193,157,150,241,196,193,103,215,41,40,23,111,232,79,87,152,+ 210,232,53,67,43,52,247,225,178,16,89,103,195,96,45,234,78,102,53,108,+ 243,106,222,87,165,109,207,201,65,23,98,231,160,177,213,219,146,75,228,183,+ 181,126,46,72,197,160,95,155,133,161,121,132,50,248,101,188,110,5,144,102,+ 80,140,97,235,254,73,27,54,94,52,23,16,67,195,240,143,86,19,36,36,+ 183,232,132,192,98,226,71,103,19,5,45,89,161,165,168,78,151,105,34,113,+ 63,57,164,155,12,224,139,+};+static const int REF6[800] = {+ -373,168,-250,-607,-642,-88,-468,-235,-83,219,-283,224,422,495,+ 560,1070,1088,1104,326,-888,-1497,-2091,-4144,-4716,-3704,-688,916,1866,+ 1645,1380,609,1013,2131,3169,3728,3977,3168,1114,-434,-1952,-3295,-5966,+ -6093,-2314,507,3183,3135,426,-1213,-2473,-1974,-1500,466,2880,4412,5102,+ 4566,3366,816,232,50,-1273,-5654,-6474,-1254,718,2254,770,-1354,-4818,+ -5176,-1262,1828,4288,4420,3165,3068,3058,2686,2054,1402,1558,-2065,-5064,+ -7019,-1546,854,3854,966,-1954,-7088,-6886,-1123,2354,4920,3662,2650,2074,+ 3080,2994,3242,1875,500,-2695,-6492,-6246,-1082,1624,4431,426,-3119,-7288,+ -6906,-579,3810,5834,2806,1449,740,3182,5014,3396,1322,-836,-2456,-4835,+ -6615,-1254,1506,4518,152,-3888,-8364,-6014,175,3980,5604,2053,952,1010,+ 3908,5534,4424,1564,-1368,-2913,-5578,-5626,-1162,818,3260,-186,-3254,-6184,+ -7283,-238,3338,5927,2281,268,944,4406,6938,4216,1890,-1760,-2487,-4359,+ -5038,-2136,533,3370,-444,-2362,-6351,-6118,-1968,3074,5437,2598,796,1615,+ 3564,5542,4261,2334,-1024,-2351,-4680,-4810,-2332,151,3216,494,-1642,-5630,+ -5722,-2528,2888,4818,2740,1361,1398,3060,3820,3846,2723,682,-1326,-4018,+ -5052,-4096,-1096,1634,975,24,-3914,-5421,-4042,-172,3526,3830,2658,1624,+ 2060,2233,3090,1750,1218,583,464,-152,-1384,-1822,-2170,-2082,-2248,-4278,+ -5085,-3740,-710,2075,2311,2184,1306,1053,820,1752,1960,2807,2858,2955,+ 2322,1207,218,-648,-1008,-1103,-1704,-2628,-3138,-2296,-1598,-478,168,724,+ 976,1201,1200,1208,1247,1147,1041,870,465,205,34,182,116,184,+ 99,-32,-770,-54,-412,0,-266,694,286,3090,629,2041,675,1028,+ -1485,-2322,-2944,-3083,-1736,-1358,492,308,830,75,247,734,1426,1986,+ 1996,1946,338,-447,-2832,-4419,-7740,-5108,-2449,3819,3083,2033,180,-1740,+ -1178,-300,1752,1838,2409,2472,2888,3024,2183,-49,-2234,-5350,-8154,-4152,+ -399,5201,3168,649,-2832,-5230,-4492,-1498,2419,4430,4726,2374,3640,2452,+ 1711,1444,-748,-3032,-7916,-5182,-27,6811,4418,-10,-3740,-5932,-4580,294,+ 3785,5804,3802,2675,2825,2556,2040,1360,-1520,-6108,-7348,-2574,3192,6462,+ 2736,-2242,-3792,-4406,-3280,403,2031,5296,2560,2516,3477,2480,1785,-1055,+ -1734,-7642,-7165,973,6410,4344,-222,-3187,-3762,-2764,-1990,2292,1988,3136,+ 3636,2456,4277,904,64,-550,-6126,-7938,-3920,2006,7702,2152,-2587,-3358,+ -2122,-1842,-644,1267,3305,2288,3147,3748,1512,1161,-1402,-3580,-7409,-5798,+ 2726,7103,3438,-1974,-4183,-1832,-1235,-1817,602,585,3784,3922,3282,2634,+ 459,-294,-2350,-7426,-6626,327,6168,4266,-773,-3472,-1773,-584,-1406,-432,+ 543,3602,3189,3516,2390,-275,-556,-2193,-6743,-6904,626,5332,4368,-127,+ -3331,-777,292,-1074,-924,66,4110,4085,3375,1494,-1130,-834,-3688,-6048,+ -5770,-24,6374,2750,90,-2533,-638,2156,-1754,-1782,1101,4286,4842,1965,+ -1015,-1968,-1963,-5231,-5868,-530,3766,3045,829,-730,417,1530,610,-2254,+ -2021,2675,6339,3408,259,-2432,-3098,-4435,-5986,-632,2394,2172,1582,-626,+ 498,796,290,-865,-2845,2033,6064,3851,1630,-2308,-1554,-3545,-5132,-3778,+ -752,4680,1296,292,-602,126,1724,-1538,-1654,1248,4002,3940,1828,-625,+ -2074,-2261,-3963,-3783,-1626,2505,3205,747,1138,-1170,951,-1214,-1793,180,+ 2025,5512,1664,-1009,-1252,-2358,-1586,-3978,-1192,1036,2209,2565,-20,162,+ -1264,-1018,-668,-456,1440,3772,1758,7,-972,-433,56,-2490,-2478,-1394,+ 1145,2808,938,489,-678,-1328,-1396,-1580,104,2002,1864,1722,950,734,+ 724,-1266,-1546,-3224,-1409,716,270,1562,-696,230,91,-1520,-181,45,+ 1718,3185,1466,2578,-738,-1938,-2712,-4178,-1978,-606,900,1048,930,1092,+ 986,404,-34,-347,584,2164,1689,1442,-530,-2594,-3017,-2798,-1033,-196,+ 392,1710,1338,1872,730,-152,-334,-1180,490,1284,1891,1386,-56,-1350,+ -2560,-1972,-1669,-794,-99,994,1158,1316,992,4,-482,334,1230,1630,+ 1561,1106,-550,-2126,-2906,-2839,-2281,-1110,262,1347,1805,2218,1738,915,+ 560,872,934,923,356,-752,-2502,-2892,-2894,-2360,-1142,196,1508,1974,+ 2800,2466,1580,876,498,430,230,-142,-1160,-2516,-3091,-2658,-2310,-1017,+ 240,1580,2214,3072,2524,1954,808,621,470,144,-374,-1570,-2549,-3121,+ -2638,-2234,-1070,360,1609,2476,3192,2762,2118,1043,608,384,-148,-707,+ -1864,-2826,-3230,-2625,-2156,-854,394,1792,2738,3258,2868,2055,1114,597,+ 248,-258,-1022,-2158,-2806,-3124,-2436,-2042,-578,706,1958,2952,3290,2709,+ 1794,900,343,-60,-654,-1486,-2462,-2931,-2869,-2286,-1536,-109,1052,2232,+ 3054,3448,+};+static const unsigned char CLIP7[796] = {+ 79,103,103,83,0,2,0,0,0,0,0,0,0,0,216,253,82,202,0,0,+ 0,0,197,21,127,78,1,19,79,112,117,115,72,101,97,100,1,1,56,1,+ 128,62,0,0,0,0,0,79,103,103,83,0,0,0,0,0,0,0,0,0,+ 0,216,253,82,202,1,0,0,0,57,160,94,72,1,61,79,112,117,115,84,+ 97,103,115,12,0,0,0,76,97,118,102,54,49,46,55,46,49,48,51,1,+ 0,0,0,29,0,0,0,101,110,99,111,100,101,114,61,76,97,118,99,54,+ 49,46,49,57,46,49,48,49,32,108,105,98,111,112,117,115,79,103,103,83,+ 0,4,56,76,0,0,0,0,0,0,216,253,82,202,2,0,0,0,189,206,+ 150,9,21,29,31,25,31,38,27,31,30,40,29,33,29,33,32,24,30,27,+ 30,22,26,15,72,131,17,98,252,13,209,202,210,145,105,71,5,216,36,114,+ 38,28,124,229,195,182,152,48,121,92,252,177,47,72,186,146,241,241,252,117,+ 170,244,125,15,245,85,108,123,1,131,108,247,175,175,12,255,219,104,22,18,+ 209,157,46,112,72,187,61,120,208,128,141,83,113,169,164,159,125,159,114,242,+ 183,30,182,135,109,214,41,68,44,72,184,77,9,37,183,70,28,42,7,34,+ 182,42,207,192,92,225,244,146,196,167,203,227,7,247,220,39,51,27,190,176,+ 72,184,194,230,249,163,208,93,95,7,27,219,7,141,153,83,87,229,25,126,+ 249,230,229,90,32,84,25,190,213,20,178,186,172,93,183,240,142,128,72,185,+ 158,55,165,63,120,72,128,11,120,38,152,37,182,188,59,253,21,239,175,47,+ 2,50,41,167,84,72,186,118,58,145,93,136,138,251,70,96,200,70,10,24,+ 215,115,0,236,118,167,17,186,34,156,87,16,107,132,127,128,72,186,87,70,+ 170,238,163,57,89,200,212,140,45,178,210,203,43,177,156,95,147,61,49,224,+ 208,104,152,62,247,204,72,184,86,47,47,155,184,173,129,188,255,63,51,238,+ 214,158,236,71,40,122,52,58,203,227,82,165,46,142,122,238,229,145,59,45,+ 203,115,181,192,46,244,72,174,23,46,215,109,113,145,170,238,80,145,163,180,+ 16,110,215,132,36,220,133,72,192,36,131,25,61,84,198,72,169,49,20,77,+ 252,203,121,30,11,252,98,175,187,93,140,249,60,253,46,220,105,149,18,188,+ 207,167,106,73,96,89,63,92,72,131,59,99,144,72,165,56,152,59,36,87,+ 252,199,117,58,24,89,225,52,171,196,220,74,153,242,192,126,176,72,148,55,+ 209,59,149,20,9,218,103,60,127,154,223,183,23,70,200,109,218,16,70,210,+ 106,123,52,185,144,159,129,73,43,156,72,150,82,154,134,226,225,129,6,119,+ 198,89,151,37,203,230,224,160,99,75,154,127,8,129,162,163,39,189,173,196,+ 252,160,72,133,128,143,58,76,243,145,17,123,7,244,161,253,137,239,5,225,+ 242,32,212,152,19,232,72,133,41,134,39,251,124,90,0,21,243,44,218,57,+ 201,101,198,88,214,69,84,9,24,254,20,49,12,44,50,120,72,188,29,114,+ 129,249,51,171,2,37,196,245,15,102,185,126,254,215,115,254,144,177,173,237,+ 46,66,8,72,187,11,176,69,81,58,76,61,69,176,161,150,139,166,168,84,+ 170,50,140,181,226,62,49,7,235,180,17,216,195,72,60,247,205,182,20,41,+ 159,44,119,174,105,233,64,97,136,62,77,201,222,95,120,72,54,244,126,31,+ 63,149,126,68,191,196,253,202,117,216,4,80,147,166,223,221,104,252,49,122,+ 128,72,4,188,64,26,230,247,201,238,175,163,105,40,15,128,+};+static const int REF7[4800] = {+ -134,-705,-311,87,-158,-96,135,470,359,572,572,566,333,172,+ 78,108,-95,-215,-337,-283,-196,-160,-141,106,120,240,295,485,+ 316,384,488,445,376,285,232,330,355,564,1175,1140,1176,1355,+ 1540,1538,1403,1266,1309,1268,1065,982,930,1004,969,830,570,364,+ -170,-1419,-1566,-2334,-2801,-3257,-4143,-4692,-5410,-6291,-6490,-6324,-6241,-5864,+ -5119,-4060,-2934,-939,607,1615,2521,3758,4604,6002,5687,6088,6076,5711,+ 5042,4418,3456,3218,3281,2032,1777,1887,1923,2524,2498,3796,4386,4883,+ 4558,5013,4848,5204,4770,4288,3632,3238,2201,1683,831,211,-620,-1188,+ -2020,-2320,-2726,-4450,-5957,-5546,-6418,-7422,-8410,-11137,-11914,-13425,-12542,-12054,+ -10827,-10004,-5625,-1308,723,3095,6287,8034,9236,9107,9309,8642,6986,3224,+ 3321,1226,-540,-2391,-2182,-2415,-2503,-2011,0,404,1373,2217,3436,4065,+ 4095,4317,4527,4303,3462,4728,3721,3265,2305,3536,3626,3201,3242,3575,+ 3586,3183,2772,2535,1556,-29,-850,-1163,-2821,-5048,-6025,-6319,-7382,-8448,+ -9777,-11124,-12567,-14721,-15111,-12763,-13686,-13228,-7793,-3737,-1466,335,3328,7107,+ 9359,9764,8250,9224,6258,5040,3489,2765,876,-948,-1505,-1249,-1272,-1274,+ -416,287,915,1689,2613,3420,3523,3503,4509,6036,5108,5266,4421,4484,+ 3623,4062,3570,3333,2487,2447,2776,2572,2119,1482,1354,748,209,-784,+ -1489,-2631,-3964,-4961,-5470,-6340,-7667,-8818,-10187,-11517,-12808,-14604,-16155,-14685,+ -13373,-9929,-6403,-3311,-807,2195,6091,8574,10280,8477,8426,7494,5918,3798,+ 2489,975,-441,-1533,-1553,-1355,-1338,-1096,-120,1103,3506,3249,4213,4363,+ 5312,5185,6115,5450,4781,3788,3465,3043,2904,2268,2124,2005,2000,2451,+ 3234,4194,2366,2374,1711,1707,87,-953,-2652,-3826,-5547,-5783,-6383,-7776,+ -9353,-9922,-10743,-12275,-15132,-14806,-13986,-11726,-9964,-4221,-1910,162,2441,7068,+ 9995,10976,8660,8092,7409,5246,3597,2265,1741,-936,-1615,-1327,-444,-1403,+ -784,177,1245,2392,3468,3895,4127,4145,4777,5318,4894,3836,3495,3134,+ 2567,2347,2235,2204,2248,2505,2947,3232,3442,3984,3730,2643,1834,1043,+ 8,-1440,-2711,-4125,-5169,-6345,-6639,-7337,-10347,-14714,-11870,-13829,-14884,-17084,+ -14557,-11626,-6519,-2522,1374,2308,3977,7911,11617,10964,8197,4196,4273,3456,+ 2141,-128,-850,-2020,-1532,-150,731,271,-154,1897,5515,5059,4737,4191,+ 4923,4477,4651,4323,3346,1860,1261,2203,2082,1346,787,1572,3324,4491,+ 3724,4097,4050,4075,3730,3088,1482,-73,-1575,-1926,-2931,-4253,-5553,-6117,+ -7191,-8951,-9784,-12590,-13620,-16010,-13543,-14004,-12213,-8425,-2742,-749,723,1173,+ 5835,7290,7929,5622,4474,2805,2376,2337,1403,-180,-1483,-631,372,692,+ 320,529,2097,5133,4833,5029,4157,4437,4253,4478,3522,2320,2055,3207,+ 2328,2389,1244,2233,4359,4520,4989,4464,4754,4604,4954,4149,2585,954,+ -162,-588,-1664,-3387,-6367,-6705,-6556,-6626,-8292,-9583,-11020,-11447,-11595,-11004,+ -10779,-10320,-7678,-3626,-379,863,1305,3251,5043,6453,5575,4316,2295,1721,+ 1713,1682,206,-1044,-1360,-309,460,678,621,1164,2752,3799,4632,3945,+ 3517,3269,3666,3489,3279,2987,2774,2319,2456,2849,3553,4320,4353,4655,+ 4828,5015,4660,4172,2910,1789,729,-129,-1356,-2881,-4660,-6130,-6386,-6697,+ -7464,-8881,-9926,-11133,-13047,-13540,-14138,-12136,-11588,-8357,-4827,-214,174,2913,+ 4491,7109,6974,6713,5331,4242,3045,2539,2014,278,-784,-1102,-280,-169,+ 9,48,1146,2183,3370,5129,4789,4206,3897,4692,4504,4163,3400,3223,+ 2812,2435,2982,3148,3465,3541,3964,4168,4268,3863,3486,2664,1677,744,+ -114,-1231,-2775,-4056,-5242,-5754,-6284,-6976,-7963,-9289,-11515,-12820,-13091,-13597,+ -14100,-12227,-9542,-5991,-2552,-510,1731,3587,5820,6982,7037,5658,4588,4089,+ 3573,2764,1208,170,-334,-94,85,209,-68,-187,420,1234,2080,2517,+ 2672,2930,3339,3865,5283,5523,4623,4438,5034,5434,5627,4808,4346,4210,+ 4169,4263,3872,1674,1041,713,304,-519,-1777,-2404,-1669,-2787,-3063,-3270,+ -3618,-4090,-5336,-4667,-4658,-5250,-6269,-6654,-6546,-6342,-6209,-5580,-4514,-3346,+ -2147,-2130,-1346,-169,440,457,512,98,43,-183,-153,-164,9,177,+ 8,391,856,1142,1501,1599,1861,2206,2562,2796,2891,2808,2763,3008,+ 3109,2857,2558,2421,3038,2874,2693,3133,2919,2798,2796,2756,2498,2114,+ 1595,1457,1150,748,1089,1199,605,342,305,292,280,-373,-632,-744,+ -1005,-1367,-1586,-1933,-2145,-2234,-2175,-1957,-2326,-2325,-1978,-1873,-1831,-1724,+ -1848,-1744,-1839,-1737,-1788,-2262,-1921,-1802,-2201,-1867,-1822,-1586,-1202,-465,+ -295,-49,171,737,1318,1530,1580,1828,2072,2210,2320,2356,2015,1554,+ 1736,1827,1737,1325,915,840,696,550,305,6,70,294,54,209,+ -375,-637,-266,-259,-415,-608,-961,-908,-817,-956,-1626,-1726,-1837,-1762,+ -1776,-1809,-1948,-2029,-1872,-1626,-1410,-1523,-1470,-1270,-1072,-864,-711,-633,+ -423,-291,-178,77,129,259,475,745,830,803,851,997,977,965,+ 955,1451,2642,1677,243,657,1296,3481,3907,2607,2424,3238,4121,6316,+ 3810,-340,250,1913,2988,1609,-879,-1656,-1534,-921,-548,-1461,-5473,-6633,+ -5407,-4460,-4862,-6782,-8303,-7646,-6610,-5626,-5296,-5635,-4639,-2620,-996,-429,+ -594,-175,1085,1713,1593,2129,3003,2626,2528,2742,2652,2155,1582,1580,+ 1595,2132,2533,2937,3856,4205,5684,5739,5305,5089,4747,4191,3731,2801,+ 2263,1499,908,769,375,-353,-2089,-2536,-2318,-3707,-4441,-5312,-6373,-8337,+ -8876,-8528,-10491,-13226,-12543,-10675,-9631,-6034,-3412,-956,613,4557,6782,7184,+ 6589,7327,7498,6186,4791,3683,2334,2232,2080,496,-394,-581,11,1920,+ 3700,3178,3003,3852,5710,6553,5608,5809,6119,6689,6829,6220,5190,4421,+ 4990,5570,4076,2669,2573,2608,2391,1513,668,-534,-1545,-2158,-4177,-5141,+ -5851,-7020,-8141,-9280,-10896,-13836,-15221,-15852,-15879,-15371,-12345,-7586,-4544,-2777,+ 1500,5815,8849,9194,9526,8885,7737,7308,6176,3398,1025,253,1078,411,+ -1949,-2025,-1014,174,2111,3855,3262,3188,4774,6951,7103,5515,5579,4814,+ 4965,4881,3832,2290,1768,2786,4514,4175,2812,3258,3955,4164,3514,985,+ -1216,-2012,-2848,-5218,-6708,-8026,-9600,-12003,-12962,-14321,-17420,-18711,-18249,-17087,+ -14777,-9509,-4511,-1708,630,5953,10282,11675,10435,9999,9467,6515,4963,3281,+ 840,-968,-1439,-1747,-2443,-2951,-1404,978,2171,2702,5090,7188,7731,8180,+ 8949,8554,7325,6485,6403,5196,3219,2680,3125,3395,3025,2732,2824,2852,+ 2866,2414,810,-1237,-2260,-3316,-5115,-6798,-8017,-9696,-11833,-13285,-14636,-16836,+ -18351,-17989,-16535,-13545,-7617,-2121,-576,882,6471,10861,11542,9827,8446,7900,+ 6409,4408,1921,-559,-1425,-1282,-1685,-2728,-2939,-923,1529,2845,4890,5952,+ 6885,7787,8963,8708,7030,5510,5440,5256,3495,1762,1438,2232,2905,2811,+ 2578,2721,3186,3594,2878,988,-835,-1740,-2785,-4472,-6268,-7653,-8915,-10386,+ -12333,-15450,-17363,-17625,-16396,-15707,-12806,-5223,228,2060,2798,7248,11818,11651,+ 7827,5314,5333,3330,709,-1135,-2516,-2736,-2364,-2061,-1607,-890,2476,5033,+ 6028,6878,7767,9503,10913,8651,6607,4220,3791,4182,2179,-400,-625,850,+ 2579,2717,2348,2961,4284,4995,4241,2352,490,-603,-1613,-3677,-6032,-7464,+ -8382,-9710,-11683,-14193,-16170,-16685,-15792,-14869,-13099,-9109,-2831,1660,2476,4084,+ 8347,10909,9464,5974,4667,3959,2641,912,-1282,-2365,-2326,-1799,-1193,-650,+ 996,3424,5341,6755,7286,8189,9306,8578,6557,4330,3471,3662,2273,262,+ -280,747,3120,4509,3117,2912,4351,5228,4441,1694,-421,-1158,-1887,-3888,+ -7315,-9710,-9005,-9383,-11749,-14538,-15842,-14814,-13651,-12726,-11158,-7008,-1413,2729,+ 3557,4541,7464,9749,8640,5319,3305,2684,2013,557,-1614,-2751,-1782,757,+ 324,148,1580,4377,6335,6311,6013,6568,7508,6775,4791,2810,2233,2345,+ 1930,2021,1675,1583,3335,5104,4534,3778,4937,4888,3520,1335,-250,-1554,+ -3730,-7115,-7640,-8377,-9453,-10708,-11658,-12818,-14282,-14369,-12498,-10482,-7749,-3464,+ -35,2718,4569,6431,8053,7722,5800,2545,1878,1290,-130,-2355,-2946,-1845,+ -439,-374,289,1739,3891,7404,7420,6870,7138,8632,8055,5515,3059,2204,+ 1884,1105,922,-51,34,1642,3308,4225,4861,4652,5159,5212,3660,1762,+ -246,-2332,-4561,-5721,-7000,-8410,-9535,-10764,-13271,-14872,-15728,-13724,-12552,-10178,+ -5037,-372,1836,4248,7235,9123,9136,6975,2700,1980,1140,-479,-3284,-4675,+ -3541,-1852,-979,1340,2593,5210,7362,8694,8640,7970,7338,6584,4817,2563,+ 805,373,627,685,2221,2014,2945,5179,6977,6391,4976,4452,4250,2737,+ -130,-3137,-5050,-5716,-6261,-7849,-9564,-11599,-14527,-15852,-16866,-15472,-15001,-11757,+ -5494,522,1589,2209,6702,10340,10285,5957,2657,2461,2506,206,-2985,-4773,+ -3613,-1343,-26,272,1169,4177,7366,8116,8231,8128,7224,8135,7123,4742,+ 2878,2561,4134,4526,2777,2376,3820,5391,5419,3479,1877,1575,1722,10,+ -3385,-5898,-5993,-6295,-8735,-9693,-13194,-15398,-16365,-15497,-15804,-15492,-11658,-4398,+ -94,845,2570,6853,9793,9858,6428,3541,2900,1481,-236,-1881,-3186,-3934,+ -1413,603,1343,1762,4111,6663,7672,8506,7477,6765,6511,6302,4988,2703,+ 2457,4849,4039,4084,5291,5171,5982,6092,5549,3467,1811,1201,-237,-2950,+ -4940,-5990,-7241,-9742,-11169,-12846,-14478,-15308,-15367,-13203,-11701,-8226,-4575,-1960,+ 243,2983,5629,6063,5082,2592,2903,1635,-96,-1419,-1842,-1691,-1150,69,+ 856,1511,2935,5320,7479,6211,6804,6816,7013,5770,4419,3209,3320,5178,+ 4088,3462,3222,4709,6738,5949,4433,4680,4516,3472,1847,307,-1527,-3376,+ -4173,-4419,-6500,-9100,-11919,-12497,-13215,-14462,-15496,-14227,-11344,-5327,-2587,-730,+ -1300,2964,6195,6918,3295,1135,489,1607,-403,-2089,-3208,-2626,-790,1257,+ 3229,3615,4250,6985,9591,8771,7194,6166,6432,5976,6015,3624,2992,3996,+ 5319,4615,3837,3454,5569,5767,4733,2989,1879,1512,776,-1145,-2987,-6313,+ -7024,-6668,-7861,-10145,-11624,-11639,-11894,-12305,-12591,-8888,-6960,-3590,-2641,309,+ 1997,4111,4429,4570,3000,2513,1255,-990,-1789,-879,-298,-735,-277,1636,+ 3526,4377,4669,5333,5504,6309,7531,5811,5245,4070,5035,5495,6345,3993,+ 4297,3791,5476,4612,3290,1502,1545,891,592,-1548,-2505,-3565,-3686,-4716,+ -5426,-7521,-9295,-9111,-9391,-10258,-12087,-11680,-9169,-6187,-4608,-3096,-1418,1264,+ 3508,3994,1960,2566,3216,2491,131,-1467,-948,593,250,393,499,1858,+ 3342,4230,4405,4051,4279,5483,5743,4693,3597,3505,4444,4934,4350,3627,+ 3440,4021,4328,3735,2480,1485,1067,839,-211,-1628,-2657,-3049,-2021,-2561,+ -4386,-5629,-5570,-5741,-7530,-9570,-9815,-7765,-7102,-6798,-5517,-3225,-933,405,+ 824,758,1934,1888,2246,976,758,773,1443,1245,1241,960,1595,2047,+ 2467,2302,2287,2515,3182,3113,2882,2557,2880,3216,3269,2838,2618,2451,+ 2498,2223,2288,2796,1705,1340,1548,2442,952,166,-452,-250,-607,-1159,+ -3043,-3994,-5367,-5464,-6050,-7105,-7734,-8685,-8821,-7449,-5450,-5009,-4564,-3226,+ -84,1163,1163,-50,1642,3663,3018,2048,-443,902,1111,2039,336,-84,+ -460,1903,3424,3063,2459,3430,5268,6641,5961,4985,4956,5564,6050,5330,+ 3661,2968,3147,3524,2847,1356,401,503,828,-58,-2594,-3394,-3983,-3510,+ -4745,-7491,-8246,-8404,-7620,-8124,-9165,-9534,-6837,-5248,-3911,-3803,-3016,-1143,+ 325,1510,1293,1495,1385,2929,2380,2915,1526,2359,2607,3489,3066,3154,+ 3443,4109,4815,5022,4747,4622,4853,5141,5148,5228,4490,4583,4221,4349,+ 3689,3054,2253,2197,1712,1261,-454,-1715,-2574,-2758,-3753,-5197,-7259,-7611,+ -7763,-7880,-8217,-7606,-6959,-5777,-4217,-3324,-2477,-2328,-1847,-479,-41,20,+ -110,-155,734,1612,1585,1762,1654,2165,2538,2893,2800,2900,3073,3724,+ 3559,3322,3106,3136,3386,3551,3306,3386,3612,3634,3818,3346,3077,2806,+ 2554,2379,2107,881,288,-449,-637,-1557,-2891,-4156,-5153,-5858,-6501,-6597,+ -7462,-6937,-6783,-5565,-5384,-5063,-5145,-4273,-3694,-2799,-2354,-1850,-1060,-239,+ 748,1355,1627,1661,1990,2461,2966,3412,3459,3810,4018,4002,4160,3974,+ 3811,3783,3694,4026,4256,3999,3947,3818,3725,3638,2989,2602,2106,1703,+ 1097,365,-418,-1128,-2314,-3312,-4554,-5401,-6241,-6787,-7076,-7295,-7153,-6914,+ -6338,-6049,-5656,-5460,-4660,-3710,-3022,-2099,-1130,-267,708,1495,2207,2718,+ 2957,3324,3774,4112,4488,4618,4626,4637,4589,4608,4507,4371,4371,4345,+ 4250,4337,4308,4110,4077,3767,3463,2966,2308,1809,1051,410,-544,-1874,+ -2824,-3987,-5122,-6098,-6869,-7259,-7359,-7686,-7707,-7125,-6637,-5923,-5749,-5188,+ -4535,-3576,-2628,-1599,-886,-26,812,1643,2348,2748,3089,3500,3948,4440,+ 4667,4707,4683,4723,4765,4804,4652,4566,4477,4442,4531,4459,4273,4076,+ 3777,3432,2948,2321,1688,991,271,-704,-1904,-2950,-4078,-5368,-6490,-7055,+ -7427,-7605,-7870,-7908,-7297,-6400,-5898,-5513,-5197,-4381,-3383,-2278,-1368,-591,+ 120,1045,1928,2609,3032,3311,3710,4218,4670,4907,4868,4792,4839,4906,+ 4897,4760,4547,4417,4379,4424,4328,4084,3815,3519,3161,2693,2080,1399,+ 711,-106,-1266,-2199,-3262,-4213,-5318,-6240,-6881,-7171,-7428,-7497,-7425,-7023,+ -6503,-6251,-5686,-5183,-4228,-3179,-2381,-1543,-717,101,1080,1805,2378,2880,+ 3259,3758,4248,4848,5140,5168,5114,5224,5232,5278,5053,4870,4672,4581,+ 4519,4328,4009,3679,3307,2915,2427,1763,1104,359,-525,-1747,-2675,-3731,+ -4877,-5778,-6585,-7091,-7469,-7699,-7724,-7538,-7282,-6767,-6356,-5892,-5200,-4383,+ -3335,-2188,-1429,-548,234,1157,1986,2641,3097,3664,4328,4886,5413,5559,+ 5689,5698,5789,5634,5624,5456,5220,4969,4802,4526,4272,3832,3499,3030,+ 2513,1982,1313,619,-300,-1356,-2256,-3376,-4525,-5346,-6256,-6857,-7335,-7715,+ -7820,-7719,-7274,-6963,-6541,-6134,-5509,-4790,-3761,-2718,-1866,-1003,-155,733,+ 1582,2234,2883,3491,4056,4632,5110,5417,5649,5570,5495,5498,5424,5258,+ 5164,5086,4835,4543,4244,3891,3514,2986,2507,1954,1341,681,-152,-1187,+ -2047,-3221,-4067,-4952,-5959,-6534,-7104,-7406,-7549,-7399,-7143,-6868,-6584,-6088,+ -5563,-4914,-4121,-3099,-2077,-1219,-395,558,1562,2324,3001,3610,4235,4768,+ 5251,5658,5908,6004,5843,5665,5542,5361,5125,4931,4607,4386,4222,3822,+ 3414,2876,2380,1882,1296,557,-264,-1105,-2096,-2901,-3758,-4608,-5372,-6120,+ -6588,-7008,-7243,-7305,-7212,-7011,-6666,-6301,-5826,-5266,-4623,-3838,-2965,-2075,+ -1161,-312,578,1414,2179,2881,3479,4040,4570,4983,5341,5585,5671,5795,+ 5849,5643,5550,5499,5275,5027,4688,4389,4094,3617,3134,2635,2049,1439,+ 694,38,-690,-1549,-2322,-3123,-3822,-4542,-5232,-5773,-6090,-6393,-6557,-6628,+ -6480,-6291,-6021,-5665,-5232,-4753,-4184,-3523,-2775,-2006,-1235,-458,301,1040,+ 1742,2312,2720,3252,3693,3952,4170,4366,4673,4861,4904,4875,5075,5027,+ 4963,4737,4581,4318,4003,3662,3314,2842,2311,1672,1133,392,-265,-893,+ -1527,-2118,-2740,-3386,-3868,-4384,-4766,-5065,-5337,-5452,-5543,-5508,-5315,-5024,+ -4788,-4452,-4064,-3613,-3061,-2436,-1875,-1259,-661,-58,573,1077,1577,2066,+ 2502,2903,3230,3526,3797,4013,4186,4297,4357,4297,4166,4068,3917,3655,+ 3439,3118,2697,2346,2036,1616,1117,639,169,-256,-646,-1044,-1367,-1770,+ -2131,-2342,-2675,-2839,-3175,-3331,-3431,-3352,-3380,-3463,-3403,-3200,-3098,-2805,+ -2575,-2283,-1966,-1691,-1301,-948,-642,-296,57,355,656,965,1371,1456,+ 1795,2108,2126,2272,2247,2246,2341,2346,2205,2155,2054,2113,1927,1736,+ 1511,1369,1090,840,614,375,112,-75,-281,-470,-625,-789,-943,-1195,+ -1437,-1443,-1534,-1655,-1807,-1916,-2062,-1950,-2009,-1832,-1735,-1891,-1645,-1481,+ -1098,-1005,-800,-594,-383,-193,-45,120,85,191,320,531,584,734,+ 809,897,955,982,978,1133,1219,1115,1128,1183,1090,1219,1184,1056,+ 1011,987,878,792,649,551,479,376,296,109,-212,-346,-545,-860,+ -870,-758,-897,-999,-916,-936,-818,-750,-726,-726,-680,-626,-610,-546,+ -430,-636,-862,-584,-651,-465,-451,-441,-249,-165,-87,-40,-78,-1,+ 135,168,391,783,451,733,679,594,631,575,587,1054,890,1099,+ 727,691,683,554,642,1051,1232,1033,1063,870,847,746,638,593,+ 385,315,187,-238,-118,-323,-343,-486,-447,-504,-609,-551,-607,-519,+ -570,-465,-481,-985,-980,-809,-873,-876,-706,-828,-749,-486,-105,-413,+ -90,-196,-34,99,93,247,177,243,263,354,312,394,436,419,+ 388,381,816,701,614,662,585,460,475,320,332,153,99,141,+ 113,4,-135,-133,-191,-340,-399,-456,-498,-451,-561,-607,-630,-233,+ -698,-397,-579,-533,-255,-414,300,-461,329,-417,-76,-183,-232,-60,+ -623,-112,-470,-26,-528,135,-254,6,-323,64,32,100,-26,347,+ -443,641,173,-125,783,117,341,300,344,158,755,-20,531,210,+ 394,35,344,-121,70,142,-374,361,-390,-180,203,-65,320,20,+ -391,-27,-294,-738,343,-998,-334,-448,-699,-301,-48,-1329,734,-1451,+ 175,-811,-51,-718,158,-137,-372,773,-622,181,453,51,156,267,+ -138,380,600,280,213,703,107,190,726,81,851,397,54,403,+ 773,-545,1507,-1143,1406,540,-77,629,147,-285,-1,131,-357,396,+ -184,-397,115,-879,145,-481,-381,-633,-139,-948,79,-523,-65,-50,+ -894,689,-577,19,-166,-598,289,-627,-272,-253,-95,-389,276,-690,+ 834,-67,-586,1499,-1019,927,355,-469,400,384,-169,582,645,-543,+ 1120,-365,-571,1778,-1996,1868,-287,-446,310,611,-1355,1214,-461,-694,+ 349,177,-228,200,-405,-190,-583,169,-593,-141,426,-602,389,-37,+ 270,-186,287,-318,-533,1400,-1652,1193,-506,-125,631,309,205,-98,+ 816,-632,824,387,-509,474,993,-1455,1233,410,-854,283,148,-1307,+ 491,550,-1534,1092,-945,-664,81,84,-1790,629,-269,-439,885,-836,+ 392,-155,-75,108,636,-553,957,-1139,477,-229,401,-429,567,-10,+ -273,847,-545,1113,-807,1668,-978,729,544,-780,1979,-535,-148,822,+ -607,499,124,787,-1528,2273,-2514,1300,-856,-53,239,-834,-2,421,+ -876,93,-155,-1069,-236,1460,-2202,1178,531,-2118,1504,-848,-65,436,+ -663,-625,1265,-1878,1716,-1758,352,-89,-657,1734,-1694,1271,124,-372,+ 765,119,-383,383,1395,-1159,1688,-312,443,763,-1201,1475,-840,-734,+ 1806,-1160,45,1691,-2398,1980,-846,-324,1078,-940,205,442,-1048,1518,+ -1373,439,-834,-571,795,-666,-367,188,-807,743,-91,-560,-609,132,+ -768,190,418,-916,119,146,-1148,845,-447,341,890,-903,751,503,+ -432,646,245,-1083,1474,-1094,1235,-187,732,969,-1239,2126,-1966,1398,+ 629,-890,333,726,-1392,1897,-272,-73,627,-1361,1166,-779,-1593,1119,+ -1500,119,548,-985,961,-933,-836,1595,-1526,1651,-60,-631,2478,-3321,+ 3625,-2276,121,984,-1680,582,614,-1182,1572,-1096,775,67,-682,-829,+ 1342,-1912,2173,-1086,1893,-1652,2380,-1985,1552,-82,-874,1248,53,-1002,+ 987,-784,-277,-105,-1277,224,636,-1423,1074,-169,-1504,2095,-2183,1784,+ -339,-793,2474,-2953,2298,-1062,208,-52,72,264,64,184,-1139,1668,+ -3665,5431,-4588,3760,-2598,1655,-539,-353,1853,-2822,1781,-2011,402,713,+ 325,597,-445,195,-2074,2132,-1303,534,-665,-549,-867,712,-204,680,+ -479,342,-286,-124,-903,1492,-1739,1977,-39,-748,800,-160,-612,1093,+ -1019,-755,1196,-708,1931,-2132,1852,-713,-430,1826,-2254,2240,-1679,654,+ -244,-294,-49,-92,-353,490,-242,34,624,-564,566,-24,186,-219,+ 581,467,-633,887,-658,-472,993,-1509,1298,-823,304,-22,-283,191,+ -167,-262,305,-286,350,-214,364,-192,207,-254,737,843,-873,1332,+ -621,-2457,2910,-3837,3409,-2538,938,401,-882,2313,-1729,1468,-637,-208,+ 382,-384,105,140,-147,-328,482,-577,971,-971,1039,470,-792,1508,+ -2511,2345,-2050,1131,174,-627,407,-54,39,403,-30,-187,492,-1049,+ 1572,-359,-308,-1105,2154,-3292,2498,-471,-1177,1362,-2941,1629,-1397,-10,+ -1543,838,-1542,1689,-1001,-9,1289,-1253,1153,-242,255,-353,584,-425,+ 19,-305,283,257,69,477,14,-109,-21,255,-431,173,77,-313,+ 130,-279,-54,1401,1066,-136,721,335,-372,386,-50,-367,133,-496,+ 82,-506,224,-53,-160,-1483,-222,-301,571,-950,667,-45,1958,-981,+ 1524,-1011,783,357,-596,829,-611,-51,120,-120,-475,441,-470,443,+ -368,123,250,-461,191,-365,-31,0,-51,255,-164,238,-314,300,+ -100,-52,73,179,115,-13,167,-86,167,-48,-107,4,-86,66,+ -85,612,882,-416,914,-282,-119,234,287,446,-154,-93,402,-697,+ 320,-43,-263,224,-213,57,-174,-85,-15,104,98,250,61,-102,+ -187,-24,-139,194,470,789,896,568,-440,-332,-1100,-829,-1063,-1345,+ -1242,-1111,-769,-342,6,337,667,1018,1398,1924,3292,3764,3471,2441,+ 2267,1265,667,300,-209,-786,-865,354,439,1205,1419,1200,893,121,+ -251,-609,-1552,-2788,-3138,-3465,-3347,-3472,-3204,-4790,-5170,-5510,-5586,-5581,+ -5322,-6652,-6237,-6349,-3661,-2372,-604,845,1514,1878,4045,4996,5586,5363,+ 4705,3987,4794,5121,6127,6033,6155,5664,5318,4976,4357,3492,2688,3600,+ 2678,3333,2871,2732,2433,1817,1792,1595,1189,644,-37,-926,-1687,-2304,+ -2509,-3588,-5381,-5384,-7134,-9325,-12213,-13390,-14013,-14218,-13551,-11548,-8386,-5600,+ -3229,-318,1802,2349,3622,3135,3058,2639,1893,1933,1527,1065,1322,1451,+ 1679,2955,5526,5219,6993,6810,7211,6215,7380,8821,8027,7616,7274,6335,+ 5354,4184,3750,2038,520,-678,-1727,-3115,-4093,-4595,-4998,-5954,-6182,-6439,+ -9672,-9503,-10799,-10867,-13603,-12172,-13008,-11925,-9722,-4989,-2530,418,2834,5581,+ 6659,7666,8387,7926,4007,3345,1960,1434,244,796,699,517,154,1721,+ 2112,1738,3082,4709,3590,5429,7366,7004,7583,6162,6497,5391,4113,3425,+ 2301,690,-732,-1228,-1734,-2285,-2657,-2949,-3217,-3465,-2764,-2165,-2524,-4461,+ -4580,-10170,-12234,-13753,-14264,-14428,-12816,-8239,-4337,307,3118,6773,7497,7712,+ 7844,6995,3477,-526,-1611,-3490,-2874,-2376,-820,-676,-286,1023,2948,5730,+ 6534,7830,7294,7317,8182,9023,8161,7229,5259,4194,3320,2381,1791,493,+ -1410,-2202,-2529,-2276,-2531,-2778,-3394,-3603,-3304,-2103,-2273,-3169,-4975,-7849,+ -11271,-14726,-14651,-15287,-15994,-12198,-5839,-683,2696,6767,8479,11724,10193,11566,+ 7612,3501,17,-785,-1898,-1465,-1462,-1264,-1296,-754,878,2308,2956,4325,+ 4673,5525,6010,6792,7365,6802,5903,5125,4464,3784,3089,2018,1049,-54,+ -810,-1093,-1339,-1988,-2193,-2602,-2543,-2258,-1810,-1690,-2392,-3526,-5644,-10805,+ -13208,-15524,-15567,-15210,-12669,-8855,-3898,215,4280,7640,9422,10513,9755,8037,+ 5404,2268,576,-648,-1172,-1452,-1339,-1339,-661,2412,3172,4354,4555,5023,+ 5214,5818,5977,5876,5192,4464,4357,3543,3373,2766,1792,1028,445,188,+ -137,-883,-1173,-1641,-1892,-1906,-1800,-1599,-1614,-1498,-3446,-5417,-8161,-10704,+ -15897,-15998,-17497,-14971,-10091,-4999,-663,3572,6062,8872,10642,11235,10524,7080,+ 1849,999,-2525,-1621,-2590,-2800,-3084,-3111,-1756,967,2771,4098,6702,5332,+ 7342,6784,7251,6615,5750,4626,4423,4539,5151,3994,3262,2051,1035,706,+ 426,-160,-1163,-2050,-2263,-3625,-4083,-3507,-3843,-3271,-3626,-4363,-8110,-11541,+ -13206,-15688,-16024,-15423,-13036,-9462,-4835,-36,4296,6652,8615,9779,9922,9121,+ 6703,3821,962,-886,-1772,-1942,-2444,-2581,-2499,-1563,1250,3891,5861,9003,+ 7738,9057,8042,8167,7194,6099,4279,3804,3260,3337,3396,2646,2036,1071,+ 780,279,23,-930,-1603,-2317,-3392,-3650,-3833,-3551,-3435,-4240,-6565,-8445,+ -13717,-15137,-17678,-18013,-17907,-14125,-10150,-3315,863,6222,8942,11215,11142,9796,+ 9017,5851,3370,315,-1321,-2373,-2788,-2686,-2105,-1738,-1238,403,2032,4562,+ 7736,7931,8991,8410,8065,7344,6701,5381,4802,3843,3501,3547,2955,2579,+ 1957,1335,735,428,-279,-662,-1270,-2052,-2498,-2771,-2992,-4145,-5262,-6719,+ -8456,-11304,-12896,-14284,-14862,-14777,-13065,-11285,-7118,-4095,-109,2235,4480,4991,+ 5247,6297,6081,5445,4204,3405,2467,2286,2251,2502,2289,1943,2194,2805,+ 4052,5674,6462,6646,6324,5876,5314,4788,3789,2914,2011,1428,1156,974,+ 765,824,1584,1576,2092,1761,1128,631,-123,-1073,-1734,-2560,-3441,-3905,+ -3763,-4901,-5471,-6270,-6565,-7009,-6922,-6762,-6011,-5318,-4439,-3264,-2119,-1504,+ -1082,-867,-1560,-1616,-1282,-1220,-330,252,971,1762,2731,3237,3988,4558,+ 4665,5033,4675,4001,3669,3032,2924,3060,3018,2971,3351,3360,3658,4164,+ 4318,4477,4360,4115,3906,3653,3208,2831,2409,1899,1492,1150,594,-61,+ -458,-943,-1345,-1709,-2093,-2224,-1954,-1961,-1768,-1604,-1492,-1852,-2142,-2368,+ -2615,-2996,-3378,-3667,-3821,-3975,-4032,-3844,-3623,-3499,-3238,-2671,-1880,-1402,+ -903,-381,54,357,580,865,1104,1208,1210,1405,1542,1705,1781,1993,+ 2137,2292,2378,2511,2603,2594,2520,2444,2364,2252,2134,2001,1760,1499,+ 1187,899,664,376,53,-123,-310,-593,-803,-842,-926,-875,-837,-626,+ -607,-653,-765,-767,-945,-1123,-1234,-1130,-1094,-1072,-939,-759,-575,-548,+ -454,-248,-249,-164,-221,-152,-37,46,256,365,491,425,370,412,+ 507,599,594,744,853,832,913,949,919,689,625,507,304,198,+ 131,80,-2,-72,-179,-356,-533,-636,-649,-644,-637,-582,-511,-474,+ -292,-116,123,261,410,470,443,384,385,328,333,305,247,208,+ 209,231,269,323,364,413,405,401,376,389,273,269,282,312,+ 312,420,454,437,401,359,270,129,-36,-142,-218,-283,-338,-403,+ -434,-517,-601,-755,-842,-893,-908,-834,-711,-529,-332,-117,67,156,+ 165,70,-8,-40,-31,-6,17,153,289,415,525,547,532,469,+ 389,294,252,223,206,220,251,282,334,352,289,202,120,65,+ 23,-2,-31,-58,-35,22,47,63,12,-39,-85,-133,-199,-305,+ -344,-360,-376,-391,-417,-408,-394,-394,-336,-268,-215,-160,-113,-60,+ -27,-27,10,53,59,67,98,96,119,175,272,374,457,526,+ 550,542,492,389,285,185,119,-13,-70,-127,-156,-174,-188,-147,+ -178,-218,-259,-255,-270,-253,-235,-206,-199,-162,-91,-32,-65,-102,+ -115,-158,-212,-267,-282,-293,-312,-323,-315,-284,-289,-285,-252,-205,+ -142,-49,67,133,214,258,274,307,324,387,433,482,539,543,+ 561,569,533,460,380,261,162,104,-11,-102,-192,-253,-258,-262,+ -272,-257,-274,-260,-282,-322,-308,-265,-256,-233,-214,-200,-220,-259,+ -302,-342,-362,-366,-359,-294,-231,-170,-79,6,77,114,130,166,+ 138,135,134,130,113,102,94,104,96,104,125,155,165,174,+ 184,205,209,228,228,179,157,110,62,21,-14,-48,-66,-94,+ -83,-40,-32,-61,-82,-123,-149,-187,-214,-259,-289,-314,-311,-257,+ -201,-144,-79,-19,15,54,82,83,59,46,46,66,85,118,+ 145,196,214,226,231,232,215,180,153,138,101,95,83,101,+ 94,84,84,97,78,66,28,-11,-53,-106,-124,-172,-212,-233,+ -240,-225,-235,-220,-238,-268,-289,-286,-312,-322,-308,-311,-299,-281,+ -227,-155,-88,-33,13,60,73,88,110,101,88,91,104,147,+ 194,257,301,333,353,354,311,316,288,258,248,242,246,246,+ 269,290,284,220,130,57,8,-33,-69,-74,-80,-62,-70,-88,+ -95,-118,-146,-203,-268,-300,-317,-327,-331,-338,-333,-322,-297,-283,+ -261,-232,-176,-116,-67,-32,1,30,48,67,80,76,92,89,+ 105,110,117,141,153,168,177,213,272,310,315,312,304,277,+ 255,225,194,151,113,106,91,72,21,10,-23,-48,-81,-114,+ -137,-170,-178,-190,-202,-206,-206,-206,-215,-221,-233,-244,-243,-243,+ -242,-234,-221,-205,-187,-161,-144,-124,-98,-73,-51,-23,4,26,+ 48,75,89,114,131,143,154,169,176,188,192,191,183,183,+ 182,175,177,166,158,147,132,102,86,62,39,19,+};+static const unsigned char CLIP8[3317] = {+ 79,103,103,83,0,2,0,0,0,0,0,0,0,0,1,192,159,239,0,0,+ 0,0,85,65,226,152,1,19,79,112,117,115,72,101,97,100,1,2,120,0,+ 128,62,0,0,0,0,0,79,103,103,83,0,0,0,0,0,0,0,0,0,+ 0,1,192,159,239,1,0,0,0,144,34,204,23,1,61,79,112,117,115,84,+ 97,103,115,12,0,0,0,76,97,118,102,54,49,46,55,46,49,48,51,1,+ 0,0,0,29,0,0,0,101,110,99,111,100,101,114,61,76,97,118,99,54,+ 49,46,49,57,46,49,48,49,32,108,105,98,111,112,117,115,79,103,103,83,+ 0,4,184,56,0,0,0,0,0,0,1,192,159,239,2,0,0,0,216,116,+ 191,9,19,255,32,176,183,255,1,197,173,181,171,163,142,134,227,181,150,218,+ 255,41,188,125,176,110,70,251,182,120,219,58,214,158,45,45,5,20,0,124,+ 174,220,184,172,17,221,198,158,195,139,207,205,123,228,147,69,15,87,183,162,+ 87,200,185,199,57,195,217,220,233,65,129,75,225,161,48,218,208,15,191,31,+ 115,151,101,154,42,170,22,222,124,73,58,152,183,250,255,57,118,252,247,157,+ 185,175,212,74,52,43,172,85,191,158,159,195,51,150,126,92,240,186,69,46,+ 245,28,192,255,189,19,60,63,178,11,152,171,229,30,13,219,167,114,175,228,+ 40,200,93,17,17,106,166,60,255,80,159,230,107,73,219,168,112,156,73,159,+ 215,33,151,217,233,132,112,198,128,156,17,30,140,4,17,25,37,40,89,97,+ 39,159,47,186,143,54,206,115,138,140,225,19,197,132,45,78,206,43,129,184,+ 146,180,153,213,57,125,197,7,80,49,208,25,22,59,151,30,120,233,128,241,+ 222,51,6,177,153,67,42,135,73,192,120,25,37,230,65,49,220,161,156,164,+ 215,157,205,219,242,134,136,255,89,56,27,29,39,8,226,54,64,133,54,3,+ 65,129,90,184,27,0,177,178,117,211,26,225,41,31,215,150,152,22,212,137,+ 90,238,42,195,18,165,87,139,223,38,100,89,105,114,52,111,68,22,187,160,+ 153,37,51,204,94,158,109,60,89,188,192,34,110,234,218,199,178,202,22,29,+ 24,143,91,132,174,253,49,82,27,197,159,140,1,66,84,251,142,122,169,45,+ 212,243,240,137,224,232,129,149,177,245,238,164,46,59,82,1,168,127,121,113,+ 226,51,15,47,87,178,132,78,10,24,119,139,68,19,9,209,3,218,40,148,+ 117,155,74,40,215,111,133,179,253,174,91,169,25,246,166,213,120,24,161,83,+ 210,61,148,163,84,204,134,104,26,87,253,37,3,160,207,188,80,137,72,220,+ 141,78,172,150,32,188,149,202,13,56,83,73,227,163,156,254,31,222,249,104,+ 176,6,71,195,187,34,40,49,150,72,198,234,208,83,5,27,87,143,212,128,+ 207,244,66,150,111,81,175,145,111,93,195,129,63,78,74,1,233,230,152,223,+ 167,210,139,242,253,188,195,56,9,103,159,112,20,142,253,212,176,237,209,89,+ 105,170,218,5,10,140,127,70,99,146,247,249,164,221,121,77,225,74,227,215,+ 210,1,126,37,142,63,126,163,211,177,223,231,75,29,172,134,85,33,170,128,+ 204,51,137,187,86,214,157,173,31,161,169,11,110,174,28,238,217,143,77,202,+ 63,131,193,12,101,81,62,221,14,15,41,244,33,227,114,11,114,53,121,92,+ 58,239,185,154,253,173,223,163,143,216,129,111,158,19,99,211,186,0,121,12,+ 76,251,29,157,31,168,121,221,45,85,51,91,181,89,140,130,21,52,87,196,+ 169,107,88,109,56,154,31,16,98,22,183,59,164,122,37,186,240,83,128,20,+ 244,201,223,251,137,133,206,90,160,222,142,198,7,123,124,23,197,80,198,84,+ 193,176,153,7,50,28,170,253,188,124,155,41,184,155,104,162,190,39,91,143,+ 151,116,231,12,42,114,149,82,145,13,29,40,166,239,115,90,203,30,28,226,+ 166,54,66,221,5,158,108,229,97,29,157,223,53,194,182,114,40,5,182,15,+ 14,219,22,174,96,33,24,190,160,127,41,140,114,191,169,113,248,151,11,38,+ 133,200,222,137,197,222,72,241,121,189,228,24,117,185,226,217,162,205,45,51,+ 44,105,0,241,228,97,61,126,197,112,143,125,70,190,101,239,204,62,192,133,+ 50,92,213,253,124,7,103,135,44,152,245,248,112,17,51,140,81,225,132,113,+ 127,175,93,172,101,229,110,68,247,148,177,25,150,48,56,219,75,96,202,193,+ 21,219,35,218,78,12,135,103,205,13,239,22,108,0,127,237,230,31,230,0,+ 93,41,217,124,128,218,73,108,32,167,140,25,97,25,237,185,7,134,113,110,+ 212,176,170,59,11,70,208,202,142,140,170,239,88,47,187,145,157,155,247,174,+ 87,192,81,148,176,176,134,30,19,1,103,192,90,197,33,236,11,37,243,56,+ 44,64,103,11,116,33,91,213,220,207,104,194,107,90,61,95,89,83,79,10,+ 130,92,224,249,188,202,61,247,172,170,166,130,212,144,6,62,138,37,93,148,+ 42,72,78,50,86,33,62,162,22,94,217,1,219,87,235,236,36,89,12,26,+ 77,181,117,28,7,228,235,115,146,137,103,157,82,62,149,163,31,21,9,225,+ 137,61,53,18,186,215,215,136,158,12,110,236,223,227,241,154,181,110,191,226,+ 153,3,110,179,227,166,212,98,209,170,44,180,202,127,121,62,30,171,190,70,+ 59,158,15,143,206,156,1,69,238,89,25,204,23,147,190,5,201,52,159,188,+ 236,228,120,19,52,69,41,101,24,95,59,245,213,43,148,118,79,216,235,3,+ 222,249,158,20,3,94,27,122,91,145,183,115,10,38,79,111,18,62,96,33,+ 127,109,214,58,159,55,243,172,253,209,21,75,224,2,126,49,88,185,30,53,+ 30,19,1,147,127,138,146,122,25,83,220,53,40,204,242,79,158,154,99,82,+ 241,188,191,255,190,133,126,149,40,211,228,199,107,92,136,204,72,40,246,247,+ 168,4,22,149,227,114,15,140,14,210,123,109,96,228,36,48,159,89,88,71,+ 167,168,13,243,116,166,143,253,55,29,46,126,211,218,136,149,113,166,93,8,+ 67,20,168,166,197,236,225,142,172,0,136,56,151,13,61,42,135,32,91,58,+ 62,171,150,200,147,174,104,106,103,57,153,244,147,254,196,181,192,165,13,155,+ 180,15,248,1,33,161,24,240,235,86,2,116,145,97,241,65,119,232,236,14,+ 252,225,176,255,42,14,42,233,246,168,135,99,18,177,66,34,12,23,110,202,+ 185,194,148,225,255,58,7,80,14,226,206,124,80,177,82,51,55,169,223,8,+ 177,115,153,88,156,93,9,170,170,85,197,13,14,235,188,194,5,184,218,69,+ 214,180,69,110,165,37,255,100,28,70,65,196,239,107,252,236,211,43,126,44,+ 88,211,216,51,174,28,135,32,224,112,223,111,191,245,144,101,89,240,49,90,+ 216,209,145,220,198,6,40,67,252,209,228,169,207,170,79,216,95,1,167,106,+ 21,16,36,122,155,34,61,79,110,206,136,242,2,104,230,1,228,182,78,19,+ 39,229,153,65,113,18,201,79,54,134,255,189,253,226,40,77,130,81,241,78,+ 70,236,227,5,68,182,138,167,206,58,206,231,126,224,226,226,63,242,33,9,+ 237,108,47,202,53,17,223,229,75,56,192,70,63,28,75,11,136,39,1,234,+ 199,240,62,97,195,56,66,173,9,30,130,105,142,43,117,27,206,248,55,24,+ 48,140,207,203,57,65,167,145,252,2,91,92,23,157,217,188,76,176,170,84,+ 34,29,23,66,72,145,50,101,222,215,83,96,107,213,134,170,241,102,132,57,+ 42,39,111,245,39,248,113,112,244,211,105,31,178,218,152,36,37,186,118,190,+ 229,17,179,11,66,79,76,88,62,33,92,238,146,110,6,196,103,19,10,156,+ 100,82,198,133,184,112,0,11,228,79,232,41,25,206,98,60,179,77,158,79,+ 50,32,73,64,202,111,74,151,143,74,200,36,215,180,250,189,244,120,165,108,+ 217,83,120,56,117,124,66,92,56,99,17,81,105,137,50,142,141,243,190,92,+ 44,200,22,14,148,179,159,40,107,85,10,180,204,11,44,3,255,5,171,24,+ 185,26,94,236,73,79,90,57,114,173,200,146,191,165,138,81,86,12,248,128,+ 237,109,179,191,233,83,188,2,123,28,42,10,15,173,6,85,108,79,163,210,+ 204,9,55,183,10,155,120,107,199,13,91,70,137,197,98,52,199,240,185,252,+ 177,12,84,76,64,215,42,230,206,212,183,151,150,90,209,120,232,88,69,28,+ 172,22,87,222,140,175,43,231,71,158,186,86,200,138,249,239,187,71,110,230,+ 22,214,48,203,167,68,156,191,32,181,131,235,187,203,98,244,100,20,90,130,+ 82,212,152,21,225,132,80,215,36,148,234,157,159,220,8,35,189,112,121,247,+ 236,233,231,32,176,44,1,203,183,67,250,165,214,189,202,41,238,56,188,223,+ 160,23,244,122,106,186,183,90,7,34,160,87,163,140,50,109,247,94,116,22,+ 200,155,170,77,68,147,168,94,191,188,194,0,242,167,87,144,88,29,126,120,+ 218,93,205,142,224,105,243,31,88,69,163,217,125,128,76,150,165,52,103,94,+ 173,8,254,159,81,83,7,42,196,80,208,170,146,113,166,168,160,168,121,40,+ 81,190,22,152,117,126,135,95,239,58,238,5,180,209,204,114,1,118,26,251,+ 208,192,4,174,94,227,177,32,231,234,11,127,137,133,132,203,248,172,25,31,+ 15,61,105,243,65,31,54,31,79,217,145,248,199,87,150,159,53,11,134,11,+ 148,3,2,171,16,1,167,185,36,10,10,146,178,179,178,59,186,156,112,64,+ 207,72,236,93,230,100,7,120,184,191,77,188,198,200,130,205,23,60,138,229,+ 86,231,171,119,133,207,241,32,60,255,51,6,230,12,189,209,71,54,187,169,+ 194,178,193,1,96,203,1,179,233,132,59,162,84,238,107,135,128,93,121,68,+ 108,99,42,76,191,195,192,0,0,201,243,97,209,42,105,241,243,231,164,5,+ 97,92,170,201,192,152,8,125,250,136,179,149,220,34,38,66,64,50,34,212,+ 193,176,205,90,155,193,29,119,133,117,38,11,40,247,81,83,215,1,22,167,+ 143,64,252,158,127,65,187,225,167,136,90,218,95,154,30,171,208,183,80,232,+ 238,158,236,126,205,188,124,156,11,161,57,206,118,141,40,160,119,182,209,20,+ 206,209,42,198,7,228,55,21,180,11,101,154,107,54,158,8,43,173,226,85,+ 177,153,229,164,40,169,115,242,23,104,80,84,41,253,136,131,35,124,129,252,+ 19,120,166,51,212,182,213,161,109,105,43,200,173,49,133,39,28,192,88,179,+ 175,244,177,180,25,11,74,251,55,68,192,24,85,202,48,58,143,7,61,104,+ 211,43,145,36,219,11,155,220,44,23,215,248,91,163,91,141,36,19,160,187,+ 80,121,217,71,215,103,113,162,231,240,172,132,124,0,132,229,111,39,95,19,+ 165,96,242,61,224,215,81,105,190,197,156,44,63,240,106,129,68,139,244,44,+ 87,129,60,216,139,52,215,125,213,80,7,184,103,113,84,16,144,27,235,14,+ 34,38,166,104,60,18,81,212,191,124,34,55,32,226,95,110,241,198,146,88,+ 101,44,113,25,77,43,110,41,153,18,91,228,34,234,45,31,44,57,204,59,+ 128,0,73,125,249,181,72,18,205,98,212,180,188,84,27,102,70,136,96,123,+ 112,123,207,51,177,195,112,224,245,31,193,229,117,27,36,228,233,142,98,61,+ 231,173,112,145,202,187,165,37,76,191,112,102,105,135,234,49,198,225,23,132,+ 200,20,98,86,62,41,222,170,197,189,181,11,80,83,106,40,152,105,76,160,+ 203,72,58,26,76,231,212,44,30,57,81,45,240,41,101,36,198,230,22,187,+ 250,45,82,51,70,84,135,65,134,239,91,229,45,134,247,103,11,118,45,46,+ 233,126,180,1,239,43,153,214,222,90,168,204,211,211,182,65,65,120,146,64,+ 236,107,18,165,27,159,226,213,220,199,230,91,106,212,116,67,226,240,36,177,+ 131,133,23,68,74,36,148,12,97,246,167,220,82,133,32,213,153,219,110,11,+ 107,68,21,195,23,237,96,142,225,6,57,121,159,188,67,169,188,2,217,234,+ 113,170,28,2,235,227,204,7,169,69,8,76,235,16,90,30,38,228,107,119,+ 39,99,13,39,40,203,95,41,187,106,78,239,150,210,148,203,232,116,126,218,+ 226,34,32,137,17,215,98,21,5,249,186,52,48,91,42,245,153,154,169,49,+ 193,5,116,162,99,132,235,114,38,120,68,252,38,158,195,182,189,40,164,72,+ 4,231,212,244,216,60,212,3,236,3,253,106,13,253,161,255,98,167,45,4,+ 44,63,187,175,76,71,93,146,144,155,207,59,82,119,145,116,33,202,152,100,+ 155,34,31,52,141,65,231,59,141,171,116,217,78,213,67,113,225,221,73,42,+ 58,231,226,188,124,27,78,211,18,186,22,112,153,79,166,18,71,193,91,104,+ 113,198,174,234,144,50,70,234,37,52,172,2,162,88,71,196,148,16,193,166,+ 206,68,24,24,216,25,55,88,49,130,211,11,211,134,240,5,14,103,31,111,+ 204,217,153,202,178,239,173,101,127,175,93,85,133,81,116,72,254,167,212,132,+ 81,246,110,161,176,78,247,187,93,161,153,246,87,19,192,132,130,88,170,26,+ 55,77,250,250,178,89,75,98,35,72,110,142,9,49,29,106,87,183,89,34,+ 74,182,231,162,20,130,168,148,217,114,139,116,178,34,192,161,181,95,70,154,+ 81,93,183,100,63,137,33,113,110,104,207,94,44,30,254,99,144,22,121,211,+ 84,143,221,92,202,47,8,229,61,104,234,14,199,227,79,159,131,9,153,157,+ 206,229,46,83,215,8,234,198,187,41,130,108,49,70,195,76,101,205,182,177,+ 94,54,53,233,0,181,5,159,83,221,99,115,49,232,61,41,17,231,18,160,+ 238,188,120,5,41,59,185,42,33,23,79,22,160,167,209,170,101,212,43,64,+ 161,251,115,137,119,12,102,94,25,195,1,37,250,92,69,141,96,79,175,250,+ 161,80,121,54,244,17,9,204,128,175,157,237,56,0,0,4,181,20,199,190,+ 0,1,202,138,134,40,0,0,8,179,37,0,221,0,0,54,169,31,157,30,+ 128,122,176,133,191,106,0,41,117,248,110,175,152,0,8,41,123,163,64,0,+ 5,245,95,143,171,211,1,144,0,88,62,47,196,37,115,102,0,32,23,184,+ 85,45,26,141,102,0,143,70,115,172,14,2,4,0,39,91,19,125,192,215,+ 151,187,0,82,237,11,238,3,169,212,0,72,174,195,236,64,254,67,220,21,+ 146,130,199,68,35,149,66,32,88,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,193,254,89,169,101,+ 128,41,12,160,142,8,8,0,4,8,4,74,36,64,64,32,16,104,224,214,+ 255,186,87,132,204,182,152,2,83,203,134,20,179,189,69,138,121,+};+static const int REF8[3600] = {+ -634,-907,-749,-196,-218,-751,-382,98,-316,-824,-362,-793,-874,-963,+ -1138,-473,-906,-751,-538,-502,-326,-160,501,230,181,-87,14,314,+ -152,-347,-468,105,159,-7,60,-261,97,391,483,606,548,475,+ 656,903,1053,1040,1015,1220,1207,1228,1113,1135,953,603,146,-168,+ -374,-1027,-1279,-1348,-1655,-2016,-2106,-2177,-2774,-3640,-4308,-4073,-4526,-4735,+ -4513,-3836,-3834,-3684,-2005,-629,175,358,789,2163,2053,1787,2173,2475,+ 1631,1069,1194,1401,1002,804,623,1011,1093,1202,1426,2214,2507,2294,+ 2991,3430,3543,3637,3768,3959,4341,3950,3681,3635,3357,2571,1863,1395,+ 303,-129,-224,-1083,-1703,-1843,-2348,-2705,-3409,-4166,-4970,-6083,-6714,-6363,+ -5903,-5910,-4578,-2052,-782,63,859,2235,3527,3578,3367,3566,3289,1135,+ 853,521,280,-1069,-1303,-1460,-1650,-2300,-2283,-2033,-1987,-1952,-1760,-1262,+ -463,-133,478,1162,2044,2955,3815,4043,4454,4649,5486,5116,5410,4746,+ 4582,3919,3702,3322,2827,1449,788,1211,1225,288,26,360,167,-284,+ -181,-1044,-2540,-3533,-5333,-4675,-4432,-6171,-4322,-2339,-1063,-1210,-275,816,+ 2026,3193,2295,3066,1293,610,65,-273,-1299,-3166,-4335,-4961,-4954,-5077,+ -5109,-4264,-3261,-1340,-300,1284,1505,2489,3223,4104,4100,4429,4001,3261,+ 3194,3182,3766,3006,2521,2888,3164,3097,2518,3034,2700,2489,2003,2166,+ 2423,1806,1265,956,1111,1244,449,-660,-2204,-3800,-4479,-5415,-7007,-7933,+ -7246,-5906,-3504,-1241,-1105,-303,1032,3112,5066,4442,2702,1948,1073,948,+ -518,-2133,-4619,-5491,-6938,-6298,-6003,-6487,-4526,-2958,-476,258,1583,2224,+ 4153,4335,4214,4929,3894,3016,2201,2582,2354,2015,826,1402,2360,1935,+ 2771,3067,3086,2807,3383,3585,3293,1961,1821,1581,893,220,163,-711,+ -2409,-2693,-3352,-4410,-6300,-7082,-6993,-6166,-6049,-3230,-1363,-424,55,1402,+ 3688,4922,4459,2067,2319,898,35,-2092,-3377,-5584,-6739,-7170,-7709,-7636,+ -6795,-4711,-2425,-38,1082,2461,3841,4639,5920,4989,4172,3088,1949,1274,+ 1024,672,183,392,252,1438,2667,2811,3144,3194,4659,4653,4010,3253,+ 3203,2241,959,647,202,-1143,-1892,-1337,-2424,-4954,-5360,-4447,-5149,-6240,+ -6225,-5239,-3882,-1322,1345,1835,864,1554,4290,4108,2134,-354,-176,-1619,+ -3071,-4254,-5160,-6646,-7962,-7432,-6591,-6059,-5205,-2705,885,2085,2843,3831,+ 5036,6391,5174,4383,2770,1360,399,591,81,-325,-535,1054,1964,3395,+ 3881,4959,5104,5887,6075,5882,4523,3143,2716,1428,370,-677,-1419,-1836,+ -2069,-3108,-3812,-4855,-5649,-5781,-6093,-5388,-6227,-4186,-1458,453,607,389,+ 1779,3221,3084,1582,779,-576,-1951,-1977,-3385,-5307,-6514,-6128,-6019,-6248,+ -7113,-4986,-3354,26,1817,2972,3029,4133,5658,5772,4178,1650,1468,1417,+ 631,-133,-180,-180,1238,2456,4024,4852,4831,5739,7517,7209,5906,4572,+ 4034,3216,1982,920,-765,-1880,-2077,-1551,-2512,-3847,-4624,-4454,-4753,-5058,+ -4643,-5061,-4096,-2632,-92,1253,550,366,1709,2798,2315,1183,-577,-1215,+ -1572,-2415,-3527,-5466,-6359,-7253,-6621,-6190,-6121,-4173,-1957,246,1713,2586,+ 3713,4865,5096,4099,3678,2351,1807,964,484,606,676,1611,2246,3231,+ 3948,4832,5392,6003,5881,5698,4841,4152,3348,2176,784,-336,-799,-1426,+ -1816,-2681,-3005,-4143,-4696,-5390,-5369,-4969,-4615,-4113,-2506,200,95,147,+ 955,2203,2917,2954,1074,174,-548,-1516,-1675,-3107,-4603,-5918,-6185,-5929,+ -5957,-5963,-4509,-2514,-464,1498,2450,2727,3459,4474,4863,4209,2331,1646,+ 1338,1356,1099,833,1258,1199,2458,3192,3951,4089,4110,4694,4955,4423,+ 3767,3350,2614,1736,986,465,-384,-1208,-1555,-1541,-2575,-3884,-4750,-5011,+ -5036,-5017,-5006,-3972,-2207,-1484,-1077,-409,699,1399,2298,1799,524,530,+ 81,-315,-1497,-3158,-4298,-5417,-5988,-5985,-5703,-5757,-4069,-1836,-766,-65,+ 1042,2749,3635,3671,3843,3884,2852,2140,2772,2952,2370,1822,1913,2314,+ 2058,2268,2878,2570,2126,2781,3017,2532,1878,1485,1865,1693,643,41,+ 400,153,-625,15,235,70,-81,-50,-7,-563,-1336,-1305,-1186,-1675,+ -1472,-1732,-2318,-2158,-2285,-2390,-2414,-2727,-2748,-2782,-3820,-4552,-4815,-5094,+ -5478,-5217,-4954,-3810,-2208,-1560,-638,78,1334,2233,2980,2881,2530,2380,+ 2416,2381,2169,1576,1638,1217,1425,1294,1159,1042,1137,1560,1949,1902,+ 1966,2089,2157,2409,2861,2969,2914,2750,3004,3134,3180,2994,2636,2500,+ 2331,2045,1849,1227,757,420,240,-189,-247,-600,-655,-563,-1064,-1217,+ -1115,-1280,-1694,-2013,-1957,-2226,-2411,-2869,-2839,-3007,-3309,-3185,-2713,-2351,+ -1747,-1933,-1493,-1003,-651,-341,-28,187,381,541,672,837,997,1028,+ 964,978,1100,1099,1089,1064,983,1109,1159,1013,1058,1083,1097,1107,+ 1085,1099,1135,1012,1024,1005,970,886,765,634,463,493,309,248,+ 153,5,-3,-74,-72,210,121,40,94,142,238,120,127,160,+ 28,-297,-373,-156,-84,-662,-824,187,-186,-168,-229,-346,-482,-450,+ -462,-69,146,542,-313,-553,108,763,1552,754,329,1017,1717,3215,+ 2922,789,647,1617,2419,2052,1197,812,680,587,1382,979,-633,-1483,+ -1549,-910,-1568,-2362,-2904,-3800,-2953,-2731,-2977,-3094,-3213,-2826,-1689,-1052,+ -1312,-1446,-983,-163,436,413,388,161,132,597,687,121,-294,-92,+ -175,64,-35,160,612,561,1084,1419,1260,1513,1981,1960,2114,1994,+ 1995,1909,1726,2014,1907,940,373,570,-272,-537,-1463,-2093,-2955,-3790,+ -4533,-4660,-5391,-6751,-8069,-7868,-6664,-5215,-3520,-2986,-2520,-304,1807,3468,+ 3601,2500,2851,3420,3279,2141,677,108,284,-99,-713,-1994,-2386,-1745,+ -737,-425,-414,31,606,1182,1694,1759,2014,1823,1429,1724,2311,2152,+ 1696,2390,3194,3354,3066,3536,3997,3631,2801,2443,2294,1407,393,-364,+ -662,-1319,-2428,-3527,-4274,-5782,-7061,-8184,-8354,-7960,-6670,-4159,-2194,-1581,+ -82,1808,3822,4801,4296,3778,2662,2090,2380,1058,-956,-2484,-2687,-2830,+ -3553,-5026,-5503,-5182,-4029,-2904,-2387,-959,704,1846,2627,3994,4312,4048,+ 3678,4241,4503,3957,2954,2320,3088,3794,3732,3056,2796,2711,2939,2868,+ 1670,916,1108,1145,720,-154,-1012,-1679,-2322,-3588,-5709,-7915,-8132,-7426,+ -7237,-5203,-2078,40,458,2299,5442,6888,5742,4571,4275,3031,1896,400,+ -1567,-3228,-3672,-3909,-4629,-5874,-6611,-5643,-4052,-3193,-1830,647,2578,3486,+ 4036,4605,6209,5349,4318,4210,3843,2894,3126,2772,2663,2302,2889,3612,+ 3321,2638,2869,2504,1944,1207,1270,1454,-195,-1253,-1521,-1569,-3202,-6314,+ -8268,-7983,-7541,-7384,-5950,-2969,-148,1033,3507,5898,7259,6785,5898,4979,+ 3139,1485,64,-1970,-3298,-3456,-3386,-3746,-4206,-4134,-3999,-3916,-2851,-1585,+ -1471,501,3135,3146,1975,2493,4451,4809,3301,2234,2503,2918,2911,2479,+ 2019,2142,3239,4030,3400,2642,2026,2024,1907,1076,-165,-998,-375,-528,+ -1748,-4397,-6103,-7536,-7556,-7273,-7235,-5477,-2162,-2,610,2735,6121,7521,+ 6046,4349,3808,2224,98,-2022,-2884,-2977,-3330,-3679,-3508,-2911,-2369,-2588,+ -2640,-2154,-1820,-731,1104,2182,1790,805,1774,3639,3885,2777,2193,2387,+ 3501,4128,3350,2534,3013,4223,4516,3039,1367,1200,2158,1725,191,-725,+ -728,-386,-1220,-3469,-5978,-6905,-7290,-8107,-8028,-7238,-4078,-785,308,1225,+ 3898,6685,7335,5311,3682,2436,972,-336,-2451,-3874,-3738,-2962,-2608,-2128,+ -1641,-1600,-1920,-1732,-1094,-905,-296,197,773,925,748,1663,2175,2599,+ 2502,2078,2390,3601,3471,2790,2973,3889,4134,2919,2004,1631,1553,1501,+ 289,-1139,-1140,-387,-967,-3429,-5988,-7063,-6978,-6522,-6532,-5414,-2763,695,+ 2134,2728,4657,6599,6539,4826,2751,685,-1136,-2432,-2856,-3443,-3977,-2524,+ -1380,-1716,-1100,-423,-451,-143,-1227,-1534,-1211,-1052,-194,561,-173,-193,+ 1618,3416,3588,2585,3082,4106,4692,4875,4288,4205,4033,3069,2259,1502,+ 23,-1053,-782,-94,-1210,-2500,-2622,-3246,-5630,-7634,-7371,-6895,-6662,-4344,+ -808,893,1558,3409,5646,6276,5761,3747,2230,680,-1214,-2632,-3211,-3385,+ -3015,-2428,-1278,-563,-652,-336,170,-657,-1159,-1330,-437,-568,-307,419,+ 297,819,2663,3175,3315,3863,3459,3037,3816,4076,3978,2985,2335,2365,+ 819,-718,-789,-1050,-869,-700,-1619,-1962,-2701,-4181,-6175,-6980,-6567,-6597,+ -4950,-1616,-65,450,2346,4753,6725,6040,3969,2451,1163,-268,-1805,-2946,+ -3445,-3666,-2785,-1173,-972,-695,493,640,293,-48,-425,-787,-805,-295,+ -411,-316,1085,2356,3527,3664,3492,4186,4428,4051,2813,1991,1803,1445,+ 604,-658,-755,-914,-1367,-531,-623,-2184,-3925,-5242,-5698,-6092,-7015,-7733,+ -5845,-1250,599,-507,643,4701,5832,5242,3463,2295,2369,1665,635,-1069,+ -2660,-2410,-507,-225,-801,-1366,542,2619,1507,-408,-1690,-904,-916,-1683,+ -730,93,1069,2213,2792,3923,4617,4580,4108,3940,3120,1818,830,25,+ -129,-647,-1204,-1625,-1404,-1323,-2073,-3695,-5120,-5673,-6382,-6491,-6409,-5124,+ -2814,-914,195,1495,3852,5602,4848,2902,2495,2303,954,-401,-895,-960,+ -1022,-559,270,173,682,2189,2330,1748,631,-928,-1883,-2462,-2255,-2806,+ -2004,944,2423,2604,3430,5502,6014,5568,4312,3282,2111,1296,777,-955,+ -1954,-1936,-667,-1648,-3484,-4106,-3775,-4612,-5596,-6056,-6229,-4511,-2702,-982,+ -721,10,2041,3984,3673,1824,1292,2174,1734,601,-273,14,490,852,+ 1091,394,237,552,2191,1222,-476,-1098,-1169,-1128,-2167,-2483,-2398,38,+ 2490,2463,2289,3913,6083,6305,4594,3403,2973,2118,1233,-491,-1413,-2187,+ -2164,-1643,-1897,-2722,-3238,-3528,-3358,-3827,-4542,-4544,-5252,-3790,-1362,-387,+ -1120,-45,2638,4139,3382,1734,1207,1714,2121,589,-1197,-676,112,291,+ 160,273,861,569,699,852,-584,-1607,-1790,-1261,-1183,-840,280,1513,+ 2125,3331,3836,4346,4179,3067,2676,2557,1416,-80,-857,-693,-824,-1533,+ -2023,-1631,-1976,-2197,-1812,-2753,-3770,-4380,-4632,-3714,-3428,-2996,-2121,-535,+ 1330,2189,1561,2467,2912,3285,2506,652,-95,-39,1320,772,-80,-991,+ -525,848,912,-161,-1216,-1297,-369,-585,-1794,-2223,-1892,17,2193,2236,+ 1897,2662,4416,5187,3923,1847,1286,1325,867,-1289,-2631,-2175,-1355,-1247,+ -1297,-2004,-2327,-1749,-1677,-2458,-3704,-3625,-3039,-2324,-1048,-769,-150,1332,+ 3093,3722,2371,2053,2848,2861,2277,594,-165,186,83,-327,-908,-1366,+ -1122,-432,-889,-1190,-1328,-1096,-678,-1666,-1974,-688,224,1104,1107,1378,+ 2504,3467,2944,2437,1592,1290,1024,-524,-786,-1058,-1120,-523,-226,-259,+ -575,118,441,62,-1304,-1985,-2302,-2245,-2425,-2952,-2892,-1569,251,901,+ 959,1336,1999,2871,3037,2302,971,776,777,611,-41,-841,-823,-799,+ -551,-1075,-1348,-1579,-1366,-1569,-1854,-1701,-1637,-912,-186,631,1218,1906,+ 2200,2334,1704,2058,2073,1341,1074,673,1140,1408,976,541,329,585,+ 988,20,-958,-1665,-1792,-1559,-1488,-2804,-3203,-2824,-1682,-905,-1137,-1246,+ -247,1009,1893,950,309,904,1554,1377,521,-501,-506,422,557,121,+ -896,-511,433,171,-649,-1629,-1655,-585,-619,-753,-801,-336,1306,2039,+ 1811,1846,2185,3148,3441,2674,1597,1501,1775,2592,1054,-411,-649,-52,+ -137,-1701,-2809,-2990,-2633,-2899,-4052,-4526,-3906,-3081,-2367,-2147,-2060,-1517,+ -654,148,550,398,375,685,1298,1439,1122,1166,1396,1320,1410,1501,+ 1330,971,831,792,847,525,262,-68,-107,-3,366,618,764,1072,+ 1351,1893,1939,1701,1631,1823,1697,1174,77,-302,-451,-718,-1289,-2559,+ -3126,-3114,-2837,-2999,-3027,-2863,-2275,-1261,-769,-586,-823,-180,804,936,+ 595,408,799,1640,1915,1658,1258,1255,1619,1792,1191,606,585,876,+ 865,-140,-585,-322,-318,-399,-1132,-944,-256,478,851,850,1089,1472,+ 1826,2124,1981,1504,1061,1029,981,562,-265,-901,-1190,-1593,-1991,-2301,+ -2615,-2624,-2375,-2012,-1765,-1816,-1615,-1239,-918,-569,-392,-131,45,550,+ 987,1196,1049,1093,1290,1295,1343,1289,1022,958,971,715,361,-40,+ -43,-240,-375,-255,14,323,548,764,932,1167,1132,1482,1629,1675,+ 1547,1352,1243,1131,792,179,-443,-1140,-1626,-2141,-2512,-2768,-2875,-2910,+ -2814,-2868,-2586,-2373,-2350,-1912,-1466,-934,-497,-93,396,877,1199,1382,+ 1430,1705,2098,2149,2253,2376,2490,2339,1860,1428,1140,815,641,545,+ 217,169,289,538,747,667,633,722,777,925,830,549,375,153,+ -95,-534,-1164,-1636,-2076,-2440,-2477,-2611,-2819,-2820,-2696,-2477,-2414,-2429,+ -2229,-1866,-1248,-748,-396,61,537,940,1299,1548,1652,1765,2064,2336,+ 2405,2277,2114,2008,1833,1603,1259,928,820,788,663,563,443,421,+ 415,358,432,362,139,64,-120,-264,-480,-838,-1216,-1750,-2170,-2445,+ -2714,-2862,-2949,-3060,-2850,-2491,-2193,-2226,-2352,-2149,-1719,-1153,-720,-398,+ -91,381,962,1335,1631,1811,1977,2293,2623,2829,2712,2391,2221,2130,+ 1931,1639,1185,776,653,599,600,395,216,347,406,445,332,71,+ -90,-261,-409,-689,-1121,-1585,-1923,-2285,-2497,-2679,-2968,-3026,-3009,-2783,+ -2539,-2509,-2412,-2248,-1969,-1510,-1088,-683,-246,203,701,1129,1488,1918,+ 2114,2303,2657,2810,2975,2852,2570,2400,2249,2142,1809,1319,1031,841,+ 721,669,468,255,263,318,313,111,-238,-442,-611,-819,-1090,-1542,+ -2017,-2364,-2652,-2825,-2901,-3081,-3213,-3054,-2738,-2492,-2454,-2418,-2067,-1812,+ -1339,-819,-502,-131,333,827,1309,1632,1913,2256,2537,2857,3010,2961,+ 2896,2754,2532,2296,1997,1714,1395,1050,886,727,510,337,208,93,+ 7,-132,-297,-535,-704,-885,-1139,-1423,-1806,-2148,-2467,-2646,-2691,-2753,+ -2888,-2905,-2703,-2493,-2244,-2144,-2080,-1844,-1366,-816,-474,-186,192,634,+ 1079,1423,1777,2082,2434,2719,2888,3025,2903,2784,2606,2351,2130,1791,+ 1477,1233,1000,789,568,339,179,59,-70,-181,-463,-662,-822,-1072,+ -1271,-1575,-1846,-2102,-2485,-2768,-2930,-2935,-2973,-2997,-2877,-2655,-2427,-2264,+ -1941,-1747,-1408,-988,-471,21,297,554,1006,1377,1734,2070,2384,2661,+ 2805,3028,3187,3123,2889,2627,2367,2136,1793,1476,1193,945,839,577,+ 357,99,-11,-209,-341,-656,-912,-1165,-1314,-1484,-1715,-1953,-2240,-2464,+ -2589,-2697,-2730,-2696,-2583,-2542,-2483,-2290,-2192,-1969,-1758,-1460,-1151,-743,+ -329,77,436,776,1157,1476,1888,2142,2398,2569,2698,2763,2701,2568,+ 2405,2230,1910,1665,1368,1160,950,730,528,351,210,27,-92,-292,+ -472,-694,-827,-884,-1077,-1215,-1432,-1625,-1836,-1929,-2012,-2000,-2049,-2000,+ -1965,-1861,-1710,-1684,-1618,-1489,-1259,-1008,-733,-475,-161,130,521,829,+ 1111,1407,1601,1794,1964,2036,1987,1894,1820,1740,1592,1367,1165,1010,+ 916,717,586,481,371,282,191,33,-93,-224,-339,-452,-554,-611,+ -678,-714,-804,-947,-1053,-1092,-1142,-1202,-1277,-1273,-1316,-1320,-1313,-1243,+ -1147,-1077,-940,-754,-538,-354,-114,90,371,604,827,1008,1154,1214,+ 1311,1369,1355,1326,1229,1180,1058,899,785,698,545,441,283,240,+ 180,72,-59,-205,-272,-290,-353,-445,-478,-545,-510,-524,-518,-519,+ -521,-504,-517,-513,-519,-573,-602,-602,-566,-572,-512,-420,-360,-258,+ -179,-89,37,137,238,312,440,596,650,679,743,744,800,757,+ 728,705,622,557,326,260,86,-2,-83,-161,-268,-259,-322,-347,+ -377,-465,-479,-555,-452,-502,-508,-357,-458,-301,-252,-167,-144,-73,+ -81,56,90,141,120,176,152,184,137,196,216,190,179,258,+ 312,334,383,328,381,343,324,290,120,58,-31,-98,-119,-115,+ -71,-179,-148,-260,-275,-288,-265,-335,-393,-432,-387,-336,-394,-302,+ -419,-265,-323,-252,-200,-188,-164,-104,-37,11,4,-33,110,-29,+ -11,127,129,192,150,292,223,335,295,371,351,468,433,391,+ 433,316,163,108,9,-74,-200,-198,-331,-294,-288,-253,-302,-364,+ -524,-429,-374,-288,-370,-347,-426,-384,-524,-368,-472,-237,-121,-115,+ -49,-125,-163,-288,-196,-44,349,404,453,510,535,470,394,328,+ 363,298,363,112,333,440,450,362,322,252,267,155,93,74,+ 41,103,77,-244,-62,-368,-170,-301,-308,-354,-542,-356,-287,-361,+ -510,-394,-549,-375,-504,-251,-464,-332,-244,-190,-242,45,-27,-115,+ 173,-159,284,55,32,3,140,156,-2,138,185,2,245,-26,+ -63,96,9,172,-25,155,96,68,115,-49,105,179,-30,353,+ -33,6,-28,-385,-79,94,292,508,419,447,425,379,220,51,+ 119,-71,-29,-132,-289,30,-65,-216,-135,-208,-158,-50,-145,-248,+ -272,-204,105,-477,123,-200,189,-46,-80,113,23,180,158,136,+ 199,-12,221,-139,-41,-50,-296,98,-178,-103,-114,197,213,196,+ 233,79,487,-337,406,-236,232,-269,60,-296,151,-229,-25,-273,+ -185,-120,-202,-142,-138,-141,6,-350,-104,-271,46,-434,256,-518,+ 84,-261,-149,-95,-187,-89,107,-264,306,-13,100,125,75,-96,+ 319,-95,226,-370,229,243,38,343,105,-15,-69,357,-158,191,+ 23,-45,131,-179,2,186,-48,-96,-68,-12,-149,-154,16,-7,+ -57,84,-195,43,105,-857,671,-961,400,-438,-241,0,-175,258,+ -220,562,-494,146,165,-373,576,-757,223,-648,566,-754,820,-1004,+ 595,-320,-180,194,-558,426,-263,293,-246,39,109,-19,-77,-128,+ -219,-146,254,56,-76,34,-329,-98,-74,-404,266,-119,147,-102,+ 223,189,-29,-78,113,-57,308,-96,375,14,-247,708,-160,362,+ 117,49,233,-124,278,-348,601,-804,829,-522,-16,-94,-176,-5,+ -400,51,-105,-20,-554,117,-599,-49,-496,-173,-470,-58,-415,105,+ 9,-165,152,-100,452,-117,574,-727,500,-521,783,-401,549,-342,+ 246,-43,132,235,-258,432,344,-68,-19,180,184,-96,726,-1255,+ 1026,-436,-332,1308,-1410,1066,-888,288,-294,229,-462,266,30,-601,+ 1662,-1658,1425,-1567,780,-348,476,-471,512,-84,-278,620,-518,609,+ -484,-230,525,-799,607,-793,110,-445,421,-557,748,-970,875,-723,+ 63,466,-253,89,90,189,-741,1617,-2230,2765,-2188,980,359,-1462,+ 1941,-1489,325,678,-782,777,215,-1202,1808,-2011,1467,-619,-208,745,+ -1062,952,-701,142,393,-950,1959,-2178,1184,-740,467,-297,729,-1669,+ 1432,-398,-810,1457,-1195,83,467,-1558,820,44,-901,1432,-1296,349,+ 540,-457,-94,239,-330,410,-363,398,-401,1257,-1393,2079,-1946,1751,+ -951,241,-198,316,-691,789,-342,-475,1759,-2259,1786,-833,-848,1174,+ -1648,815,132,-812,1051,-315,-901,1852,-1961,2534,-1480,500,1127,-1973,+ 2304,-1680,905,-557,420,-1016,1892,-1992,1818,-1215,623,69,-974,1470,+ -2754,2986,-2023,-138,2578,-3323,3371,-2305,1363,-622,902,-966,684,-385,+ -482,1286,-1651,668,-414,-1099,1188,-1443,562,-463,-418,1058,-994,560,+ 263,-1037,1203,-650,546,-99,-208,364,-722,2132,-2808,3770,-3220,2668,+ -2286,2231,-1707,1942,-1260,426,500,-942,774,74,-1010,238,-284,-510,+ 922,-349,120,-423,595,-1205,1338,-1110,542,-619,14,-751,1248,-938,+ 841,-512,130,-742,729,-1238,1078,-710,995,-612,1017,-953,310,-301,+ 223,-984,1420,-2328,3503,-2990,1862,107,-722,1693,-1868,1572,-1776,2022,+ -2201,1858,-927,-529,1482,-2885,3280,-3204,2166,-1245,441,260,-171,-78,+ 67,88,-134,593,-180,90,-64,69,-498,1009,-1689,2101,-2203,2014,+ -2300,1892,-1329,1002,-560,63,-266,-490,1135,-1215,1034,-878,1191,-524,+ 776,-889,104,-917,341,-35,-114,493,-152,65,138,-838,1142,-1117,+ 1114,-995,654,-617,92,-92,-506,608,30,-515,837,-696,1127,-1400,+ 1391,-1521,726,-265,-132,1199,-1498,1854,-1855,2360,-2310,2507,-2151,1201,+ -426,380,-45,-706,719,-859,707,-487,157,-268,-454,353,-1327,854,+ -1345,277,-709,271,-699,912,-925,1322,-98,-749,477,-633,921,293,+ -407,-64,-117,383,-137,-79,-168,-378,397,-757,1092,-810,1095,-1113,+ 395,-1520,681,-326,1359,-648,414,-80,597,-6,45,-61,-408,116,+ 389,-403,320,-888,672,-1184,-136,-383,-222,-846,565,-358,1092,-265,+ 965,-354,623,-364,697,346,-890,475,-351,762,-106,334,-346,199,+ 226,397,-442,64,-29,65,85,-185,-281,-345,-293,450,-498,163,+ -749,-24,-224,-56,-270,-190,157,334,29,-366,159,-466,847,165,+ 215,-9,343,-131,358,51,371,-141,6,295,242,446,36,-69,+ -406,-487,606,599,487,-19,-124,93,121,141,62,-233,-883,28,+ -517,-267,-414,-897,-318,-1153,-31,-573,-165,-953,-257,-562,294,-217,+ -19,-516,-34,-109,487,-109,149,167,157,505,494,1265,939,1225,+ 910,1111,597,1171,1016,624,762,627,1004,1180,1292,1717,1251,1198,+ 874,846,743,-28,-634,-881,-943,-896,-906,-1615,-1977,-2370,-2961,-3202,+ -3511,-4939,+};+static const unsigned char CLIP9[1320] = {+ 79,103,103,83,0,2,0,0,0,0,0,0,0,0,72,218,43,148,0,0,+ 0,0,188,175,18,146,1,19,79,112,117,115,72,101,97,100,1,2,56,1,+ 128,62,0,0,0,0,0,79,103,103,83,0,0,0,0,0,0,0,0,0,+ 0,72,218,43,148,1,0,0,0,174,153,31,243,1,61,79,112,117,115,84,+ 97,103,115,12,0,0,0,76,97,118,102,54,49,46,55,46,49,48,51,1,+ 0,0,0,29,0,0,0,101,110,99,111,100,101,114,61,76,97,118,99,54,+ 49,46,49,57,46,49,48,49,32,108,105,98,111,112,117,115,79,103,103,83,+ 0,4,120,57,0,0,0,0,0,0,72,218,43,148,2,0,0,0,201,153,+ 201,201,16,73,80,70,80,86,70,75,80,84,69,66,71,68,76,56,37,76,+ 174,66,103,246,187,83,210,251,145,63,118,109,217,99,15,208,98,93,163,236,+ 212,196,114,26,54,241,27,54,111,75,198,35,106,62,53,95,232,170,70,47,+ 17,79,17,58,32,51,159,49,5,82,199,172,108,210,229,198,251,117,193,31,+ 86,30,58,197,34,210,253,33,71,135,189,18,76,173,116,223,182,168,176,57,+ 171,81,248,168,190,247,192,35,97,43,52,134,195,28,181,1,229,166,95,171,+ 55,142,53,253,121,84,99,44,158,112,16,156,61,224,34,38,78,108,84,155,+ 240,105,177,124,91,77,220,33,247,99,25,185,100,51,143,225,139,232,168,111,+ 88,187,24,160,6,171,44,129,187,74,242,32,76,172,201,76,66,14,142,116,+ 28,137,63,97,133,220,232,227,149,158,156,92,104,56,87,185,237,143,16,151,+ 14,78,29,196,249,110,71,4,187,75,181,116,143,23,75,91,112,212,224,71,+ 65,165,196,207,157,66,65,86,144,156,153,204,41,153,211,106,190,143,126,177,+ 16,128,76,173,63,219,134,126,202,67,78,196,137,221,157,12,64,148,197,152,+ 209,122,226,100,156,182,110,18,209,132,98,227,39,187,85,137,120,172,0,162,+ 220,83,209,2,104,164,224,82,35,120,194,20,40,1,170,72,191,28,160,223,+ 51,2,71,90,228,100,30,158,35,244,233,84,170,55,230,144,204,158,190,97,+ 10,28,76,174,40,165,141,219,28,230,109,244,182,207,79,235,183,243,171,135,+ 224,235,218,246,7,17,92,69,54,120,100,236,75,93,75,75,56,28,110,11,+ 88,101,240,5,183,243,55,191,41,118,53,197,30,41,22,13,13,116,1,248,+ 190,151,217,233,22,185,34,206,207,244,237,77,46,103,185,72,105,71,204,141,+ 116,90,161,106,101,4,59,8,76,173,190,9,230,6,128,103,219,237,135,209,+ 114,11,86,219,18,176,39,228,199,234,253,33,139,70,146,233,43,38,179,86,+ 192,18,42,68,232,236,149,132,222,99,52,56,153,4,85,209,247,0,86,203,+ 42,161,128,18,106,125,81,127,222,136,229,204,231,147,19,128,53,160,76,174,+ 88,53,86,6,232,254,228,250,145,188,181,226,67,55,33,246,192,85,76,185,+ 121,221,205,117,56,137,15,51,97,29,155,229,57,79,234,116,171,196,39,209,+ 80,173,137,157,94,105,112,237,3,83,132,118,123,36,42,129,91,17,215,187,+ 107,16,98,45,114,105,159,220,191,234,69,81,128,76,174,110,162,205,154,173,+ 230,221,28,18,136,76,221,216,201,195,20,234,34,20,116,200,88,222,116,140,+ 48,102,57,48,21,225,2,236,239,110,137,0,12,156,88,119,46,41,246,47,+ 146,199,237,53,62,70,46,136,111,186,16,45,162,130,133,111,84,176,37,63,+ 128,75,195,50,183,131,43,51,99,249,16,19,128,76,175,105,227,30,7,49,+ 115,132,21,88,254,187,219,61,223,178,134,117,4,37,54,145,162,185,156,148,+ 17,191,12,51,104,42,107,163,42,248,58,71,249,54,1,41,71,155,244,24,+ 87,43,220,29,46,16,124,188,61,141,27,64,216,161,139,94,111,160,56,169,+ 214,49,93,186,72,55,63,209,78,130,37,36,53,175,104,31,64,76,173,31,+ 204,57,141,215,14,217,254,98,109,237,96,217,185,95,66,242,209,124,121,130,+ 87,64,151,73,18,109,70,143,46,65,10,105,244,124,90,25,70,250,220,150,+ 195,73,45,12,35,247,142,172,7,219,116,247,164,30,105,182,195,166,200,41,+ 18,29,33,206,69,152,76,175,114,158,154,23,64,15,198,158,121,207,89,141,+ 155,144,79,234,189,103,100,237,172,74,186,192,11,28,10,216,220,167,55,37,+ 34,0,107,101,55,21,160,178,30,202,52,12,19,46,43,205,177,109,10,109,+ 49,146,194,99,80,86,113,62,10,40,146,152,76,160,177,18,163,234,98,31,+ 15,142,13,116,167,193,221,203,91,52,215,86,175,38,101,189,3,28,94,171,+ 169,239,59,125,244,252,59,118,196,71,62,182,246,45,141,163,174,239,244,181,+ 71,238,183,240,98,71,5,63,205,96,12,58,153,137,27,200,201,56,187,67,+ 207,111,128,76,173,102,76,235,83,105,121,11,253,219,179,229,0,216,16,143,+ 15,235,245,8,141,239,225,254,71,50,54,36,162,138,122,253,229,193,133,204,+ 132,113,218,232,173,49,106,54,27,187,71,96,116,94,144,226,244,43,43,119,+ 35,245,241,140,74,128,93,209,146,20,202,76,173,138,2,141,119,119,96,64,+ 50,193,254,51,152,130,17,216,242,212,174,87,248,126,198,144,188,143,175,31,+ 148,84,80,156,242,238,8,249,208,37,156,96,107,79,156,156,26,130,132,10,+ 19,179,222,173,59,153,145,61,29,104,164,203,184,154,72,75,123,192,119,157,+ 159,123,63,241,121,195,64,76,172,250,16,242,110,138,128,142,105,230,173,80,+ 204,40,163,57,32,12,117,138,149,48,135,15,106,24,202,223,47,1,123,49,+ 42,97,247,30,191,13,130,105,236,231,245,50,140,114,117,239,170,14,208,167,+ 192,90,64,76,15,184,55,59,222,135,37,153,6,56,216,177,48,53,32,217,+ 16,214,191,203,99,161,133,95,93,215,185,237,138,121,90,14,94,139,216,96,+};+static const int REF9[3600] = {+ -356,-200,-193,132,-88,-249,122,34,-256,-326,-10,-252,-338,-358,+ -104,172,-484,-143,-42,100,-28,95,306,251,215,242,490,352,+ -202,15,110,323,40,134,86,-53,447,412,434,479,527,640,+ 682,694,724,704,704,656,744,721,700,586,524,380,-354,-418,+ -677,-1336,-1636,-1827,-1960,-2328,-2476,-2545,-2872,-3298,-3226,-3010,-3088,-3072,+ -2493,-1880,-1800,-1268,341,985,1359,1942,2556,3310,3140,3414,3702,3538,+ 2881,2819,2799,2456,1738,1639,1292,1030,1076,1402,1298,1424,1530,1750,+ 2124,2155,1848,2300,2348,1988,1978,1878,1730,1442,859,238,94,-798,+ -1963,-2171,-2575,-3175,-3722,-3832,-3922,-4134,-4293,-4546,-5013,-5562,-5280,-5124,+ -4602,-3934,-2500,-706,-164,750,1868,3007,3194,2780,3388,3317,2745,1427,+ 1740,1494,784,34,-256,-360,-1002,-1324,-1274,-1368,-1560,-1594,-1282,-898,+ -304,367,965,1643,2552,3039,3285,3924,4080,4136,4098,3826,3930,3663,+ 3128,2808,2472,2028,987,112,-978,-786,-1112,-2220,-2118,-2208,-2494,-2586,+ -2746,-3465,-4786,-5168,-6122,-4668,-5032,-5141,-3268,-1460,-363,40,980,2362,+ 2974,2921,3197,2924,1394,1126,332,-110,-1608,-2876,-4202,-3626,-3920,-3678,+ -3624,-3182,-1640,-630,392,1094,2172,3292,4226,4378,4594,4258,3716,3485,+ 3150,2855,2778,1660,1835,1670,2073,1878,2059,1931,1914,1673,1109,910,+ 360,-25,-734,-918,-620,-1094,-1828,-2813,-4088,-4608,-5316,-5850,-7354,-7026,+ -6346,-4352,-2180,-1172,-779,-25,2245,3334,4214,3004,1692,1444,846,710,+ -822,-2326,-4096,-4519,-5179,-4346,-4466,-4026,-2286,-820,690,1703,2790,3682,+ 4752,5057,5079,4702,3677,3061,2902,2798,1794,1333,1038,2082,1981,2288,+ 2235,2612,2736,2740,2570,1925,1300,334,90,-464,-953,-1489,-1926,-2398,+ -3110,-3703,-4420,-4994,-6487,-6344,-6246,-4998,-3964,-1615,-576,332,1245,2713,+ 3886,3789,2616,906,854,-166,-682,-2174,-3400,-4432,-4876,-4640,-4936,-4723,+ -3688,-2054,460,1553,2432,3126,4389,5142,5609,5080,4006,3114,2333,2206,+ 1712,944,818,1296,1707,2783,3122,3293,3621,3934,4184,3972,3059,1932,+ 1029,214,-539,-1334,-2154,-2704,-2911,-3167,-4260,-6338,-5392,-5467,-5462,-6245,+ -5503,-4347,-2058,754,1685,2379,2228,3660,4283,3308,1642,-418,-1255,-2846,+ -3740,-4382,-5392,-6018,-6184,-5140,-4564,-3456,-2058,912,2961,3926,4550,5258,+ 6030,5882,5356,4044,2737,1454,1230,932,728,259,360,1100,1984,2658,+ 3761,3431,3688,4268,3904,3107,1856,913,21,-586,-1608,-1892,-2546,-3323,+ -3937,-4571,-4799,-5616,-5500,-5359,-4586,-4404,-3884,-1656,480,1246,1297,1306,+ 2346,2292,1647,332,-464,-1926,-1998,-2118,-3412,-4836,-4704,-4428,-3968,-4254,+ -3387,-1706,-130,3050,3862,4225,4052,5470,6064,5220,3703,2354,1726,1120,+ 866,750,397,272,1648,2314,4002,3709,3749,4747,6051,4391,3538,2116,+ 1514,558,-535,-1456,-2869,-3715,-3908,-3556,-4373,-4920,-5473,-4796,-4274,-3746,+ -3476,-3230,-2270,-129,1536,1990,323,957,1290,1638,409,-570,-1674,-1950,+ -2386,-2736,-3671,-4831,-4800,-5218,-3590,-3590,-2886,-984,1285,2680,3324,3914,+ 4542,5122,4292,3819,2990,1893,1075,975,754,718,1448,1832,2520,2944,+ 3516,3842,4423,4246,3806,2763,1858,1143,408,-542,-1486,-2398,-2870,-3076,+ -3299,-3730,-4101,-4558,-5114,-4756,-4626,-3396,-3362,-2238,-562,1568,365,642,+ 830,1876,2113,830,-74,-685,-1044,-1780,-1811,-3248,-4002,-4522,-4139,-3457,+ -3428,-3712,-1416,354,2612,3374,3436,3951,4648,4946,5176,3531,2130,1338,+ 1388,1200,910,739,1167,1703,2328,3114,3448,3496,3670,3738,3306,2452,+ 1448,682,-11,-760,-1560,-2174,-2736,-3040,-2966,-2602,-3622,-4182,-4618,-4208,+ -4408,-3985,-3806,-1687,-481,-45,-194,396,1097,1748,1488,630,-50,-618,+ -880,-1230,-2152,-3299,-3830,-3902,-3470,-3372,-3376,-2828,-358,1111,1590,2058,+ 3356,3798,4382,4218,3882,3358,2735,2828,3279,2628,1952,1624,1835,1948,+ 1835,1491,1296,1128,1218,1341,1053,683,510,531,613,495,308,178,+ 168,235,258,80,-48,-104,-256,-968,-999,-1650,-1856,-1980,-1993,-2222,+ -2218,-2860,-2779,-2398,-2239,-2246,-2232,-2102,-1779,-2110,-3196,-3220,-3334,-3248,+ -3274,-3122,-2448,-1170,-134,644,1260,1866,2616,3503,3564,3377,2999,2954,+ 2996,2610,2267,1768,1456,1262,1282,1040,783,546,695,1294,1138,1257,+ 1584,1562,1736,1789,2181,2208,1956,1777,2080,2342,2006,1778,1426,1107,+ 678,299,-170,-654,-1038,-1148,-1178,-1426,-1790,-1942,-1777,-1694,-2094,-2081,+ -2057,-2103,-2420,-2246,-2428,-2760,-2738,-3008,-2764,-3111,-3038,-2740,-2066,-1778,+ -1516,-1322,-922,-558,-218,344,337,664,812,996,1015,1158,1362,1256,+ 1295,1572,1493,1393,1310,1294,1260,1403,1542,1321,1224,1173,1261,1290,+ 1089,914,797,750,723,650,507,352,44,-244,-141,-222,-350,-440,+ -548,-460,-410,-354,-318,-384,-465,-3,117,-98,-362,-275,256,28,+ -288,-584,-714,-774,-840,-990,-860,-192,-649,-552,-380,-282,-862,-732,+ -625,-55,438,-218,-272,-62,366,576,628,342,516,1124,1950,2560,+ 1384,-53,927,1578,1457,788,29,60,202,-152,66,-760,-1922,-1708,+ -1967,-1787,-2219,-2744,-3327,-3412,-2951,-2612,-2704,-2880,-2332,-1434,-724,-381,+ -204,162,679,1122,1244,1099,898,866,948,814,520,309,338,1000,+ 1000,693,746,1613,1448,1357,1339,1389,1130,1364,2002,1563,1133,1021,+ 1218,1238,958,831,614,177,-73,-28,-753,-1446,-1891,-2688,-3421,-4110,+ -4826,-4398,-5053,-6292,-6084,-5144,-4094,-1725,-181,-40,1092,3487,4756,5761,+ 4911,4008,4234,4234,3524,2376,968,145,-142,-416,-1003,-1810,-2204,-1214,+ -276,-285,-140,406,892,1462,1916,2102,1678,1437,1660,1935,2476,2270,+ 2098,2734,2999,2773,2564,2338,2043,1800,1348,804,128,-520,-1012,-1500,+ -2154,-2929,-3557,-4030,-4954,-6446,-7497,-7470,-7248,-6682,-4794,-1830,-645,216,+ 2254,4472,5542,5546,4628,3684,2780,2622,1686,-734,-2235,-2288,-2160,-2776,+ -3364,-4096,-4232,-3666,-2054,-1418,-1115,484,2200,3046,3662,4362,4600,4312,+ 3948,3905,4484,3900,2730,2372,2862,3663,2935,1480,1100,1486,1998,1106,+ -109,-202,384,-102,-675,-1297,-1990,-2487,-3032,-4743,-6778,-7990,-7390,-6972,+ -6118,-3306,-390,523,1940,4438,6314,6156,4292,4214,4156,2006,96,-1676,+ -2823,-3680,-4168,-4487,-5241,-6090,-6000,-4176,-2844,-2146,-218,2194,3269,4193,+ 4647,5582,6337,4550,3808,4321,3789,2776,2630,2344,2094,1636,2102,1968,+ 978,738,961,576,25,-326,-556,-1324,-2696,-2756,-2792,-3792,-5512,-7126,+ -7192,-6468,-5961,-4962,-2754,-470,1027,2520,4361,5404,5222,4583,4067,2914,+ 1182,-312,-1534,-2682,-3520,-3892,-4198,-4737,-4916,-4195,-3173,-2728,-1689,-72,+ 1054,2923,3832,3476,3720,4404,4984,4370,3422,2958,3162,2830,2324,1614,+ 1198,1398,2204,1779,600,64,439,436,-343,-1410,-2158,-2057,-1248,-1974,+ -3922,-6090,-6730,-6883,-6200,-5520,-4472,-1864,1099,2692,3579,4727,6086,5813,+ 4368,3092,1987,476,-938,-2074,-3040,-3809,-3901,-3696,-4044,-3898,-2833,-2286,+ -1862,-782,293,1847,3532,3700,2842,2584,3618,4650,4228,2814,2470,3182,+ 3160,2505,1473,1023,1443,2154,1768,470,-123,204,522,-218,-1522,-2282,+ -1808,-1166,-2424,-5576,-7009,-6850,-6766,-7350,-6508,-4162,-344,2038,3195,4268,+ 6374,7127,6298,4489,2800,1508,48,-1286,-2888,-4020,-4310,-3708,-2862,-3258,+ -3069,-2288,-1114,-214,378,1368,2752,3233,2738,2526,3184,3890,3924,3094,+ 2586,2680,2834,2432,1636,1130,1320,1786,1498,616,6,104,168,-354,+ -1352,-2060,-1893,-1600,-2682,-5012,-6473,-6460,-6080,-5365,-4958,-2930,125,2522,+ 3464,4248,5599,6112,5319,3782,2008,-196,-1062,-1740,-2665,-3721,-3856,-2752,+ -1958,-1796,-1684,-1139,-580,-2,-368,-332,680,1690,2730,2520,2128,2624,+ 3434,3243,2330,1640,2338,2640,2294,2276,1900,2372,2116,1406,684,-195,+ -482,-822,-1224,-1762,-1990,-2527,-3256,-4985,-6800,-7747,-6493,-5410,-4274,-1826,+ 398,2414,3863,5001,6198,5624,4392,2498,1481,206,-1106,-2260,-2714,-2777,+ -2688,-2010,-970,-1138,-1214,-628,-731,-1799,-961,-124,623,1338,1996,2220,+ 1817,2758,3223,2937,2958,3001,3228,3002,2934,2559,2208,1564,1030,461,+ -342,-873,-1230,-1413,-1549,-1706,-2270,-3071,-4256,-5778,-6656,-6236,-5468,-5157,+ -2738,-175,1526,2608,4168,6006,7006,5086,3288,1994,1115,-260,-1730,-2934,+ -3325,-2860,-2018,-1497,-1394,-336,400,-170,-560,-116,-90,-300,983,1018,+ 803,1306,2732,3248,3218,3388,3480,3468,3408,3187,2552,1764,1338,965,+ -532,-2034,-1664,-1740,-1990,-2154,-2970,-4066,-4907,-5642,-6592,-6917,-6168,-5587,+ -3488,626,1668,2460,3999,6226,7350,5938,3610,2022,1292,266,-1354,-3113,+ -3690,-2614,-853,-1095,-208,330,2170,2166,1123,-26,-912,-980,-961,-394,+ 111,288,1846,3383,3920,4338,3845,3609,3442,3014,1846,750,-26,-546,+ -1158,-1770,-2081,-2508,-3298,-2802,-3364,-4541,-5218,-5488,-5890,-4822,-4108,-2418,+ -520,1668,2958,3794,4618,5346,4924,3426,1948,725,-216,-1132,-1166,-1686,+ -2097,-401,812,425,531,1235,1170,675,-198,-955,-1586,-1591,-1349,-1026,+ 597,2694,2855,3732,4386,5212,4615,3220,2344,1511,476,-370,-1398,-2244,+ -2336,-2130,-2406,-2911,-3616,-4518,-4002,-4396,-4907,-5645,-4492,-2018,-476,501,+ 1076,1828,4074,4670,3694,1367,1133,1372,720,-881,-1338,-216,280,590,+ 695,478,482,1664,2512,592,-722,-605,-202,-1001,-1865,-1890,-536,1218,+ 2330,2458,2504,4036,5300,4618,3056,1937,1182,390,-480,-1804,-2828,-2872,+ -2176,-1818,-2681,-3034,-3674,-3454,-3384,-3756,-4452,-4198,-3678,-1171,671,974,+ 1386,2547,4294,4639,2962,1862,1362,1430,554,-583,-1234,-764,-279,128,+ 606,403,441,498,809,-30,-1048,-1596,-1310,-1530,-929,-130,445,1764,+ 2716,3506,3503,3600,3609,2128,1635,706,278,-1026,-1586,-1777,-1706,-2094,+ -1954,-1951,-2573,-2694,-2434,-3288,-3752,-3970,-3688,-2294,-1306,-1060,364,1476,+ 3139,3113,3069,2974,3006,2598,1761,-158,-398,-618,502,-607,-859,-1068,+ 76,192,103,-884,-815,-643,-160,-586,-1124,-1124,-48,2004,2819,1994,+ 2332,3316,4036,3205,1637,1010,578,403,-1096,-2247,-2790,-2422,-2189,-2200,+ -2769,-2688,-2898,-1632,-1948,-2580,-3448,-2118,-1286,-758,-124,510,1695,2573,+ 3336,3084,2571,2342,2610,2025,1002,-30,-344,-389,-578,-1010,-1434,-1459,+ -1037,-900,-1112,-1397,-1134,-670,-431,-482,-328,802,1162,2355,2576,2852,+ 2722,2968,2688,2208,992,685,-550,-1034,-1530,-1976,-2094,-1376,-1660,-1066,+ -1402,-654,-701,-1227,-1836,-2125,-1979,-1930,-1838,-1624,-811,56,1016,1292,+ 1616,1821,2157,2352,2526,1442,634,552,698,114,-880,-1119,-808,-590,+ -1002,-1962,-1868,-1546,-1060,-1229,-1348,-1170,-318,389,1102,1576,1560,2304,+ 2658,2081,1586,1809,1472,1124,874,888,620,869,498,438,-46,-334,+ -344,-837,-1290,-2474,-2484,-2084,-2149,-3102,-2765,-2570,-1494,-692,-644,-474,+ 18,1570,1988,1459,706,1207,1632,1672,420,-77,-66,453,466,-290,+ -1106,-836,-422,-164,-738,-1237,-1064,-382,-22,295,284,544,1784,1888,+ 2098,2294,2050,2605,2508,2322,1366,1246,1090,1218,-87,-901,-1295,-1282,+ -1702,-2838,-3584,-3608,-3280,-3186,-3502,-3694,-3066,-2136,-1226,-650,-737,166,+ 996,1714,1764,1586,1703,2212,2350,2228,1756,1577,1755,1762,1370,836,+ 396,427,326,14,-548,-864,-828,-532,-572,-570,-145,260,1062,1562,+ 1348,1604,1660,2106,1900,1598,717,478,-528,-780,-1414,-1878,-2598,-3224,+ -3429,-3265,-3228,-2764,-2489,-1970,-1442,-836,-380,-394,183,478,944,964,+ 1142,1254,1615,2065,2010,1906,1767,1926,1906,1682,1204,930,748,624,+ 258,-582,-789,-977,-896,-1161,-1310,-1210,-456,-71,488,596,694,1200,+ 1492,1958,1679,1082,694,868,642,237,-858,-1162,-1690,-1986,-2156,-2468,+ -2698,-2408,-2258,-1821,-1844,-1754,-1406,-926,-596,-324,-58,542,857,1320,+ 1552,1633,1648,1670,1830,1688,1510,1471,1004,1102,658,403,-66,-199,+ -441,-450,-631,-218,22,417,612,890,1031,1212,1308,1458,1346,990,+ 712,514,335,-6,-538,-930,-1500,-2061,-2310,-2540,-2584,-2886,-2900,-2612,+ -2358,-2149,-1934,-1756,-1534,-1036,-524,0,438,730,1246,1644,1842,1920,+ 1950,2263,2409,2306,2256,2190,2082,1799,1248,904,626,528,574,430,+ 475,510,705,756,826,654,605,472,364,94,-186,-532,-792,-1122,+ -1417,-1814,-2256,-2652,-2834,-2882,-2906,-2928,-2822,-2384,-2048,-1756,-1548,-1232,+ -903,-370,98,570,942,1313,1704,2052,2230,2320,2395,2633,2898,2794,+ 2698,2512,2328,2057,1654,1275,894,471,428,324,196,64,-77,-77,+ -61,-141,-107,-392,-594,-682,-921,-1099,-1406,-1720,-2037,-2496,-2722,-2874,+ -2941,-2963,-2968,-2799,-2255,-1796,-1504,-1440,-1178,-790,-203,306,882,1144,+ 1520,1913,2347,2592,2632,2599,2767,3008,3071,2936,2590,2248,2024,1721,+ 1372,1112,562,338,170,120,-8,-168,-330,-338,-382,-339,-472,-705,+ -918,-1069,-1221,-1493,-1884,-2177,-2456,-2657,-2720,-2802,-2797,-2750,-2505,-2112,+ -1788,-1708,-1463,-1223,-796,-331,103,524,921,1444,1833,2174,2368,2662,+ 2737,2932,3038,3083,2988,2779,2542,2311,1996,1675,1289,802,578,368,+ 238,49,-160,-296,-366,-438,-526,-731,-1118,-1285,-1462,-1581,-1878,-2214,+ -2505,-2679,-2813,-2850,-2894,-2890,-2780,-2423,-1980,-1712,-1528,-1300,-970,-513,+ -80,332,706,1108,1560,1972,2268,2476,2640,2820,3102,3176,3172,3010,+ 2852,2640,2283,1989,1632,1270,878,608,380,190,-30,-221,-376,-487,+ -600,-730,-946,-1201,-1398,-1568,-1734,-1977,-2353,-2612,-2762,-2805,-2863,-2870,+ -2862,-2682,-2384,-2025,-1746,-1530,-1294,-949,-440,78,439,785,1176,1576,+ 1990,2250,2440,2595,2768,2946,3051,3017,2896,2715,2506,2253,1957,1624,+ 1272,950,642,262,46,-180,-334,-530,-701,-850,-981,-1154,-1388,-1711,+ -1856,-2024,-2194,-2412,-2627,-2734,-2764,-2736,-2671,-2552,-2248,-1988,-1654,-1387,+ -1103,-827,-488,-68,498,898,1247,1556,1919,2237,2524,2825,2936,3056,+ 3089,3145,3102,2946,2628,2289,2018,1679,1298,975,653,397,211,-52,+ -218,-470,-634,-792,-944,-1196,-1476,-1709,-1855,-2024,-2272,-2480,-2651,-2720,+ -2756,-2740,-2687,-2568,-2338,-2090,-1886,-1600,-1344,-1044,-708,-331,120,558,+ 1026,1366,1715,1980,2292,2558,2773,2882,2951,2970,2954,2852,2646,2367,+ 2082,1782,1478,1152,846,575,331,100,-105,-309,-399,-580,-701,-864,+ -1014,-1189,-1350,-1486,-1601,-1740,-1924,-2054,-2110,-2112,-2092,-2040,-1936,-1764,+ -1568,-1372,-1197,-1078,-870,-654,-358,-88,206,506,804,1061,1390,1586,+ 1796,1934,2054,2131,2136,2040,1905,1774,1612,1420,1214,996,795,618,+ 442,264,90,-74,-197,-310,-410,-534,-638,-733,-835,-934,-1018,-1080,+ -1072,-1154,-1208,-1255,-1246,-1204,-1208,-1220,-1132,-1076,-1019,-908,-846,-722,+ -572,-420,-210,-16,172,364,554,726,902,1091,1231,1332,1380,1428,+ 1458,1450,1363,1255,1149,1020,890,680,506,367,254,116,-6,-119,+ -202,-322,-446,-527,-556,-584,-656,-724,-746,-738,-702,-726,-694,-605,+ -564,-498,-536,-468,-478,-479,-460,-386,-296,-237,-192,-33,31,94,+ 218,208,307,340,372,472,511,552,554,547,564,557,530,498,+ 495,488,460,333,175,155,8,-20,-119,-220,-288,-262,-318,-322,+ -407,-440,-464,-499,-518,-512,-492,-450,-417,-268,-214,-125,-100,-43,+ -40,94,122,116,96,113,156,120,151,171,254,191,246,316,+ 348,384,364,396,344,304,314,186,82,30,-86,-168,-120,-126,+ -217,-254,-315,-354,-392,-417,-434,-474,-532,-492,-394,-318,-324,-272,+ -215,-175,-79,-72,52,52,104,163,213,282,202,240,271,142,+ 203,230,305,254,331,316,300,460,348,352,418,358,346,323,+ 247,156,-21,-164,-146,-196,-212,-306,-349,-350,-363,-350,-486,-490,+ -608,-433,-314,-382,-307,-380,-298,-268,-248,-88,-88,54,214,276,+ 269,176,150,26,200,316,618,479,616,404,472,395,276,301,+ 372,328,214,123,322,234,112,146,60,64,46,-35,-12,-211,+ -131,-41,-328,-412,-386,-376,-303,-252,-446,-429,-270,-253,-114,-230,+ -398,-224,-293,-336,-278,-239,-144,-148,-40,-33,2,123,52,230,+ 104,72,118,182,-32,232,141,146,168,94,59,-23,8,80,+ -100,-24,-62,66,27,83,176,-54,147,42,192,194,133,158,+ 32,96,-256,29,-108,224,521,52,398,189,163,300,-23,-106,+ 214,-460,-10,-327,-294,-74,-184,-340,-176,-354,-159,-124,-223,-214,+ -152,-77,30,-230,-124,-31,-94,41,-151,254,-97,352,138,46,+ 266,0,174,-70,60,-294,-50,43,-206,-16,144,140,252,-13,+ 290,-91,302,-208,284,-160,204,-282,6,51,-197,-102,-94,-438,+ 118,-467,14,-233,-129,26,70,-313,172,-242,-24,-31,100,-464,+ 297,-444,232,-250,213,-192,162,-100,308,258,-134,496,-114,307,+ 212,-144,288,-392,512,-132,372,-26,175,74,17,186,-46,114,+ 68,-130,178,-252,88,88,-123,104,20,-207,79,-34,-312,605,+ -476,-44,383,-410,-160,-98,-350,-200,183,-571,42,-152,139,124,+ -198,324,-288,507,-282,154,-22,-173,155,-240,306,-294,162,-509,+ 663,-680,264,-124,-224,382,-154,216,-202,630,-323,266,-76,-131,+ -94,280,-402,574,-238,-112,-94,-113,-190,4,32,256,2,392,+ -10,240,88,-45,89,142,-112,351,470,-401,702,-194,64,124,+ 38,-132,194,-187,262,-248,61,-107,-124,-164,-280,7,-489,410,+ -547,408,-432,-48,-342,-370,59,-310,-208,-98,-91,-316,4,112,+ -115,356,74,18,357,131,-26,-86,-147,315,276,-90,248,-61,+ -115,516,-364,640,-451,900,-633,527,110,-244,758,-681,432,-264,+ -99,308,-112,405,-277,-158,98,-228,80,-236,-468,588,-608,618,+ 100,-769,967,-1500,766,-390,56,95,431,-738,277,191,-504,92,+ 530,-1168,1100,-531,-134,111,-304,-87,72,186,208,-53,356,-480,+ 666,-466,810,-702,656,-386,187,7,-246,784,-1050,836,-748,391,+ -341,222,-420,663,48,-246,632,-844,1160,-1182,736,-278,-189,403,+ -621,445,-536,266,-136,12,502,-1310,810,-260,179,234,-455,166,+ -568,812,-677,504,-456,-272,-110,-570,658,-615,811,-358,-265,766,+ -454,380,-155,-209,126,450,-591,725,166,-164,334,381,-589,1047,+ -980,814,-812,424,-371,352,-566,597,-193,-366,620,-776,434,-768,+ 368,-581,826,-672,782,-634,76,542,-126,176,610,-1096,1045,-262,+ 1,-62,134,-195,-166,635,-790,571,70,-474,631,-912,439,-376,+ -277,788,-1010,170,938,-1098,1207,-1258,1013,-74,-525,905,-838,122,+ -57,-133,-76,-658,4,-309,677,-975,368,-468,151,602,-706,818,+ -401,158,680,-633,946,-454,-440,856,-766,1104,-782,798,-661,654,+ -1185,1918,-1352,1112,-362,-7,564,-822,828,-807,-294,-352,-186,30,+ 554,-678,777,-610,-24,170,-476,759,-1030,144,-450,181,409,-344,+ 768,-771,576,-512,219,240,-442,635,130,-56,414,-691,108,331,+ -824,1040,-822,126,1222,-1578,1531,-292,-264,988,-1264,974,-663,-25,+ -190,968,-1358,794,-608,-44,276,-602,625,-680,696,-52,-298,714,+ -943,821,422,-884,1090,-1110,855,-486,203,-80,163,-390,412,-208,+ -45,318,-462,446,-296,-53,214,-256,266,-70,-170,148,74,-187,+ 232,-316,-458,404,-520,568,149,-143,370,-573,577,-594,338,-199,+ 96,-117,174,-166,-72,540,-820,866,-610,198,-180,194,-314,276,+ -90,102,-6,-163,496,195,-168,320,-100,-16,231,-340,298,-204,+ 224,-362,558,-503,420,-399,248,-178,229,-492,-460,394,-782,-272,+ 254,-1034,446,-108,78,132,2,369,-112,283,-16,-76,369,-75,+ -74,19,-31,-140,52,-20,-94,-82,-134,-60,113,61,196,-214,+ -455,-313,301,508,134,238,38,586,392,-365,842,-1184,858,-364,+ 3,-42,-143,-140,-120,-756,114,-348,-264,-439,217,1167,-518,1500,+ -359,281,-120,456,-551,481,-489,208,464,-426,440,-304,-58,204,+ -253,140,-52,-80,319,-177,254,9,-173,-310,-193,84,29,-245,+ -384,10,-380,410,-70,-92,185,-195,148,228,-27,160,173,508,+ -422,580,-310,-122,204,-178,62,170,-53,572,-28,174,-500,323,+ -488,102,317,868,428,-56,38,-146,300,-34,56,-642,-692,-734,+ -663,-1088,-769,-757,-842,-592,244,-234,76,-329,238,185,141,176,+ 93,274,150,274,403,450,254,292,365,318,598,505,540,751,+ 562,727,738,930,961,134,362,230,410,230,217,240,-122,-133,+ -89,-412,-532,-1418,-1311,-1510,-1506,-1968,-1945,-2108,-2403,-2252,-2326,-2472,+ -2684,-1585,+};+static const unsigned char CLIP10[2911] = {+ 0,0,0,28,102,116,121,112,77,52,65,32,0,0,2,0,77,52,65,32,+ 105,115,111,109,105,115,111,50,0,0,0,8,102,114,101,101,0,0,8,41,+ 109,100,97,116,222,2,0,76,97,118,99,54,49,46,49,57,46,49,48,49,+ 0,66,83,159,244,144,77,136,19,22,168,34,170,2,137,209,160,149,239,117,+ 87,91,186,206,185,117,188,152,235,121,245,190,120,153,151,215,136,47,157,216,+ 53,194,35,69,180,128,236,97,68,241,7,233,139,176,75,49,6,235,153,240,+ 214,192,184,116,9,126,151,232,115,103,33,143,221,12,99,37,87,118,42,232,+ 191,185,235,116,249,105,34,249,14,184,220,51,195,142,230,210,9,96,24,161,+ 203,199,134,162,225,167,87,241,232,94,141,13,69,108,1,91,157,0,111,175,+ 119,13,3,125,123,0,229,80,168,168,71,46,122,4,24,169,196,63,80,188,+ 193,135,16,97,212,238,236,8,142,197,80,248,138,79,251,195,248,186,192,127,+ 175,126,143,167,122,142,238,0,167,176,0,1,68,67,20,187,207,30,14,183,+ 18,24,161,248,54,96,179,7,237,15,192,123,187,159,252,0,6,128,143,95,+ 232,247,112,11,79,143,44,0,0,167,121,139,60,29,132,72,128,145,24,66,+ 36,14,143,131,161,87,189,213,93,76,185,87,146,247,199,50,163,91,188,154,+ 197,212,7,140,230,129,246,12,108,127,60,29,145,49,70,7,69,41,149,1,+ 46,11,250,115,19,165,254,112,102,130,148,51,231,22,107,31,237,210,113,56,+ 204,238,236,10,200,153,14,207,249,198,136,187,187,117,25,180,188,143,252,254,+ 29,186,147,45,103,129,228,203,145,145,210,80,5,41,96,222,34,233,43,3,+ 230,108,76,4,159,58,20,160,126,228,148,158,84,116,207,242,185,210,50,54,+ 157,197,162,212,129,7,184,251,112,234,170,174,235,131,0,234,184,15,8,134,+ 219,155,57,101,32,185,157,179,206,190,252,140,76,160,12,160,0,182,90,194,+ 215,103,103,155,135,248,248,231,222,3,182,126,192,122,224,10,224,33,79,108,+ 132,128,235,215,8,37,4,69,168,251,174,200,254,44,169,31,172,171,47,17,+ 199,250,67,237,241,223,195,135,126,223,237,255,246,217,254,37,215,143,62,62,+ 249,93,126,255,255,118,250,215,227,158,119,196,233,212,246,255,251,57,230,125,+ 211,197,207,137,171,118,239,59,16,3,110,208,1,248,86,179,141,247,245,178,+ 11,92,157,175,73,144,183,135,99,39,122,198,87,121,3,220,169,163,50,49,+ 100,200,81,211,176,86,40,145,185,52,139,130,115,154,160,55,146,2,48,144,+ 64,8,4,192,22,249,72,192,192,81,36,199,176,114,217,43,183,8,135,39,+ 239,145,13,8,197,26,190,95,230,151,231,247,101,133,49,151,125,234,41,138,+ 94,122,133,21,77,253,7,174,157,33,27,11,179,75,13,159,86,46,221,249,+ 191,80,210,40,15,83,39,35,143,86,57,184,171,125,135,179,217,157,28,105,+ 11,97,124,132,96,235,162,140,157,218,80,113,61,100,188,215,100,124,228,38,+ 200,251,202,201,121,237,253,43,175,191,183,57,156,111,233,229,227,245,252,127,+ 253,183,95,26,87,143,57,230,174,255,127,255,181,62,39,198,111,126,197,243,+ 63,219,255,233,95,83,245,204,241,229,213,41,219,160,250,8,6,221,199,255,+ 136,26,69,76,196,21,163,217,40,123,9,7,165,62,84,145,29,131,97,132,+ 181,118,114,187,8,227,37,99,199,140,115,172,96,73,36,118,86,130,98,53,+ 88,242,131,32,64,153,58,134,16,184,126,129,59,208,200,132,4,107,65,36,+ 88,4,155,200,96,224,124,76,196,83,228,112,82,231,244,133,79,55,199,60,+ 252,194,49,127,15,215,112,212,198,183,144,52,16,241,83,8,148,136,36,57,+ 15,59,90,33,51,77,199,130,140,137,104,11,31,238,33,122,143,153,221,254,+ 255,1,39,65,103,10,144,194,93,16,143,66,231,244,249,212,239,167,141,121,+ 222,106,95,62,106,51,89,93,249,130,107,121,8,67,161,183,12,245,37,142,+ 56,204,4,248,7,58,22,246,99,54,106,78,159,55,207,17,4,128,122,221,+ 235,242,95,160,104,20,52,70,106,102,238,213,235,210,130,0,64,66,120,166,+ 253,17,43,131,96,243,120,163,88,84,151,58,146,241,70,8,246,8,182,252,+ 229,191,252,215,15,84,135,141,82,36,206,11,20,46,7,135,149,239,38,223,+ 14,206,168,26,161,6,26,183,54,174,164,188,30,105,61,90,160,68,91,96,+ 128,194,22,168,253,201,252,125,4,68,151,7,12,242,236,106,217,50,245,13,+ 4,28,96,168,36,9,56,34,21,196,175,66,9,124,58,62,14,137,207,233,+ 243,237,127,110,184,247,158,119,172,151,106,140,214,82,65,53,189,193,84,156,+ 6,142,19,57,236,159,241,102,142,79,248,212,255,132,116,168,224,42,224,174,+ 247,201,31,113,56,241,189,79,183,192,204,192,167,215,244,206,126,220,84,134,+ 49,95,14,124,112,16,165,234,98,64,47,20,157,72,70,115,24,20,0,78,+ 244,4,64,33,24,155,153,135,150,101,226,127,100,210,188,23,221,122,151,186,+ 20,119,7,125,59,117,243,150,131,113,121,194,178,86,90,239,224,85,193,139,+ 225,40,183,222,98,99,24,148,194,5,5,184,33,26,139,251,255,255,254,5,+ 37,8,84,138,24,66,125,8,135,68,34,48,232,83,159,251,126,47,59,234,+ 172,181,77,85,214,183,89,21,172,226,92,129,251,235,118,129,148,238,167,168,+ 130,80,19,32,152,243,28,123,45,251,170,171,97,24,206,82,146,165,57,173,+ 176,69,188,208,23,223,245,253,61,157,177,69,87,118,123,254,120,23,139,86,+ 69,82,251,247,2,219,135,164,30,69,49,114,60,93,117,146,193,55,187,203,+ 132,0,132,218,153,177,153,1,235,242,121,48,119,16,63,140,113,167,206,32,+ 45,74,163,68,105,252,114,31,195,156,210,241,88,91,0,184,175,241,240,173,+ 66,243,5,191,128,231,141,47,55,240,192,41,133,252,63,129,111,55,155,248,+ 127,250,94,138,84,0,71,209,88,196,51,166,135,195,162,241,232,89,207,253,+ 191,23,157,252,115,101,170,106,174,181,186,200,173,103,18,228,19,122,168,144,+ 71,10,206,135,12,105,98,196,1,67,117,61,251,58,180,248,225,4,17,149,+ 114,84,172,79,250,242,203,112,128,0,54,62,21,246,158,101,109,148,227,55,+ 187,133,186,41,231,187,66,65,28,68,165,210,141,211,69,12,221,160,237,118,+ 112,207,255,210,255,117,234,163,255,139,255,233,118,108,111,112,157,23,11,55,+ 74,6,110,105,163,19,53,146,227,183,5,50,137,41,83,71,154,47,35,117,+ 186,56,33,26,140,155,24,64,206,1,43,3,40,139,180,176,53,120,253,156,+ 113,91,250,239,46,106,185,145,199,119,202,221,86,218,136,78,55,155,80,189,+ 16,224,184,172,110,220,100,76,212,140,158,33,101,104,196,196,21,40,10,242,+ 92,144,9,246,141,140,196,95,23,129,227,251,18,151,214,59,160,52,250,36,+ 103,35,3,111,214,142,208,167,61,82,81,72,112,41,94,150,58,187,9,57,+ 187,124,148,21,219,11,140,198,126,208,142,218,106,130,205,60,27,255,62,136,+ 123,72,0,206,28,24,152,123,184,237,129,105,98,57,58,179,168,208,35,87,+ 31,200,238,49,17,219,192,12,49,200,254,250,172,71,139,64,155,137,151,183,+ 109,150,1,177,78,254,69,98,119,194,35,65,137,179,190,72,147,17,19,212,+ 75,232,48,49,17,45,41,190,28,131,38,38,17,75,33,17,111,215,74,193,+ 10,2,237,47,140,127,199,92,85,95,25,156,76,169,36,201,76,151,84,149,+ 16,109,222,243,148,4,5,170,72,150,38,139,100,30,36,89,36,220,194,17,+ 214,34,53,108,159,107,161,160,163,184,174,67,17,79,116,107,20,166,158,67,+ 106,24,78,30,81,227,51,197,72,105,148,233,191,111,88,210,28,4,218,56,+ 109,111,204,42,124,230,88,70,13,169,54,237,190,220,209,62,246,70,95,183,+ 56,82,95,244,219,253,39,121,116,175,214,188,33,26,132,173,13,138,87,17,+ 232,132,72,70,115,246,151,132,155,225,73,85,23,151,66,229,66,13,155,137,+ 64,243,89,130,234,207,213,220,165,226,205,90,191,227,178,30,51,22,18,30,+ 51,87,128,179,21,110,109,187,71,198,126,108,217,200,67,155,62,92,202,248,+ 221,205,241,159,155,54,119,219,135,225,216,115,58,251,122,31,73,102,207,51,+ 109,4,232,3,51,165,240,99,182,31,78,128,147,252,12,134,25,0,111,56,+ 16,228,7,204,31,4,29,35,123,144,247,15,225,3,201,44,15,52,37,226,+ 172,176,24,176,220,50,224,18,241,30,39,152,151,139,47,63,14,81,228,112,+ 171,65,89,113,172,184,82,83,66,151,97,219,63,154,217,107,88,20,208,42,+ 161,255,139,64,41,225,27,250,170,156,87,198,150,249,86,20,211,196,85,151,+ 180,20,21,236,106,19,78,43,231,95,173,53,23,3,43,160,72,250,95,26,+ 167,231,156,101,201,55,46,153,231,191,110,239,152,146,241,82,161,142,107,18,+ 132,157,175,115,220,226,178,23,41,46,23,117,95,192,119,64,119,220,255,25,+ 147,238,7,190,135,243,59,238,19,238,89,147,238,89,13,239,238,95,13,140,+ 206,205,240,97,144,29,223,114,200,102,243,251,251,140,159,9,247,25,51,103,+ 208,61,253,206,213,159,58,58,54,28,159,13,141,2,16,7,199,192,25,131,+ 255,6,0,100,109,223,120,70,127,125,0,12,141,252,55,239,55,193,131,190,+ 224,124,124,24,14,0,0,3,18,109,111,111,118,0,0,0,108,109,118,104,+ 100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,232,0,0,1,+ 44,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 2,0,0,2,61,116,114,97,107,0,0,0,92,116,107,104,100,0,0,0,+ 3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,+ 44,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,36,101,100,116,115,0,0,0,28,101,108,115,116,0,0,0,+ 0,0,0,0,1,0,0,1,44,0,0,4,0,0,1,0,0,0,0,1,+ 181,109,100,105,97,0,0,0,32,109,100,104,100,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,62,128,0,0,22,192,85,196,0,0,0,0,0,+ 45,104,100,108,114,0,0,0,0,0,0,0,0,115,111,117,110,0,0,0,+ 0,0,0,0,0,0,0,0,0,83,111,117,110,100,72,97,110,100,108,101,+ 114,0,0,0,1,96,109,105,110,102,0,0,0,16,115,109,104,100,0,0,+ 0,0,0,0,0,0,0,0,0,36,100,105,110,102,0,0,0,28,100,114,+ 101,102,0,0,0,0,0,0,0,1,0,0,0,12,117,114,108,32,0,0,+ 0,1,0,0,1,36,115,116,98,108,0,0,0,106,115,116,115,100,0,0,+ 0,0,0,0,0,1,0,0,0,90,109,112,52,97,0,0,0,0,0,0,+ 0,1,0,0,0,0,0,0,0,0,0,2,0,16,0,0,0,0,62,128,+ 0,0,0,0,0,54,101,115,100,115,0,0,0,0,3,128,128,128,37,0,+ 1,0,4,128,128,128,23,64,21,0,0,0,0,0,187,128,0,0,178,168,+ 5,128,128,128,5,20,16,86,229,0,6,128,128,128,1,2,0,0,0,32,+ 115,116,116,115,0,0,0,0,0,0,0,2,0,0,0,5,0,0,4,0,+ 0,0,0,1,0,0,2,192,0,0,0,28,115,116,115,99,0,0,0,0,+ 0,0,0,1,0,0,0,1,0,0,0,6,0,0,0,1,0,0,0,44,+ 115,116,115,122,0,0,0,0,0,0,0,0,0,0,0,6,0,0,1,157,+ 0,0,1,141,0,0,1,62,0,0,1,54,0,0,1,54,0,0,1,77,+ 0,0,0,20,115,116,99,111,0,0,0,0,0,0,0,1,0,0,0,44,+ 0,0,0,26,115,103,112,100,1,0,0,0,114,111,108,108,0,0,0,2,+ 0,0,0,1,255,255,0,0,0,28,115,98,103,112,0,0,0,0,114,111,+ 108,108,0,0,0,1,0,0,0,6,0,0,0,1,0,0,0,97,117,100,+ 116,97,0,0,0,89,109,101,116,97,0,0,0,0,0,0,0,33,104,100,+ 108,114,0,0,0,0,0,0,0,0,109,100,105,114,97,112,112,108,0,0,+ 0,0,0,0,0,0,0,0,0,0,44,105,108,115,116,0,0,0,36,169,+ 116,111,111,0,0,0,28,100,97,116,97,0,0,0,1,0,0,0,0,76,+ 97,118,102,54,49,46,55,46,49,48,51,+};+static const int REF10[1280] = {+ -173,-44,-167,-500,-502,-137,-330,-96,-415,106,-278,483,319,616,+ 674,1033,1254,941,205,-772,-1717,-2470,-4386,-5103,-3905,-240,1074,1962,+ 1829,1383,728,834,2053,2951,3930,3950,3302,1197,-470,-2002,-3326,-6201,+ -6189,-2542,329,3099,2765,977,-1140,-2312,-1870,-1234,341,2623,4362,5286,+ 4489,3177,940,312,-35,-1103,-5266,-6395,-1511,468,2247,415,-1620,-5091,+ -5067,-1556,1970,4470,4502,3609,2974,3030,2686,2134,1347,1098,-1887,-4803,+ -7014,-1828,367,3675,1127,-2263,-6507,-6424,-1214,2880,5397,3466,2686,2053,+ 3182,2969,3095,1595,313,-2627,-6270,-6387,-1214,1383,4289,701,-2745,-7335,+ -6757,-816,3871,6646,3189,823,-98,1974,3972,3627,1465,-1215,-4282,-8250,+ -10143,-2852,2062,6391,3996,459,-6042,-6208,-77,4134,7467,5757,4682,3255,+ 1277,1354,2661,4236,-1629,-6926,-12950,-9871,-1484,3843,6651,3220,-2465,-7935,+ -8868,-1396,1730,4318,490,3955,5043,6930,7938,5024,3236,-1800,-4694,-8108,+ -6380,-651,1919,4401,-190,-2455,-6090,-5138,-530,2506,2750,1309,306,3129,+ 3643,5084,4431,3986,-552,-2181,-5319,-5926,-2792,-1089,4140,1307,-1930,-6911,+ -6989,-705,5150,5454,2121,1806,934,1986,1117,2730,3704,1932,569,-3102,+ -5390,-4865,-1102,1976,1576,-1546,-6284,-6708,-2576,3521,5633,3340,773,19,+ 1203,1434,2703,3440,3687,2838,-848,-2957,-3161,-2119,-849,-1074,-2611,-3529,+ -3129,-2035,-660,-281,-327,360,1249,2066,2786,2445,2316,2607,3073,2645,+ 2067,941,362,-58,-890,-1599,-2581,-2926,-2616,-1663,-1849,-1112,-319,776,+ 1417,1762,2036,1949,1676,1225,938,354,-410,-727,-1195,-135,5,1255,+ 807,1323,-110,-100,-979,458,-406,1090,-261,1849,-257,2217,1360,2113,+ -865,-2133,-3281,-3983,-2380,-1389,1989,1404,469,-310,-657,889,1509,1939,+ 1785,1618,1029,290,-1831,-5471,-8648,-5173,-2436,4717,2390,1333,-684,-1669,+ -864,56,2309,2013,3128,3427,3420,2348,95,-2246,-3392,-4781,-5406,-2236,+ 843,5028,2831,158,-3988,-6420,-4529,-1626,2603,4417,4726,2827,4011,3678,+ 1622,213,-2038,-3700,-7654,-5058,772,6914,5203,1021,-3551,-6395,-4745,-978,+ 1934,4655,4661,4239,4373,4062,2284,-45,-2716,-7703,-8060,-2304,3581,6313,+ 3827,-1349,-3572,-4940,-3542,-438,1805,5626,3510,1886,1994,1806,2831,518,+ -1056,-6959,-7160,114,5574,3440,610,-2637,-3989,-3766,-2205,2250,2478,3957,+ 2929,1537,2941,1203,844,-171,-5364,-6674,-3508,1725,6613,2268,-2496,-3461,+ -2698,-2482,-724,1571,3152,3427,3512,3858,1185,518,-1164,-3015,-6449,-5118,+ 1865,6394,2938,-1809,-4421,-2806,-1730,-1823,404,1073,4716,5124,3662,2062,+ 37,-454,-1953,-6427,-6301,-389,5621,4114,-499,-3674,-2246,-233,-2091,-442,+ 1637,3816,3896,3244,2273,-411,-1028,-2512,-6718,-6648,713,5604,4939,106,+ -3627,-1785,-68,-793,-278,593,4607,4638,3074,1282,-1591,-1123,-3804,-6152,+ -5427,391,6605,2684,-322,-2681,-626,1514,-1045,-681,844,3850,4382,2201,+ -440,-2436,-3107,-5118,-4625,321,4128,3232,611,-1441,-268,1335,1108,-1423,+ -1919,1865,5885,2777,840,-1473,-3475,-4908,-6208,-209,3094,2753,1492,-1158,+ -329,583,678,-978,-2444,1444,5202,3855,2418,-1534,-1342,-3528,-4829,-3999,+ -75,5196,1160,-433,-981,-217,1257,-920,-864,1179,3219,3444,1495,-289,+ -2007,-1615,-3653,-4008,-1629,2712,3110,833,497,-1272,322,-1572,-521,538,+ 1926,4609,902,-1370,-1153,-2492,-1165,-2775,-358,1056,2389,2742,-120,-580,+ -2329,-1320,-880,-272,2449,3685,1768,-796,-1753,-736,134,-1994,-1756,-1162,+ 1573,3128,1177,161,-1344,-1513,-1462,-1656,128,2408,1849,1273,711,707,+ 512,-1482,-1410,-2814,-959,956,343,1510,-547,119,-206,-1874,-419,106,+ 1114,2439,1796,3395,104,-1866,-2462,-4049,-2418,-493,1293,1191,899,827,+ 451,-243,-299,140,231,1820,2001,2035,-127,-2675,-3224,-2570,-991,33,+ 736,1834,1295,1415,54,-1102,-782,-998,511,1548,2536,2046,180,-1308,+ -2365,-2082,-1841,-765,12,643,806,696,698,-79,-597,589,1324,1922,+ 1999,1671,26,-1896,-3100,-3062,-2693,-1503,-43,1086,1533,1810,1538,1073,+ 902,1215,1492,1233,488,-643,-2278,-2891,-2851,-2503,-1421,-209,1068,1798,+ 2617,2302,1347,938,837,640,614,145,-828,-2306,-3016,-2657,-2356,-1182,+ -108,1311,1958,3019,2380,1934,954,669,439,183,-120,-1419,-2297,-3057,+ -2646,-2430,-1226,320,1565,2389,3028,2827,2173,945,597,261,-104,-564,+ -1726,-2688,-3396,-2621,-2259,-882,407,1759,2720,3331,3026,2182,1167,513,+ -1,-276,-906,-2050,-2961,-3201,-2565,-2128,-663,644,1984,2956,3397,2967,+ 1968,1044,370,-153,-779,-1480,-2385,-3066,-2950,-2349,-1429,-23,1064,2226,+ 2785,3352,2586,1779,805,200,-60,-1041,-1557,-2413,-2652,-2424,-1991,-1256,+ -317,818,1975,2640,2614,2088,1333,581,169,-478,-825,-1231,-1462,-1789,+ -1589,-1375,-1065,-165,502,1412,1902,1809,1399,822,449,-63,-357,-778,+ -717,-873,-913,-1157,-966,-809,-250,518,1122,1344,1304,1194,753,-11,+ -9,-314,-502,-1177,-437,-973,-106,-341,-424,-288,402,760,1193,705,+ 941,337,248,-241,-445,-580,-931,-939,-248,-127,144,315,428,127,+ 551,813,444,183,296,-150,-458,-662,-690,-433,-367,-59,-121,227,+ -4,129,267,240,577,499,194,-237,-315,-831,-270,-399,-394,-284,+ 262,-460,417,236,366,334,808,113,207,-174,-11,-451,282,-699,+ -100,-790,145,53,285,54,146,368,903,-866,262,-199,-436,581,+ -195,-536,117,953,-421,73,80,-86,163,-94,48,-268,-178,770,+ 162,-255,-30,-585,-356,79,73,409,-255,-133,-657,99,170,-378,+ 476,-797,944,-3,-285,-621,507,452,-141,78,310,121,-522,-197,+ -157,101,263,-558,295,294,146,-298,233,-175,-961,999,-607,-237,+ -450,-485,-452,-190,580,1214,63,679,-387,1,208,-304,329,-370,+ -478,321,-582,-19,-382,-182,130,367,933,407,-1148,531,-412,-122,+ 747,944,-1379,103,-567,-679,1048,-624,-1045,1299,570,-315,-381,41,+ -164,-183,-249,1648,724,-1650,-870,-642,1363,1335,-528,-914,533,-846,+ -657,867,-332,173,-883,17,291,72,-1282,551,450,31,1930,-1538,+ -1685,-257,-389,2085,1743,-2749,-532,-707,698,2151,-1173,-1600,1516,23,+ -1362,713,1088,-1276,-954,-548,1182,1160,-1368,1603,-405,-418,537,-206,+ 1284,-552,-88,-1232,548,-826,-193,-1010,1158,-378,1587,-2044,-299,2229,+ -2413,288,2854,-1647,-582,-268,-589,1012,-2213,1648,-598,395,-214,-550,+ 1465,-785,220,520,1398,-809,179,-1199,538,-915,1538,1481,-1628,1285,+ -2303,-1248,2290,321,-1090,565,-719,881,-968,190,776,672,17,-493,+ -286,27,-384,300,-332,-255,256,-1067,-33,-631,754,1424,371,252,+ 431,-526,-138,66,-198,-242,-608,402,-326,453,523,141,329,-39,+ -1229,1254,349,483,-992,-352,-426,-690,-198,-408,222,278,178,1020,+ 623,706,279,1231,1286,991,-670,-382,-1891,-3124,-244,-429,-369,80,+ -391,-21,131,-309,-53,-67,-65,569,-319,163,93,150,42,286,+ -8,-529,97,7,-109,199,-33,-17,9,-57,404,250,-160,12,+ 44,57,-230,38,107,296,-63,103,-91,-88,-109,-4,-60,262,+ -47,51,-241,-153,158,247,-39,24,-375,-122,222,-78,241,-87,+ 151,-214,51,-66,-28,83,44,-50,-116,-9,-2,309,-259,-36,+ 249,-55,-47,249,126,-433,+};+static const unsigned char CLIP11[9631] = {+ 0,0,0,28,102,116,121,112,77,52,65,32,0,0,2,0,77,52,65,32,+ 105,115,111,109,105,115,111,50,0,0,0,8,102,114,101,101,0,0,34,193,+ 109,100,97,116,0,0,0,0,0,15,12,1,219,254,190,0,143,255,144,0,+ 161,255,155,255,4,137,255,129,101,143,240,175,248,40,84,227,213,165,235,16,+ 169,243,93,136,5,222,127,172,54,73,144,67,228,143,36,216,14,115,132,216,+ 254,52,204,89,200,135,46,103,233,208,33,87,90,8,218,55,236,79,182,103,+ 96,226,2,162,52,249,109,121,15,93,123,108,102,122,206,174,234,223,195,229,+ 64,82,189,171,42,82,124,44,100,122,64,74,140,115,4,138,144,136,172,72,+ 52,242,228,155,192,188,231,227,73,65,142,186,223,128,54,142,108,158,222,152,+ 18,93,176,191,44,246,58,123,117,60,135,143,17,16,115,195,223,191,10,109,+ 92,131,114,169,208,163,156,186,249,7,140,84,134,182,110,63,37,242,156,168,+ 216,218,34,34,70,168,149,85,97,53,190,50,197,69,92,191,199,124,67,215,+ 163,83,175,69,113,177,71,30,237,105,227,30,197,159,82,19,204,194,131,31,+ 241,240,206,206,67,34,26,162,120,29,65,188,9,96,249,215,124,249,184,87,+ 180,146,129,138,212,229,234,246,242,75,18,178,183,246,86,83,20,201,186,92,+ 62,230,99,73,95,71,5,104,236,5,231,206,133,64,123,130,104,170,240,51,+ 199,127,199,31,28,77,68,172,70,39,107,164,153,200,152,133,166,161,171,14,+ 252,154,58,71,41,76,138,194,2,68,227,72,230,18,176,94,165,13,53,130,+ 72,174,31,79,204,68,133,35,69,69,189,208,26,89,187,75,120,36,70,90,+ 87,172,149,217,69,146,78,56,35,6,143,114,75,97,15,22,157,101,181,189,+ 130,165,50,103,228,110,61,45,118,4,239,237,73,96,174,119,255,165,143,41,+ 251,222,3,159,242,159,41,97,253,13,89,24,130,41,228,47,168,6,92,191,+ 13,30,45,52,11,236,131,44,184,120,16,49,134,139,176,241,29,192,31,136,+ 47,54,37,164,154,134,145,9,199,38,218,181,54,49,8,118,208,90,5,93,+ 205,98,63,62,90,67,90,207,240,54,65,164,110,222,68,12,155,46,181,180,+ 64,150,99,78,117,4,25,134,69,55,158,2,195,225,34,29,87,76,159,171,+ 60,166,197,79,37,154,40,221,124,62,55,103,131,4,6,2,223,77,122,0,+ 142,45,224,219,220,100,226,143,118,59,159,200,30,221,235,74,54,224,207,111,+ 255,54,210,178,138,133,168,158,235,110,230,192,150,193,198,50,173,14,35,46,+ 115,73,93,0,251,94,9,249,1,113,40,100,107,72,55,201,192,73,1,125,+ 193,80,120,142,156,239,31,220,122,86,183,32,250,7,167,212,251,183,203,6,+ 153,184,222,195,60,79,201,147,242,180,30,39,150,222,152,180,143,4,108,98,+ 88,102,245,226,183,179,148,125,115,169,66,100,144,183,195,102,46,169,106,119,+ 166,71,70,88,214,150,202,200,204,160,197,83,191,36,122,161,212,139,166,243,+ 122,222,185,42,230,197,132,224,74,91,26,22,189,79,47,12,160,200,47,164,+ 182,144,154,179,131,204,212,243,203,110,140,208,181,23,197,241,179,179,27,39,+ 85,143,32,160,204,34,240,51,107,20,27,53,240,165,168,20,73,97,32,0,+ 4,147,217,6,134,20,192,38,118,36,122,207,205,83,86,187,66,54,184,170,+ 215,139,245,130,133,187,174,161,168,27,23,166,218,122,131,25,192,48,253,46,+ 253,15,47,102,65,181,208,158,220,20,193,231,194,40,204,240,17,225,155,148,+ 78,232,15,80,46,189,25,104,44,190,50,123,48,60,190,77,99,99,113,137,+ 43,81,62,182,111,209,146,111,101,119,130,185,28,29,253,20,78,50,38,166,+ 108,67,74,200,108,196,70,165,117,214,124,9,18,66,150,20,242,94,50,126,+ 6,16,137,171,60,37,50,248,76,151,221,132,232,75,169,173,131,201,24,74,+ 12,123,150,170,47,79,198,194,12,148,148,213,178,22,100,53,15,71,253,0,+ 227,130,134,101,10,47,221,30,123,17,82,242,66,18,144,32,162,208,214,130,+ 0,22,135,100,204,243,217,39,238,189,139,206,140,38,228,7,44,190,103,104,+ 139,24,191,57,207,101,219,185,172,97,81,183,155,159,200,202,45,57,182,141,+ 38,23,224,75,131,161,196,230,203,206,132,88,207,251,142,105,48,205,217,193,+ 153,158,92,120,45,89,80,14,18,6,222,195,185,37,90,15,81,108,3,174,+ 17,44,217,129,28,6,4,208,132,20,68,129,44,57,166,146,136,126,35,19,+ 37,201,99,230,47,50,112,128,41,12,157,228,108,78,234,209,5,85,218,85,+ 24,250,84,66,145,34,26,169,205,79,40,28,70,145,37,73,62,115,130,234,+ 169,193,81,201,79,23,81,225,58,17,129,63,127,126,149,24,212,125,82,252,+ 96,202,103,25,216,159,89,16,32,46,222,178,72,145,15,91,69,76,153,148,+ 132,72,151,52,44,111,215,89,151,173,230,135,54,24,42,161,8,227,97,60,+ 174,101,6,194,75,38,30,144,206,200,27,137,163,176,203,20,2,28,154,107,+ 43,58,4,63,146,201,121,215,125,107,134,168,220,89,25,151,193,194,236,27,+ 242,30,120,236,36,50,226,241,112,190,74,85,82,77,35,4,177,211,33,5,+ 252,227,26,154,111,27,52,143,72,102,65,200,192,133,131,97,109,35,83,0,+ 76,248,190,247,195,210,123,10,188,13,194,240,232,214,34,91,96,90,194,153,+ 221,189,202,190,250,179,7,76,161,242,199,192,203,65,176,84,123,161,176,123,+ 44,203,230,46,17,150,146,12,174,17,26,105,11,113,95,17,255,22,16,203,+ 79,130,151,168,118,235,16,103,238,13,174,63,206,89,242,144,193,24,222,171,+ 225,76,80,100,120,180,229,64,25,159,125,200,160,139,145,153,7,43,142,131,+ 197,40,39,244,87,28,185,75,60,144,4,0,56,191,143,28,57,151,135,194,+ 90,222,200,109,107,115,31,211,188,221,242,92,28,240,33,225,45,155,240,81,+ 80,24,203,53,14,3,163,112,87,125,144,115,139,55,88,48,51,116,15,48,+ 15,137,109,40,174,116,83,84,172,243,12,165,242,106,170,104,90,202,88,159,+ 23,233,246,27,95,251,81,74,120,12,127,135,198,179,108,249,74,169,128,236,+ 21,171,221,23,154,130,89,119,64,230,71,159,80,223,15,192,133,7,24,175,+ 100,99,52,163,52,60,131,95,82,104,210,79,198,15,55,49,178,86,73,213,+ 47,95,36,231,6,241,9,177,142,220,245,182,60,16,52,235,106,177,178,54,+ 7,202,141,147,205,32,12,76,80,220,248,226,22,48,9,136,86,39,44,244,+ 160,110,2,193,17,16,16,10,162,225,112,167,181,8,187,144,87,132,111,58,+ 86,164,42,33,20,9,215,171,216,118,221,73,38,89,233,34,128,9,69,199,+ 36,209,27,182,57,13,236,173,99,135,189,203,152,178,121,20,182,43,154,243,+ 124,140,130,96,193,213,238,112,15,191,115,126,23,70,249,72,57,124,93,120,+ 115,213,249,250,141,226,38,40,75,126,217,128,213,213,43,234,50,230,66,234,+ 166,168,170,210,23,78,122,215,224,153,88,16,86,62,90,42,185,201,44,251,+ 116,105,209,86,57,1,167,58,138,107,200,100,9,118,205,88,150,187,77,130,+ 228,53,104,44,189,194,97,82,104,125,25,30,201,48,116,61,33,210,36,231,+ 0,184,128,28,105,72,4,254,35,150,244,75,49,220,159,3,53,243,141,251,+ 253,125,143,180,128,179,131,97,105,170,210,131,24,209,86,168,21,118,209,159,+ 250,108,232,82,232,92,173,173,255,225,68,8,119,224,39,238,238,27,99,129,+ 137,100,157,229,141,37,139,6,151,213,143,197,227,228,96,251,76,94,57,165,+ 120,26,130,120,144,133,142,145,45,74,93,65,102,11,204,33,62,240,82,10,+ 93,53,23,118,139,38,97,36,86,90,185,84,227,170,219,237,151,134,178,88,+ 62,140,69,168,64,174,42,141,72,222,198,0,56,241,59,37,228,152,16,196,+ 181,37,163,27,210,195,104,113,76,199,17,200,158,120,70,129,96,123,5,198,+ 91,11,169,192,206,131,194,11,247,104,217,218,211,51,176,83,59,67,227,237,+ 231,174,208,70,39,248,211,84,184,178,105,96,61,198,210,160,110,128,68,87,+ 120,205,90,117,1,51,144,143,103,61,221,68,146,71,148,145,25,145,154,39,+ 106,123,59,76,68,151,133,193,190,90,91,24,245,199,77,75,251,92,152,253,+ 188,207,121,207,88,112,219,241,153,154,250,95,48,97,121,21,70,46,53,208,+ 181,48,187,160,176,239,25,99,165,32,154,152,218,209,115,6,11,77,7,198,+ 113,30,198,136,82,174,229,35,70,216,133,94,81,230,117,42,32,54,129,131,+ 12,52,82,42,185,3,164,28,112,161,68,175,175,153,65,141,55,26,11,137,+ 206,86,210,66,245,179,61,121,27,114,99,48,44,94,244,78,28,104,180,3,+ 204,27,175,155,15,135,100,61,57,171,11,252,150,143,12,249,79,165,168,253,+ 132,239,128,110,131,54,38,90,118,181,191,200,24,3,180,66,147,224,146,136,+ 250,69,30,69,9,78,219,114,95,90,19,2,85,235,209,78,227,46,198,22,+ 252,80,175,93,250,198,225,139,97,229,2,41,90,4,20,68,203,140,146,65,+ 32,53,179,73,20,109,66,173,253,97,118,168,2,174,229,160,188,198,24,140,+ 26,46,103,184,216,56,43,5,236,206,21,188,41,161,250,61,156,241,180,222,+ 229,20,193,228,15,148,90,33,241,19,211,229,0,100,95,243,179,158,92,223,+ 156,216,100,113,229,162,16,41,92,126,160,174,0,231,170,13,63,120,102,159,+ 82,41,46,161,175,89,166,98,94,25,110,23,215,96,159,233,243,16,115,220,+ 121,198,28,159,14,129,56,47,229,118,185,105,71,26,227,51,138,185,53,172,+ 161,27,106,181,31,194,209,122,37,105,172,8,34,215,51,46,208,50,207,12,+ 104,11,208,175,88,157,134,97,42,17,33,52,111,104,147,191,110,9,10,125,+ 40,239,96,216,104,93,124,134,85,196,46,83,102,140,253,186,241,100,193,57,+ 120,191,166,158,166,246,45,76,205,42,51,26,117,177,232,134,24,53,132,29,+ 128,49,76,59,240,77,67,18,194,137,17,56,191,210,14,77,221,141,121,141,+ 112,245,212,233,72,130,68,55,228,91,153,176,247,71,179,200,241,82,150,90,+ 139,212,201,63,188,127,177,177,244,86,96,129,83,196,242,169,46,216,60,162,+ 183,156,250,158,244,120,197,190,14,50,28,133,133,254,143,92,45,207,204,95,+ 103,50,34,2,248,210,244,250,244,98,0,162,126,93,121,154,191,68,139,224,+ 47,76,199,53,139,70,7,150,17,178,185,8,53,43,178,51,81,135,30,16,+ 88,92,38,78,86,232,163,44,17,212,122,56,74,207,202,181,57,15,250,29,+ 50,117,164,157,8,234,83,88,212,14,227,29,68,21,87,100,197,244,83,78,+ 39,46,4,175,187,95,166,30,61,99,10,245,170,120,232,181,89,199,227,174,+ 148,104,196,152,11,236,14,109,163,107,192,252,155,174,51,229,105,241,127,193,+ 60,24,217,227,253,170,129,219,190,89,164,86,255,217,22,17,199,148,161,247,+ 33,248,47,141,115,92,229,229,125,50,28,44,178,3,22,67,23,167,140,199,+ 74,23,30,19,24,43,227,100,109,66,193,53,176,200,138,58,124,1,174,136,+ 181,17,86,33,33,187,186,108,138,73,11,154,17,201,49,38,172,21,178,115,+ 217,198,13,144,150,235,249,126,185,225,48,101,222,103,236,215,132,214,82,88,+ 226,108,255,83,62,42,48,97,150,190,83,28,46,32,186,13,198,228,49,205,+ 166,199,112,239,153,249,171,203,62,226,251,144,206,60,2,177,32,94,108,104,+ 228,76,117,211,119,175,236,46,67,40,41,69,25,211,70,218,86,109,138,140,+ 36,119,113,73,84,66,20,245,151,202,21,128,241,13,54,28,202,199,187,171,+ 37,173,201,16,10,174,87,87,244,111,59,24,98,132,187,36,153,51,129,146,+ 157,104,194,69,69,130,210,60,146,9,253,208,244,15,29,112,134,86,33,135,+ 98,152,46,80,156,217,51,92,59,74,169,117,2,48,254,157,147,208,95,237,+ 10,218,165,186,139,188,109,242,148,233,236,117,236,240,189,41,28,90,119,215,+ 70,188,87,186,163,42,183,152,235,204,208,187,120,20,132,97,58,230,29,225,+ 118,138,163,71,55,103,79,232,0,182,253,146,172,29,177,166,93,81,216,125,+ 204,198,59,148,174,120,154,241,135,165,54,225,154,108,95,225,252,49,145,149,+ 31,5,218,94,226,189,83,94,155,150,113,202,51,130,188,89,78,50,100,1,+ 80,60,132,37,77,56,71,226,110,155,25,39,168,55,51,234,63,136,103,47,+ 157,143,82,255,123,196,202,228,18,167,34,147,87,131,253,21,251,46,193,163,+ 55,35,181,2,224,116,113,88,146,140,50,128,177,74,190,156,42,190,77,180,+ 59,120,156,145,99,149,194,200,152,24,179,52,175,38,128,237,119,103,136,178,+ 227,139,138,173,37,200,77,197,46,216,23,92,28,25,90,193,209,48,70,219,+ 153,80,9,41,193,193,180,105,19,99,201,99,127,109,213,21,166,131,159,46,+ 113,126,13,39,132,70,44,79,112,121,117,36,60,226,41,143,209,255,202,178,+ 208,241,139,196,128,144,113,213,178,215,39,88,227,231,161,147,148,154,168,96,+ 20,6,104,119,45,28,41,179,118,106,219,242,14,60,198,221,60,130,44,201,+ 155,187,54,220,220,60,27,96,83,42,88,13,2,127,133,33,185,184,224,83,+ 222,197,151,54,9,95,111,68,99,37,52,244,43,216,21,107,205,188,71,146,+ 214,73,66,43,8,160,52,168,199,125,247,92,207,65,166,178,178,205,3,244,+ 133,214,193,196,210,126,9,207,151,150,191,142,13,212,196,60,150,202,166,120,+ 234,16,169,232,67,232,14,92,230,46,32,249,208,136,165,216,90,151,184,207,+ 116,182,249,197,52,103,67,91,123,233,195,97,35,178,33,35,35,167,53,160,+ 236,243,109,77,69,176,120,237,16,6,109,13,177,154,20,212,148,97,119,57,+ 101,13,140,144,163,64,248,45,180,53,29,217,69,234,126,175,105,68,177,53,+ 91,35,231,36,76,192,249,48,140,17,83,195,5,135,69,77,184,119,161,171,+ 94,85,33,90,101,228,139,191,11,140,74,3,131,169,61,203,15,143,45,53,+ 158,117,18,103,151,77,22,208,245,115,76,52,176,191,212,1,57,158,54,209,+ 15,154,184,149,248,177,152,146,76,157,200,158,188,42,217,162,147,32,227,120,+ 146,189,168,233,168,210,82,174,109,42,62,161,95,97,65,130,73,108,133,71,+ 3,57,60,240,57,96,163,70,100,98,202,89,5,214,178,32,196,25,188,200,+ 54,159,133,23,132,12,149,234,119,77,211,233,252,20,114,104,19,173,47,192,+ 68,105,165,141,75,135,122,194,113,146,51,126,148,241,166,145,72,77,86,157,+ 65,160,48,103,186,157,148,208,184,205,84,218,239,231,135,37,33,220,204,28,+ 104,178,119,35,232,100,207,243,25,117,88,164,10,67,123,127,32,133,236,156,+ 75,47,170,193,5,74,87,38,204,96,253,199,62,232,177,33,149,30,211,112,+ 185,62,108,97,178,118,13,87,33,210,17,135,130,197,169,91,210,37,152,112,+ 248,104,217,78,162,66,231,41,129,80,180,122,123,173,220,194,248,10,46,148,+ 253,200,77,23,55,191,40,81,145,93,82,96,238,49,247,85,141,50,168,227,+ 211,51,139,23,59,163,223,27,135,74,54,226,0,24,161,85,101,177,2,46,+ 208,20,232,180,195,121,208,126,150,193,126,43,197,141,157,210,141,196,200,14,+ 13,145,86,31,126,142,199,106,172,98,92,128,56,64,239,144,211,149,42,51,+ 59,126,65,161,168,164,120,247,142,192,161,192,35,253,194,159,101,134,150,74,+ 30,124,179,69,69,39,210,94,250,4,228,222,89,24,47,52,89,212,80,112,+ 211,147,57,96,231,47,207,62,35,139,167,20,212,31,223,198,124,166,242,194,+ 171,191,164,193,105,133,200,123,94,81,164,223,223,33,103,137,171,151,0,4,+ 241,231,47,177,154,179,240,73,55,37,69,10,65,88,68,18,161,7,109,230,+ 47,60,37,0,86,198,114,133,37,95,237,155,109,200,124,238,72,231,44,82,+ 254,161,34,50,201,222,66,135,249,247,100,16,172,78,170,228,61,183,7,192,+ 6,240,229,108,252,208,138,20,10,23,60,31,140,249,128,205,64,91,183,9,+ 119,175,3,100,69,97,105,206,72,208,75,64,48,216,203,51,53,10,179,12,+ 24,229,212,169,91,35,179,82,9,214,14,39,233,141,84,193,193,127,178,115,+ 187,61,229,184,90,192,246,6,155,180,121,141,170,185,60,150,120,125,249,125,+ 85,220,39,133,178,23,24,46,255,165,115,203,19,211,183,224,68,42,137,196,+ 54,29,58,28,156,94,217,35,179,87,206,38,126,246,230,209,117,45,195,247,+ 139,96,26,223,216,247,38,217,124,172,108,24,87,222,158,202,126,107,109,37,+ 194,163,23,133,79,78,54,179,166,221,9,148,125,118,68,228,217,0,99,249,+ 62,118,253,73,162,43,84,244,18,69,96,78,23,95,28,187,211,193,106,237,+ 180,240,82,233,243,6,36,8,28,244,250,44,3,155,49,1,116,186,105,254,+ 125,106,214,2,37,9,145,221,226,251,93,205,159,176,98,213,48,216,239,205,+ 242,233,223,246,46,79,236,179,213,228,124,169,33,111,14,3,149,172,65,147,+ 53,138,49,113,14,208,8,177,163,32,101,229,192,254,150,95,9,134,42,188,+ 52,132,154,34,135,118,197,86,122,102,12,241,107,29,115,178,252,184,58,13,+ 167,83,141,208,176,138,16,164,56,87,19,18,59,81,9,72,237,120,230,148,+ 69,141,131,144,62,62,137,180,161,61,209,109,191,149,3,33,48,84,197,27,+ 55,158,164,61,36,130,211,180,171,153,240,140,255,76,192,217,160,215,63,215,+ 105,128,141,81,21,66,159,209,116,166,14,200,37,85,43,50,18,28,198,19,+ 108,23,92,114,169,74,57,21,34,236,119,36,67,205,165,19,112,73,1,195,+ 171,93,88,113,231,232,75,185,43,99,229,228,193,210,240,36,112,246,64,49,+ 98,29,43,76,34,241,152,112,56,190,180,160,204,229,186,231,30,209,73,37,+ 223,89,38,164,212,254,250,26,141,48,35,56,125,202,221,74,235,116,227,65,+ 101,97,156,216,16,206,19,209,141,67,236,132,254,223,10,82,92,214,112,55,+ 227,140,140,214,138,52,177,200,214,2,42,182,252,83,30,238,216,160,70,45,+ 136,39,2,196,99,141,89,132,20,215,129,75,101,142,72,37,214,96,96,41,+ 50,63,215,235,31,57,77,199,8,161,153,59,42,120,68,184,224,240,64,221,+ 43,5,28,148,117,98,66,39,32,145,76,9,4,141,63,196,108,10,100,114,+ 148,110,208,178,73,122,105,129,35,156,54,64,133,29,241,242,23,70,170,118,+ 153,98,69,143,77,61,165,98,251,71,78,98,227,243,175,4,226,122,105,225,+ 233,13,216,120,16,183,213,192,255,15,18,71,194,173,91,13,37,177,53,218,+ 41,91,94,135,80,171,232,234,124,118,132,125,166,143,58,29,10,17,250,82,+ 224,155,18,174,69,244,51,158,63,197,221,19,112,74,29,210,119,2,236,223,+ 66,176,162,104,100,225,147,185,50,82,214,98,13,32,236,193,23,199,23,114,+ 165,16,101,203,29,190,244,111,182,222,52,217,135,40,88,237,56,226,106,83,+ 197,251,75,170,215,203,251,114,20,3,73,30,54,79,2,44,165,219,109,57,+ 38,235,231,61,166,193,160,194,170,110,109,21,203,33,162,244,244,93,60,151,+ 49,204,151,205,88,86,48,163,88,40,89,54,96,166,1,81,40,32,138,113,+ 85,254,125,37,42,14,110,172,156,132,129,133,117,13,48,74,107,85,183,132,+ 205,209,125,92,104,202,210,200,178,210,97,109,60,159,193,115,186,101,198,172,+ 64,208,18,89,18,87,80,197,163,130,12,6,88,208,66,168,236,14,112,215,+ 71,33,206,173,97,156,201,183,147,120,64,251,100,43,51,23,194,153,143,101,+ 255,80,230,255,104,90,192,211,57,161,173,3,204,1,241,167,69,232,181,183,+ 157,97,124,246,71,95,53,108,11,105,54,13,243,144,40,111,23,91,63,113,+ 97,14,207,33,112,118,37,36,81,44,32,186,179,19,176,106,249,218,61,121,+ 133,87,159,71,18,181,172,130,67,64,245,84,133,156,147,22,166,200,133,254,+ 228,227,157,125,39,60,103,234,165,106,230,115,35,136,226,196,73,112,22,189,+ 4,154,174,0,99,24,131,75,171,80,116,203,182,80,59,115,182,45,41,39,+ 217,104,105,43,148,218,220,50,142,225,152,167,201,246,26,52,35,68,236,146,+ 139,214,72,104,212,185,217,32,35,144,18,122,36,206,69,195,176,212,21,140,+ 11,219,188,19,186,9,9,79,23,222,156,168,83,217,237,234,65,31,163,217,+ 66,18,121,166,250,206,37,15,106,18,68,227,213,119,4,73,28,92,217,38,+ 198,121,163,54,47,17,231,33,171,69,184,228,194,45,161,159,105,130,176,112,+ 240,97,188,20,109,180,127,129,14,24,244,178,220,212,15,41,174,197,15,129,+ 106,69,176,67,35,204,82,219,123,141,173,72,81,74,224,88,115,189,114,211,+ 12,32,54,196,97,225,174,213,235,51,63,140,103,191,208,195,87,161,28,6,+ 30,253,215,174,32,71,241,164,36,208,29,71,9,139,195,172,224,194,55,61,+ 173,134,64,248,250,168,54,139,88,103,148,153,56,81,154,52,60,10,16,217,+ 36,100,138,210,145,50,119,42,70,241,16,152,227,119,151,58,235,58,67,13,+ 117,228,72,60,249,64,249,243,213,218,176,51,153,193,116,121,122,141,157,25,+ 200,249,100,37,238,90,138,141,122,154,131,200,103,131,7,108,77,78,227,54,+ 217,236,252,55,43,173,234,56,230,187,9,39,94,19,71,87,123,130,160,40,+ 146,5,195,237,140,222,245,157,106,105,235,171,30,48,185,61,146,13,53,240,+ 143,35,235,139,125,211,171,100,106,61,186,96,183,23,252,233,45,57,142,27,+ 38,146,153,49,192,153,171,221,133,110,107,157,228,85,202,152,240,51,68,113,+ 226,81,47,175,102,220,123,132,201,39,42,49,56,197,35,106,12,140,239,183,+ 28,237,195,210,62,64,46,155,15,197,43,120,243,59,234,143,23,197,114,194,+ 222,51,198,182,175,196,74,11,107,57,213,223,156,199,208,192,148,73,166,22,+ 181,202,170,231,188,28,62,17,77,41,85,210,245,86,113,39,203,99,102,56,+ 38,10,173,135,144,4,210,230,34,211,66,166,76,150,214,236,149,227,175,19,+ 97,213,244,54,140,95,226,253,59,223,160,69,150,114,49,171,191,80,214,86,+ 117,145,26,252,14,61,238,221,10,213,202,104,41,142,228,225,188,227,20,222,+ 72,106,107,49,82,213,111,31,84,165,156,183,64,13,25,26,215,27,46,205,+ 48,139,117,200,147,81,157,9,98,148,230,125,130,217,72,64,27,62,145,96,+ 45,144,139,192,206,240,207,125,213,144,181,222,203,150,215,190,7,166,117,57,+ 88,116,128,4,219,145,127,18,114,47,190,125,17,241,82,204,160,159,64,212,+ 118,138,253,251,226,119,12,40,254,101,216,253,93,216,197,5,208,130,73,4,+ 132,112,137,215,26,192,96,20,62,79,25,39,147,103,218,65,233,88,63,99,+ 12,168,153,118,226,107,157,224,120,158,151,191,177,158,8,228,56,248,63,227,+ 175,97,201,212,86,188,237,223,87,120,204,143,77,43,82,160,205,52,228,170,+ 12,97,117,253,213,251,10,38,150,150,42,109,155,173,102,15,212,180,164,230,+ 90,56,137,244,127,242,23,123,133,236,255,51,121,133,240,103,55,57,132,183,+ 169,235,156,10,11,136,195,172,118,162,10,220,248,62,226,7,2,176,6,76,+ 202,138,26,106,180,43,111,149,226,205,195,136,248,85,210,60,139,140,180,75,+ 93,126,104,246,77,171,24,157,143,39,24,153,196,62,47,171,153,122,69,77,+ 21,72,155,35,146,124,96,225,138,208,164,134,56,42,6,237,151,128,27,13,+ 145,101,185,46,105,182,17,160,156,140,96,54,232,94,150,86,116,5,197,135,+ 25,49,14,21,104,103,136,168,231,92,178,223,120,87,135,125,85,34,8,99,+ 171,9,87,68,201,152,181,242,109,83,41,183,95,129,218,231,192,140,45,141,+ 18,99,5,56,93,23,143,89,222,196,152,20,20,5,37,33,56,46,176,62,+ 160,254,51,57,169,190,76,33,117,181,131,69,229,167,15,30,136,231,165,233,+ 7,151,96,64,147,21,211,140,174,130,58,109,157,196,38,207,205,41,167,34,+ 95,110,2,248,109,34,92,70,3,11,137,186,104,173,189,217,248,172,242,111,+ 138,230,84,60,109,122,55,101,29,15,227,226,53,18,200,40,227,121,105,11,+ 199,103,225,112,73,72,156,154,199,201,111,27,28,161,175,229,91,56,97,73,+ 156,234,212,101,20,1,173,19,195,6,4,40,154,243,118,81,161,185,117,193,+ 94,203,26,172,112,67,27,183,0,134,144,102,21,14,69,60,238,116,145,196,+ 63,143,221,230,57,53,106,17,129,14,233,94,34,126,107,86,225,69,26,114,+ 144,69,19,32,141,131,153,220,133,76,142,114,149,204,66,173,142,180,206,237,+ 25,18,100,244,121,137,56,54,16,153,81,186,77,230,226,155,185,63,115,78,+ 228,23,167,94,170,89,151,220,93,46,100,61,104,220,192,242,220,127,114,27,+ 111,70,49,6,196,184,28,49,136,91,234,210,77,46,43,137,212,221,122,107,+ 108,123,152,133,164,38,107,72,107,60,219,25,181,18,173,12,17,53,102,134,+ 116,252,230,119,45,170,120,181,59,99,215,134,236,19,45,181,118,101,89,123,+ 227,29,133,92,255,228,229,28,240,110,50,177,80,23,209,106,77,55,196,9,+ 138,182,2,188,199,254,184,120,191,227,123,149,53,149,26,96,82,0,228,132,+ 74,169,129,84,16,15,29,79,47,62,204,240,138,167,229,122,233,96,190,12,+ 171,17,250,38,64,220,78,203,50,102,86,129,79,145,203,33,105,224,194,100,+ 82,10,202,9,189,255,127,189,65,217,202,192,121,5,221,252,87,79,85,232,+ 30,60,196,197,135,109,214,226,142,201,24,187,173,13,198,61,47,212,145,199,+ 196,255,152,175,115,61,136,252,102,252,94,189,163,126,74,188,1,25,71,29,+ 34,20,78,0,0,16,0,0,18,0,0,0,15,10,1,95,255,216,0,9,+ 255,132,0,49,255,7,183,255,133,69,96,173,23,252,139,215,156,24,253,218,+ 14,63,227,137,95,242,178,255,220,199,253,240,60,56,251,213,170,86,49,176,+ 19,144,254,208,194,126,82,1,117,157,186,86,84,206,12,236,99,59,159,22,+ 125,58,223,171,109,193,199,95,42,148,197,250,96,1,114,24,12,219,184,48,+ 109,23,64,157,101,152,129,50,26,203,130,15,52,198,145,96,30,137,7,73,+ 60,104,104,175,16,39,138,3,219,184,121,125,127,21,175,67,119,131,191,130,+ 143,33,45,81,234,237,47,44,14,114,94,75,55,72,148,154,237,111,145,174,+ 248,75,177,226,70,9,83,57,31,195,8,44,80,18,177,211,105,35,73,79,+ 79,214,207,129,53,137,215,5,30,231,248,49,98,91,100,181,184,215,159,223,+ 97,6,216,20,0,236,46,180,1,75,153,94,36,93,137,178,144,168,218,173,+ 239,169,189,222,120,24,223,197,173,79,69,148,255,47,77,223,31,196,167,8,+ 129,209,113,251,134,219,49,71,22,115,50,30,9,236,91,51,224,24,203,235,+ 198,223,184,254,136,44,20,34,108,55,249,72,233,242,68,233,109,189,187,217,+ 217,46,75,10,93,131,143,137,7,198,147,73,5,89,13,74,3,225,152,98,+ 161,162,18,90,25,41,34,21,154,186,75,149,222,150,189,248,64,13,93,123,+ 187,112,105,229,245,123,206,70,251,176,214,18,207,90,136,106,230,156,110,32,+ 10,46,99,232,23,194,180,32,100,43,125,202,181,168,10,226,16,152,103,145,+ 192,120,24,145,220,126,110,102,91,130,151,3,94,226,23,164,235,253,219,33,+ 37,144,187,210,124,121,209,102,19,113,45,118,1,160,243,214,35,185,216,29,+ 11,35,7,43,187,245,15,249,138,117,173,172,181,99,176,206,149,252,94,167,+ 29,183,86,201,224,178,225,18,83,211,175,134,222,244,242,245,30,25,111,104,+ 205,92,69,128,77,129,89,163,176,166,114,165,234,237,21,79,186,110,129,211,+ 191,75,199,98,113,114,13,60,56,18,176,19,50,190,74,230,93,128,30,148,+ 124,60,70,118,178,215,67,158,128,136,97,23,187,231,32,174,97,112,70,86,+ 69,121,133,142,79,57,107,59,233,34,228,209,134,12,220,197,14,43,0,92,+ 28,78,166,99,145,99,212,63,128,155,18,196,74,232,51,116,25,200,58,39,+ 225,32,179,43,131,82,127,12,155,133,211,162,172,42,113,153,175,14,63,17,+ 119,128,238,111,241,192,127,209,203,227,66,144,176,114,210,225,73,99,1,150,+ 38,48,114,179,94,53,10,199,15,23,164,178,218,143,75,163,111,49,234,187,+ 67,93,54,45,168,26,247,111,182,117,64,97,216,14,205,105,65,3,128,164,+ 189,150,27,24,226,154,173,240,40,217,163,69,61,153,96,138,195,84,33,75,+ 189,54,98,27,61,226,94,100,102,98,52,27,134,191,182,43,91,186,193,113,+ 189,204,244,45,55,174,144,189,203,173,47,40,19,79,95,201,133,168,216,99,+ 196,176,38,153,91,125,87,150,24,234,160,81,254,6,89,66,83,49,17,164,+ 74,70,108,154,58,177,253,158,229,66,233,145,108,145,159,113,184,14,98,232,+ 165,49,118,7,66,77,3,153,151,97,121,35,250,135,196,215,24,202,209,37,+ 182,68,51,77,155,205,57,129,40,96,150,193,132,229,36,223,148,119,166,195,+ 110,92,19,24,66,160,107,114,98,129,104,132,90,114,146,176,142,45,192,130,+ 237,228,151,107,139,160,88,212,234,227,119,217,191,211,81,235,231,198,179,56,+ 66,146,193,74,104,18,180,105,185,153,101,231,23,122,81,245,24,180,101,46,+ 67,240,211,3,188,252,2,134,95,115,59,130,8,240,129,94,43,245,106,57,+ 70,181,83,227,63,130,81,92,31,119,108,229,56,240,197,14,25,239,42,204,+ 71,251,136,64,231,32,178,54,230,222,151,126,104,119,95,148,247,193,94,181,+ 217,89,248,140,170,36,209,228,163,69,99,217,167,62,2,195,157,240,107,117,+ 158,94,77,116,203,19,154,87,104,48,127,6,174,172,121,105,178,145,217,121,+ 63,31,19,144,148,229,236,118,10,176,24,21,109,250,163,233,64,127,111,160,+ 189,137,93,73,126,68,210,50,213,43,209,56,249,200,171,158,200,64,38,42,+ 198,189,179,75,92,176,75,128,149,16,16,113,95,168,205,119,83,40,157,139,+ 245,147,39,86,131,51,121,113,98,32,172,181,40,135,102,165,8,125,22,139,+ 177,166,103,11,244,246,129,249,193,132,18,155,146,90,90,232,140,118,222,16,+ 235,69,97,150,146,2,87,176,252,3,250,119,49,107,74,80,180,160,167,54,+ 18,90,80,37,41,191,91,39,33,25,217,67,33,224,221,83,188,15,41,33,+ 88,160,94,131,145,88,243,223,133,151,140,216,195,120,20,98,37,57,239,247,+ 241,83,1,181,5,25,6,28,9,179,68,41,82,6,36,226,142,233,50,105,+ 25,57,213,194,138,33,201,79,30,81,36,215,173,102,149,70,111,106,137,252,+ 125,116,163,177,173,61,68,153,199,5,144,251,140,238,4,116,177,193,78,125,+ 178,45,98,183,207,164,171,83,43,209,215,171,127,202,143,9,237,146,242,157,+ 156,113,241,139,99,233,236,110,185,231,154,222,27,53,109,181,127,177,35,87,+ 144,130,30,80,104,153,248,169,75,242,9,207,102,142,167,150,23,168,77,62,+ 224,68,10,164,82,246,137,143,68,56,204,123,30,30,13,250,185,114,141,203,+ 126,220,153,128,82,195,190,109,219,121,167,157,94,17,239,238,196,192,238,131,+ 190,66,253,36,95,102,7,157,207,57,105,113,58,240,195,245,247,220,215,77,+ 225,199,212,176,123,242,27,85,78,174,181,239,152,251,56,100,249,211,63,12,+ 38,210,35,184,193,151,21,42,152,68,107,152,205,13,38,65,235,255,90,71,+ 124,139,164,207,145,109,196,18,153,190,82,32,187,72,2,4,38,136,28,141,+ 222,133,97,126,120,151,37,211,214,190,113,49,33,100,121,54,196,193,171,162,+ 45,56,168,201,123,228,83,1,122,226,182,83,221,132,135,89,226,195,211,19,+ 18,16,79,206,39,97,174,199,31,165,255,209,235,2,189,93,136,152,154,149,+ 151,9,109,134,69,135,192,42,149,143,12,156,65,19,107,182,184,29,36,141,+ 182,164,76,35,242,142,180,160,70,65,92,208,119,67,145,181,44,148,185,41,+ 39,162,70,249,43,30,209,68,139,128,13,136,169,173,122,138,232,184,142,243,+ 166,129,57,41,225,135,43,11,104,97,172,107,1,203,54,33,173,87,51,75,+ 31,142,133,14,79,181,97,221,156,193,239,179,248,171,181,56,21,36,15,172,+ 254,116,111,135,233,87,249,236,236,95,144,152,117,5,246,128,200,220,23,87,+ 39,40,105,186,18,139,123,156,124,241,226,131,55,8,107,98,122,60,48,116,+ 57,22,247,42,82,168,235,161,220,51,30,241,62,118,233,132,40,27,229,17,+ 122,77,155,176,216,78,54,43,176,249,92,104,148,60,216,33,135,249,139,204,+ 86,35,20,241,214,158,203,102,95,184,42,248,233,230,230,62,246,129,135,169,+ 200,152,159,148,172,71,48,70,214,226,228,252,7,244,62,74,132,91,85,218,+ 91,204,3,237,69,250,201,226,51,99,200,221,201,135,19,44,207,196,79,32,+ 232,90,78,239,228,199,144,250,141,149,244,64,61,16,144,124,202,72,88,20,+ 84,23,216,105,234,223,196,251,151,145,62,98,221,21,22,44,134,227,42,39,+ 241,116,137,2,116,93,39,125,160,231,164,161,218,151,232,105,12,168,11,84,+ 236,230,159,7,27,194,51,89,167,188,83,100,98,160,254,141,242,82,28,194,+ 183,136,45,203,100,207,90,155,101,239,53,213,47,39,160,188,142,18,199,147,+ 174,205,123,67,231,163,208,3,239,193,144,85,70,132,55,42,252,37,4,26,+ 199,179,9,211,227,57,204,234,184,193,225,76,216,92,43,205,72,152,3,85,+ 211,89,78,94,90,19,193,192,211,103,9,26,29,31,33,110,47,210,248,76,+ 167,192,7,38,110,125,168,5,196,52,82,222,198,21,133,224,194,137,60,87,+ 189,233,111,150,201,134,160,225,89,188,45,128,84,13,78,219,110,94,133,249,+ 189,189,187,59,203,38,139,57,239,72,75,10,34,216,127,131,159,101,62,241,+ 254,219,94,150,246,188,78,89,162,4,246,85,197,165,75,118,184,131,116,165,+ 237,219,228,205,225,125,122,6,66,205,34,206,20,160,117,82,188,112,36,204,+ 147,70,23,186,209,212,233,76,164,112,201,117,187,139,56,169,40,124,148,155,+ 98,221,108,72,166,209,185,24,202,63,24,184,52,35,216,240,138,242,31,7,+ 131,12,248,68,38,30,97,62,187,245,29,231,251,129,208,35,59,19,127,171,+ 232,17,66,53,179,228,203,234,111,49,225,192,191,165,188,161,253,62,139,12,+ 91,35,44,254,246,216,128,168,174,85,234,189,66,24,171,194,173,58,75,170,+ 13,183,253,172,13,189,99,218,62,146,132,187,114,137,166,152,223,143,236,15,+ 136,30,23,24,249,133,58,221,200,10,93,112,113,152,236,83,216,36,68,37,+ 165,122,39,131,176,142,22,145,67,210,252,199,31,56,118,85,38,21,12,70,+ 138,29,129,34,170,138,161,63,35,156,216,208,158,31,44,254,237,166,90,47,+ 236,150,133,218,246,152,191,4,93,106,224,99,199,139,126,57,202,36,37,74,+ 185,111,119,146,185,148,143,193,124,20,89,18,213,120,249,80,46,50,80,98,+ 199,186,200,104,52,251,66,177,44,200,167,68,226,138,19,132,96,213,194,141,+ 235,185,231,246,24,34,103,55,98,207,13,11,67,194,212,132,26,232,98,3,+ 153,182,9,252,198,199,12,129,36,76,169,35,94,89,132,189,160,150,107,140,+ 63,248,110,150,245,228,120,197,212,193,216,126,37,12,46,45,75,215,67,245,+ 79,168,216,162,201,199,77,121,30,55,138,15,134,78,154,167,162,7,33,251,+ 216,255,239,85,233,204,104,64,227,136,238,166,248,167,7,149,81,58,246,137,+ 91,190,89,152,134,67,65,225,250,102,6,183,91,36,35,22,148,166,130,112,+ 166,236,111,169,48,130,8,104,45,108,43,81,66,123,26,242,132,70,177,56,+ 12,215,170,38,56,140,73,195,204,1,105,135,5,169,180,245,76,114,251,87,+ 185,86,98,66,92,204,219,42,67,113,165,112,130,197,192,37,106,221,212,149,+ 241,22,188,96,150,2,165,184,216,205,230,154,14,177,40,181,153,45,67,161,+ 92,68,103,160,12,21,194,184,108,107,67,201,228,57,37,0,154,143,157,224,+ 173,28,196,203,129,46,72,136,109,211,14,29,136,25,152,17,42,211,42,40,+ 81,38,213,130,217,202,8,50,89,19,113,206,222,150,207,224,69,150,17,15,+ 220,162,174,28,223,157,149,174,12,224,135,181,2,170,157,195,217,124,220,109,+ 194,249,41,200,83,123,244,203,52,127,26,1,184,45,10,42,240,123,161,66,+ 19,38,187,177,160,165,27,95,188,127,219,92,117,68,72,19,76,88,96,150,+ 157,151,180,211,224,225,230,227,2,176,73,163,214,173,95,30,51,132,234,22,+ 147,8,65,38,165,133,75,55,183,152,174,33,134,35,14,74,66,166,86,163,+ 242,163,200,105,125,195,65,133,7,63,193,9,81,32,101,130,200,71,228,173,+ 216,16,108,44,165,125,103,177,243,56,87,1,56,137,203,219,221,63,12,232,+ 254,60,190,67,12,121,253,181,36,145,136,49,168,91,88,139,168,82,83,53,+ 213,225,158,96,118,142,115,158,106,249,191,91,43,131,132,248,24,97,164,129,+ 166,21,112,47,48,66,166,206,205,84,32,109,191,212,203,200,221,180,144,114,+ 82,108,17,44,37,173,121,198,171,95,38,236,57,2,28,62,105,203,153,69,+ 43,146,11,184,178,47,39,175,230,160,97,158,21,61,130,227,236,208,27,72,+ 226,77,197,83,250,127,29,193,140,14,49,22,46,250,67,27,85,121,129,135,+ 152,42,27,45,203,226,168,56,170,168,136,40,174,107,180,193,215,45,108,161,+ 196,254,82,21,216,93,182,216,178,146,133,141,196,70,62,128,61,10,117,226,+ 179,156,38,29,17,249,213,37,215,146,86,19,133,161,137,231,124,154,173,99,+ 95,133,65,65,9,111,119,185,139,26,198,206,235,91,202,113,58,1,186,117,+ 113,16,209,81,14,232,51,124,53,81,182,200,16,62,141,151,121,83,94,130,+ 31,9,160,131,148,168,16,26,42,185,108,110,48,181,84,146,243,167,193,227,+ 177,199,225,7,199,233,46,81,22,131,96,109,186,16,113,109,149,151,196,176,+ 16,23,152,184,64,117,79,43,150,67,166,195,151,31,15,203,92,146,226,230,+ 27,106,223,127,234,205,131,120,140,247,106,192,27,83,91,215,223,215,63,50,+ 251,220,192,244,164,107,187,226,105,24,66,165,89,194,112,35,164,106,210,53,+ 69,27,205,226,42,131,12,140,226,171,179,96,237,10,161,42,116,135,206,46,+ 138,48,176,236,148,141,159,37,170,119,47,147,35,228,35,32,205,255,55,132,+ 189,71,225,75,158,32,210,237,221,104,84,229,54,137,120,103,156,62,33,93,+ 224,31,144,159,154,193,35,198,31,155,14,110,149,241,151,61,163,30,42,70,+ 158,75,112,53,72,73,166,94,79,10,188,17,113,18,60,245,16,53,132,235,+ 135,139,10,162,181,241,4,197,141,5,50,36,50,146,51,48,90,20,128,219,+ 125,89,221,100,54,82,154,51,141,140,168,55,78,253,159,25,252,254,183,22,+ 30,197,75,197,100,253,252,190,223,102,43,180,147,72,33,203,64,234,240,211,+ 5,187,16,150,241,107,242,228,222,10,139,205,194,137,16,7,213,102,88,163,+ 231,116,136,189,196,199,34,192,141,144,229,204,162,240,249,244,114,123,66,247,+ 105,64,174,22,17,210,5,143,77,57,68,156,244,53,23,37,152,177,35,126,+ 125,17,23,129,4,78,42,145,85,53,168,231,190,192,205,169,186,248,177,159,+ 122,201,175,104,104,114,205,150,138,111,70,190,131,64,34,52,117,26,123,77,+ 12,77,199,102,205,129,66,182,102,28,215,195,128,0,0,2,186,109,111,111,+ 118,0,0,0,108,109,118,104,100,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,3,232,0,0,1,144,0,1,0,0,1,0,0,0,0,0,0,+ 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,2,0,0,1,229,116,114,97,107,0,0,0,+ 92,116,107,104,100,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,+ 1,0,0,0,0,0,0,1,144,0,0,0,0,0,0,0,0,0,0,0,+ 1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,36,101,100,116,115,0,0,0,+ 28,101,108,115,116,0,0,0,0,0,0,0,1,0,0,1,144,0,0,0,+ 0,0,1,0,0,0,0,1,93,109,100,105,97,0,0,0,32,109,100,104,+ 100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,62,128,0,0,25,+ 0,85,196,0,0,0,0,0,45,104,100,108,114,0,0,0,0,0,0,0,+ 0,115,111,117,110,0,0,0,0,0,0,0,0,0,0,0,0,83,111,117,+ 110,100,72,97,110,100,108,101,114,0,0,0,1,8,109,105,110,102,0,0,+ 0,16,115,109,104,100,0,0,0,0,0,0,0,0,0,0,0,36,100,105,+ 110,102,0,0,0,28,100,114,101,102,0,0,0,0,0,0,0,1,0,0,+ 0,12,117,114,108,32,0,0,0,1,0,0,0,204,115,116,98,108,0,0,+ 0,88,115,116,115,100,0,0,0,0,0,0,0,1,0,0,0,72,97,108,+ 97,99,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,+ 0,16,0,0,0,0,62,128,0,0,0,0,0,36,97,108,97,99,0,0,+ 0,0,0,0,16,0,0,16,40,10,14,1,0,0,0,0,32,4,0,3,+ 232,0,0,0,62,128,0,0,0,32,115,116,116,115,0,0,0,0,0,0,+ 0,2,0,0,0,1,0,0,16,0,0,0,0,1,0,0,9,0,0,0,+ 0,28,115,116,115,99,0,0,0,0,0,0,0,1,0,0,0,1,0,0,+ 0,2,0,0,0,1,0,0,0,28,115,116,115,122,0,0,0,0,0,0,+ 0,0,0,0,0,2,0,0,22,147,0,0,12,38,0,0,0,20,115,116,+ 99,111,0,0,0,0,0,0,0,1,0,0,0,44,0,0,0,97,117,100,+ 116,97,0,0,0,89,109,101,116,97,0,0,0,0,0,0,0,33,104,100,+ 108,114,0,0,0,0,0,0,0,0,109,100,105,114,97,112,112,108,0,0,+ 0,0,0,0,0,0,0,0,0,0,44,105,108,115,116,0,0,0,36,169,+ 116,111,111,0,0,0,28,100,97,116,97,0,0,0,1,0,0,0,0,76,+ 97,118,102,54,49,46,55,46,49,48,51,+};+static const int REF11[1600] = {+ -581,-84,-135,-657,-889,-401,-613,-477,-18,104,-134,455,678,765,+ 1487,2063,2180,2279,864,-1842,-2891,-4626,-7951,-10013,-7481,-1532,2154,3803,+ 3641,2718,1211,2367,4797,6564,7830,8192,6368,2163,-796,-4200,-7003,-12404,+ -13087,-5937,-311,5011,5848,2073,-605,-2102,846,2993,5380,6494,7225,7991,+ 7149,5158,864,-996,-2724,-6585,-15991,-17668,-6193,326,5068,4138,1660,-1870,+ -2568,752,3487,5715,5672,4997,6497,7411,4903,2075,-359,-1009,-8267,-14916,+ -18381,-6057,1157,8515,3709,302,-4344,-3289,1334,4137,6560,4937,4560,5084,+ 7519,5543,4418,152,-2376,-8588,-16807,-16881,-5034,2039,9107,1763,-1030,-5172,+ -3089,2247,6583,6910,3350,3133,4134,8285,8866,4465,-1383,-4582,-7660,-13068,+ -17708,-5903,681,9093,1924,-2514,-6119,-2210,3395,5915,6361,3187,2487,5101,+ 9522,9434,4885,-1044,-4456,-7650,-13907,-16097,-7662,-989,7490,2609,-1282,-4458,+ -4074,2270,6060,6800,3146,2184,5349,10343,11171,5089,-198,-5134,-6445,-12048,+ -15817,-9867,-1659,7574,1829,95,-4056,-2648,164,6535,6401,4355,2117,5935,+ 9464,9354,5091,355,-3738,-6123,-12819,-15881,-11267,-2703,6728,4098,866,-3762,+ -2091,554,6559,5075,4669,2656,5398,7701,7304,3775,1520,-1229,-4029,-11521,+ -16051,-13723,-5023,3338,4203,3899,143,-1335,-2165,1155,2800,5216,4831,5708,+ 5310,4032,2261,334,-331,-375,-2283,-5100,-7162,-5678,-5071,-3334,-2094,-1368,+ -423,321,1276,1827,2089,2174,2619,2890,2889,2749,2229,2570,2622,2518,+ 1425,242,-863,-1475,-1642,-1777,-1994,-2073,-1920,-1258,-338,944,1594,2184,+ 2183,2195,1647,1306,1258,971,493,-26,-801,-1225,-1257,-1174,-1360,-1092,+ -588,-140,-978,775,157,1067,659,2655,1711,6889,1416,3496,345,730,+ -4266,-5951,-6885,-6922,-4221,-3316,696,663,2144,878,1258,2163,3676,4599,+ 4369,3920,1140,-1480,-5703,-9475,-15751,-12076,-5989,3270,5087,2300,322,-3630,+ 125,3060,7472,7844,7106,6883,5323,5696,3186,-666,-5245,-12041,-18502,-11099,+ -3455,8016,5778,2326,-2056,-4606,529,3837,8264,6748,6284,3506,7095,7080,+ 3230,908,-5923,-10801,-20054,-14617,-3325,10354,6938,450,-4031,-4218,1918,6711,+ 9134,6919,4240,4316,6820,7891,3453,185,-8006,-16095,-18470,-7402,2290,8620,+ 3843,-4911,-3938,-1924,5281,8940,5864,4364,990,5044,9324,7391,2187,-6334,+ -8739,-19118,-16667,-787,8555,3979,-2167,-6241,-2674,4671,7020,8382,1950,1400,+ 5213,8134,10957,2561,-3322,-6686,-15545,-18314,-8640,915,9622,65,-6261,-2764,+ 1426,7635,8458,2954,944,2386,7949,11099,4487,-766,-6969,-11905,-16020,-12296,+ 1944,9183,532,-5392,-5978,464,7138,7365,5453,251,2223,8009,10625,6714,+ -167,-5187,-9889,-16426,-13766,-856,7605,1843,-4056,-6180,465,6424,6692,6035,+ 10,1536,6409,10357,8176,-910,-5771,-8751,-16537,-14047,-23,5610,2232,-3952,+ -6145,1584,6637,7866,6073,587,3348,7421,9432,6553,-2686,-6163,-11216,-15477,+ -11534,-841,8730,-722,-4537,-4324,2328,9695,6449,4864,2070,4716,8853,6453,+ 1488,-5027,-8222,-15017,-15121,-1633,5414,1323,-3747,-5112,2522,6791,8260,4906,+ 1966,5865,7773,7245,3419,-2613,-7697,-14760,-15860,-4023,3225,1541,-2238,-4967,+ 306,4752,5966,7239,3167,4886,8518,7263,5647,-1585,-4829,-11412,-14969,-10524,+ -2714,5772,-2111,-3548,-3042,1841,8295,6030,5999,4699,5738,7378,5054,1597,+ -4561,-7612,-11665,-11658,-5266,2200,1133,-2815,-1030,-1170,5153,4539,5220,5916,+ 5832,7706,4482,564,-1124,-5744,-8454,-11535,-5831,-985,1027,778,-2904,-569,+ 272,2739,5673,5771,6026,6029,2985,1858,-1411,-305,-2950,-7931,-7852,-6464,+ -1282,998,-809,924,-460,979,2327,3721,3403,4280,3387,2934,3683,1592,+ 1186,-4171,-6554,-10024,-6272,-2199,-1109,2055,-487,796,1971,1860,4473,3512,+ 4253,5630,3521,4587,-1295,-4485,-6877,-10543,-6049,-3638,-413,540,-420,1312,+ 1330,3564,4001,5246,4957,5213,4099,1582,-886,-5377,-7411,-6704,-4688,-2692,+ -1659,-94,607,1213,2164,2814,3494,3773,4515,4435,4112,2393,189,-3447,+ -5975,-6215,-5792,-4750,-3209,-1054,-91,1826,2633,3353,3553,4655,5139,4770,+ 4172,2132,-1438,-5037,-7180,-7388,-6662,-4494,-2174,32,1201,3020,3531,4000,+ 4781,5433,5335,4636,2587,-989,-4961,-6943,-7403,-6710,-4450,-2426,-86,1085,+ 3153,3733,4167,4908,5343,5280,4412,2288,-1091,-5083,-6991,-7163,-6768,-4460,+ -2423,-135,1262,3424,3607,4659,4712,5465,5350,4274,1802,-1767,-5098,-6959,+ -7182,-6747,-4645,-2277,11,1788,3811,4256,5047,5235,5496,4985,3766,1140,+ -2347,-5635,-7280,-7346,-6506,-4514,-2082,322,2264,4040,4764,5309,5364,5413,+ 4795,3505,648,-3037,-5724,-7434,-7193,-6680,-4147,-1730,626,2927,4410,4853,+ 5137,5254,5099,4293,2549,-375,-3763,-6128,-7206,-7036,-5949,-3417,-1132,1233,+ 3265,4868,4980,5307,5416,5047,3959,1868,-911,-4033,-6047,-6985,-7122,-5937,+ -3782,-1430,948,2890,4245,5015,5388,5411,4979,3798,2025,-434,-3021,-4883,+ -5973,-6378,-5644,-4205,-2137,-82,1647,2830,3880,4652,4994,4873,4174,2681,+ 858,-1125,-3015,-4440,-5237,-5209,-4382,-3033,-1285,250,1657,2877,3678,4019,+ 4053,3506,2723,1420,202,-1041,-2167,-3053,-3463,-3335,-2945,-2174,-1101,46,+ 1100,1990,2203,2481,2560,2256,1753,1049,170,-380,-948,-1509,-1957,-2021,+ -1650,-1243,-733,-563,-279,295,710,1068,1182,1310,1089,892,631,2,+ -608,-655,-743,-611,-547,-692,-1018,-1078,-732,-572,-266,172,217,859,+ 993,244,1211,1378,955,454,224,86,-147,-647,-423,-1134,-1031,-608,+ -450,-160,-294,58,314,91,675,1004,851,242,144,209,80,-267,+ -558,-892,-135,100,-177,-489,-723,-422,-313,16,554,133,273,555,+ 569,-250,-231,-123,-85,213,767,-759,-129,-934,-445,-653,-309,-330,+ 188,655,808,-21,104,-386,1103,858,-228,400,491,384,-701,-488,+ 11,582,1216,-813,-338,1104,32,-701,1093,-1436,-649,1233,-292,6,+ -681,-104,-991,-981,-145,674,-267,-761,-503,29,363,-444,2049,241,+ -373,-86,-1319,-72,-362,-641,959,-532,953,-573,353,90,101,970,+ 2438,3048,-988,-502,-993,-3390,406,-1223,-1094,1093,1340,-905,-1216,-1835,+ 1041,-958,260,4564,1280,-1415,-520,-1608,1297,1506,-1225,-2082,2136,-591,+ 436,-400,1016,37,-2615,1174,-581,753,-1724,2289,-1168,902,3465,-1010,+ -2429,-987,-188,2427,668,-2757,261,-1987,1431,793,-2618,-3830,2531,-1326,+ -572,1135,1763,-1793,187,-1756,1317,310,-3228,3843,-1401,580,-992,548,+ 2772,532,1576,-2254,422,-306,1161,-1031,1020,-2060,1696,-2452,-2007,2681,+ -3457,-1003,2649,-1996,700,1089,-44,-594,-1935,1300,-571,-56,2098,-174,+ 1546,-1168,-349,1185,2306,-935,-1588,98,-377,-571,2930,2550,-240,1329,+ -1524,3,1412,335,-1793,-163,-129,1861,1765,689,843,937,-961,-267,+ -738,554,536,252,-2418,-611,566,-1713,539,-65,959,99,37,-419,+ 67,184,-76,-443,739,-228,-679,197,-625,548,947,687,237,715,+ -792,846,94,244,-1319,-505,-1368,-114,-1018,-532,-25,-63,283,2357,+ 1799,2229,1083,2656,2434,1584,-921,-1698,-3951,-6854,-9819,-8100,-1930,4237,+ 5234,4241,3038,5025,5260,7307,7099,5983,1854,-222,-3045,-7968,-16538,-14919,+ -2770,5061,6393,1042,-490,-1635,3729,6809,9103,7613,7705,4708,-664,-3290,+ -4931,-8475,-18042,-12982,-1297,4820,5660,-1220,-2211,-247,4571,6380,7452,7368,+ 7247,3624,1708,-3091,-3453,-9528,-19188,-13912,-17,6994,7200,-2701,-2530,-1483,+ 6896,7343,8105,3911,5433,3402,4677,-867,-2173,-9235,-17292,-17627,-138,8161,+ 8685,-5045,-4273,-3497,6655,9363,8406,3866,4821,4076,5455,2236,-1799,-5882,+ -16904,-19447,-7703,6707,9210,818,-5027,-4009,2301,7473,8689,6897,4203,4805,+ 4294,4141,-203,-4079,-11040,-19354,-13756,1541,8106,7769,-3920,-4152,-2517,4503,+ 8673,8699,4049,3648,4307,5256,4486,-1007,-4226,-14445,-19234,-11699,4549,7556,+ 3772,-5552,-4142,-604,6787,9571,7487,4769,3197,5085,5608,4246,-2488,-4966,+ -13227,-18264,-12980,2004,7196,4276,-2766,-3749,-967,4075,8601,7323,5461,3219,+ 4145,4820,3237,-864,-4780,-8996,-15447,-14879,-4269,2344,4852,1714,-78,555,+ 1178,4843,4925,4905,4416,4057,4829,2609,-987,-3608,-3545,-5165,-6451,-5824,+ -4282,-4149,-2769,-822,966,1710,2209,2449,2800,3146,3701,3810,3908,3303,+ 1792,504,-202,-178,-849,-1892,-2970,-3026,-2689,-1952,-1373,-1009,-269,432,+ 1419,2122,2120,1645,1514,1023,370,-223,-457,-224,-207,-519,-846,-938,+ -488,-186,7,94,128,252,190,137,-192,-225,-332,-664,-939,-767,+ -398,54,289,380,235,-7,123,140,124,223,492,539,211,-71,+ -201,-579,-964,-914,-446,-292,-378,-38,526,638,337,84,160,222,+ 187,109,131,109,4,-229,-508,-630,-469,-179,-87,-102,-26,341,+ 742,678,309,-84,-176,5,-34,54,-106,-252,-445,-450,-421,-261,+ -58,143,211,223,401,548,460,190,2,-138,-38,-37,-25,-149,+ -242,-331,-241,-124,-10,19,65,21,45,129,215,233,117,-26,+ -74,4,-4,-119,-277,-213,-63,-41,-52,45,197,194,116,111,+ 202,225,151,-48,-167,-201,-249,-332,-389,-399,-275,-140,6,67,+ 140,221,229,258,312,428,328,123,63,27,-51,-234,-329,-382,+ -345,-216,-87,-24,12,78,188,261,387,385,283,168,101,32,+ -68,-134,-227,-208,-226,-248,-308,-233,-103,-12,44,93,155,206,+ 187,150,126,73,+};+static const unsigned char CLIP12[2123] = {+ 255,241,96,128,52,159,252,222,2,0,76,97,118,99,54,49,46,49,57,46,+ 49,48,49,0,66,83,159,244,144,77,136,19,22,168,34,170,2,137,209,160,+ 149,239,117,87,91,186,206,185,117,188,152,235,121,245,190,120,153,151,215,136,+ 47,157,216,53,194,35,69,180,128,236,97,68,241,7,233,139,176,75,49,6,+ 235,153,240,214,192,184,116,9,126,151,232,115,103,33,143,221,12,99,37,87,+ 118,42,232,191,185,235,116,249,105,34,249,14,184,220,51,195,142,230,210,9,+ 96,24,161,203,199,134,162,225,167,87,241,232,94,141,13,69,108,1,91,157,+ 0,111,175,119,13,3,125,123,0,229,80,168,168,71,46,122,4,24,169,196,+ 63,80,188,193,135,16,97,212,238,236,8,142,197,80,248,138,79,251,195,248,+ 186,192,127,175,126,143,167,122,142,238,0,167,176,0,1,68,67,20,187,207,+ 30,14,183,18,24,161,248,54,96,179,7,237,15,192,123,187,159,252,0,6,+ 128,143,95,232,247,112,11,79,143,44,0,0,167,121,139,60,29,132,72,128,+ 145,24,66,36,14,143,131,161,87,189,213,93,76,185,87,146,247,199,50,163,+ 91,188,154,197,212,7,140,230,129,246,12,108,127,60,29,145,49,70,7,69,+ 41,149,1,46,11,250,115,19,165,254,112,102,130,148,51,231,22,107,31,237,+ 210,113,56,204,238,236,10,200,153,14,207,249,198,136,187,187,117,25,180,188,+ 143,252,254,29,186,147,45,103,129,228,203,145,145,210,80,5,41,96,222,34,+ 233,43,3,230,108,76,4,159,58,20,160,126,228,148,158,84,116,207,242,185,+ 210,50,54,157,197,162,212,129,7,184,251,112,234,170,174,235,131,0,234,184,+ 15,8,134,219,155,57,101,32,185,157,179,206,190,252,140,76,160,12,160,0,+ 182,90,194,215,103,103,155,135,248,248,231,222,3,182,126,192,122,224,10,224,+ 255,241,96,128,50,159,252,33,79,108,132,128,235,215,8,37,4,69,168,251,+ 174,200,254,44,169,31,172,171,47,17,199,250,67,237,241,223,195,135,126,223,+ 237,255,246,217,254,37,215,143,62,62,249,93,126,255,255,118,250,215,227,158,+ 119,196,233,212,246,255,251,57,230,125,211,197,207,137,171,118,239,59,16,3,+ 110,208,1,248,86,179,141,247,245,178,11,92,157,175,73,144,183,135,99,39,+ 122,198,87,121,3,220,169,163,50,49,100,200,81,211,176,86,40,145,185,52,+ 139,130,115,154,160,55,146,2,48,144,64,8,4,192,22,249,72,192,192,81,+ 36,199,176,114,217,43,183,8,135,39,239,145,13,8,197,26,190,95,230,151,+ 231,247,101,133,49,151,125,234,41,138,94,122,133,21,77,253,7,174,157,33,+ 27,11,179,75,13,159,86,46,221,249,191,80,210,40,15,83,39,35,143,86,+ 57,184,171,125,135,179,217,157,28,105,11,97,124,132,96,235,162,140,157,218,+ 80,113,61,100,188,215,100,124,228,38,200,251,202,201,121,237,253,43,175,191,+ 183,57,156,111,233,229,227,245,252,127,253,183,95,26,87,143,57,230,174,255,+ 127,255,181,62,39,198,111,126,197,243,63,219,255,233,95,83,245,204,241,229,+ 213,41,219,160,250,8,6,221,199,255,136,26,69,76,196,21,163,217,40,123,+ 9,7,165,62,84,145,29,131,97,132,181,118,114,187,8,227,37,99,199,140,+ 115,172,96,73,36,118,86,130,98,53,88,242,131,32,64,153,58,134,16,184,+ 126,129,59,208,200,132,4,107,65,36,88,4,155,200,96,224,124,76,196,83,+ 228,112,82,231,244,133,79,55,199,60,252,194,49,127,15,215,112,212,198,183,+ 144,52,16,241,83,8,148,136,36,57,15,59,90,33,51,77,199,130,140,137,+ 104,11,31,238,255,241,96,128,40,191,252,33,122,143,153,221,254,255,1,39,+ 65,103,10,144,194,93,16,143,66,231,244,249,212,239,167,141,121,222,106,95,+ 62,106,51,89,93,249,130,107,121,8,67,161,183,12,245,37,142,56,204,4,+ 248,7,58,22,246,99,54,106,78,159,55,207,17,4,128,122,221,235,242,95,+ 160,104,20,52,70,106,102,238,213,235,210,130,0,64,66,120,166,253,17,43,+ 131,96,243,120,163,88,84,151,58,146,241,70,8,246,8,182,252,229,191,252,+ 215,15,84,135,141,82,36,206,11,20,46,7,135,149,239,38,223,14,206,168,+ 26,161,6,26,183,54,174,164,188,30,105,61,90,160,68,91,96,128,194,22,+ 168,253,201,252,125,4,68,151,7,12,242,236,106,217,50,245,13,4,28,96,+ 168,36,9,56,34,21,196,175,66,9,124,58,62,14,137,207,233,243,237,127,+ 110,184,247,158,119,172,151,106,140,214,82,65,53,189,193,84,156,6,142,19,+ 57,236,159,241,102,142,79,248,212,255,132,116,168,224,42,224,174,247,201,31,+ 113,56,241,189,79,183,192,204,192,167,215,244,206,126,220,84,134,49,95,14,+ 124,112,16,165,234,98,64,47,20,157,72,70,115,24,20,0,78,244,4,64,+ 33,24,155,153,135,150,101,226,127,100,210,188,23,221,122,151,186,20,119,7,+ 125,59,117,243,150,131,113,121,194,178,86,90,239,224,85,193,139,225,40,183,+ 222,98,99,24,148,194,5,5,184,255,241,96,128,39,191,252,33,26,139,251,+ 255,255,254,5,37,8,84,138,24,66,125,8,135,68,34,48,232,83,159,251,+ 126,47,59,234,172,181,77,85,214,183,89,21,172,226,92,129,251,235,118,129,+ 148,238,167,168,130,80,19,32,152,243,28,123,45,251,170,171,97,24,206,82,+ 146,165,57,173,176,69,188,208,23,223,245,253,61,157,177,69,87,118,123,254,+ 120,23,139,86,69,82,251,247,2,219,135,164,30,69,49,114,60,93,117,146,+ 193,55,187,203,132,0,132,218,153,177,153,1,235,242,121,48,119,16,63,140,+ 113,167,206,32,45,74,163,68,105,252,114,31,195,156,210,241,88,91,0,184,+ 175,241,240,173,66,243,5,191,128,231,141,47,55,240,192,41,133,252,63,129,+ 111,55,155,248,127,250,94,138,84,0,71,209,88,196,51,166,135,195,162,241,+ 232,89,207,253,191,23,157,252,115,101,170,106,174,181,186,200,173,103,18,228,+ 19,122,168,144,71,10,206,135,12,105,98,196,1,67,117,61,251,58,180,248,+ 225,4,17,149,114,84,172,79,250,242,203,112,128,0,54,62,21,246,158,101,+ 109,148,227,55,187,133,186,41,231,187,66,65,28,68,165,210,141,211,69,12,+ 221,160,237,118,112,207,255,210,255,117,234,163,255,139,255,233,118,108,111,112,+ 157,23,11,55,74,6,110,105,163,19,53,146,227,183,5,50,137,41,83,71,+ 154,47,35,117,186,56,255,241,96,128,39,191,252,33,26,140,155,24,64,206,+ 1,43,3,40,139,180,176,53,120,253,156,113,91,250,239,46,106,185,145,199,+ 119,202,221,86,218,136,78,55,155,80,189,16,224,184,172,110,220,100,76,212,+ 140,158,33,101,104,196,196,21,40,10,242,92,144,9,246,141,140,196,95,23,+ 129,227,251,18,151,214,59,160,52,250,36,103,35,3,111,214,142,208,167,61,+ 82,81,72,112,41,94,150,58,187,9,57,187,124,148,21,219,11,140,198,126,+ 208,142,218,106,130,205,60,27,255,62,136,123,72,0,206,28,24,152,123,184,+ 237,129,105,98,57,58,179,168,208,35,87,31,200,238,49,17,219,192,12,49,+ 200,254,250,172,71,139,64,155,137,151,183,109,150,1,177,78,254,69,98,119,+ 194,35,65,137,179,190,72,147,17,19,212,75,232,48,49,17,45,41,190,28,+ 131,38,38,17,75,33,17,111,215,74,193,10,2,237,47,140,127,199,92,85,+ 95,25,156,76,169,36,201,76,151,84,149,16,109,222,243,148,4,5,170,72,+ 150,38,139,100,30,36,89,36,220,194,17,214,34,53,108,159,107,161,160,163,+ 184,174,67,17,79,116,107,20,166,158,67,106,24,78,30,81,227,51,197,72,+ 105,148,233,191,111,88,210,28,4,218,56,109,111,204,42,124,230,88,70,13,+ 169,54,237,190,220,209,62,246,70,95,183,56,82,95,244,219,253,39,121,116,+ 175,214,188,255,241,96,128,42,159,252,33,26,132,173,13,138,87,17,232,132,+ 72,70,115,246,151,132,155,225,73,85,23,151,66,229,66,13,155,137,64,243,+ 89,130,234,207,213,220,165,226,205,90,191,227,178,30,51,22,18,30,51,87,+ 128,179,21,110,109,187,71,198,126,108,217,200,67,155,62,92,202,248,221,205,+ 241,159,155,54,119,219,135,225,216,115,58,251,122,31,73,102,207,51,109,4,+ 232,3,51,165,240,99,182,31,78,128,147,252,12,134,25,0,111,56,16,228,+ 7,204,31,4,29,35,123,144,247,15,225,3,201,44,15,52,37,226,172,176,+ 24,176,220,50,224,18,241,30,39,152,151,139,47,63,14,81,228,112,171,65,+ 89,113,172,184,82,83,66,151,97,219,63,154,217,107,88,20,208,42,161,255,+ 139,64,41,225,27,250,170,156,87,198,150,249,86,20,211,196,85,151,180,20,+ 21,236,106,19,78,43,231,95,173,53,23,3,43,160,72,250,95,26,167,231,+ 156,101,201,55,46,153,231,191,110,239,152,146,241,82,161,142,107,18,132,157,+ 175,115,220,226,178,23,41,46,23,117,95,192,119,64,119,220,255,25,147,238,+ 7,190,135,243,59,238,19,238,89,147,238,89,13,239,238,95,13,140,206,205,+ 240,97,144,29,223,114,200,102,243,251,251,140,159,9,247,25,51,103,208,61,+ 253,206,213,159,58,58,54,28,159,13,141,2,16,7,199,192,25,131,255,6,+ 0,100,109,223,120,70,127,125,0,12,141,252,55,239,55,193,131,190,224,124,+ 124,24,14,+};+static const int REF12[1536] = {+ 0,0,0,1,2,-8,-6,0,7,-8,5,1,13,-5,+ 15,-7,-12,5,13,23,31,-4,-4,-2,-25,-38,-29,-37,+ 64,-1,47,3,-5,30,-47,-46,-14,5,59,2,59,-41,+ -27,-27,29,15,-30,-6,-53,109,40,26,2,-2,33,-9,+ -50,29,72,-24,-81,-26,-26,-50,-52,-53,-62,-50,32,55,+ 132,31,86,-9,22,-43,-90,-76,-13,91,69,18,-48,-86,+ 27,-12,63,-8,-104,-84,-141,-7,52,97,110,3,98,-49,+ 96,254,195,130,-58,-115,-214,80,8,59,-187,-164,3,13,+ 74,196,186,51,-247,-199,13,90,202,16,-112,-340,-106,125,+ 214,-78,-66,-46,-313,-164,71,207,-23,-164,-363,-3,74,268,+ 223,-35,-132,-196,-156,-86,7,129,77,-22,-10,-26,180,262,+ 22,-143,-163,-255,36,37,-43,-53,-86,-185,51,112,151,69,+ 182,9,-82,-44,70,155,152,-248,-87,-125,-24,24,-32,57,+ -118,-132,-106,-101,-240,-148,-83,290,215,180,202,103,-79,-23,+ 83,120,-294,-100,253,44,-150,-70,-102,-227,-123,-292,269,-21,+ 124,-136,38,86,10,-31,39,-74,-121,108,93,133,31,-258,+ -30,-41,-106,-174,-58,440,317,168,134,65,8,-342,-231,-280,+ 50,-37,120,77,121,111,-134,-30,72,66,-88,-214,130,108,+ 48,-71,128,-52,-173,-44,-167,-500,-502,-137,-330,-96,-415,106,+ -278,483,319,616,674,1033,1254,941,205,-772,-1717,-2470,-4386,-5103,+ -3905,-240,1074,1962,1829,1383,728,834,2053,2951,3930,3950,3302,1197,+ -470,-2002,-3326,-6201,-6189,-2542,329,3099,2765,977,-1140,-2312,-1870,-1234,+ 341,2623,4362,5286,4489,3177,940,312,-35,-1103,-5266,-6395,-1511,468,+ 2247,415,-1620,-5091,-5067,-1556,1970,4470,4502,3609,2974,3030,2686,2134,+ 1347,1098,-1887,-4803,-7014,-1828,367,3675,1127,-2263,-6507,-6424,-1214,2880,+ 5397,3466,2686,2053,3182,2969,3095,1595,313,-2627,-6270,-6387,-1214,1383,+ 4289,701,-2745,-7335,-6757,-816,3871,6646,3189,823,-98,1974,3972,3627,+ 1465,-1215,-4282,-8250,-10143,-2852,2062,6391,3996,459,-6042,-6208,-77,4134,+ 7467,5757,4682,3255,1277,1354,2661,4236,-1629,-6926,-12950,-9871,-1484,3843,+ 6651,3220,-2465,-7935,-8868,-1396,1730,4318,490,3955,5043,6930,7938,5024,+ 3236,-1800,-4694,-8108,-6380,-651,1919,4401,-190,-2455,-6090,-5138,-530,2506,+ 2750,1309,306,3129,3643,5084,4431,3986,-552,-2181,-5319,-5926,-2792,-1089,+ 4140,1307,-1930,-6911,-6989,-705,5150,5454,2121,1806,934,1986,1117,2730,+ 3704,1932,569,-3102,-5390,-4865,-1102,1976,1576,-1546,-6284,-6708,-2576,3521,+ 5633,3340,773,19,1203,1434,2703,3440,3687,2838,-848,-2957,-3161,-2119,+ -849,-1074,-2611,-3529,-3129,-2035,-660,-281,-327,360,1249,2066,2786,2445,+ 2316,2607,3073,2645,2067,941,362,-58,-890,-1599,-2581,-2926,-2616,-1663,+ -1849,-1112,-319,776,1417,1762,2036,1949,1676,1225,938,354,-410,-727,+ -1195,-135,5,1255,807,1323,-110,-100,-979,458,-406,1090,-261,1849,+ -257,2217,1360,2113,-865,-2133,-3281,-3983,-2380,-1389,1989,1404,469,-310,+ -657,889,1509,1939,1785,1618,1029,290,-1831,-5471,-8648,-5173,-2436,4717,+ 2390,1333,-684,-1669,-864,56,2309,2013,3128,3427,3420,2348,95,-2246,+ -3392,-4781,-5406,-2236,843,5028,2831,158,-3988,-6420,-4529,-1626,2603,4417,+ 4726,2827,4011,3678,1622,213,-2038,-3700,-7654,-5058,772,6914,5203,1021,+ -3551,-6395,-4745,-978,1934,4655,4661,4239,4373,4062,2284,-45,-2716,-7703,+ -8060,-2304,3581,6313,3827,-1349,-3572,-4940,-3542,-438,1805,5626,3510,1886,+ 1994,1806,2831,518,-1056,-6959,-7160,114,5574,3440,610,-2637,-3989,-3766,+ -2205,2250,2478,3957,2929,1537,2941,1203,844,-171,-5364,-6674,-3508,1725,+ 6613,2268,-2496,-3461,-2698,-2482,-724,1571,3152,3427,3512,3858,1185,518,+ -1164,-3015,-6449,-5118,1865,6394,2938,-1809,-4421,-2806,-1730,-1823,404,1073,+ 4716,5124,3662,2062,37,-454,-1953,-6427,-6301,-389,5621,4114,-499,-3674,+ -2246,-233,-2091,-442,1637,3816,3896,3244,2273,-411,-1028,-2512,-6718,-6648,+ 713,5604,4939,106,-3627,-1785,-68,-793,-278,593,4607,4638,3074,1282,+ -1591,-1123,-3804,-6152,-5427,391,6605,2684,-322,-2681,-626,1514,-1045,-681,+ 844,3850,4382,2201,-440,-2436,-3107,-5118,-4625,321,4128,3232,611,-1441,+ -268,1335,1108,-1423,-1919,1865,5885,2777,840,-1473,-3475,-4908,-6208,-209,+ 3094,2753,1492,-1158,-329,583,678,-978,-2444,1444,5202,3855,2418,-1534,+ -1342,-3528,-4829,-3999,-75,5196,1160,-433,-981,-217,1257,-920,-864,1179,+ 3219,3444,1495,-289,-2007,-1615,-3653,-4008,-1629,2712,3110,833,497,-1272,+ 322,-1572,-521,538,1926,4609,902,-1370,-1153,-2492,-1165,-2775,-358,1056,+ 2389,2742,-120,-580,-2329,-1320,-880,-272,2449,3685,1768,-796,-1753,-736,+ 134,-1994,-1756,-1162,1573,3128,1177,161,-1344,-1513,-1462,-1656,128,2408,+ 1849,1273,711,707,512,-1482,-1410,-2814,-959,956,343,1510,-547,119,+ -206,-1874,-419,106,1114,2439,1796,3395,104,-1866,-2462,-4049,-2418,-493,+ 1293,1191,899,827,451,-243,-299,140,231,1820,2001,2035,-127,-2675,+ -3224,-2570,-991,33,736,1834,1295,1415,54,-1102,-782,-998,511,1548,+ 2536,2046,180,-1308,-2365,-2082,-1841,-765,12,643,806,696,698,-79,+ -597,589,1324,1922,1999,1671,26,-1896,-3100,-3062,-2693,-1503,-43,1086,+ 1533,1810,1538,1073,902,1215,1492,1233,488,-643,-2278,-2891,-2851,-2503,+ -1421,-209,1068,1798,2617,2302,1347,938,837,640,614,145,-828,-2306,+ -3016,-2657,-2356,-1182,-108,1311,1958,3019,2380,1934,954,669,439,183,+ -120,-1419,-2297,-3057,-2646,-2430,-1226,320,1565,2389,3028,2827,2173,945,+ 597,261,-104,-564,-1726,-2688,-3396,-2621,-2259,-882,407,1759,2720,3331,+ 3026,2182,1167,513,-1,-276,-906,-2050,-2961,-3201,-2565,-2128,-663,644,+ 1984,2956,3397,2967,1968,1044,370,-153,-779,-1480,-2385,-3066,-2950,-2349,+ -1429,-23,1064,2226,2785,3352,2586,1779,805,200,-60,-1041,-1557,-2413,+ -2652,-2424,-1991,-1256,-317,818,1975,2640,2614,2088,1333,581,169,-478,+ -825,-1231,-1462,-1789,-1589,-1375,-1065,-165,502,1412,1902,1809,1399,822,+ 449,-63,-357,-778,-717,-873,-913,-1157,-966,-809,-250,518,1122,1344,+ 1304,1194,753,-11,-9,-314,-502,-1177,-437,-973,-106,-341,-424,-288,+ 402,760,1193,705,941,337,248,-241,-445,-580,-931,-939,-248,-127,+ 144,315,428,127,551,813,444,183,296,-150,-458,-662,-690,-433,+ -367,-59,-121,227,-4,129,267,240,577,499,194,-237,-315,-831,+ -270,-399,-394,-284,262,-460,417,236,366,334,808,113,207,-174,+ -11,-451,282,-699,-100,-790,145,53,285,54,146,368,903,-866,+ 262,-199,-436,581,-195,-536,117,953,-421,73,80,-86,163,-94,+ 48,-268,-178,770,162,-255,-30,-585,-356,79,73,409,-255,-133,+ -657,99,170,-378,476,-797,944,-3,-285,-621,507,452,-141,78,+ 310,121,-522,-197,-157,101,263,-558,295,294,146,-298,233,-175,+ -961,999,-607,-237,-450,-485,-452,-190,580,1214,63,679,-387,1,+ 208,-304,329,-370,-478,321,-582,-19,-382,-182,130,367,933,407,+ -1148,531,-412,-122,747,944,-1379,103,-567,-679,1048,-624,-1045,1299,+ 570,-315,-381,41,-164,-183,-249,1648,724,-1650,-870,-642,1363,1335,+ -528,-914,533,-846,-657,867,-332,173,-883,17,291,72,-1282,551,+ 450,31,1930,-1538,-1685,-257,-389,2085,1743,-2749,-532,-707,698,2151,+ -1173,-1600,1516,23,-1362,713,1088,-1276,-954,-548,1182,1160,-1368,1603,+ -405,-418,537,-206,1284,-552,-88,-1232,548,-826,-193,-1010,1158,-378,+ 1587,-2044,-299,2229,-2413,288,2854,-1647,-582,-268,-589,1012,-2213,1648,+ -598,395,-214,-550,1465,-785,220,520,1398,-809,179,-1199,538,-915,+ 1538,1481,-1628,1285,-2303,-1248,2290,321,-1090,565,-719,881,-968,190,+ 776,672,17,-493,-286,27,-384,300,-332,-255,256,-1067,-33,-631,+ 754,1424,371,252,431,-526,-138,66,-198,-242,-608,402,-326,453,+ 523,141,329,-39,-1229,1254,349,483,-992,-352,-426,-690,-198,-408,+ 222,278,178,1020,623,706,279,1231,1286,991,-670,-382,-1891,-3124,+ -244,-429,-369,80,-391,-21,131,-309,-53,-67,-65,569,-319,163,+ 93,150,42,286,-8,-529,97,7,-109,199,-33,-17,9,-57,+ 404,250,-160,12,44,57,-230,38,107,296,-63,103,-91,-88,+ -109,-4,-60,262,-47,51,-241,-153,158,247,-39,24,-375,-122,+ 222,-78,241,-87,151,-214,51,-66,-28,83,44,-50,-116,-9,+ -2,309,-259,-36,249,-55,-47,249,126,-433,+};+static const unsigned char CLIP13[2980] = {+ 26,69,223,163,159,66,134,129,1,66,247,129,1,66,242,129,4,66,243,129,+ 8,66,130,132,119,101,98,109,66,135,129,4,66,133,129,2,24,83,128,103,+ 1,0,0,0,0,0,11,116,17,77,155,116,186,77,187,139,83,171,132,21,+ 73,169,102,83,172,129,161,77,187,139,83,171,132,22,84,174,107,83,172,129,+ 214,77,187,140,83,171,132,18,84,195,103,83,172,130,1,64,77,187,140,83,+ 171,132,28,83,187,107,83,172,130,11,94,236,1,0,0,0,0,0,0,89,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,21,73,169,102,176,42,215,177,131,15,66,+ 64,77,128,140,76,97,118,102,54,49,46,55,46,49,48,51,87,65,140,76,+ 97,118,102,54,49,46,55,46,49,48,51,68,137,136,64,115,64,0,0,0,+ 0,0,22,84,174,107,229,174,1,0,0,0,0,0,0,92,215,129,1,115,+ 197,136,104,77,221,163,69,102,121,136,156,129,0,34,181,156,131,117,110,100,+ 136,129,0,134,134,65,95,79,80,85,83,86,170,131,99,46,160,86,187,132,+ 4,196,180,0,131,129,2,225,145,159,129,2,181,136,64,207,64,0,0,0,+ 0,0,98,100,129,16,99,162,147,79,112,117,115,72,101,97,100,1,2,56,+ 1,128,62,0,0,0,0,0,18,84,195,103,252,115,115,159,99,192,128,103,+ 200,153,69,163,135,69,78,67,79,68,69,82,68,135,140,76,97,118,102,54,+ 49,46,55,46,49,48,51,115,115,215,99,192,139,99,197,136,104,77,221,163,+ 69,102,121,136,103,200,162,69,163,135,69,78,67,79,68,69,82,68,135,149,+ 76,97,118,99,54,49,46,49,57,46,49,48,49,32,108,105,98,111,112,117,+ 115,103,200,161,69,163,136,68,85,82,65,84,73,79,78,68,135,147,48,48,+ 58,48,48,58,48,48,46,51,48,56,48,48,48,48,48,48,0,31,67,182,+ 117,73,151,231,129,0,163,64,217,129,0,0,128,188,127,81,216,191,224,195,+ 65,83,19,0,81,127,122,224,222,87,208,162,215,111,30,56,47,168,144,240,+ 124,211,71,36,2,175,244,242,30,140,254,67,236,101,88,41,28,235,85,147,+ 251,32,77,78,48,130,132,84,211,199,36,235,111,8,222,93,206,59,111,7,+ 228,79,13,207,149,219,102,134,58,0,5,54,100,105,105,10,123,242,107,112,+ 226,33,30,13,133,164,202,237,0,123,13,125,215,18,33,132,50,179,96,134,+ 87,108,166,221,102,201,124,109,208,213,190,72,217,235,102,81,4,39,131,38,+ 161,251,182,148,83,157,251,166,154,40,225,131,20,206,185,166,116,140,241,28,+ 165,46,152,81,41,204,193,46,221,159,134,167,96,249,211,44,33,103,183,159,+ 32,53,197,13,110,79,40,150,159,213,171,229,41,41,105,177,31,168,235,78,+ 93,99,172,218,225,120,87,178,93,154,190,108,210,74,167,128,210,160,143,58,+ 157,113,214,87,249,34,163,64,132,129,0,21,128,188,195,52,61,106,214,146,+ 174,167,232,163,102,55,206,44,14,39,181,18,55,188,108,251,254,199,141,88,+ 26,142,115,149,219,26,128,14,11,74,216,157,197,193,221,144,11,113,239,243,+ 200,109,76,147,231,242,58,177,82,145,61,42,14,173,207,111,157,167,147,26,+ 78,193,220,186,169,6,197,161,160,224,49,60,157,33,152,170,133,20,112,119,+ 76,138,35,179,159,163,98,36,101,185,146,182,95,135,68,32,8,198,246,71,+ 104,255,156,71,202,175,173,236,184,164,97,46,227,232,230,19,221,232,153,78,+ 253,163,64,144,129,0,41,128,188,195,227,215,234,179,11,150,22,6,29,64,+ 76,44,229,177,125,242,209,185,135,47,166,246,102,51,210,132,24,155,191,90,+ 22,189,47,55,195,67,105,48,158,179,39,253,97,204,63,196,90,65,149,177,+ 63,73,61,100,85,44,47,60,96,75,37,34,51,188,17,226,163,82,168,226,+ 10,107,47,31,161,107,193,145,11,1,252,178,196,125,33,239,51,130,250,28,+ 146,60,203,123,42,192,123,220,202,245,147,19,7,141,182,77,162,113,169,73,+ 133,5,207,248,108,167,28,95,231,255,243,188,163,95,151,230,46,148,188,170,+ 6,202,103,85,88,237,206,253,163,64,188,129,0,61,128,188,223,147,159,218,+ 128,121,230,86,22,75,100,187,224,54,104,127,176,70,251,168,240,4,92,214,+ 14,239,109,19,183,143,18,2,17,222,34,216,29,82,18,42,15,128,62,34,+ 173,223,38,205,11,138,91,14,99,209,167,146,142,35,221,2,58,138,217,184,+ 253,117,218,47,237,181,17,187,203,7,143,226,65,157,211,7,187,179,213,10,+ 125,202,207,217,160,28,38,22,41,206,6,5,183,64,180,62,237,107,249,78,+ 18,168,253,175,79,138,206,125,174,73,86,229,177,249,170,205,41,212,36,178,+ 232,191,85,199,220,54,168,91,148,117,122,174,91,193,109,213,9,102,241,72,+ 68,63,184,58,64,162,73,147,116,68,37,109,136,19,184,139,140,114,246,50,+ 195,141,199,57,236,39,165,10,88,141,233,131,113,194,142,66,39,58,9,163,+ 64,195,129,0,81,128,188,202,57,232,1,153,70,127,166,102,84,49,41,83,+ 172,107,2,214,177,225,70,65,35,48,203,99,84,237,146,63,155,45,151,234,+ 29,121,200,5,255,160,68,194,240,146,192,232,151,228,90,243,149,42,175,246,+ 205,82,8,197,60,166,3,65,14,254,15,48,35,185,11,70,245,13,207,103,+ 254,232,147,43,128,37,61,176,113,101,153,200,206,48,25,88,6,62,237,100,+ 188,105,62,130,172,218,179,165,0,141,91,205,182,154,208,132,96,196,227,142,+ 116,10,206,46,18,160,135,171,54,153,212,31,37,234,245,19,221,114,100,142,+ 215,224,9,112,40,223,251,194,248,98,145,221,107,100,126,91,4,37,130,239,+ 54,35,197,199,164,160,115,42,136,242,135,68,64,180,193,146,55,88,150,167,+ 27,145,255,163,33,188,183,153,143,216,104,212,139,218,241,118,113,163,64,135,+ 129,0,101,128,188,194,147,173,35,208,162,183,177,177,194,6,119,15,66,167,+ 203,218,116,92,73,122,66,125,1,31,96,194,103,140,235,12,57,47,24,210,+ 81,103,112,53,94,11,166,24,227,28,254,13,75,100,249,239,78,210,31,146,+ 176,223,197,47,147,35,216,30,39,249,47,110,43,204,40,44,130,18,74,151,+ 24,26,109,208,53,203,150,165,87,122,235,201,182,61,113,40,193,254,119,228,+ 140,19,148,237,128,5,249,69,6,116,75,24,219,230,221,0,14,42,54,3,+ 156,177,147,142,124,156,139,225,247,73,31,239,93,78,107,163,64,130,129,0,+ 121,128,188,196,137,127,14,21,50,229,206,30,93,121,46,29,26,231,123,139,+ 49,197,156,94,151,218,145,145,107,49,105,3,173,179,42,136,49,146,170,231,+ 181,29,35,192,82,144,185,51,75,159,58,201,26,204,212,8,120,25,153,26,+ 195,238,184,94,40,145,136,247,205,131,136,225,194,97,123,47,186,31,103,180,+ 64,90,108,87,203,118,166,110,96,14,9,73,115,239,150,238,219,220,82,202,+ 181,74,229,102,133,154,46,47,228,126,16,225,26,116,26,48,55,183,122,215,+ 164,55,71,64,46,136,62,101,163,64,137,129,0,141,128,188,235,31,34,245,+ 132,22,153,89,114,108,84,132,123,180,57,91,45,9,154,57,207,53,90,97,+ 41,242,107,110,240,174,243,247,42,41,129,2,23,25,165,198,235,225,166,53,+ 55,42,227,189,59,189,250,112,68,176,134,171,176,17,209,147,174,252,4,97,+ 71,136,231,188,226,41,200,151,50,16,166,207,131,254,223,99,231,154,98,167,+ 49,25,36,207,193,136,203,8,247,34,228,77,182,103,124,182,61,142,27,77,+ 228,151,130,18,140,28,192,143,63,101,161,12,28,235,11,207,229,21,138,154,+ 71,89,172,171,183,207,6,149,163,64,129,129,0,161,128,188,54,89,236,171,+ 244,56,110,2,198,73,183,85,183,68,40,117,52,72,151,182,172,53,51,224,+ 78,23,182,231,215,123,121,232,9,132,216,79,131,31,248,240,209,45,157,148,+ 28,218,171,86,75,67,34,68,111,100,106,56,80,252,89,215,96,48,12,198,+ 48,154,21,175,222,161,223,142,77,220,197,253,216,81,65,113,151,246,235,125,+ 146,28,219,209,242,21,7,73,107,150,128,25,140,42,137,29,200,220,122,44,+ 219,59,113,144,142,143,40,208,20,103,67,106,11,249,164,177,230,29,48,132,+ 163,240,129,0,181,128,188,192,235,112,213,114,117,197,243,210,253,161,28,132,+ 53,224,254,23,155,130,46,50,216,68,230,52,206,45,115,192,190,70,174,228,+ 18,166,165,70,151,174,38,94,115,140,106,104,68,44,79,11,178,102,36,207,+ 221,164,255,175,226,90,245,172,179,4,42,199,100,177,134,202,84,146,111,77,+ 78,245,181,47,244,32,28,14,58,181,145,225,88,161,87,106,55,230,91,76,+ 255,197,44,177,128,71,6,28,183,84,160,74,187,77,163,227,129,0,201,128,+ 188,192,37,122,72,242,111,16,203,233,126,155,56,53,38,13,225,243,53,81,+ 146,143,233,236,60,27,116,242,114,253,191,221,66,235,144,118,84,51,235,81,+ 1,137,89,64,108,193,71,131,180,237,189,196,227,206,46,182,135,140,144,49,+ 210,251,91,38,218,6,79,91,137,156,8,73,61,165,109,119,96,59,149,146,+ 190,208,67,243,160,100,178,231,23,152,67,8,142,103,77,163,64,166,129,0,+ 221,128,188,113,217,239,113,152,189,11,176,11,80,188,196,164,183,138,95,167,+ 54,198,189,244,236,89,98,163,121,2,119,174,93,93,9,237,246,10,110,194,+ 74,220,190,57,38,202,235,175,202,192,163,128,229,95,10,244,76,252,229,23,+ 151,231,27,12,113,81,118,19,106,143,231,124,232,168,177,122,249,77,190,144,+ 171,5,76,16,214,112,251,171,86,102,232,250,61,53,209,111,153,201,189,41,+ 204,20,161,111,28,116,23,233,120,121,34,17,170,23,145,179,138,229,19,200,+ 31,184,78,13,115,96,170,136,192,89,175,255,63,177,192,61,62,155,45,215,+ 37,103,229,251,232,46,89,245,116,226,109,238,102,228,74,200,247,102,28,134,+ 234,15,198,130,163,251,129,0,241,128,188,65,229,198,163,105,248,235,220,26,+ 50,223,106,189,32,90,79,242,72,79,255,6,173,180,249,212,151,182,198,61,+ 171,104,232,177,74,186,17,235,54,72,91,206,146,155,132,250,113,166,234,112,+ 171,181,94,121,29,50,6,108,164,29,109,37,128,23,49,127,246,205,98,225,+ 209,196,161,82,176,138,120,170,65,219,7,243,64,108,30,26,114,200,28,154,+ 41,236,251,156,50,209,27,69,126,183,155,37,119,254,43,136,234,178,39,17,+ 15,204,82,41,198,119,244,31,113,163,241,129,1,5,128,188,65,127,92,215,+ 232,87,174,178,172,158,139,156,243,65,164,239,105,8,247,118,165,110,41,233,+ 159,175,63,232,176,123,63,145,27,96,207,12,228,76,143,115,73,9,27,150,+ 56,16,217,189,201,171,18,122,70,178,92,4,27,33,21,38,173,1,125,200,+ 187,194,203,117,152,17,215,178,244,213,170,44,132,137,43,40,182,95,167,60,+ 247,200,111,133,0,20,56,186,170,228,29,144,146,30,101,154,28,46,34,220,+ 37,115,68,178,163,64,154,129,1,25,128,188,117,119,182,22,126,168,1,129,+ 185,219,240,59,152,105,25,225,48,117,229,58,55,206,248,49,214,126,40,78,+ 11,52,209,91,123,82,135,61,222,169,214,16,97,75,254,244,214,40,55,229,+ 74,98,129,56,192,102,87,136,1,181,209,190,52,151,37,22,147,118,201,148,+ 66,153,65,150,92,56,40,150,82,245,224,63,11,106,131,188,162,56,162,82,+ 16,118,1,131,227,151,47,87,16,73,123,75,29,154,87,65,245,179,136,144,+ 213,46,88,46,57,93,238,143,94,22,18,46,135,57,253,162,152,219,13,168,+ 232,95,53,162,111,201,209,144,35,175,144,35,244,32,86,143,220,211,139,199,+ 248,160,64,234,161,64,224,129,1,45,0,188,123,69,126,222,138,11,37,128,+ 162,127,189,147,44,5,176,176,248,20,168,205,252,136,225,122,89,231,124,121,+ 10,29,38,117,23,150,249,71,201,233,19,137,229,156,229,11,39,222,83,13,+ 181,143,253,125,109,22,106,168,54,100,100,204,222,251,243,225,183,220,100,136,+ 232,41,111,28,159,196,128,152,175,206,187,114,57,114,178,233,61,169,67,41,+ 120,182,97,217,102,103,238,103,35,95,169,22,227,133,227,120,137,1,71,65,+ 184,73,209,233,62,166,236,156,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,51,151,174,95,202,82,123,76,70,+ 211,131,212,124,230,50,241,79,133,80,16,138,193,180,203,245,112,213,131,90,+ 118,220,85,22,195,129,120,173,188,94,72,252,21,193,233,154,250,211,182,192,+ 82,79,7,94,0,63,205,64,25,45,44,187,56,124,60,89,242,142,3,52,+ 45,146,16,158,72,66,63,77,146,193,108,117,162,132,0,205,254,96,28,83,+ 187,107,145,187,143,179,129,0,183,138,247,129,1,241,130,1,193,240,129,3,+};+static const int REF13[3600] = {+ -463,-489,-256,-15,49,-275,51,313,125,-217,-1,-509,-278,-221,+ -789,156,-340,-413,-321,-474,-289,-460,150,-387,-229,-625,-496,-13,+ -401,-400,-533,270,359,316,353,-15,453,524,649,659,520,362,+ 581,800,759,949,835,1002,961,926,1003,1109,904,688,472,103,+ -238,-740,-764,-996,-1418,-1845,-1712,-1864,-2486,-3245,-3842,-3801,-4387,-4328,+ -4234,-3585,-3652,-3387,-1695,-542,354,592,863,2059,2072,1892,2053,2364,+ 1508,960,944,1376,1066,656,624,1116,1080,1271,1653,2258,2527,2421,+ 3119,3449,3514,3703,3844,4078,4320,3875,3701,3460,3012,2243,1408,822,+ -93,-480,-710,-1327,-1835,-2147,-2520,-2709,-3489,-4352,-5058,-6084,-6746,-6632,+ -6283,-6066,-4842,-2544,-1266,-315,354,1546,2940,3105,2988,3332,2977,1123,+ 959,551,416,-756,-1224,-1482,-1454,-2368,-2338,-1819,-1951,-2047,-1806,-1332,+ -858,-406,313,761,1623,2639,3451,3664,4215,4071,5170,4783,4956,4470,+ 4348,3710,3593,3453,2606,1360,998,1365,1326,767,-107,366,364,-56,+ -45,-547,-2352,-3767,-5167,-5169,-4790,-6604,-5130,-3576,-2363,-2028,-1593,-257,+ 1146,2525,1839,3251,1268,490,-6,-167,-774,-2735,-4234,-5079,-4904,-5087,+ -4643,-3977,-3104,-1578,-330,1127,1484,2570,2808,3796,3660,4353,3660,2929,+ 2844,3105,3628,3138,2824,3450,3721,3332,2978,3725,3344,2703,1868,2268,+ 2649,2252,1607,1125,1123,1185,455,-758,-2098,-3487,-4224,-5141,-7160,-7715,+ -7060,-6018,-3680,-2195,-1869,-876,647,2221,4294,3530,2241,1772,797,68,+ -917,-2155,-5100,-5330,-7288,-6702,-6432,-6481,-4076,-2810,-648,24,2134,2137,+ 4309,4606,4542,4831,3238,3230,2814,2905,2381,2889,1858,2189,2817,2984,+ 4048,3249,3317,2824,3649,2950,3105,1999,1278,965,592,478,110,-1734,+ -2878,-2279,-3053,-4416,-6363,-6890,-7044,-6078,-5755,-2830,-1544,-837,-465,1700,+ 3698,4097,3759,1803,1849,-102,-599,-2640,-3435,-5681,-7042,-7340,-7735,-7719,+ -6746,-4730,-2748,-219,996,3364,3845,4952,6328,6088,5120,3642,2709,1853,+ 1539,1259,1306,1103,407,1836,3654,3730,3737,3754,4702,4174,3227,2626,+ 2613,1404,-1,226,-337,-1213,-2049,-1779,-2314,-4135,-5112,-4746,-5281,-5983,+ -6839,-5395,-3598,-1527,546,730,1174,1718,3803,3616,2892,270,203,-902,+ -2306,-3228,-4411,-6409,-7835,-7681,-6989,-6486,-5816,-3155,747,2090,2550,3950,+ 5648,7183,5847,4945,3294,1704,1185,1199,245,-280,-703,391,1470,2737,+ 3769,4322,4728,5167,5453,4978,4043,2758,1843,422,-161,-973,-2088,-2638,+ -3066,-2928,-3950,-4743,-5871,-5655,-6136,-5252,-5236,-3624,-1542,-254,881,710,+ 1919,3020,3794,2441,1295,-105,-542,-880,-2715,-4043,-5784,-5921,-6172,-5845,+ -6973,-5127,-3344,-18,2115,2663,2969,4528,6000,5512,4349,1407,1261,982,+ 242,-640,-983,-861,171,1742,2991,4509,4507,5379,6726,6641,5798,4789,+ 3668,2984,1896,1151,-302,-1786,-2151,-1894,-1906,-3378,-4230,-4323,-4536,-5201,+ -4576,-4843,-3979,-2681,-550,1437,992,779,2020,3599,3156,2018,173,5,+ -874,-1550,-2715,-5236,-5951,-6930,-5904,-5559,-5723,-3300,-1411,1003,2066,3127,+ 4280,5119,5144,4176,3808,1878,1640,848,265,565,710,1527,2009,3144,+ 4112,5265,5526,5778,6096,6026,5090,3994,2995,2075,1243,22,-949,-1298,+ -1462,-2282,-2450,-3638,-4069,-5031,-4956,-4677,-4310,-3665,-2535,161,471,622,+ 1063,2495,3668,3359,1410,803,9,-1051,-1247,-2769,-4547,-5342,-6351,-5797,+ -5537,-5925,-4123,-2492,-23,1695,2970,2962,3853,4698,4850,4381,2258,1888,+ 1806,1352,1303,1037,1553,1246,2385,3185,3800,3938,3619,4495,4453,3607,+ 3409,3084,2528,1524,1223,781,113,-606,-1250,-903,-2349,-3852,-4573,-4899,+ -5057,-5263,-4985,-3982,-2249,-1304,-603,-90,620,2157,2868,2242,1146,1020,+ 692,179,-471,-2362,-3633,-5094,-5382,-4869,-5491,-5485,-3989,-1916,-821,-224,+ 899,2468,3361,3448,3548,3722,2999,2255,2618,2838,1999,1603,1303,1809,+ 1754,1823,2419,2150,1921,2560,2889,2349,2053,1966,2221,2315,1431,726,+ 1348,912,276,515,509,84,-100,-319,-264,-679,-1664,-1542,-1509,-1909,+ -1424,-1544,-2028,-1823,-1856,-1668,-1672,-2102,-1995,-2177,-3314,-3993,-4181,-4529,+ -4852,-5043,-4594,-3497,-2153,-1321,-888,26,1029,1965,2760,2680,2316,2266,+ 2317,2333,1971,1309,1182,1129,1168,1164,726,637,896,1299,1656,1653,+ 1712,1896,1870,2234,2642,2669,2614,2575,2666,2815,2873,2677,2282,2097,+ 1993,1741,1539,1002,424,321,28,-333,-477,-703,-687,-565,-931,-1217,+ -943,-973,-1438,-1644,-1851,-2047,-2350,-2786,-2772,-2835,-3159,-3103,-2800,-2321,+ -1785,-1854,-1647,-1055,-863,-525,-86,-1,200,343,424,700,850,881,+ 931,1045,1147,1139,1238,1326,1168,1261,1386,1345,1355,1204,1208,1334,+ 1232,1179,1139,1004,984,1075,1036,764,518,409,471,396,64,24,+ 50,-25,48,-84,-96,205,160,181,358,298,520,476,336,521,+ 259,22,-29,89,344,-479,-548,452,230,-54,30,-293,-395,-348,+ -129,-261,247,574,-344,-415,-6,703,1475,671,486,1245,1758,3302,+ 3173,979,833,1842,2440,2153,1093,1005,649,455,1357,720,-875,-1675,+ -1691,-1170,-1942,-2744,-3008,-3856,-3284,-2795,-3281,-3196,-3385,-3115,-2063,-1251,+ -1068,-1331,-1012,-142,983,1228,1009,752,625,1072,884,447,-216,-183,+ 42,157,146,247,764,1108,1541,1416,1408,1853,1977,1592,1640,1549,+ 1655,1259,1177,1392,1243,781,99,529,100,-140,-1316,-1643,-2356,-3357,+ -3829,-4265,-5053,-6444,-7349,-7412,-6660,-4999,-3135,-2361,-2363,-430,2083,3795,+ 3358,2320,2637,2957,2966,1490,348,-339,-221,-370,-1215,-1827,-2382,-1876,+ -797,-110,15,-37,899,1900,2323,2069,2311,2265,2276,2373,2522,2691,+ 2274,2406,2739,3031,3184,3205,3134,3048,2640,2176,1685,1048,56,-512,+ -600,-1193,-2028,-3114,-3546,-4727,-6218,-7328,-7624,-7613,-6393,-4271,-2232,-1783,+ -790,1441,3849,4938,4300,3528,2741,2347,1794,361,-1474,-3210,-3814,-4227,+ -4863,-6154,-6581,-5948,-4694,-3540,-2773,-1183,237,1771,2556,3343,3842,4276,+ 3840,3744,4090,4111,3109,2107,2899,3832,3651,2880,2573,2629,2767,2498,+ 1715,807,620,1080,1197,110,-942,-1358,-1561,-2799,-5027,-6812,-7307,-7006,+ -6599,-4747,-2132,-423,-152,1823,4782,6237,5193,3916,3731,2915,1610,-358,+ -1832,-3464,-4279,-4551,-5291,-6254,-7038,-6204,-4793,-3547,-2204,-337,2048,2992,+ 3462,4121,5556,5420,4308,3546,3571,2961,2745,2351,2186,1800,2575,3135,+ 2887,2012,2065,2222,1613,684,685,1009,-293,-1461,-1905,-1791,-3353,-6079,+ -7777,-7563,-7184,-6972,-5428,-2375,93,984,3146,5337,6860,5979,5103,4099,+ 2229,393,-1041,-2319,-3988,-4277,-3846,-4132,-4592,-4295,-4117,-4362,-3637,-1999,+ -1471,-84,2578,3107,2356,2873,4622,4566,3027,2175,2511,2873,2548,2223,+ 2067,2424,3260,4162,3352,2365,1727,1631,1567,889,-863,-2108,-1132,-934,+ -2011,-4550,-6503,-7435,-6606,-6371,-6644,-5009,-1591,797,1557,3343,6350,7467,+ 6223,4713,3886,2069,-518,-2322,-3342,-4038,-4656,-4807,-4197,-3698,-3506,-3348,+ -2816,-2106,-1656,-858,1307,2696,2478,1436,2033,3744,3864,2767,2257,2365,+ 3009,3086,2812,2811,3434,3818,4102,3035,1445,382,778,817,-236,-1493,+ -1613,-914,-1077,-3212,-6157,-6982,-6654,-6933,-7149,-6841,-3584,65,1723,1756,+ 3962,6647,7250,5287,3439,1940,1016,-662,-2786,-4444,-4269,-3701,-2967,-2278,+ -1924,-2176,-2098,-1475,-976,-1371,-954,451,1276,880,1030,2407,3678,3548,+ 2919,2761,3270,3531,2667,2482,3065,3282,2761,2234,1528,998,865,825,+ -34,-1575,-1317,-300,-1088,-3482,-5932,-7011,-7322,-6376,-6170,-5322,-3005,1030,+ 3073,3827,5516,7217,7292,5395,3460,1053,-1178,-2451,-2993,-3827,-4686,-3541,+ -2564,-1857,-1500,-854,-531,-124,-872,-1610,-1259,-451,141,-143,-117,285,+ 2460,3599,3226,2584,3215,4072,4225,3869,3443,3780,3585,2415,1380,1218,+ 497,-744,-842,-95,-754,-2122,-2398,-2975,-5083,-7613,-7559,-7112,-6317,-3830,+ -559,757,2210,4788,7131,7120,6020,3907,2172,632,-1218,-3290,-4287,-4256,+ -3873,-3394,-2392,-1443,-961,-437,712,-111,-287,-29,355,60,1029,1229,+ 187,817,2364,3215,2896,3031,3419,3233,4006,4636,4490,3351,2774,2578,+ 1213,-131,-729,-865,-405,-736,-1793,-2861,-3571,-4875,-7415,-7628,-7253,-7095,+ -5282,-1405,730,1478,3609,6301,7794,6549,4435,2643,1147,-692,-2214,-3057,+ -3964,-3789,-2441,-864,-781,-606,632,1486,380,-805,-1264,-837,-929,-659,+ -159,273,1211,2039,4132,4519,4303,4525,4606,5105,3822,2691,1827,1742,+ 444,-1186,-1382,-1455,-1823,-1204,-1028,-2476,-4313,-5274,-6015,-6718,-7103,-8186,+ -6581,-1844,336,-49,1156,5576,6894,5958,4511,3566,2525,1521,272,-1670,+ -2931,-3054,-1456,-1174,-990,-1084,342,2194,1868,266,-1747,-1036,-945,-1555,+ -1716,-826,1079,1986,2415,3626,4871,5685,5039,4299,3783,2447,1597,790,+ 199,-483,-1185,-1678,-1398,-1138,-1895,-3307,-4854,-5356,-5909,-5640,-6250,-5265,+ -3083,-1317,-291,352,2727,4504,4980,4052,3435,2879,1123,374,74,-776,+ -1846,-1306,63,-193,-786,562,1748,1685,316,-762,-1388,-1642,-1655,-2683,+ -1501,1427,2946,2497,2925,5059,5798,4973,3414,2704,1730,1236,649,-521,+ -1338,-1471,120,-777,-2316,-3553,-3475,-4029,-5299,-5928,-7026,-5269,-3405,-1893,+ -1548,-455,2365,3682,3783,2139,1437,2187,1940,715,-1275,-1021,40,450,+ 1,-542,338,1007,2111,984,-227,-490,-805,-1270,-2197,-2436,-2681,-912,+ 1052,1720,1963,3563,5433,5791,4764,4236,3582,2065,1339,6,-937,-2460,+ -1925,-977,-909,-2547,-2894,-2883,-3163,-4136,-5296,-4883,-6416,-4536,-1921,-770,+ -1860,-406,2694,4768,3485,2385,1696,1982,2070,880,-1432,-1689,-1042,159,+ 490,-133,175,659,1828,1391,-553,-1751,-1469,-1537,-1239,-1441,-549,1385,+ 2032,3219,3627,4245,4756,3989,3233,2757,1840,929,-92,13,-334,-1052,+ -1555,-1423,-1987,-2377,-1843,-3345,-4417,-5076,-4983,-3988,-3343,-2967,-1823,-132,+ 1766,2137,1929,3101,3570,3102,2404,573,21,26,1359,392,73,-1174,+ -403,56,538,-557,-1130,-1311,-677,-613,-1619,-1519,-2063,362,1978,2259,+ 2013,2469,4251,5230,4569,2117,1782,1870,1897,-577,-2176,-2314,-1404,-1228,+ -791,-2031,-2579,-2364,-1275,-2392,-3699,-4395,-3637,-2529,-1377,-1053,-637,1175,+ 2845,3697,2059,1674,2489,2566,1757,364,-396,541,127,10,-1254,-1178,+ -1277,47,-1265,-1323,-2116,-1196,-1078,-1707,-2140,-556,300,1390,1793,2393,+ 3161,4249,3494,3636,1582,1977,818,-504,-1271,-1225,-1647,-1021,-579,-633,+ -916,-381,271,-543,-2079,-2436,-2986,-2441,-2603,-2609,-2364,-914,1041,1575,+ 1885,1639,2610,3011,3118,1572,478,-15,391,-122,-680,-1504,-998,-1269,+ -637,-1320,-1184,-1410,-1119,-1199,-1325,-979,-1132,-544,243,1148,1580,1657,+ 2041,2409,1942,2165,2070,1507,855,1086,485,1647,307,730,-238,854,+ 674,532,-1074,-1308,-2128,-1529,-1764,-3020,-3682,-3635,-1932,-1190,-1295,-1296,+ -265,1230,1976,1024,540,1287,2039,1739,743,82,-237,463,748,-135,+ -1261,-784,68,63,-704,-1297,-1114,-338,230,18,-529,245,1835,2176,+ 1777,1436,2019,3058,3175,2681,1386,1503,2126,2670,1206,-242,-528,-77,+ -451,-1790,-2833,-2941,-2636,-2738,-3662,-3748,-3007,-2208,-1643,-1266,-761,-107,+ 477,1058,1234,1498,999,886,1106,1371,1153,966,1016,975,1098,1376,+ 942,471,305,295,-21,-574,-867,-1025,-1098,-870,-341,243,514,1027,+ 1522,2360,2461,2123,1855,1960,2305,1293,103,-540,-516,-615,-1302,-2710,+ -3361,-3330,-2894,-2980,-3075,-2855,-2584,-1424,-711,-462,-956,-363,364,551,+ 249,-151,425,954,1347,1142,1039,1204,1675,1835,1503,800,1058,1223,+ 1128,145,-347,-347,-264,-452,-1153,-1205,-872,-194,341,537,780,1046,+ 1725,2232,2303,1830,1484,1420,1572,949,-34,-701,-1076,-1433,-1843,-2241,+ -2529,-2504,-2204,-1803,-1675,-1768,-1658,-1544,-1149,-1152,-905,-612,-287,367,+ 713,900,1033,1257,1424,1293,1130,1093,969,819,679,349,55,-221,+ -301,-537,-665,-373,119,174,432,947,1370,1467,1314,1426,1510,1447,+ 1147,877,686,574,213,-379,-952,-1434,-1885,-2271,-2501,-2888,-3040,-2867,+ -2628,-2563,-2535,-2434,-2340,-1810,-1351,-1020,-699,-149,327,762,1025,1224,+ 1293,1514,1809,1860,1894,1941,2152,1965,1538,1217,951,663,536,551,+ 403,364,578,773,927,914,879,866,866,948,821,560,276,-4,+ -310,-754,-1314,-1990,-2562,-2824,-2755,-2886,-3164,-3092,-2798,-2452,-2393,-2290,+ -1989,-1564,-1008,-576,-229,147,662,1099,1351,1592,1831,2030,2252,2602,+ 2750,2653,2589,2444,2246,1936,1605,1300,1054,969,824,658,554,574,+ 554,455,382,295,187,83,-90,-366,-595,-838,-1258,-1776,-2324,-2680,+ -2822,-2946,-3075,-3202,-3038,-2599,-2269,-2285,-2344,-2100,-1593,-1000,-516,-122,+ 74,541,1100,1436,1754,1925,2081,2345,2746,3029,2926,2672,2473,2397,+ 2196,1855,1412,1003,794,719,726,547,356,419,456,450,305,72,+ -98,-282,-431,-706,-1077,-1513,-1935,-2409,-2692,-2813,-3017,-3120,-3072,-2838,+ -2603,-2443,-2274,-2072,-1743,-1273,-788,-351,94,490,879,1257,1607,1939,+ 2135,2309,2550,2743,2888,2839,2666,2369,2208,2122,1790,1394,1096,1002,+ 909,834,723,564,583,615,525,228,-44,-225,-499,-762,-1138,-1569,+ -2033,-2533,-2949,-3266,-3337,-3448,-3573,-3434,-3113,-2812,-2628,-2415,-2074,-1671,+ -1145,-599,-117,218,638,1066,1469,1862,2095,2306,2596,2914,3123,3025,+ 2860,2731,2476,2193,1970,1666,1280,967,850,802,616,425,362,308,+ 262,145,-122,-368,-568,-827,-1068,-1458,-1906,-2337,-2801,-3087,-3280,-3368,+ -3454,-3507,-3306,-3031,-2685,-2441,-2283,-1985,-1400,-738,-340,52,477,876,+ 1214,1514,1909,2139,2375,2645,2805,2978,2866,2705,2514,2232,2024,1801,+ 1448,1195,962,745,691,481,312,285,193,115,-71,-317,-539,-825,+ -1045,-1233,-1647,-1989,-2361,-2717,-2870,-3043,-3150,-3127,-3025,-2819,-2649,-2377,+ -2111,-1936,-1582,-1158,-603,-71,229,536,1020,1386,1716,2106,2431,2689,+ 2851,3110,3289,3230,2994,2703,2438,2187,1893,1530,1238,1026,875,738,+ 477,315,212,75,-75,-303,-633,-985,-1139,-1340,-1607,-1908,-2293,-2518,+ -2695,-2831,-2943,-2895,-2760,-2711,-2577,-2438,-2249,-2019,-1807,-1485,-1083,-661,+ -225,176,500,854,1210,1500,1860,2132,2338,2507,2574,2683,2631,2452,+ 2336,2091,1822,1636,1332,1126,960,731,612,478,321,255,121,-39,+ -202,-455,-625,-747,-957,-1107,-1318,-1553,-1732,-1956,-2059,-2093,-2118,-2053,+ -2035,-1995,-1844,-1742,-1652,-1518,-1333,-1013,-689,-402,-80,258,602,898,+ 1196,1448,1631,1791,1927,1954,1888,1801,1680,1605,1457,1271,1128,977,+ 874,720,619,603,461,388,350,252,136,-38,-221,-332,-446,-583,+ -654,-755,-808,-954,-1154,-1245,-1302,-1355,-1425,-1473,-1498,-1517,-1472,-1407,+ -1301,-1200,-1052,-806,-581,-345,-120,99,327,596,767,916,1082,1128,+ 1283,1358,1337,1354,1304,1262,1215,1068,974,869,745,669,544,435,+ 356,246,116,18,-57,-150,-242,-339,-442,-461,-485,-545,-534,-561,+ -579,-586,-590,-586,-596,-702,-709,-723,-633,-615,-564,-431,-329,-189,+ -86,-11,106,217,355,421,496,633,655,716,764,743,782,764,+ 704,731,608,583,308,302,106,28,-40,-136,-279,-265,-336,-351,+ -377,-465,-513,-563,-502,-582,-563,-428,-503,-380,-309,-212,-172,-98,+ -127,-27,5,85,108,136,118,206,183,245,239,202,244,348,+ 436,498,483,449,480,472,438,365,165,99,44,-107,-139,-189,+ -186,-299,-276,-395,-429,-455,-454,-529,-571,-617,-566,-459,-510,-381,+ -482,-254,-330,-223,-194,-190,-164,-89,-32,58,8,28,123,-62,+ -61,116,60,144,86,252,196,285,217,349,353,486,441,401,+ 459,313,184,64,9,-137,-208,-219,-340,-293,-338,-294,-371,-364,+ -604,-471,-421,-310,-356,-420,-416,-420,-508,-393,-439,-182,-95,-29,+ -5,-86,-145,-270,-134,-2,367,417,453,479,481,412,375,311,+ 327,264,330,70,290,365,404,280,294,242,307,171,160,84,+ 72,105,105,-197,-78,-315,-191,-264,-300,-285,-505,-331,-189,-378,+ -461,-369,-513,-360,-542,-178,-441,-262,-251,-144,-200,147,39,-88,+ 235,-191,326,74,11,16,120,94,-115,30,82,-110,68,-180,+ -162,-50,-115,8,-139,71,37,-74,87,-77,101,147,22,303,+ -17,106,-78,-212,-118,106,283,595,316,594,241,423,74,158,+ -45,54,-314,-223,-285,-143,-32,-281,-139,-177,-230,38,-238,-123,+ -209,-294,100,-315,-37,-189,230,-186,97,-152,166,85,210,87,+ 159,-68,143,-114,7,-272,-223,109,-297,48,-210,241,323,322,+ 81,306,236,-6,457,-398,284,-339,166,-241,52,-214,74,-369,+ 88,-446,-29,-349,101,-218,6,-305,-174,-161,-18,-381,110,-366,+ 15,-244,-199,24,-249,-43,47,-153,297,-201,424,-161,274,-307,+ 416,-273,193,-364,110,274,22,453,-13,209,-139,447,-276,267,+ -52,-99,162,-134,-39,247,-118,139,-235,222,-326,88,-101,124,+ -94,100,-175,78,28,-760,519,-876,287,-535,-128,-3,-354,555,+ -759,848,-712,367,44,-309,580,-726,338,-731,638,-664,701,-710,+ 296,-167,-313,104,-610,611,-505,452,-462,188,-105,202,-172,-107,+ -214,-144,223,-25,-94,-134,-320,-112,-224,-357,245,-313,123,-37,+ 217,99,8,-237,288,-168,387,-121,257,74,-83,325,87,119,+ 41,-113,206,-196,241,-590,565,-834,867,-692,353,-316,-262,-14,+ -389,121,-242,62,-571,232,-686,138,-590,-110,-301,-75,-251,68,+ -22,-55,74,-25,487,-191,471,-710,577,-821,827,-562,608,-425,+ 124,-5,-8,339,-537,706,-40,118,36,-160,406,-180,727,-1170,+ 942,-615,-9,1455,-1835,1782,-1306,184,231,-626,175,221,-311,190,+ 488,-994,542,-948,523,-551,887,-1205,929,-132,-630,1305,-1628,1296,+ -649,-445,1251,-1245,825,-376,-957,1272,-1076,441,784,-1604,1868,-1520,+ 568,310,-737,787,-381,408,-458,985,-1560,2043,-1777,941,-1,-963,+ 1374,-1419,868,104,212,-96,616,-1266,1738,-1526,830,287,-1023,1575,+ -1584,1038,-176,-1147,1920,-2417,2738,-2425,1404,-1265,1364,-1212,1300,-1507,+ 551,539,-1125,1252,-710,-546,859,-2017,915,3,-848,1435,-1381,768,+ 95,-272,-85,133,-303,642,-563,552,-527,1358,-1492,2398,-2401,2417,+ -1984,1258,-851,705,-528,228,115,-779,1658,-1959,1598,-713,-908,1359,+ -1637,578,715,-1454,2126,-1317,-180,1364,-1535,2211,-1236,216,1114,-1689,+ 2382,-2080,1367,-624,-216,185,433,-1069,1788,-1980,1451,-573,-1073,2183,+ -3105,3409,-2526,519,1831,-3132,3764,-3655,2632,-1493,1446,-1150,933,-574,+ -296,1393,-1969,1269,-944,-841,1608,-1916,978,-559,-561,1176,-815,326,+ 533,-1654,2467,-2148,2197,-1183,200,311,-720,1879,-2127,3010,-3100,2768,+ -2417,1418,-185,-139,698,-817,601,-119,-260,977,-1599,737,-898,83,+ 900,-698,761,-337,-248,608,-1019,970,-918,300,-124,-548,741,-192,+ 55,195,-588,420,-357,-360,707,-1047,1747,-1385,1585,-1305,555,-996,+ 1058,-1328,1073,-1002,1794,-1860,1631,-268,-77,688,-1377,1628,-1997,1961,+ -1682,775,-206,-556,673,-1090,1247,-1902,2051,-2328,2114,-1671,1279,-695,+ 157,487,-731,987,-586,679,-886,849,-879,1156,-1634,1771,-1664,1696,+ -2209,1983,-1553,1327,-584,84,-115,-943,1809,-1491,1293,-1263,1184,-529,+ 685,-749,-63,-673,291,-251,335,-130,332,55,137,-847,795,-954,+ 886,-802,542,-760,422,-590,255,-238,828,-1203,1256,-780,889,-1130,+ 956,-1172,595,-91,-77,1216,-1426,1649,-1516,2174,-1982,2583,-2444,1648,+ -1018,1122,-862,-27,381,-898,1126,-1054,753,-597,-344,218,-1355,811,+ -1772,940,-1469,890,-772,328,-347,514,110,-628,115,-203,483,567,+ -299,-154,-150,251,-204,-137,-130,-474,661,-751,836,-539,894,-896,+ 320,-1360,586,-437,1527,-1072,822,-639,939,-136,59,122,-633,311,+ 259,-304,19,-656,388,-1301,42,-631,144,-1088,512,-236,790,-44,+ 690,-197,273,-138,460,541,-749,487,-318,676,-287,446,-303,-50,+ 412,163,-594,51,1,-143,172,-178,-385,-465,-93,217,-456,-55,+ -842,248,-733,225,-441,-228,536,224,205,-373,-147,-124,692,256,+ 384,-502,727,-320,135,218,238,-58,-311,487,-62,501,16,-82,+ -559,-447,528,498,717,-275,73,118,269,-37,414,-636,-682,-149,+ -552,-203,-699,-804,-640,-891,-498,-112,-674,-588,-582,-488,20,54,+ -233,-156,-115,-19,395,178,16,390,320,325,726,1019,1260,942,+ 1100,964,603,1005,977,606,631,401,859,1152,1172,1768,1130,989,+ 987,673,778,20,-766,-795,-976,-759,-859,-1519,-1921,-2205,-3061,-2914,+ -3700,-4697,+};+static const unsigned char CLIP14[6344] = {+ 26,69,223,163,163,66,134,129,1,66,247,129,1,66,242,129,4,66,243,129,+ 8,66,130,136,109,97,116,114,111,115,107,97,66,135,129,4,66,133,129,2,+ 24,83,128,103,1,0,0,0,0,0,24,148,17,77,155,116,192,191,132,36,+ 130,93,149,77,187,139,83,171,132,21,73,169,102,83,172,129,161,77,187,139,+ 83,171,132,22,84,174,107,83,172,129,239,77,187,140,83,171,132,18,84,195,+ 103,83,172,130,14,126,77,187,140,83,171,132,28,83,187,107,83,172,130,24,+ 120,236,1,0,0,0,0,0,0,83,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,21,73,169,102,201,191,132,+ 112,159,27,119,42,215,177,131,15,66,64,77,128,140,76,97,118,102,54,49,+ 46,55,46,49,48,51,87,65,140,76,97,118,102,54,49,46,55,46,49,48,+ 51,115,164,144,58,177,83,55,225,240,30,245,235,84,179,146,77,14,146,231,+ 68,137,136,64,122,0,0,0,0,0,0,22,84,174,107,77,137,191,132,82,+ 41,24,152,174,1,0,0,0,0,0,13,122,215,129,1,115,197,136,216,182,+ 17,111,101,251,90,4,156,129,0,34,181,156,131,117,110,100,136,129,0,134,+ 136,65,95,86,79,82,66,73,83,131,129,2,225,145,159,129,1,181,136,64,+ 207,64,0,0,0,0,0,98,100,129,32,85,238,129,0,99,162,77,55,2,+ 30,93,1,118,111,114,98,105,115,0,0,0,0,1,128,62,0,0,0,0,+ 0,0,128,187,0,0,0,0,0,0,169,1,3,118,111,114,98,105,115,52,+ 0,0,0,88,105,112,104,46,79,114,103,32,108,105,98,86,111,114,98,105,+ 115,32,73,32,50,48,50,48,48,55,48,52,32,40,82,101,100,117,99,105,+ 110,103,32,69,110,118,105,114,111,110,109,101,110,116,41,1,0,0,0,21,+ 0,0,0,101,110,99,111,100,101,114,61,76,97,118,99,54,49,46,49,57,+ 46,49,48,49,1,5,118,111,114,98,105,115,34,66,67,86,1,0,64,0,+ 0,24,66,16,42,5,173,99,142,58,200,21,33,140,25,162,160,66,202,41,+ 199,29,66,208,33,163,36,67,136,58,198,53,199,24,99,71,185,100,138,66,+ 201,129,208,144,85,0,0,64,0,0,164,28,87,80,114,73,45,231,156,115,+ 163,24,87,204,113,232,32,231,156,115,229,32,103,204,113,9,37,231,156,115,+ 142,57,231,146,114,142,49,231,156,115,163,24,87,14,114,41,45,231,156,115,+ 129,20,71,138,113,167,24,231,156,115,164,28,71,138,113,168,24,231,156,115,+ 109,49,183,146,114,206,57,231,156,115,230,32,135,82,114,174,53,231,156,115,+ 164,24,103,14,114,11,37,231,156,115,198,32,103,204,113,235,32,231,156,115,+ 140,53,183,212,114,206,57,231,156,115,206,57,231,156,115,206,57,231,156,115,+ 140,49,231,156,115,206,57,231,156,115,110,49,231,22,115,174,57,231,156,115,+ 206,57,231,28,115,206,57,231,156,115,32,52,100,21,0,144,0,0,160,161,+ 40,138,226,40,14,16,26,178,10,0,200,0,0,16,64,113,20,71,145,20,+ 75,177,28,203,209,36,13,8,13,89,5,0,0,1,0,8,0,0,160,72,+ 134,164,72,138,165,88,142,102,105,158,38,122,162,40,154,162,42,171,178,105,+ 202,178,44,203,178,235,186,46,16,26,178,10,0,72,0,0,80,81,20,197,+ 112,20,7,8,13,89,5,0,100,0,0,8,96,40,138,163,56,142,228,88,+ 146,165,89,158,7,132,134,172,2,0,128,0,0,4,0,0,80,12,71,177,+ 20,77,241,36,207,242,60,207,243,60,207,243,60,207,243,60,207,243,60,207,+ 243,60,207,243,60,13,8,13,89,5,0,32,0,0,0,130,40,100,24,3,+ 66,67,86,1,0,64,0,0,8,33,26,25,67,157,82,18,92,10,22,66,+ 28,17,67,29,66,206,67,169,165,131,224,41,133,37,99,210,83,172,65,8,+ 33,124,239,61,247,222,123,239,129,208,144,85,0,0,16,0,0,97,20,56,+ 136,129,199,36,8,33,132,98,20,39,68,113,166,32,8,33,132,229,36,88,+ 202,121,232,36,8,221,131,16,66,184,156,123,203,185,247,222,123,32,52,100,+ 21,0,0,8,0,192,32,132,16,66,8,33,132,16,66,8,41,164,148,82,+ 72,41,166,152,98,138,41,199,28,115,204,49,199,32,131,12,50,232,160,147,+ 78,58,201,164,146,78,58,202,36,163,142,82,107,41,181,20,83,76,177,229,+ 22,99,173,181,214,156,115,175,65,41,99,140,49,198,24,99,140,49,198,24,+ 99,140,49,198,24,35,8,13,89,5,0,128,0,0,16,6,25,100,144,65,+ 8,33,132,20,82,72,41,166,152,114,204,49,199,28,3,66,67,86,1,0,+ 128,0,0,2,0,0,0,28,69,82,36,71,114,36,71,146,36,201,146,44,+ 73,147,60,203,179,60,203,179,60,77,212,68,77,21,85,213,85,109,215,246,+ 109,95,246,109,223,213,101,223,246,101,219,213,101,93,150,101,221,181,109,93,+ 214,93,93,215,117,93,215,117,93,215,117,93,215,117,93,215,117,93,215,129,+ 208,144,85,0,128,4,0,128,142,228,56,142,228,56,142,228,72,142,164,72,+ 10,16,26,178,10,0,144,1,0,16,0,128,163,56,138,227,72,142,228,88,+ 142,37,89,146,38,105,150,103,121,150,167,121,154,168,137,30,16,26,178,10,+ 0,0,4,0,16,0,0,0,0,0,128,162,40,138,163,56,142,36,89,150,+ 166,105,158,167,122,162,40,154,170,170,138,166,169,170,170,106,154,166,105,154,+ 166,105,154,166,105,154,166,105,154,166,105,154,166,105,154,166,105,154,166,105,+ 154,166,105,154,166,105,154,166,105,2,161,33,171,0,0,9,0,0,29,199,+ 113,28,71,113,28,199,113,36,71,146,36,32,52,100,21,0,32,3,0,32,+ 0,0,67,81,28,69,114,44,199,146,52,75,179,60,203,211,68,207,244,92,+ 81,54,117,83,87,109,32,52,100,21,0,0,8,0,32,0,0,0,0,0,+ 0,199,115,60,199,115,60,201,147,60,203,115,60,199,147,60,73,211,52,77,+ 211,52,77,211,52,77,211,52,77,211,52,77,211,52,77,211,52,77,211,52,+ 77,211,52,77,211,52,77,211,52,77,211,52,77,211,52,77,211,52,77,3,+ 66,67,86,2,0,100,0,0,16,147,144,74,78,177,87,70,41,198,36,180,+ 94,42,164,20,147,212,123,168,152,98,76,58,237,169,66,6,41,7,185,135,+ 74,33,165,160,211,222,50,165,144,82,12,123,167,152,66,200,24,234,161,131,+ 144,49,133,176,215,218,115,207,189,247,30,8,13,89,17,0,68,1,0,0,+ 198,32,198,16,99,200,49,38,37,131,18,49,199,36,100,82,34,231,156,148,+ 78,74,38,165,164,86,90,204,164,132,152,74,139,145,115,78,74,39,37,147,+ 82,90,11,169,101,146,74,107,37,166,2,0,0,2,28,0,0,2,44,132,+ 66,67,86,4,0,81,0,0,136,49,72,41,164,20,82,74,49,167,152,67,+ 74,41,199,148,99,72,41,229,156,114,78,57,199,152,116,16,42,231,24,116,+ 14,74,164,148,114,142,57,167,156,115,18,50,7,149,115,14,66,38,157,0,+ 0,128,0,7,0,128,0,11,161,208,144,21,1,64,156,0,0,128,144,115,+ 138,49,8,17,99,16,66,9,41,133,80,82,170,156,147,210,65,73,169,131,+ 146,82,73,169,197,146,82,140,149,115,82,58,9,41,117,18,82,42,41,197,+ 88,82,138,45,164,84,99,105,45,215,210,82,141,45,198,156,91,140,189,134,+ 148,98,45,169,213,90,90,171,185,197,88,115,139,53,247,200,57,74,157,148,+ 214,58,41,173,165,214,106,77,173,213,218,73,105,45,164,214,98,105,45,198,+ 214,98,205,41,198,156,51,41,173,133,150,98,43,169,197,216,98,203,53,181,+ 152,115,105,45,215,20,99,207,41,198,158,107,172,185,199,156,131,48,173,213,+ 156,90,203,57,197,152,123,204,177,231,152,115,15,146,115,148,58,41,173,117,+ 82,90,75,173,213,154,90,171,53,147,210,90,105,173,198,144,90,139,45,198,+ 156,91,139,49,103,82,90,44,169,197,88,90,138,49,197,152,115,139,45,215,+ 208,90,174,41,198,156,83,139,57,199,90,131,146,177,246,94,90,171,57,197,+ 152,123,138,173,231,152,115,48,54,199,158,59,74,185,150,214,122,46,173,245,+ 94,115,46,66,214,220,139,104,45,231,212,106,15,42,198,158,115,206,193,216,+ 220,131,16,173,229,156,106,236,61,197,216,123,238,57,24,219,115,240,173,214,+ 224,91,205,69,200,156,131,208,185,248,166,123,48,70,213,218,131,204,181,8,+ 153,115,16,58,232,34,116,240,201,120,148,106,46,173,229,92,90,235,61,214,+ 26,124,205,57,8,209,90,238,41,198,222,83,139,189,215,158,155,176,189,7,+ 33,90,203,61,197,216,131,138,49,248,154,115,48,58,231,98,84,173,193,199,+ 156,131,144,181,22,161,123,47,74,231,32,148,170,181,7,153,107,80,50,215,+ 34,116,240,197,232,160,139,47,0,0,96,192,1,0,32,192,132,50,80,104,+ 200,138,0,32,78,0,128,65,200,57,165,24,132,74,41,8,161,132,148,66,+ 40,41,85,140,73,200,152,131,146,49,39,165,148,82,90,8,37,181,138,49,+ 8,153,99,82,50,199,164,132,18,90,42,37,180,18,74,105,169,148,210,90,+ 40,165,181,150,90,140,41,181,22,67,41,169,133,82,90,43,165,180,150,90,+ 170,49,181,86,99,196,152,148,204,57,41,153,99,82,74,41,173,149,82,90,+ 171,28,147,146,49,40,169,131,144,74,41,41,197,82,82,139,149,115,82,50,+ 232,168,116,16,74,42,169,196,84,82,105,173,164,210,82,41,165,197,146,82,+ 108,41,197,84,91,139,181,134,82,90,44,169,196,86,82,106,49,181,84,91,+ 139,49,215,136,49,41,25,115,82,50,231,164,148,82,82,43,165,180,150,57,+ 39,165,131,142,74,230,160,164,146,82,107,165,164,20,51,230,164,116,14,74,+ 202,32,163,82,82,138,45,165,18,83,40,165,181,146,82,108,165,164,214,90,+ 140,181,166,212,90,45,37,181,86,82,106,177,148,18,91,139,49,215,22,75,+ 77,157,148,214,74,42,49,134,82,90,107,49,230,154,90,139,49,148,18,91,+ 41,41,198,146,74,108,173,197,154,91,108,57,134,82,90,44,169,196,86,74,+ 106,177,213,150,99,107,177,230,212,82,141,41,181,154,91,108,185,198,148,83,+ 143,181,246,156,90,171,53,181,84,99,107,177,230,88,91,111,181,214,156,59,+ 41,173,133,82,90,43,37,197,152,90,139,177,197,88,115,40,37,182,146,82,+ 108,165,164,24,91,108,185,182,22,99,15,161,180,88,74,106,177,164,18,99,+ 107,49,230,24,91,142,169,181,90,91,108,185,166,212,98,173,181,246,28,91,+ 110,61,165,22,107,139,177,230,210,82,141,53,215,222,99,77,57,21,0,0,+ 48,224,0,0,16,96,66,25,40,52,100,37,0,16,5,0,0,24,195,24,+ 99,16,26,165,156,115,78,74,131,148,115,206,73,201,156,131,16,66,74,153,+ 115,16,66,72,41,115,78,66,74,45,101,206,65,72,169,181,80,74,74,173,+ 197,22,74,73,169,181,22,11,0,0,40,112,0,0,8,176,65,83,98,113,+ 128,66,67,86,2,0,81,0,0,136,49,74,49,6,161,49,70,41,231,32,+ 52,198,40,197,24,132,74,41,198,156,147,80,41,197,152,115,80,50,199,156,+ 131,80,74,230,156,115,16,74,9,33,148,82,74,74,33,132,82,74,73,169,+ 0,0,128,2,7,0,128,0,27,52,37,22,7,40,52,100,69,0,16,5,+ 0,0,24,99,156,51,206,33,10,157,165,206,82,36,169,163,214,81,107,40,+ 165,26,75,140,157,198,86,123,235,185,211,26,123,109,185,55,148,74,141,169,+ 214,142,107,203,185,213,222,105,77,61,183,28,11,0,0,59,112,0,0,59,+ 176,16,10,13,89,9,0,228,1,0,16,198,40,197,152,115,206,25,133,24,+ 115,206,57,231,12,82,140,57,231,156,115,138,49,231,156,131,16,66,197,152,+ 115,206,65,8,33,115,206,57,8,161,132,146,57,231,28,132,16,74,232,156,+ 131,80,74,41,165,116,206,65,8,161,148,82,58,231,32,132,82,74,41,157,+ 115,16,74,41,165,148,2,0,128,10,28,0,0,2,108,20,217,156,96,36,+ 168,208,144,149,0,64,30,0,0,96,12,66,206,73,105,173,97,204,57,8,+ 45,213,216,48,198,28,148,148,98,139,156,131,144,82,139,185,70,204,65,72,+ 41,198,160,59,40,41,181,24,108,240,157,132,148,90,139,57,7,147,82,139,+ 53,231,222,131,72,169,181,154,131,206,61,213,86,115,207,189,247,156,98,172,+ 53,231,222,115,47,0,0,119,193,1,0,236,192,70,145,205,9,70,130,10,+ 13,89,9,0,228,1,0,16,8,41,197,152,115,206,25,165,24,115,204,57,+ 231,140,82,140,49,230,156,115,138,49,198,156,115,206,65,197,24,99,206,57,+ 7,33,99,204,57,231,32,132,144,49,230,156,115,16,66,232,156,115,14,66,+ 8,33,116,206,57,7,33,132,16,58,231,160,131,16,66,8,157,115,16,66,+ 8,33,132,2,0,128,10,28,0,0,2,108,20,217,156,96,36,168,208,144,+ 149,0,64,56,0,0,0,33,132,16,66,8,33,132,16,66,8,33,132,16,+ 66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,+ 66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,+ 66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,+ 66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,+ 66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,+ 66,8,33,132,16,66,232,156,115,206,57,231,156,115,206,57,231,156,115,206,+ 57,231,156,115,206,57,39,0,200,183,194,1,192,255,193,198,25,86,146,206,+ 10,71,131,11,13,89,9,0,132,3,0,0,10,65,40,165,98,16,74,41,+ 37,146,78,58,41,157,147,80,74,41,145,131,82,74,233,164,148,82,74,9,+ 165,148,82,74,8,165,148,82,74,8,29,148,82,66,41,165,148,82,74,41,+ 165,148,82,74,41,165,148,82,58,41,165,148,82,74,41,165,148,202,57,41,+ 165,147,82,74,41,165,68,206,73,41,33,148,82,74,41,165,132,82,74,41,+ 165,148,82,74,41,165,148,82,74,41,165,148,82,74,41,165,132,16,66,8,+ 33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,33,132,16,66,8,+ 33,132,16,66,8,33,132,16,66,8,33,132,2,0,184,27,28,0,32,18,+ 108,156,97,37,233,172,112,52,184,208,144,149,0,64,72,0,0,160,20,115,+ 142,74,8,41,148,144,82,168,152,162,142,66,41,41,164,82,74,10,17,99,+ 206,73,234,28,133,80,82,40,169,131,202,57,8,165,164,148,66,42,33,117,+ 206,65,7,37,133,144,82,9,33,149,142,58,232,40,148,80,82,42,37,148,+ 210,57,40,165,132,20,74,74,41,149,144,66,72,169,116,148,82,40,37,149,+ 148,66,42,33,149,82,74,72,37,149,16,74,10,157,164,84,74,10,169,164,+ 84,82,8,157,116,144,66,39,37,164,146,74,10,169,147,148,82,42,37,165,+ 148,74,74,37,116,82,66,42,41,165,16,66,74,169,148,16,74,72,41,165,+ 78,82,73,169,164,20,66,40,33,133,148,82,74,37,165,146,74,74,33,149,+ 84,66,9,165,164,148,82,40,161,164,84,82,74,41,165,146,82,41,0,0,+ 224,192,1,0,32,192,8,58,201,168,178,8,27,77,184,240,0,20,26,178,+ 18,0,32,3,0,64,148,116,214,105,167,73,34,8,49,69,153,39,13,41,+ 198,32,181,164,44,195,16,83,146,137,241,20,99,140,57,40,70,67,14,49,+ 228,148,24,23,74,8,161,131,98,60,38,149,67,202,80,81,185,183,212,57,+ 5,197,22,99,124,239,177,23,1,0,0,8,2,0,4,132,4,0,24,32,+ 40,152,1,0,6,7,8,35,7,2,29,1,4,14,109,0,128,129,8,153,+ 9,12,10,161,193,65,38,0,60,64,68,72,5,0,137,9,138,210,133,46,+ 8,33,130,116,17,100,241,192,133,19,55,158,184,225,132,14,109,16,0,0,+ 0,0,0,16,0,240,1,0,144,80,0,17,17,209,204,85,88,92,96,100,+ 104,108,112,116,120,124,128,132,4,0,0,0,0,0,8,0,124,0,0,36,+ 34,64,68,68,52,115,21,22,23,24,25,26,27,28,29,30,31,32,33,1,+ 0,0,0,0,0,0,0,0,64,64,64,0,0,0,0,0,32,0,0,0,+ 64,64,18,84,195,103,64,132,191,132,166,163,28,232,115,115,159,99,192,128,+ 103,200,153,69,163,135,69,78,67,79,68,69,82,68,135,140,76,97,118,102,+ 54,49,46,55,46,49,48,51,115,115,217,99,192,139,99,197,136,216,182,17,+ 111,101,251,90,4,103,200,164,69,163,135,69,78,67,79,68,69,82,68,135,+ 151,76,97,118,99,54,49,46,49,57,46,49,48,49,32,108,105,98,118,111,+ 114,98,105,115,103,200,161,69,163,136,68,85,82,65,84,73,79,78,68,135,+ 147,48,48,58,48,48,58,48,48,46,52,49,54,48,48,48,48,48,48,0,+ 31,67,182,117,73,106,191,132,209,214,152,8,231,129,0,163,231,129,0,0,+ 128,132,55,84,214,107,91,123,194,2,219,81,43,254,225,165,73,199,59,255,+ 177,142,92,108,248,220,194,47,190,111,157,53,42,103,38,244,151,246,191,253,+ 122,52,216,98,157,175,86,204,15,163,116,240,254,245,20,31,12,52,155,217,+ 124,213,130,55,49,61,0,108,76,74,108,80,52,97,57,83,159,217,106,221,+ 120,93,234,184,206,254,16,152,33,245,240,172,254,226,249,255,237,73,69,8,+ 163,234,129,0,16,128,108,55,161,23,122,185,97,91,102,45,156,0,118,0,+ 202,77,218,213,101,69,253,18,95,250,235,163,23,215,61,208,26,188,122,71,+ 106,43,138,182,13,202,252,25,202,139,109,105,2,168,136,55,62,26,16,73,+ 52,29,250,27,253,21,193,95,241,24,43,162,18,56,128,149,186,241,56,249,+ 13,78,205,21,102,125,101,250,116,253,16,64,168,208,182,72,148,253,103,218,+ 154,15,145,197,246,115,134,51,163,240,129,0,32,128,84,57,173,95,196,231,+ 43,108,3,213,6,0,121,123,178,195,1,148,43,211,163,131,225,52,72,51,+ 184,160,125,106,17,125,16,158,6,161,73,140,116,76,125,210,246,246,214,124,+ 240,156,17,187,3,77,148,193,202,241,214,218,39,247,99,39,105,109,235,17,+ 164,229,116,190,144,144,98,0,138,137,92,148,41,150,147,43,91,199,191,31,+ 77,36,27,5,195,255,160,162,48,146,70,106,154,75,246,147,198,188,151,132,+ 29,0,163,231,129,0,48,128,100,61,201,125,158,220,165,216,36,112,75,0,+ 11,112,19,32,167,71,111,6,7,162,242,243,211,168,239,230,186,182,125,122,+ 254,225,142,172,171,233,160,205,106,238,30,202,201,103,124,161,102,83,136,89,+ 34,94,187,165,161,5,49,81,16,118,254,30,81,157,58,28,149,25,167,177,+ 55,135,201,251,240,7,195,81,212,188,84,134,90,79,80,38,202,98,53,84,+ 119,20,153,107,103,239,108,163,237,129,0,64,128,124,55,87,132,85,29,204,+ 93,12,80,23,32,247,47,220,158,47,25,251,125,105,107,172,116,244,135,107,+ 94,220,255,142,199,116,192,60,207,243,188,136,55,219,109,132,19,219,79,30,+ 150,100,16,236,185,248,237,122,27,161,120,100,102,217,228,253,218,250,243,197,+ 83,103,41,38,231,230,40,198,29,31,219,69,158,63,255,247,241,209,7,163,+ 226,183,46,233,110,200,197,199,241,41,116,23,40,183,238,120,200,170,163,64,+ 146,129,0,80,128,218,234,163,17,136,91,53,96,189,27,56,44,160,93,192,+ 9,48,1,46,128,214,122,150,199,137,166,55,195,60,249,125,57,54,244,252,+ 240,245,173,55,155,159,43,148,62,150,226,231,139,15,8,239,36,28,202,88,+ 110,144,202,237,65,26,233,151,65,192,237,251,183,25,137,90,40,39,92,175,+ 15,134,190,116,78,63,140,158,28,125,172,62,144,245,246,36,34,99,103,198,+ 231,182,196,149,206,45,166,171,191,16,46,214,106,185,203,76,77,98,243,172,+ 73,254,79,20,105,18,196,173,100,21,47,19,138,162,227,253,116,115,103,173,+ 14,65,134,226,21,62,1,163,64,136,129,0,104,128,254,186,163,25,104,39,+ 5,140,110,55,216,95,42,120,160,125,0,4,180,29,46,219,158,0,224,121,+ 250,75,2,204,165,32,173,245,161,95,17,212,139,15,14,129,45,107,81,12,+ 64,195,100,32,223,133,72,135,207,58,160,6,115,208,178,187,71,43,94,204,+ 204,117,217,12,233,173,41,22,53,221,172,139,85,187,155,92,107,77,237,11,+ 23,247,186,229,235,108,248,201,239,139,81,225,63,140,82,155,172,119,1,214,+ 249,45,115,151,200,4,103,62,186,88,163,158,154,33,44,123,70,13,175,166,+ 183,141,251,254,48,5,163,64,141,129,0,136,128,62,170,227,26,224,158,12,+ 145,159,223,13,116,155,50,225,220,227,183,206,165,102,189,203,172,93,0,88,+ 111,171,167,173,40,197,199,147,223,26,217,175,238,111,225,68,46,236,107,240,+ 97,255,84,190,47,134,76,172,166,198,52,213,15,136,130,49,131,222,161,190,+ 204,98,12,35,49,53,168,179,192,214,116,171,229,21,245,200,204,166,207,129,+ 114,186,245,178,134,49,156,37,107,243,168,249,9,80,28,152,25,17,142,168,+ 222,106,42,76,61,177,72,226,96,91,232,251,221,20,134,97,237,172,119,251,+ 26,103,248,129,10,39,93,22,173,3,163,64,130,129,0,168,128,214,74,75,+ 49,161,40,21,234,180,215,186,171,10,0,208,108,21,153,145,228,128,49,0,+ 34,146,84,147,131,248,238,107,56,37,203,38,88,247,158,12,251,30,143,22,+ 142,14,236,143,10,163,31,208,167,179,157,127,188,232,45,248,244,93,33,175,+ 94,119,46,44,114,145,137,27,121,94,27,119,62,66,106,111,150,217,166,50,+ 102,33,37,88,108,180,74,233,181,121,68,7,102,164,70,75,44,174,231,49,+ 202,248,227,117,210,94,223,35,58,35,123,69,182,156,40,252,124,94,154,245,+ 66,84,167,163,201,129,0,200,128,100,57,167,7,122,241,171,194,0,228,200,+ 128,235,249,84,182,19,65,184,23,188,115,20,165,181,167,219,247,52,114,248,+ 42,196,69,62,215,201,125,90,182,78,130,176,61,132,147,130,240,87,244,234,+ 218,172,188,178,178,241,241,33,102,5,231,34,127,103,116,239,59,2,163,225,+ 129,0,224,128,44,65,245,164,151,87,2,0,181,105,170,52,189,120,176,122,+ 219,162,236,202,182,70,24,159,70,241,177,146,253,16,31,246,161,154,105,141,+ 34,22,223,66,19,198,81,78,15,146,214,160,169,110,242,245,75,128,8,10,+ 136,115,14,52,249,208,209,194,60,174,143,208,38,179,108,19,118,81,29,72,+ 62,127,156,139,129,227,12,156,135,252,198,70,49,144,106,118,48,163,230,129,+ 0,240,128,28,79,217,224,69,31,0,92,211,248,215,27,255,240,186,179,161,+ 31,179,131,53,109,124,43,185,77,183,64,152,231,100,219,83,92,142,71,92,+ 109,239,221,222,151,199,110,176,214,30,60,90,114,122,122,142,122,102,79,230,+ 225,52,231,89,111,146,55,98,50,217,121,59,191,29,159,73,122,52,247,248,+ 3,186,109,64,94,254,247,229,161,231,205,142,161,186,142,143,48,39,67,31,+ 49,163,238,129,1,0,128,252,84,0,248,184,131,4,0,84,229,3,0,200,+ 209,119,249,132,214,183,117,92,19,40,59,7,241,193,90,251,213,185,53,146,+ 67,109,163,57,228,70,255,24,72,52,87,239,135,222,254,217,34,62,231,113,+ 121,51,34,115,203,144,239,27,131,233,48,171,162,127,28,189,151,40,242,148,+ 51,205,85,159,180,242,163,209,146,251,180,101,230,60,223,106,108,98,151,167,+ 138,152,130,241,75,102,141,147,207,253,223,27,206,163,234,129,1,16,128,4,+ 85,137,189,22,160,113,11,176,156,240,144,95,12,173,92,116,211,186,176,123,+ 92,228,161,52,157,190,90,187,183,47,127,135,174,211,167,230,124,250,239,122,+ 26,110,65,142,107,120,227,85,36,255,98,189,69,15,145,179,164,54,19,176,+ 232,123,183,245,82,251,177,16,133,38,75,155,59,63,127,71,231,215,121,201,+ 152,121,153,10,121,234,16,57,218,194,154,62,191,11,107,21,58,79,16,5,+ 0,163,237,129,1,32,128,60,85,213,132,243,9,0,191,0,11,208,68,79,+ 79,254,238,237,184,116,57,52,26,108,245,47,132,117,183,182,135,36,226,12,+ 115,112,233,38,249,207,109,126,18,188,175,186,149,126,180,49,106,41,203,112,+ 252,100,208,183,210,31,23,57,24,247,82,182,80,216,47,88,116,33,201,179,+ 181,41,109,169,101,255,66,120,189,127,58,217,121,80,126,218,132,86,105,84,+ 216,107,23,186,202,126,22,243,20,81,18,23,163,235,129,1,48,128,116,67,+ 10,159,54,207,94,2,32,47,11,80,23,128,120,53,216,232,167,139,118,231,+ 56,201,122,248,127,28,134,90,210,135,144,126,204,152,254,195,72,27,237,178,+ 106,252,25,13,169,56,52,46,253,9,121,94,30,163,79,175,174,126,216,210,+ 224,235,179,53,9,83,9,110,176,52,61,183,8,217,217,132,207,98,196,202,+ 147,208,204,209,116,139,106,182,162,79,43,58,110,81,180,201,9,108,76,48,+ 0,163,232,129,1,64,128,100,65,165,125,63,78,114,241,144,0,43,11,144,+ 54,176,27,85,213,78,137,124,110,237,114,235,195,98,50,244,66,255,180,173,+ 121,190,178,42,85,43,19,78,175,239,218,99,122,90,135,89,133,255,205,31,+ 194,220,170,173,220,202,52,103,171,188,240,86,152,57,59,67,180,110,103,81,+ 23,162,84,24,250,249,52,99,118,236,106,167,190,216,240,163,184,130,172,95,+ 83,189,23,91,200,0,0,163,236,129,1,80,128,108,57,10,182,243,253,56,+ 68,27,201,48,64,102,1,154,6,91,154,247,163,61,79,237,221,123,117,187,+ 239,231,131,74,191,196,252,62,58,108,155,27,155,61,200,135,138,43,204,81,+ 100,81,129,73,222,230,16,173,174,137,158,21,111,193,186,19,224,208,92,150,+ 149,2,173,207,80,209,150,213,44,53,49,172,73,42,144,19,75,151,245,171,+ 117,214,63,58,241,244,146,64,238,180,77,92,108,145,162,137,1,163,234,129,+ 1,96,128,100,45,213,131,61,141,6,128,164,236,107,209,161,250,142,179,196,+ 191,62,107,27,142,239,93,180,157,202,173,253,198,21,175,105,186,134,183,125,+ 126,224,115,251,233,82,218,117,250,236,191,151,218,200,52,83,223,59,15,39,+ 166,117,128,173,241,151,53,137,203,163,156,167,173,56,9,203,147,235,197,157,+ 115,118,66,167,0,248,253,34,194,18,58,239,39,66,62,45,247,231,2,9,+ 231,20,128,36,0,163,64,133,129,1,112,128,154,120,2,3,220,6,192,197,+ 111,109,26,43,245,182,109,219,206,93,210,207,222,101,145,194,124,250,217,200,+ 196,96,229,254,228,79,200,184,175,61,79,247,73,172,255,69,188,10,59,11,+ 169,40,1,59,162,127,163,23,201,137,186,194,173,111,125,113,34,142,177,221,+ 16,98,198,198,139,151,192,99,241,220,242,105,204,36,65,32,17,192,248,196,+ 47,231,133,69,235,234,170,156,53,96,150,146,29,200,137,241,4,102,235,185,+ 97,225,54,64,85,153,105,252,88,226,16,98,252,244,204,68,31,72,69,9,+ 10,163,241,129,1,136,128,254,246,65,2,40,131,6,6,117,177,215,4,85,+ 103,176,157,132,199,248,147,120,51,178,87,51,177,150,150,78,167,19,97,255,+ 244,41,82,207,168,72,235,204,85,146,27,22,0,121,225,176,96,97,193,226,+ 126,241,205,120,146,169,226,92,188,234,253,40,64,4,152,204,207,63,64,211,+ 151,122,190,160,200,0,144,42,227,152,82,183,111,26,79,221,80,125,142,159,+ 113,241,116,25,10,184,78,252,94,211,243,92,47,158,94,0,28,83,187,107,+ 151,191,132,125,61,20,66,187,143,179,129,0,183,138,247,129,1,241,130,15,+ 8,240,129,9,+};+static const int REF14[1632] = {+ -479,162,63,-515,-694,-245,-411,-106,361,565,299,828,1067,1150,+ 1887,2559,2818,2866,1390,-1481,-2807,-4859,-8436,-10315,-7914,-1760,1959,3560,+ 3303,2534,1131,2000,4474,6066,7508,7929,6448,2140,-1114,-4760,-7294,-12588,+ -13280,-6113,-349,5094,6157,1933,-734,-2130,1206,2955,4885,5865,6504,7388,+ 7013,5213,630,-1333,-3040,-6585,-15977,-17891,-6466,262,5050,4195,1902,-1594,+ -2491,229,3435,5939,5813,5081,6652,7589,4914,2015,-124,-1441,-8552,-14937,+ -18493,-6324,782,8355,3457,-67,-4540,-3846,1437,4227,6389,4862,4298,5135,+ 7307,5456,4332,208,-2477,-8535,-16785,-17069,-5208,2108,9261,1758,-1080,-4747,+ -3153,2075,6518,6796,3210,3302,4305,8133,9390,4412,-1965,-4461,-7355,-12757,+ -17387,-6035,627,8796,1543,-2319,-5521,-2248,2809,5482,6612,3644,2500,5113,+ 9648,9691,5230,-921,-4510,-7945,-13914,-16210,-7904,-1032,8018,3185,-1138,-4476,+ -3985,1898,6075,6802,3167,2410,5541,10091,10858,4636,-791,-5070,-6511,-12180,+ -15625,-9960,-1902,7701,1973,122,-3870,-2240,376,6494,6286,4507,2088,5869,+ 9350,9578,5308,592,-3467,-6171,-12642,-15616,-11484,-3168,6892,4908,1865,-3358,+ -2120,296,6350,5073,4423,2368,5642,7812,7134,3354,1574,-908,-4287,-12196,+ -16366,-14054,-5477,2998,4189,4055,58,-1731,-2118,747,2612,5048,4523,5526,+ 5223,4048,2290,407,-333,-344,-2105,-5104,-6901,-5299,-4480,-3122,-1658,-1151,+ 171,988,2101,2606,2639,2443,2891,3189,2989,2995,2654,2869,2945,2757,+ 1291,6,-1005,-1705,-2073,-1973,-2167,-2107,-1799,-1071,-18,1349,1860,2367,+ 2338,2212,1754,1494,1539,1238,710,334,-443,-986,-1161,-969,-1128,-668,+ -493,-287,-812,646,-20,815,277,3038,1788,6322,1479,3734,418,856,+ -3985,-6009,-6509,-7044,-3980,-3133,1001,1373,2254,706,1121,2502,3290,4445,+ 4138,3706,2162,-795,-5945,-9721,-16651,-12087,-6923,3293,5400,2881,222,-3862,+ -186,3157,7847,8608,6678,6704,5983,6082,3302,-89,-5523,-12074,-18351,-10254,+ -2910,8729,6245,2382,-1395,-4429,-132,3484,8591,6461,6467,3750,7531,6839,+ 3104,650,-5964,-10581,-20444,-14292,-3677,10287,6850,662,-4528,-3844,1974,6089,+ 9659,7375,4501,3975,6865,7718,3585,355,-8479,-15782,-18757,-7981,2411,9367,+ 4165,-4912,-3608,-1665,6124,8530,5074,4019,1390,5164,9087,7152,2541,-5340,+ -8258,-19571,-16159,-438,7979,4159,-1634,-5765,-2744,4625,6694,8561,955,1342,+ 4728,8251,10851,2318,-3573,-6766,-14806,-17990,-8033,1294,9590,-80,-6262,-2235,+ 1051,7484,8236,3569,1543,2414,8242,11520,4331,-1459,-6186,-11410,-16914,-12755,+ 1437,8927,665,-6019,-6007,855,8289,6745,5178,412,2652,8552,10403,6036,+ -74,-5217,-10048,-15357,-13798,-1464,7090,1615,-4255,-7136,361,7158,6999,6140,+ -206,1354,6461,9519,7310,-341,-5736,-8107,-16023,-13946,-1056,5378,1201,-3995,+ -6347,1240,7860,8472,5533,328,4448,7470,9344,6205,-2398,-6582,-11617,-14607,+ -11690,-464,9005,-1417,-5544,-4644,2448,9372,6187,5349,1887,4263,9024,6811,+ 2443,-5744,-8939,-14424,-14951,-2730,4707,1909,-3022,-5409,2443,6030,7853,6251,+ 2422,4792,7678,8229,3598,-3116,-8380,-14413,-16091,-4507,3150,1196,-2691,-5084,+ -208,4726,6112,7440,3520,4815,8577,7192,5186,-1081,-4470,-11489,-15491,-10223,+ -2749,5718,-1859,-3971,-3373,2531,7473,5597,6032,5430,5350,6903,5085,1967,+ -4831,-8473,-11652,-11250,-5423,2670,658,-2634,-881,-1430,5737,3535,5598,6249,+ 5361,7670,4274,613,-1096,-6464,-8166,-11957,-5836,-614,972,452,-2964,-604,+ 244,2746,5895,6174,5730,6309,2995,1735,-1266,-222,-2923,-8318,-7953,-6155,+ -1898,1109,-731,1232,-159,927,2069,3835,3158,4291,2945,3416,3625,1751,+ 1315,-4602,-6350,-10167,-6046,-2449,-1007,1708,9,757,2380,2278,3846,3788,+ 3745,5908,3345,4736,-1155,-4780,-6468,-10599,-5680,-3373,67,761,-760,1311,+ 1696,3130,4063,5061,4674,5710,3527,1636,-723,-5432,-7451,-6834,-4484,-2676,+ -1921,44,-520,969,2304,2460,3507,3677,4473,4352,4152,2328,69,-3088,+ -6294,-6295,-5540,-4930,-3135,-1118,-385,1914,2930,3561,3433,4974,4866,4939,+ 4082,1958,-1391,-5010,-7102,-7305,-6734,-4217,-2357,229,1429,2919,3577,3891,+ 4960,5471,5297,4554,2617,-810,-4796,-6937,-7201,-6802,-4714,-2582,-297,927,+ 2941,3585,3843,4696,5367,5423,4519,2279,-1185,-4910,-7085,-7187,-6944,-4547,+ -2423,-164,1357,3551,3604,4631,4769,5573,5371,4280,1846,-1714,-5202,-7088,+ -7319,-6597,-4652,-2205,-97,1519,3553,4135,4831,5114,5549,4924,3767,1163,+ -2406,-5686,-7391,-7359,-6484,-4462,-2001,369,2340,4124,4732,5091,5122,5230,+ 4484,3374,630,-3043,-5681,-7344,-7081,-6569,-4102,-1612,651,2930,4211,4770,+ 4994,5099,4860,4042,2428,-413,-3818,-5991,-7133,-6976,-5897,-3376,-1101,1362,+ 3483,5007,5008,5281,5431,4979,3825,1725,-1105,-4175,-6097,-6983,-7079,-5870,+ -3713,-1358,1043,2964,4281,5025,5449,5485,5057,3973,2183,-313,-2971,-4875,+ -5997,-6436,-5725,-4337,-2221,-121,1619,2808,3900,4685,5037,4926,4191,2638,+ 822,-1176,-3021,-4476,-5282,-5266,-4405,-3105,-1346,197,1587,2810,3588,3962,+ 3973,3467,2704,1485,219,-1055,-2217,-3112,-3522,-3403,-3030,-2269,-1159,35,+ 1014,1987,2158,2480,2469,2192,1723,965,20,-427,-992,-1565,-1933,-2057,+ -1656,-1282,-754,-512,-236,275,716,1052,1158,1259,1066,899,693,-3,+ -651,-702,-741,-638,-612,-673,-1052,-1076,-698,-561,-208,203,230,961,+ 1045,223,1189,1371,877,448,282,91,-142,-607,-372,-987,-964,-527,+ -350,-54,-350,-37,222,34,660,1103,1092,493,211,346,125,-369,+ -506,-837,-74,128,-138,-381,-661,-516,-269,193,505,169,379,501,+ 590,-336,-123,-265,-146,132,795,-704,-262,-978,-479,-564,-309,-349,+ 168,630,959,21,44,-409,1090,730,-301,524,409,306,-631,-452,+ -205,556,1260,-869,-468,946,124,-636,1116,-1412,-566,1369,-100,68,+ -568,10,-900,-870,-34,551,-450,-766,-455,19,186,-748,2238,328,+ -551,-122,-1379,-97,-428,-714,1101,-413,855,-770,487,192,220,1140,+ 2468,2904,-1223,-602,-1061,-3464,605,-970,-878,1070,1142,-786,-1087,-1749,+ 1142,-1039,119,4878,1625,-1227,-497,-1546,1174,1750,-935,-1914,2315,-676,+ 576,-321,668,-156,-2424,949,-304,454,-1421,1942,-901,811,3538,-1278,+ -2200,-1029,-584,2627,655,-2602,578,-1807,1179,1106,-2660,-3465,2136,-920,+ -988,901,1238,-1701,-205,-1934,1923,194,-2708,3690,-1307,607,-713,606,+ 2779,706,2198,-1728,854,-725,1224,-1422,1335,-1676,1589,-2670,-1959,2414,+ -2917,-1895,2550,-1864,910,1109,-8,-431,-2106,1166,-643,-91,1932,-268,+ 1547,-787,60,1318,2105,-692,-1233,421,-631,-693,2980,2269,-365,1563,+ -1559,82,1584,528,-1922,-336,-269,1388,1627,433,757,715,-948,-327,+ -705,508,582,24,-2166,-386,516,-1726,376,-359,994,-89,214,-556,+ -209,254,-25,-238,413,-251,-813,114,-869,661,1034,996,671,1260,+ -349,668,465,281,-1452,-1079,-1689,-179,-1242,-726,325,529,207,1872,+ 1584,2341,189,1535,1723,986,-1300,-2129,-4163,-7111,-10557,-8221,-2065,3450,+ 5024,4490,2690,4428,5202,7323,7110,5965,1784,-623,-3863,-8981,-17489,-14661,+ -1798,5721,6560,538,-1020,-1397,4448,7737,9571,7048,7695,4574,-1218,-3733,+ -5054,-8772,-18407,-12729,-761,4212,5061,-1516,-2183,297,4666,6025,7421,7729,+ 7193,2942,1988,-2310,-3163,-9669,-19497,-14190,-228,6999,7206,-2865,-2506,-1706,+ 6017,7140,8423,4216,6002,3534,5072,-785,-2445,-10065,-17667,-17535,-199,7928,+ 8538,-4998,-4328,-3183,6745,9229,8174,3217,5242,4767,5671,2205,-1767,-5665,+ -16926,-19780,-7976,7187,9219,720,-5028,-4198,2391,7918,9171,6654,3959,4834,+ 4574,3811,-222,-4097,-11317,-19606,-13555,1569,8346,7399,-4479,-4791,-2242,4642,+ 8818,8499,3522,3511,4665,5302,4163,-1438,-4525,-14769,-19733,-12069,4408,7911,+ 4287,-5805,-4690,-776,6786,9656,7625,5035,3606,4850,5572,4784,-2704,-5425,+ -13538,-17853,-12602,2350,7088,3813,-3175,-4168,-954,3858,8184,7295,5070,3037,+ 4230,5029,3387,-774,-4410,-8705,-15837,-15316,-4813,2100,5040,2675,767,811,+ 959,4324,4726,5430,5060,4295,4289,1836,-1433,-3637,-3282,-4779,-6342,-5924,+ -4456,-4264,-2864,-736,1062,1771,2278,2505,2720,2966,3567,3858,3931,3411,+ 1951,572,-240,-306,-865,-1690,-2875,-3066,-2804,-1979,-1388,-979,-309,441,+ 1572,2231,2004,1399,1295,796,298,-180,-457,-346,-298,-533,-818,-924,+ -661,-522,-272,-259,-136,202,213,250,6,-93,-195,-477,-752,-664,+ -339,191,434,458,288,-22,22,15,47,141,434,516,190,-86,+ -214,-576,-947,-893,-440,-303,-400,-52,505,619,333,130,194,224,+ 156,97,117,87,12,-213,-493,-613,-498,-229,-112,-106,6,395,+ 795,702,325,-96,-175,34,-9,54,-138,-262,-451,-468,-474,-307,+ -74,136,173,204,407,551,447,173,-44,-134,-14,-27,6,-111,+ -235,-359,-248,-96,27,13,34,19,41,103,209,250,131,-50,+ -128,-67,-29,-108,-278,-204,-54,-12,10,114,244,183,80,107,+ 200,213,123,-62,-189,-214,-225,-260,-344,-408,-275,-122,32,100,+ 182,212,214,265,349,438,325,121,92,66,-32,-241,-321,-360,+ -316,-202,-66,-13,34,113,211,264,394,394,298,164,94,41,+ -40,-113,-216,-194,-223,-249,-291,-207,-94,-11,38,81,147,202,+ 186,147,119,77,20,-24,-42,-66,-71,-66,-58,-43,-30,-20,+ 4,7,21,29,28,23,21,6,8,1,-7,-10,-7,-4,+ 0,10,2,6,8,5,2,6,+};+static const struct clip {+ const char *what;+ const unsigned char *data;+ size_t size;+ const int *ref; /* every 4th sample */+ size_t nref;+ unsigned rate;+ size_t n;+ double tol; /* the largest gap, in 1/32768 */+} CLIPS[] = {+ {"FLAC, stereo, 16 kHz", CLIP0, sizeof CLIP0, REF0, 800, 16000, 3200, 1.5},+ {"MP3, MPEG-2 Layer III, 32 kbit/s", CLIP1, sizeof CLIP1, REF1, 1600, 16000, 6400, 3},+ {"MP3, MPEG-1 Layer III, joint stereo, 44.1 kHz", CLIP2, sizeof CLIP2, REF2, 3308, 44100, 13230, 3},+ {"MPEG-2 Layer II, 22.05 kHz", CLIP3, sizeof CLIP3, REF3, 2304, 22050, 9216, 3},+ {"MPEG-1 Layer I, stereo modes at random", CLIP4, sizeof CLIP4, REF4, 576, 48000, 2304, 3},+ {"Ogg Vorbis, a single page", CLIP5, sizeof CLIP5, REF5, 1536, 16000, 6400, 3},+ {"Ogg FLAC, stereo", CLIP6, sizeof CLIP6, REF6, 800, 16000, 3200, 1.5},+ {"Opus SILK, narrowband 12 kbit/s", CLIP7, sizeof CLIP7, REF7, 4800, 48000, 19200, 12},+ {"Opus CELT, stereo, low delay", CLIP8, sizeof CLIP8, REF8, 3600, 48000, 14400, 12},+ {"Opus hybrid, stereo", CLIP9, sizeof CLIP9, REF9, 3600, 48000, 14400, 12},+ {"M4A, AAC LC, stereo, trimmed by its edit list", CLIP10, sizeof CLIP10, REF10, 1280, 16000, 4800, 3},+ {"M4A, ALAC", CLIP11, sizeof CLIP11, REF11, 1600, 16000, 6400, 1.5},+ {"AAC in ADTS frames", CLIP12, sizeof CLIP12, REF12, 1536, 16000, 6144, 3},+ {"WebM, Opus, stereo", CLIP13, sizeof CLIP13, REF13, 3600, 48000, 14400, 3},+ {"Matroska, Vorbis", CLIP14, sizeof CLIP14, REF14, 1632, 16000, 6528, 3},+};diff --git a/tests/fb/test_oop.bas b/tests/fb/test_oop.basindex df79c84..5be1a94 100644--- a/tests/fb/test_oop.bas+++ b/tests/fb/test_oop.bas@@ -28,8 +28,18 @@ end sub#assert sizeof(janas_llm_chat_params) = 80#assert sizeof(janas_llm_chat_stats) = 192#assert offsetof(janas_llm_params, reserve_bytes) = 48+#assert offsetof(janas_llm_params, min_quality) = 60#assert offsetof(janas_llm_chat_params, max_reasoning) = 72#assert offsetof(janas_llm_chat_stats, reasoning_cut) = 188+#assert sizeof(janas_llm_speech_params) = 24+#assert sizeof(janas_llm_transcribe_params) = 112+#assert offsetof(janas_llm_transcribe_params, speaker_names) = 88+#assert offsetof(janas_llm_transcribe_params, chunking) = 64+#assert offsetof(janas_llm_transcribe_params, seed) = 24+#assert offsetof(janas_llm_transcribe_params, temperature) = 32+#assert offsetof(janas_llm_transcribe_params, words) = 36+#assert offsetof(janas_llm_transcribe_params, on_segment) = 40+#assert offsetof(janas_llm_transcribe_params, stop) = 56#endifcheck(Janas.Model.AbiVersion() = JANAS_LLM_ABI, "the library's ABI")@@ -52,6 +62,9 @@ scope '' the options, with the library's defaults"the default files and GPU")oo.CacheGiB = 2check(oo.cache_bytes = 2147483648ull, "CacheGiB")+ check(oo.min_quality = Janas.Quality.Low, "the default quality")+ oo.min_quality = Janas.Quality.High+ check(oo.min_quality = JANAS_LLM_QUALITY_HIGH, "Quality.High")dim as Janas.ReplyStats stcheck(st.size = sizeof(janas_llm_chat_stats), "ReplyStats.size")check(st.TokensPerSecond() = 0 andalso st.FinishText() = "" andalso _diff --git a/tests/jns_check.c b/tests/jns_check.cindex 9cff41d..3e7f28b 100644--- a/tests/jns_check.c+++ b/tests/jns_check.c@@ -89,6 +89,8 @@ int main(int argc, char **argv)for (uint32_t l = 0; l < h->n_layer; l++)for (uint32_t e = 0; e < h->n_expert; e++) {uint64_t n = j.layers[l].slot_bytes;+ if (janas_jns_expert_offset(&j, l, e) == 0)+ continue; /* absent (a layer with fewer experts) */if (pread(fd, buf, n,(off_t)janas_jns_expert_offset(&j, l, e)) !=(ssize_t)n) {diff --git a/tests/test_attention.c b/tests/test_attention.cindex 25f9a7c..552061b 100644--- a/tests/test_attention.c+++ b/tests/test_attention.c@@ -161,6 +161,89 @@ static void run(struct janas_pool *pool, uint32_t n_head, uint32_t n_kv,janas_attn_destroy(a);}+/*+ * A span that attends both ways (a picture's tokens, janas_attn_set_last):+ * n tokens from pos0, the picture's at pic .. pic_end - 1 seeing up to its+ * end, run as the engine runs a block - JANAS_ATTN_MAX_BLOCK tokens a call,+ * the span cut by a call's end - against the reference over the positions+ * each token sees. Not fed to the checksum.+ */+static void run_both(struct janas_pool *pool, uint32_t pos0, uint32_t n,+ uint32_t pic, uint32_t pic_end)+{+ uint32_t n_head = 16, n_kv = 2, hd = 128, n_ctx = 1024;+ struct janas_attn *a = janas_attn_create(+ n_head, n_kv, hd, n_ctx, janas_pool_size(pool), JANAS_ATTN_FLOAT);+ size_t qd = (size_t)n_head * hd, kv = (size_t)n_kv * n_ctx * hd;+ size_t nsc = (size_t)n_kv * n_ctx;+ int8_t *K = malloc(kv), *V = malloc(kv);+ float *ks = malloc(nsc * sizeof(float)), *vs = malloc(nsc * sizeof(float));+ float *q = malloc(n * qd * sizeof(float)), *out = malloc(n * qd * 4);+ uint32_t *last = malloc(n * sizeof(uint32_t));+ double *ref = malloc(hd * sizeof(double));+ for (size_t i = 0; i < kv; i++) {+ K[i] = (int8_t)(int)(rndf(-127, 127));+ V[i] = (int8_t)(int)(rndf(-127, 127));+ }+ for (size_t i = 0; i < nsc; i++) {+ ks[i] = rndf(0.001f, 0.02f);+ vs[i] = rndf(0.001f, 0.02f);+ }+ for (size_t i = 0; i < n * qd; i++)+ q[i] = rndf(-3, 3);+ for (uint32_t j = 0; j < n; j++) {+ uint32_t p = pos0 + j;+ last[j] = p >= pic && p < pic_end ? pic_end - 1 : p;+ }+ for (uint32_t s = 0; s < n; s += JANAS_ATTN_MAX_BLOCK) {+ uint32_t len =+ n - s < JANAS_ATTN_MAX_BLOCK ? n - s : JANAS_ATTN_MAX_BLOCK;+ janas_attn_set_last(a, last + s);+ janas_attn_run(a, pool, q + s * qd, K, ks, V, vs, len, pos0 + s,+ out + s * qd);+ }+ double worst = 0;+ uint32_t group = n_head / n_kv;+ for (uint32_t j = 0; j < n; j++)+ for (uint32_t h = 0; h < n_head; h++) {+ size_t base = (size_t)(h / group) * n_ctx * hd;+ size_t sb = (size_t)(h / group) * n_ctx;+ reference(q + j * qd + (size_t)h * hd, K + base, ks + sb, V + base,+ vs + sb, last[j] + 1, hd, ref);+ for (uint32_t i = 0; i < hd; i++) {+ double e = fabs(ref[i] - out[j * qd + (size_t)h * hd + i]);+ if (e > worst)+ worst = e;+ }+ }+ /* and the call after, causal again: set_last holds for one call */+ janas_attn_run(a, pool, q, K, ks, V, vs, 1, pos0 + n, out);+ for (uint32_t h = 0; h < n_head; h++) {+ size_t base = (size_t)(h / group) * n_ctx * hd;+ size_t sb = (size_t)(h / group) * n_ctx;+ reference(q + (size_t)h * hd, K + base, ks + sb, V + base, vs + sb,+ pos0 + n + 1, hd, ref);+ for (uint32_t i = 0; i < hd; i++) {+ double e = fabs(ref[i] - out[(size_t)h * hd + i]);+ if (e > worst)+ worst = e;+ }+ }+ CHECK(worst < 1e-5, "both ways, picture %u..%u in %u..%u: error %g", pic,+ pic_end - 1, pos0, pos0 + n - 1, worst);+ printf("both ways, picture %4u..%4u in %4u..%4u: error %.2e\n", pic,+ pic_end - 1, pos0, pos0 + n - 1, worst);+ free(K);+ free(V);+ free(ks);+ free(vs);+ free(q);+ free(out);+ free(last);+ free(ref);+ janas_attn_destroy(a);+}+/** Gemma 4's settings: keys and values of sixteen bits, a sliding window and* a score scale of 1, against a double-precision reference that sees the@@ -272,6 +355,9 @@ int main(void)run16(pool, 16, 8, 256, 2048, 700, 64, 256); /* a window of 256 */run16(pool, 16, 1, 512, 1024, 250, 17, 0); /* Gemma 4's full layers */run16(pool, 16, 8, 256, 2048, 1030, 5, 1024);+ run_both(pool, 200, 100, 230, 290); /* across a call and a chunk */+ run_both(pool, 0, 64, 0, 64); /* the whole call */+ run_both(pool, 500, 200, 510, 690); /* over three calls */janas_pool_destroy(pool);if (failures) {fprintf(stderr, "%d failures\n", failures);diff --git a/tests/test_audio.c b/tests/test_audio.cnew file mode 100644index 0000000..14a9107--- /dev/null+++ b/tests/test_audio.c@@ -0,0 +1,347 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * test_audio.c - the sound decoder and the resampler (src/common/audio*.c):+ * WAV files made here of every kind it reads (PCM of 8, 16, 24 and 32 bits,+ * float of 32 and 64, stereo, WAVE_FORMAT_EXTENSIBLE, a chunk of odd length+ * before the samples, a data chunk longer than the file) give back the+ * sound written into them; the kinds it does not read are refused with a+ * reason; small clips of FLAC, MP3, MPEG Layers I and II, Ogg Vorbis, Ogg+ * FLAC, Opus (SILK, CELT, hybrid), AAC in M4A and in ADTS frames, ALAC,+ * Opus in WebM and Vorbis in Matroska made by FFmpeg (audio_clips.h) give+ * back FFmpeg's decoding of them, sample for sample; a tone resampled from+ * 44.1 and 8 kHz to 16 kHz is the same tone, one above the new Nyquist+ * frequency is gone; then truncations and thousands of corrupted copies of+ * every file must be read or refused, never crash (the sanitizer builds+ * watch).+ */+#include "audio_clips.h"+#include "common/audio.h"++#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++static int failures;++#define CHECK(c, ...) \+ do { \+ if (!(c)) { \+ fprintf(stderr, "test_audio: " __VA_ARGS__); \+ fputc('\n', stderr); \+ failures++; \+ } \+ } while (0)++struct buf {+ unsigned char *p;+ size_t n, cap;+};++static void put(struct buf *b, const void *s, size_t n)+{+ if (b->n + n > b->cap) {+ b->cap = (b->n + n) * 2;+ b->p = realloc(b->p, b->cap);+ if (!b->p)+ exit(1);+ }+ memcpy(b->p + b->n, s, n);+ b->n += n;+}++static void u16(struct buf *b, unsigned v)+{+ unsigned char c[2] = {(unsigned char)v, (unsigned char)(v >> 8)};+ put(b, c, 2);+}++static void u32(struct buf *b, unsigned long v)+{+ u16(b, (unsigned)(v & 0xffff));+ u16(b, (unsigned)(v >> 16));+}++static double tone(size_t i, double rate, double hz)+{+ return 0.5 * sin(2 * M_PI * hz * (double)i / rate);+}++/* a WAV of frames samples of the tone in every channel (the second, when+ there is one, its negative plus the tone twice: the mix is the tone);+ fmt 1 PCM, 3 float; ext: WAVE_FORMAT_EXTENSIBLE; odd: a chunk of odd+ length before the data; long_data: a data length past the file */+static struct buf wav(unsigned fmt, unsigned bits, unsigned ch, unsigned rate,+ size_t frames, int ext, int odd, int long_data)+{+ struct buf b = {0};+ unsigned align = ch * bits / 8;+ put(&b, "RIFF", 4);+ u32(&b, 0); /* a size nobody checks */+ put(&b, "WAVE", 4);+ put(&b, "fmt ", 4);+ u32(&b, ext ? 40 : 16);+ u16(&b, ext ? 0xFFFE : fmt);+ u16(&b, ch);+ u32(&b, rate);+ u32(&b, (unsigned long)rate * align);+ u16(&b, align);+ u16(&b, bits);+ if (ext) {+ u16(&b, 22);+ u16(&b, bits);+ u32(&b, 0);+ u16(&b, fmt); /* the subformat's GUID, its first two bytes */+ put(&b, "\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71", 14);+ }+ if (odd) {+ put(&b, "LIST", 4);+ u32(&b, 3);+ put(&b, "abc\0", 4); /* and its pad byte */+ }+ put(&b, "data", 4);+ u32(&b, long_data ? 0xFFFFFFF0ul : (unsigned long)(frames * align));+ for (size_t i = 0; i < frames; i++)+ for (unsigned c = 0; c < ch; c++) {+ double s = tone(i, rate, 440);+ if (c == 1)+ s = -s;+ if (c == 2)+ s = 2 * s;+ if (fmt == 3 && bits == 32) {+ float f = (float)s;+ put(&b, &f, 4);+ } else if (fmt == 3) {+ put(&b, &s, 8);+ } else if (bits == 8) {+ unsigned char v = (unsigned char)lround(s * 127 + 128);+ put(&b, &v, 1);+ } else if (bits == 16) {+ u16(&b, (unsigned)(long)lround(s * 32767) & 0xffff);+ } else if (bits == 24) {+ long v = lround(s * 8388607);+ unsigned char c3[3] = {(unsigned char)v,+ (unsigned char)(v >> 8),+ (unsigned char)(v >> 16)};+ put(&b, c3, 3);+ } else {+ u32(&b, (unsigned long)lround(s * 2147483647.0) & 0xffffffffu);+ }+ }+ return b;+}++static void kinds(void)+{+ static const struct {+ unsigned fmt, bits, ch;+ int ext, odd, long_data;+ double tol;+ } K[] = {+ {1, 8, 1, 0, 0, 0, 1.0 / 100}, {1, 16, 1, 0, 0, 0, 1e-4},+ {1, 24, 1, 0, 0, 0, 1e-6}, {1, 32, 1, 0, 0, 0, 1e-6},+ {3, 32, 1, 0, 0, 0, 1e-7}, {3, 64, 1, 0, 0, 0, 1e-7},+ {1, 16, 3, 0, 0, 0, 1e-4}, {1, 16, 1, 1, 0, 0, 1e-4},+ {3, 32, 2, 1, 0, 0, 1e-7}, {1, 16, 1, 0, 1, 0, 1e-4},+ {1, 16, 1, 0, 0, 1, 1e-4},+ };+ char why[200];+ for (size_t k = 0; k < sizeof K / sizeof *K; k++) {+ size_t frames = 2205;+ struct buf b = wav(K[k].fmt, K[k].bits, K[k].ch, 22050, frames,+ K[k].ext, K[k].odd, K[k].long_data);+ struct janas_audio a;+ int rc = janas_audio_decode(b.p, b.n, &a, why, sizeof why);+ CHECK(rc == 0, "kind %zu (%u bits, %u channels) refused: %s", k,+ K[k].bits, K[k].ch, why);+ if (rc == 0) {+ CHECK(a.rate == 22050 && a.n == frames,+ "kind %zu: %u Hz, %zu samples", k, a.rate, a.n);+ double err = 0;+ /* the channels' mean: s; s and -s; s, -s and 2 s */+ double gain = K[k].ch == 1 ? 1 : K[k].ch == 2 ? 0 : 2.0 / 3;+ for (size_t i = 0; i < a.n && i < frames; i++) {+ double e = fabs(a.pcm[i] - gain * tone(i, 22050, 440));+ if (e > err)+ err = e;+ }+ CHECK(err <= K[k].tol, "kind %zu: off by %g", k, err);+ janas_audio_free(&a);+ }+ free(b.p);+ }+ /* refused, with a reason */+ struct buf b = wav(2, 4, 1, 8000, 100, 0, 0, 0); /* ADPCM */+ struct janas_audio a;+ CHECK(janas_audio_decode(b.p, b.n, &a, why, sizeof why) != 0 && why[0],+ "ADPCM read");+ free(b.p);+ b = wav(1, 12, 1, 8000, 100, 0, 0, 0); /* 12 bits */+ CHECK(janas_audio_decode(b.p, b.n, &a, why, sizeof why) != 0,+ "12-bit read");+ free(b.p);+ static const char *const NOT[] = {"fLaC....", "ID3.....", "OggS....",+ "hello, world"};+ for (size_t i = 0; i < sizeof NOT / sizeof *NOT; i++)+ CHECK(janas_audio_decode(NOT[i], strlen(NOT[i]), &a, why, sizeof why) !=+ 0 &&+ why[0],+ "'%s' read", NOT[i]);+}++static void formats(void)+{+ char why[200];+ for (size_t k = 0; k < sizeof CLIPS / sizeof *CLIPS; k++) {+ const struct clip *c = &CLIPS[k];+ struct janas_audio a;+ int rc = janas_audio_decode(c->data, c->size, &a, why, sizeof why);+ CHECK(rc == 0, "%s refused: %s", c->what, why);+ if (rc != 0)+ continue;+ CHECK(a.rate == c->rate && a.n == c->n, "%s: %u Hz, %zu samples",+ c->what, a.rate, a.n);+ double err = 0;+ for (size_t i = 0; i * 4 < a.n && i < c->nref; i++) {+ double e = fabs(a.pcm[i * 4] * 32768.0 - c->ref[i]);+ if (e > err)+ err = e;+ }+ CHECK(err <= c->tol, "%s: off by %g / 32768", c->what, err);+ janas_audio_free(&a);+ }+}++/* the largest gap to the tone at the new rate, away from the ends */+static double resampled(unsigned from, unsigned to, double hz, double *rms)+{+ size_t n = (size_t)from; /* a second */+ struct janas_audio a = {+ .pcm = malloc(sizeof(float) * n), .n = n, .rate = from};+ if (!a.pcm)+ exit(1);+ for (size_t i = 0; i < n; i++)+ a.pcm[i] = (float)tone(i, from, hz);+ if (janas_audio_resample(&a, to) != 0)+ exit(1);+ double err = 0, sq = 0;+ size_t edge = to / 50, cnt = 0;+ for (size_t i = edge; i + edge < a.n; i++) {+ double e = fabs(a.pcm[i] - tone(i, to, hz));+ if (e > err)+ err = e;+ sq += (double)a.pcm[i] * a.pcm[i];+ cnt++;+ }+ *rms = sqrt(sq / (cnt ? cnt : 1));+ CHECK(a.n == (size_t)to, "%u -> %u: %zu samples", from, to, a.n);+ janas_audio_free(&a);+ return err;+}++static void resampling(void)+{+ double rms;+ double e = resampled(44100, 16000, 1000, &rms);+ CHECK(e < 2e-3, "44100 -> 16000, 1 kHz: off by %g", e);+ e = resampled(8000, 16000, 1000, &rms);+ CHECK(e < 2e-3, "8000 -> 16000, 1 kHz: off by %g", e);+ e = resampled(48000, 16000, 3000, &rms);+ CHECK(e < 2e-3, "48000 -> 16000, 3 kHz: off by %g", e);+ resampled(44100, 16000, 10000, &rms);+ CHECK(rms < 1e-3, "44100 -> 16000, 10 kHz: left at %g", rms);+}++static unsigned long long rng = 0x9e3779b97f4a7c15ULL;++static unsigned next(void)+{+ rng ^= rng << 13;+ rng ^= rng >> 7;+ rng ^= rng << 17;+ return (unsigned)(rng >> 32);+}++int main(void)+{+ kinds();+ formats();+ resampling();+ long read = 0, refused = 0;+ char why[200];+ struct buf w[3] = {wav(1, 16, 2, 8000, 300, 0, 1, 0),+ wav(3, 32, 1, 8000, 300, 1, 0, 0),+ wav(1, 24, 1, 8000, 300, 0, 0, 1)};+ for (int k = 0; k < 3; k++) {+ unsigned char *b = malloc(w[k].n);+ if (!b)+ return 1;+ for (size_t cut = 0; cut <= w[k].n; cut++) {+ memcpy(b, w[k].p, cut);+ struct janas_audio a;+ if (janas_audio_decode(b, cut, &a, why, sizeof why) == 0) {+ read++;+ janas_audio_free(&a);+ } else {+ refused++;+ }+ }+ for (int i = 0; i < 3000; i++) {+ memcpy(b, w[k].p, w[k].n);+ int flips = 1 + (int)(next() % 8);+ for (int f = 0; f < flips; f++)+ b[next() % 80 % w[k].n] = (unsigned char)next();+ struct janas_audio a;+ if (janas_audio_decode(b, w[k].n, &a, why, sizeof why) == 0) {+ read++;+ janas_audio_free(&a);+ } else {+ refused++;+ }+ }+ free(b);+ free(w[k].p);+ }+ for (size_t k = 0; k < sizeof CLIPS / sizeof *CLIPS; k++) {+ const struct clip *c = &CLIPS[k];+ unsigned char *b = malloc(c->size);+ if (!b)+ return 1;+ for (size_t cut = 0; cut <= c->size; cut += 1 + cut / 16) {+ memcpy(b, c->data, cut);+ struct janas_audio a;+ if (janas_audio_decode(b, cut, &a, why, sizeof why) == 0) {+ read++;+ janas_audio_free(&a);+ } else {+ refused++;+ }+ }+ for (int i = 0; i < 400; i++) {+ memcpy(b, c->data, c->size);+ int flips = 1 + (int)(next() % 8);+ for (int f = 0; f < flips; f++) {+ /* the headers half of the time, anywhere the rest */+ size_t at = i & 1 ? next() % 200 : next();+ b[at % c->size] = (unsigned char)next();+ }+ struct janas_audio a;+ if (janas_audio_decode(b, c->size, &a, why, sizeof why) == 0) {+ read++;+ janas_audio_free(&a);+ } else {+ refused++;+ }+ }+ free(b);+ }+ if (failures) {+ fprintf(stderr, "test_audio: %d failures\n", failures);+ return 1;+ }+ printf("test_audio: ok (WAV of 11 kinds, %zu clips of other formats, "+ "resampling; broken copies: %ld read, %ld refused)\n",+ sizeof CLIPS / sizeof *CLIPS, read, refused);+ return 0;+}diff --git a/tests/test_deflate.c b/tests/test_deflate.cnew file mode 100644index 0000000..6600d61--- /dev/null+++ b/tests/test_deflate.c@@ -0,0 +1,90 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * test_deflate.c - deflate.c's sizes against those Python's zlib gives+ * for the same bytes (zlib.compress at its default level): texts, loops,+ * UTF-8, and generated bytes up to a window over several blocks.+ */+#include "common/deflate.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++static const struct {+ const char *s;+ long size;+} TEXTS[] = {+ {"", 8},+ {"a", 9},+ {"ab", 10},+ {"abc", 11},+ {"Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.", 22},+ {" And so my fellow Americans, ask not what your country can do for you, ask what you can do for your country.",+ 79},+ {" Buongiorno a tutti. Oggi parliamo di come funziona un motore di inferenza per i modelli linguistici.",+ 87},+ {"la la la la la la la la la la la la la la la la la la la la la la la la la la la la",+ 14},+ {"\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf\xe4\xb8\x96\xe7\x95\x8c",+ 32},+ {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",+ 12},+ {"The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. ",+ 57},+ {"012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",+ 23},+};++static const struct {+ unsigned long long seed;+ size_t n;+ const char *alpha;+ size_t na;+ long size;+} GEN[] = {+ {1, 700, "abc ", 4, 260}, {2, 5000, NULL, 256, 5011},+ {3, 20000, "ab", 2, 3254}, {4, 32000, "abcdefghij ", 11, 16617},+ {5, 1000, "x", 1, 17}, {6, 17000, NULL, 256, 17016},+};++int main(void)+{+ int bad = 0;+ for (size_t i = 0; i < sizeof TEXTS / sizeof *TEXTS; i++) {+ long z =+ janas_deflate_size((const uint8_t *)TEXTS[i].s, strlen(TEXTS[i].s));+ if (z != TEXTS[i].size) {+ fprintf(stderr, "test_deflate: text %zu: %ld bytes, zlib %ld\n", i,+ z, TEXTS[i].size);+ bad++;+ }+ }+ for (size_t i = 0; i < sizeof GEN / sizeof *GEN; i++) {+ uint8_t *b = malloc(GEN[i].n);+ if (!b)+ return 1;+ unsigned long long s = GEN[i].seed;+ for (size_t k = 0; k < GEN[i].n; k++) {+ s ^= s << 13;+ s ^= s >> 7;+ s ^= s << 17;+ unsigned v = (unsigned)((s >> 32) % GEN[i].na);+ b[k] = GEN[i].alpha ? (uint8_t)GEN[i].alpha[v] : (uint8_t)v;+ }+ long z = janas_deflate_size(b, GEN[i].n);+ if (z != GEN[i].size) {+ fprintf(stderr,+ "test_deflate: generated %zu: %ld bytes, zlib %ld\n", i, z,+ GEN[i].size);+ bad++;+ }+ free(b);+ }+ if (bad)+ return 1;+ printf(+ "test_deflate: ok (%zu texts, %zu generated, the sizes zlib gives)\n",+ sizeof TEXTS / sizeof *TEXTS, sizeof GEN / sizeof *GEN);+ return 0;+}diff --git a/tests/test_grammar.c b/tests/test_grammar.cindex e6754c6..cce21f0 100644--- a/tests/test_grammar.c+++ b/tests/test_grammar.c@@ -311,6 +311,194 @@ static void test_schemas(void)(const char *const[]){"1", "{}", NULL});}+/* Every integer in [-1200, 1200] against a schema of bounds and a multiple:+ accepted exactly when it fits (want: the check). */+static void ints_against(const char *name, const char *schema,+ int (*want)(long))+{+ struct gram x;+ if (make_schema(&x, schema) != 0) {+ CHECK(0, "%s: grammar not made", name);+ gram_free(&x);+ return;+ }+ int bad = 0;+ for (long v = -1200; v <= 1200 && bad < 5; v++) {+ char t[32];+ snprintf(t, sizeof(t), "%ld", v);+ int got = matches(&x, t, 1);+ if (got != want(v)) {+ CHECK(0, "%s: %ld %s", name, v, got ? "accepted" : "refused");+ bad++;+ }+ }+ CHECK(matches(&x, "1.5", 1) == 0 && matches(&x, "1e2", 1) == 0 &&+ matches(&x, "007", 1) == 0 && matches(&x, "-0", 1) == 0,+ "%s: not an integer accepted", name);+ gram_free(&x);+}++static int w_range(long v)+{+ return v >= -5 && v <= 300;+}+static int w_mult(long v)+{+ return v >= 0 && v < 1000 && v % 7 == 0;+}+static int w_neg(long v)+{+ return v > -1000 && v < -10 && v % 25 == 0;+}+static int w_lo(long v)+{+ return v >= 17;+}+static int w_mult12(long v)+{+ return v % 12 == 0;+}++/* Numbers: whatever the grammar takes is inside the bounds, and the+ integers and simple fractions well inside them are taken. */+static void reals_against(const char *name, const char *schema, double lo,+ int lox, double hi, int hix)+{+ struct gram x;+ if (make_schema(&x, schema) != 0) {+ CHECK(0, "%s: grammar not made", name);+ gram_free(&x);+ return;+ }+ int bad = 0;+ for (int k = -4000; k <= 4000 && bad < 5; k++) {+ double v = k / 100.0;+ char t[32];+ snprintf(t, sizeof(t), "%.2f", v);+ if (v == 0 && t[0] == '-')+ continue;+ int in = (lox ? v > lo : v >= lo) && (hix ? v < hi : v <= hi);+ int got = matches(&x, t, 1);+ if (got && !in) {+ CHECK(0, "%s: %s accepted, outside", name, t);+ bad++;+ }+ /* well inside: at least one unit from each bound */+ if (!got && v >= lo + 1 && v <= hi - 1) {+ CHECK(0, "%s: %s refused, inside", name, t);+ bad++;+ }+ }+ gram_free(&x);+}++static void test_bounds_and_formats(void)+{+ ints_against("int range",+ "{\"type\":\"integer\",\"minimum\":-5,\"maximum\":300}",+ w_range);+ ints_against("int multiple",+ "{\"type\":\"integer\",\"minimum\":0,"+ "\"exclusiveMaximum\":1000,\"multipleOf\":7}",+ w_mult);+ ints_against("int negative",+ "{\"type\":\"integer\",\"exclusiveMinimum\":-1000,"+ "\"exclusiveMaximum\":-10,\"multipleOf\":25}",+ w_neg);+ ints_against("int lower only", "{\"type\":\"integer\",\"minimum\":16.5}",+ w_lo);+ ints_against("int multiple only",+ "{\"type\":\"integer\",\"multipleOf\":12}", w_mult12);+ reals_against("real range",+ "{\"type\":\"number\",\"minimum\":-2.5,\"maximum\":10}", -2.5,+ 0, 10, 0);+ reals_against("real exclusive",+ "{\"type\":\"number\",\"exclusiveMinimum\":0,"+ "\"exclusiveMaximum\":1}",+ 0, 1, 1, 1);+ reals_against("real negative",+ "{\"type\":\"number\",\"minimum\":-30,"+ "\"exclusiveMaximum\":-3.25}",+ -30, 0, -3.25, 1);+ reals_against("real lower", "{\"type\":\"number\",\"exclusiveMinimum\":-7}",+ -7, 1, 1e9, 0);+ expect(+ "real fractions",+ "{\"type\":\"number\",\"exclusiveMinimum\":0,\"maximum\":1}",+ (const char *const[]){"0.5", "0.001", "1", "0.999999", NULL},+ (const char *const[]){"0", "0.0", "1.0001", "1.5", "-0.5", "2", NULL});+ expect("date", "{\"type\":\"string\",\"format\":\"date\"}",+ (const char *const[]){"\"2026-10-28\"", "\"1999-02-31\"", NULL},+ (const char *const[]){"\"2026-13-01\"", "\"2026-1-01\"",+ "\"28/10/2026\"", "\"2026-10-32\"", NULL});+ expect("date-time", "{\"type\":\"string\",\"format\":\"date-time\"}",+ (const char *const[]){"\"2026-10-28T21:05:00Z\"",+ "\"2026-10-28T21:05:00.123+01:00\"", NULL},+ (const char *const[]){"\"2026-10-28 21:05:00Z\"",+ "\"2026-10-28T24:00:00Z\"",+ "\"2026-10-28T21:05:00\"", NULL});+ expect("time", "{\"type\":\"string\",\"format\":\"time\"}",+ (const char *const[]){"\"23:59:60-05:30\"", NULL},+ (const char *const[]){"\"23:60:00Z\"", NULL});+ expect(+ "uuid", "{\"type\":\"string\",\"format\":\"uuid\"}",+ (const char *const[]){"\"123e4567-e89b-12d3-a456-426614174000\"", NULL},+ (const char *const[]){"\"123e4567e89b12d3a456426614174000\"",+ "\"123e4567-e89b-12d3-a456-42661417400g\"",+ NULL});+ expect("email", "{\"type\":\"string\",\"format\":\"email\"}",+ (const char *const[]){"\"[email protected]\"", "\"[email protected]\"",+ NULL},+ (const char *const[]){"\"ann@localhost\"", "\"@b.it\"",+ NULL});+ expect("ipv4", "{\"type\":\"string\",\"format\":\"ipv4\"}",+ (const char *const[]){"\"192.168.0.1\"", "\"255.255.255.255\"",+ "\"0.0.0.0\"", NULL},+ (const char *const[]){"\"256.1.1.1\"", "\"01.2.3.4\"", "\"1.2.3\"",+ NULL});+ /* patterns: anchored, or not and written from the start all the same */+ expect("pattern anchored",+ "{\"type\":\"string\",\"pattern\":\"^[A-Z]{2}-\\\\d{3,4}$\"}",+ (const char *const[]){"\"AB-123\"", "\"ZZ-0000\"", NULL},+ (const char *const[]){"\"ab-123\"", "\"AB-12\"", "\"AB-12345\"",+ "\"xAB-123\"", "\"AB-123x\"", NULL});+ expect("pattern anywhere", "{\"type\":\"string\",\"pattern\":\"o+k\"}",+ (const char *const[]){"\"ok\"", "\"oook!\"", "\"ok then\"", NULL},+ (const char *const[]){"\"k\"", "\"o k\"", "\"\"", "\"it is ok\"",+ NULL});+ expect("pattern start only",+ "{\"type\":\"string\",\"pattern\":\"^(cat|dog)s?\"}",+ (const char *const[]){"\"cats and\"", "\"dog\"", NULL},+ (const char *const[]){"\"a cat\"", "\"cow\"", NULL});+ /* a quote and a backslash in the expression: written escaped */+ expect("pattern escapes",+ "{\"type\":\"string\",\"pattern\":\"^say \\\"[a-z]+\\\"\\\\\\\\$\"}",+ (const char *const[]){"\"say \\\"hi\\\"\\\\\"", NULL},+ (const char *const[]){"\"say \"hi\"\\\\\"", "\"say \\\"hi\\\"\"",+ NULL});+ expect("pattern control", "{\"type\":\"string\",\"pattern\":\"^a\\\\nb$\"}",+ (const char *const[]){"\"a\\nb\"", NULL},+ (const char *const[]){"\"anb\"", "\"a\\\\nb\"", NULL});+ /* not read: anchors inside, back references; the string unbounded */+ expect("pattern unread", "{\"type\":\"string\",\"pattern\":\"a|^b\"}",+ (const char *const[]){"\"zzz\"", NULL}, (const char *const[]){NULL});+ /* a length past the old limit of 64 */+ {+ char good[402], bad[403];+ good[0] = bad[0] = '"';+ memset(good + 1, 'a', 399);+ memset(bad + 1, 'a', 400);+ good[400] = '"';+ good[401] = 0;+ bad[401] = '"';+ bad[402] = 0;+ expect("long string", "{\"type\":\"string\",\"maxLength\":399}",+ (const char *const[]){good, "\"\"", NULL},+ (const char *const[]){bad, NULL});+ }+}+/* Text up to a closing tag, and special tokens as symbols. */static void test_until_and_tokens(void){@@ -375,6 +563,7 @@ int main(void)test_json();test_json_mangled();test_schemas();+ test_bounds_and_formats();test_until_and_tokens();test_long();if (failures) {diff --git a/tests/test_image.c b/tests/test_image.cnew file mode 100644index 0000000..93f8cfb--- /dev/null+++ b/tests/test_image.c@@ -0,0 +1,124 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * test_image.c - the picture decoders (src/common/image*.c): small PNGs+ * and JPEGs of every kind (palette with transparency, 16-bit gray, RGBA,+ * Adam7; baseline and progressive, 4:2:0, 4:2:2, 4:4:0, gray, restart+ * intervals) give the pixels Pillow gives, by the hash of their RGB, and+ * so does the bicubic resize, up and down; then+ * every truncation and thousands of corrupted copies of each must be read+ * or refused, never crash (the sanitizer builds watch).+ */+#include "common/image.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "test_image_data.h"++static int failures;++static unsigned long long fnv(const unsigned char *p, size_t n)+{+ unsigned long long h = 0xcbf29ce484222325ULL;+ for (size_t i = 0; i < n; i++)+ h = (h ^ p[i]) * 0x100000001b3ULL;+ return h;+}++static unsigned long long rng = 0x9e3779b97f4a7c15ULL;++static unsigned next(void)+{+ rng ^= rng << 13;+ rng ^= rng >> 7;+ rng ^= rng << 17;+ return (unsigned)(rng >> 32);+}++int main(void)+{+ const int n = (int)(sizeof PICS / sizeof PICS[0]);+ char why[200];+ for (int i = 0; i < n; i++) {+ const struct pic *p = &PICS[i];+ struct janas_image im;+ if (janas_image_decode(p->data, p->n, &im, why, sizeof why) != 0) {+ fprintf(stderr, "test_image: %s: %s\n", p->name, why);+ failures++;+ continue;+ }+ unsigned long long h = fnv(im.rgb, (size_t)im.w * (size_t)im.h * 3);+ if (im.w != p->w || im.h != p->h || h != p->fnv) {+ fprintf(stderr,+ "test_image: %s: %dx%d, not the pixels Pillow "+ "gives\n",+ p->name, im.w, im.h);+ failures++;+ }+ janas_image_free(&im);+ }+ /* resized as Pillow's resize(BICUBIC): up and down */+ static const struct {+ int pic, w, h;+ unsigned long long fnv;+ } RS[] = {{3, 13, 9, 0x22722aaa28b4a0cfULL},+ {4, 37, 21, 0xaf2f59852ea157f3ULL}};+ for (size_t i = 0; i < sizeof RS / sizeof RS[0]; i++) {+ const struct pic *p = &PICS[RS[i].pic];+ struct janas_image im, out;+ if (janas_image_decode(p->data, p->n, &im, why, sizeof why) != 0 ||+ janas_image_resize(&im, RS[i].w, RS[i].h, &out) != 0) {+ fprintf(stderr, "test_image: %s not resized\n", p->name);+ failures++;+ continue;+ }+ if (fnv(out.rgb, (size_t)RS[i].w * (size_t)RS[i].h * 3) != RS[i].fnv) {+ fprintf(stderr, "test_image: %s to %dx%d: not Pillow's pixels\n",+ p->name, RS[i].w, RS[i].h);+ failures++;+ }+ janas_image_free(&im);+ janas_image_free(&out);+ }+ /* broken files: read or refused, never more */+ long refused = 0, read = 0;+ for (int i = 0; i < n; i++) {+ const struct pic *p = &PICS[i];+ unsigned char *b = malloc(p->n);+ if (!b)+ return 1;+ for (unsigned cut = 0; cut < p->n; cut += 1 + p->n / 300) {+ struct janas_image im;+ if (janas_image_decode(p->data, cut, &im, why, sizeof why) == 0) {+ read++;+ janas_image_free(&im);+ } else {+ refused++;+ }+ }+ for (int k = 0; k < 1500; k++) {+ memcpy(b, p->data, p->n);+ int flips = 1 + (int)(next() % 8);+ for (int f = 0; f < flips; f++)+ b[next() % p->n] = (unsigned char)next();+ struct janas_image im;+ if (janas_image_decode(b, p->n, &im, why, sizeof why) == 0) {+ read++;+ janas_image_free(&im);+ } else {+ refused++;+ }+ }+ free(b);+ }+ if (failures) {+ fprintf(stderr, "test_image: %d failures\n", failures);+ return 1;+ }+ printf("test_image: ok (%d pictures as Pillow; broken copies: %ld read, "+ "%ld refused)\n",+ n, read, refused);+ return 0;+}diff --git a/tests/test_image_data.h b/tests/test_image_data.hnew file mode 100644index 0000000..ef0610e--- /dev/null+++ b/tests/test_image_data.h@@ -0,0 +1,703 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/* test_image_data.h - small pictures for test_image.c, made with Pillow and+ cjpeg, and the FNV-1a hash of the RGB Pillow reads from each (generated by+ a probe outside the repository: not formatted) */++struct pic {+ const char *name;+ int w, h;+ unsigned long long fnv;+ unsigned n;+ const unsigned char *data;+};++static const unsigned char pic0[] = {+ 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,9,+ 0,0,0,13,4,3,0,0,1,254,41,105,163,0,0,0,48,80,76,84,+ 69,249,129,224,24,231,167,158,237,18,92,6,201,31,170,124,213,186,63,25,+ 44,66,100,16,80,55,211,70,230,31,64,15,54,198,146,125,193,174,238,45,+ 113,210,249,215,75,97,37,176,156,10,136,251,131,0,0,0,8,116,82,78,+ 83,178,119,150,21,182,185,101,121,170,1,2,209,0,0,0,102,73,68,65,+ 84,120,218,1,91,0,164,255,0,90,1,16,0,240,1,160,0,71,96,1,+ 117,187,0,103,1,208,2,119,3,30,0,173,7,0,1,49,70,217,2,180,+ 140,240,0,106,81,1,82,166,2,177,255,3,130,247,4,148,116,0,35,68,+ 1,174,143,0,21,90,90,140,144,1,79,140,244,106,7,2,244,194,95,176,+ 192,3,74,68,20,181,232,4,134,224,175,61,144,0,198,109,244,148,144,50,+ 181,33,145,142,155,237,150,0,0,0,0,73,69,78,68,174,66,96,130,+};+static const unsigned char pic1[] = {+ 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,9,+ 0,0,0,13,16,0,0,0,1,121,252,247,15,0,0,1,15,73,68,65,+ 84,120,218,1,4,1,251,254,0,219,97,244,222,1,175,245,7,2,0,233,+ 80,1,243,94,0,139,115,181,136,171,188,1,99,95,111,159,15,101,0,145,+ 164,2,31,1,45,253,158,93,2,145,214,122,129,3,19,176,202,51,0,193,+ 22,217,52,66,43,254,224,194,60,1,21,1,98,173,114,248,28,75,49,165,+ 2,45,246,69,138,107,129,51,107,11,34,0,17,181,35,56,168,79,145,170,+ 1,193,91,49,88,189,36,156,147,2,180,218,237,149,152,172,25,118,3,142,+ 128,161,103,10,55,115,204,4,148,164,143,246,5,161,217,16,0,37,15,164,+ 116,43,43,198,187,1,129,165,129,255,82,10,133,227,0,168,231,50,144,135,+ 71,40,119,12,119,243,61,100,225,120,203,188,47,1,250,139,241,252,197,157,+ 244,74,52,94,243,76,113,230,110,58,82,133,2,29,235,128,178,230,239,10,+ 88,66,158,102,93,247,235,91,236,226,198,3,93,112,242,222,78,7,227,151,+ 150,58,164,30,221,79,27,134,77,5,4,142,22,247,188,99,227,231,160,91,+ 133,109,53,10,84,96,220,89,125,0,148,73,127,168,242,187,201,123,197,64,+ 84,222,102,244,96,243,227,183,109,214,120,14,29,33,43,37,0,0,0,0,+ 73,69,78,68,174,66,96,130,+};+static const unsigned char pic2[] = {+ 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,9,+ 0,0,0,13,16,6,0,0,0,43,144,152,69,0,0,3,192,73,68,65,+ 84,120,218,1,181,3,74,252,0,223,62,159,158,64,79,136,198,115,153,93,+ 167,46,149,163,79,84,164,1,97,172,28,199,126,172,147,60,47,183,50,179,+ 73,112,188,218,32,245,185,124,180,44,27,150,163,134,24,75,102,145,61,220,+ 83,182,122,7,42,151,252,221,2,254,181,215,230,197,236,129,243,81,113,26,+ 66,1,220,42,158,155,56,86,160,36,98,183,223,189,159,7,254,147,58,87,+ 129,223,211,27,7,240,23,127,68,7,229,8,172,107,226,170,249,150,155,70,+ 214,218,21,214,41,223,194,148,213,241,7,79,27,79,81,124,199,139,174,174,+ 216,56,220,143,88,142,148,60,177,185,131,26,52,113,2,138,158,20,199,169,+ 224,121,45,112,60,205,200,199,238,55,86,246,144,235,18,49,230,40,41,229,+ 138,13,2,31,84,34,168,132,13,61,4,90,120,86,68,182,109,42,239,217,+ 238,190,199,149,211,54,253,52,21,161,176,83,20,40,234,189,121,213,146,71,+ 93,171,175,40,125,203,57,3,133,115,9,194,92,125,94,44,135,18,228,227,+ 223,73,129,224,117,136,225,194,169,25,207,218,181,32,12,194,188,243,70,60,+ 155,132,179,163,143,113,24,61,187,13,72,70,160,150,108,163,109,111,114,58,+ 228,171,0,195,78,81,144,34,72,45,223,54,157,22,29,138,254,171,235,34,+ 4,48,39,28,128,103,45,226,237,250,194,37,44,85,156,72,225,99,216,238,+ 1,213,84,89,53,133,5,184,134,135,237,163,129,187,239,159,110,111,5,95,+ 87,19,37,95,238,112,167,125,25,216,2,92,151,154,147,10,59,55,241,201,+ 151,92,19,246,22,187,160,197,194,106,155,183,181,0,133,253,106,128,107,38,+ 111,126,199,36,207,67,129,79,162,163,252,60,119,119,180,85,201,139,206,95,+ 29,218,114,248,89,244,36,210,87,138,117,32,203,227,155,62,193,51,194,82,+ 6,108,151,127,110,30,85,135,214,72,237,29,115,52,74,190,20,108,27,57,+ 142,47,15,72,69,254,1,225,195,153,48,215,14,217,229,185,228,41,68,124,+ 212,96,124,228,225,51,119,251,182,37,83,48,0,217,154,104,220,33,211,125,+ 54,66,138,214,147,58,22,92,32,92,20,22,32,215,175,183,232,206,142,105,+ 8,147,95,85,31,67,162,85,196,250,67,146,94,100,115,181,141,46,131,2,+ 85,147,76,167,124,222,13,249,83,219,113,37,210,150,80,47,87,202,7,222,+ 213,98,118,28,236,28,251,138,129,95,238,23,134,88,248,138,160,185,85,32,+ 173,146,67,93,241,127,161,21,155,181,63,131,56,151,80,133,184,25,124,145,+ 236,172,99,35,129,93,159,25,155,234,154,78,3,227,239,229,111,193,115,0,+ 73,144,99,139,133,96,197,203,239,243,88,123,204,17,30,166,158,202,181,113,+ 192,240,91,237,133,224,3,50,51,255,40,213,207,156,249,237,30,14,89,189,+ 248,248,15,90,72,161,78,148,226,157,87,42,46,107,181,252,75,78,207,54,+ 24,36,11,153,237,4,71,228,44,220,4,72,176,239,152,110,159,107,69,198,+ 76,246,38,255,51,220,253,202,139,43,139,42,165,156,93,142,246,231,251,79,+ 221,124,44,40,111,178,87,97,199,130,16,205,137,16,194,167,200,211,51,193,+ 110,237,221,162,129,11,88,42,97,194,214,59,143,191,208,105,169,15,0,238,+ 28,124,208,182,247,97,142,134,237,138,218,58,59,151,208,163,110,200,35,228,+ 91,67,11,138,72,110,237,92,155,207,138,217,165,209,167,191,226,14,187,207,+ 243,143,16,61,130,32,75,128,211,31,235,114,231,253,76,191,84,80,28,96,+ 104,56,164,91,174,217,99,13,233,36,235,1,193,217,78,245,124,84,15,231,+ 177,98,75,138,208,170,239,97,59,44,127,232,88,193,254,91,13,30,45,164,+ 122,241,227,67,225,100,57,219,135,187,41,254,52,108,194,152,200,140,102,40,+ 74,36,167,207,75,115,43,198,0,86,53,184,156,113,191,8,234,53,120,203,+ 20,19,55,212,2,70,113,60,23,211,87,188,79,194,81,209,123,39,25,63,+ 160,37,149,214,2,32,208,189,154,185,72,28,55,161,10,153,116,126,228,64,+ 12,96,88,98,33,115,58,121,191,71,239,41,94,26,98,100,4,43,34,128,+ 59,134,201,174,161,112,144,68,254,165,95,199,18,33,227,65,222,165,192,212,+ 45,54,10,224,117,0,0,0,0,73,69,78,68,174,66,96,130,+};+static const unsigned char pic3[] = {+ 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,7,+ 0,0,0,5,8,2,0,0,0,6,248,97,143,0,0,0,74,73,68,65,+ 84,120,156,99,100,128,1,245,29,183,25,24,24,62,73,113,159,221,254,128,+ 145,129,129,65,207,163,237,103,65,40,3,18,96,242,169,239,96,64,5,31,+ 175,71,50,41,87,243,160,137,126,179,53,97,98,96,96,184,199,89,253,116,+ 166,90,213,202,39,16,209,31,39,36,1,6,232,20,99,214,102,164,87,0,+ 0,0,0,73,69,78,68,174,66,96,130,+};+static const unsigned char pic4[] = {+ 255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,+ 255,219,0,67,0,16,11,12,14,12,10,16,14,13,14,18,17,16,19,24,+ 40,26,24,22,22,24,49,35,37,29,40,58,51,61,60,57,51,56,55,64,+ 72,92,78,64,68,87,69,55,56,80,109,81,87,95,98,103,104,103,62,77,+ 113,121,112,100,120,92,101,103,99,255,219,0,67,1,17,18,18,24,21,24,+ 47,26,26,47,99,66,56,66,99,99,99,99,99,99,99,99,99,99,99,99,+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,255,192,+ 0,17,8,0,48,0,64,3,1,34,0,2,17,1,3,17,1,255,196,0,+ 31,0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,+ 2,3,4,5,6,7,8,9,10,11,255,196,0,181,16,0,2,1,3,3,+ 2,4,3,5,5,4,4,0,0,1,125,1,2,3,0,4,17,5,18,33,+ 49,65,6,19,81,97,7,34,113,20,50,129,145,161,8,35,66,177,193,21,+ 82,209,240,36,51,98,114,130,9,10,22,23,24,25,26,37,38,39,40,41,+ 42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,+ 87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,+ 131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,+ 165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,+ 199,200,201,202,210,211,212,213,214,215,216,217,218,225,226,227,228,229,230,231,+ 232,233,234,241,242,243,244,245,246,247,248,249,250,255,196,0,31,1,0,3,+ 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,2,3,4,5,+ 6,7,8,9,10,11,255,196,0,181,17,0,2,1,2,4,4,3,4,7,+ 5,4,4,0,1,2,119,0,1,2,3,17,4,5,33,49,6,18,65,81,+ 7,97,113,19,34,50,129,8,20,66,145,161,177,193,9,35,51,82,240,21,+ 98,114,209,10,22,36,52,225,37,241,23,24,25,26,38,39,40,41,42,53,+ 54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,+ 90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,130,131,132,+ 133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,+ 167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,+ 201,202,210,211,212,213,214,215,216,217,218,226,227,228,229,230,231,232,233,234,+ 242,243,244,245,246,247,248,249,250,255,218,0,12,3,1,0,2,17,3,17,+ 0,63,0,226,28,237,34,95,43,204,218,193,89,155,5,9,0,113,199,208,+ 243,158,65,171,186,101,144,184,212,150,218,107,119,135,106,19,112,172,196,100,+ 0,49,193,228,100,227,60,247,236,41,44,116,246,187,183,138,67,229,170,2,+ 84,178,145,187,0,231,60,30,188,227,230,29,49,140,215,67,99,107,5,171,+ 8,162,136,2,224,134,219,156,156,246,63,79,235,78,50,247,149,206,122,181,+ 163,27,165,191,245,248,151,36,130,41,35,88,184,141,87,133,216,6,83,140,+ 2,185,232,71,81,143,74,229,38,210,46,96,72,206,90,70,76,135,9,38,+ 62,83,156,170,241,245,250,238,233,199,61,129,36,12,118,60,213,51,32,220,+ 228,42,225,253,64,39,243,237,93,234,138,168,206,124,36,165,175,99,143,130,+ 23,146,111,46,56,8,185,51,170,34,145,242,130,115,242,157,222,248,235,232,+ 107,71,94,179,77,27,88,49,133,150,84,17,33,137,229,155,45,199,66,49,+ 140,0,87,110,61,7,108,130,54,109,108,225,26,172,119,153,101,144,50,150,+ 35,7,128,121,234,14,56,244,255,0,26,93,122,196,220,106,145,92,68,130,+ 101,143,0,172,78,21,134,93,152,176,110,57,229,120,231,169,231,35,53,230,+ 87,78,149,117,7,181,143,66,60,174,46,251,156,148,227,203,133,84,50,29,+ 234,172,64,238,121,193,28,14,131,131,215,146,127,8,76,110,60,197,49,144,+ 209,253,252,140,21,228,14,127,26,238,224,130,222,38,13,20,17,66,229,58,+ 4,85,110,217,28,117,231,21,46,28,21,235,141,188,182,0,82,220,126,63,+ 254,186,217,123,63,230,64,100,217,90,139,59,24,146,69,216,89,114,114,7,+ 36,255,0,245,248,173,43,125,62,121,192,117,5,23,57,82,120,255,0,63,+ 94,105,218,100,9,36,173,36,184,242,211,25,4,245,39,167,215,158,223,74,+ 220,80,66,7,151,239,40,231,230,200,31,202,188,249,86,148,36,156,119,60,+ 250,112,231,124,236,167,253,152,100,219,230,200,48,57,33,87,161,246,38,154,+ 52,168,217,193,115,133,8,7,202,121,221,206,121,199,78,157,187,85,203,118,+ 98,128,22,14,48,10,190,115,184,122,240,0,252,170,41,26,68,181,102,19,+ 34,110,45,177,164,27,54,140,19,142,122,227,158,221,57,193,199,47,235,152,+ 139,219,152,232,140,84,116,72,132,105,113,43,169,71,147,223,35,255,0,213,+ 80,203,99,52,35,114,150,144,47,101,63,55,31,215,241,250,214,131,67,159,+ 60,96,252,228,56,218,197,121,0,14,160,231,176,233,250,243,79,147,113,25,+ 83,180,169,234,70,115,199,160,53,207,58,211,169,37,41,59,178,227,43,24,+ 165,183,103,32,156,241,147,237,197,33,206,64,3,169,237,218,175,77,108,204,+ 21,18,34,54,40,1,139,103,118,56,231,175,182,50,114,121,244,230,171,91,+ 205,28,131,106,49,118,7,104,223,128,127,167,245,224,227,165,28,209,78,215,+ 47,158,61,204,207,10,106,64,68,168,221,128,141,128,237,142,135,242,254,181,+ 212,66,234,241,97,10,231,110,71,205,187,142,128,147,223,167,255,0,94,185,+ 175,14,89,60,90,116,47,41,103,121,1,40,164,231,106,158,64,30,153,235,+ 198,58,243,210,183,90,222,120,10,184,97,142,140,20,231,7,140,102,183,171,+ 69,73,232,204,213,62,91,216,177,43,4,12,220,7,56,3,156,103,208,19,+ 143,94,63,26,96,148,131,128,160,2,123,247,168,213,157,203,23,64,184,56,+ 24,98,114,61,122,12,125,63,90,71,117,82,187,142,62,167,31,231,183,231,+ 93,180,176,112,140,109,37,118,106,160,146,212,122,77,150,44,192,51,125,208,+ 203,206,14,112,70,59,14,51,215,215,210,165,118,86,28,42,28,228,48,127,+ 151,129,158,216,228,103,240,231,63,90,228,23,4,21,89,17,248,192,29,142,+ 63,62,245,159,169,179,201,112,177,74,227,202,225,151,112,224,123,244,228,140,+ 87,46,43,13,26,75,158,59,7,178,190,145,53,5,221,185,99,251,228,15,+ 198,239,159,35,240,207,20,197,188,181,80,65,154,52,193,57,36,128,57,231,+ 250,254,121,172,37,0,228,54,225,159,78,180,223,179,188,175,229,198,160,25,+ 91,25,227,3,60,87,154,168,169,202,210,149,147,252,4,240,124,238,247,103,+ 255,217,+};+static const unsigned char pic5[] = {+ 255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,+ 255,219,0,67,0,2,1,1,1,1,1,2,1,1,1,2,2,2,2,2,+ 4,3,2,2,2,2,5,4,4,3,4,6,5,6,6,6,5,6,6,6,+ 7,9,8,6,7,9,7,6,6,8,11,8,9,10,10,10,10,10,6,8,+ 11,12,11,10,12,9,10,10,10,255,219,0,67,1,2,2,2,2,2,2,+ 5,3,3,5,10,7,6,7,10,10,10,10,10,10,10,10,10,10,10,10,+ 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,+ 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,255,194,+ 0,17,8,0,48,0,64,3,1,33,0,2,17,1,3,17,1,255,196,0,+ 28,0,0,2,3,1,0,3,0,0,0,0,0,0,0,0,0,0,5,7,+ 3,4,6,8,0,1,2,255,196,0,27,1,0,3,1,1,0,3,0,0,+ 0,0,0,0,0,0,0,0,4,5,6,7,1,0,2,3,255,218,0,12,+ 3,1,0,2,16,3,16,0,0,1,226,61,161,162,245,135,42,81,70,99,+ 24,185,221,226,212,59,127,25,29,7,242,133,98,99,181,229,129,230,199,52,+ 211,228,159,113,82,199,139,204,24,241,49,171,251,19,16,212,20,97,49,137,+ 223,200,104,68,136,250,253,209,189,177,153,130,200,88,245,9,169,253,212,161,+ 117,92,162,16,69,212,205,231,208,207,93,218,198,104,180,166,158,47,98,73,+ 199,199,152,59,255,196,0,32,16,0,2,3,1,0,2,2,3,0,0,0,+ 0,0,0,0,0,3,4,2,5,6,1,7,19,0,18,20,21,22,255,218,+ 0,8,1,1,0,1,5,2,98,126,153,98,243,48,183,214,183,83,92,226,+ 175,248,242,254,164,21,181,173,56,247,148,243,75,248,215,121,101,30,40,135,+ 83,104,125,204,227,205,161,171,204,208,82,209,150,114,152,249,38,163,237,164,+ 205,212,143,105,229,60,169,45,246,53,149,52,104,151,232,204,101,156,162,22,+ 115,59,81,142,185,180,143,241,18,107,228,124,126,145,79,28,37,120,142,230,+ 86,210,191,157,47,191,146,246,119,184,186,181,29,112,113,156,1,86,98,144,+ 46,21,229,105,201,89,217,124,111,242,37,11,26,102,11,18,211,90,168,207,+ 130,118,227,226,85,204,137,133,30,60,85,140,44,38,57,47,101,246,41,200,+ 35,70,58,10,89,20,122,60,232,35,225,236,211,72,229,75,79,115,85,49,+ 157,166,102,118,151,12,164,57,179,29,169,219,118,204,92,20,249,250,134,223,+ 99,255,196,0,49,17,0,1,2,4,4,4,5,2,7,1,0,0,0,0,+ 0,0,1,2,17,3,4,33,49,0,18,65,81,5,34,97,113,6,19,129,+ 145,161,66,193,20,35,50,130,177,209,225,240,255,218,0,8,1,3,1,1,+ 63,1,226,10,137,2,86,52,84,146,224,16,154,83,51,242,150,1,68,236,+ 192,255,0,24,240,103,18,136,132,196,128,174,66,121,133,254,163,114,74,137,+ 107,48,190,181,119,194,96,42,98,86,36,90,16,72,203,168,202,83,174,245,+ 235,81,140,200,36,33,44,237,234,90,239,187,88,156,120,131,141,205,8,222,+ 76,173,131,130,88,22,88,46,159,212,196,88,101,80,165,73,75,229,199,131,+ 33,37,70,34,179,59,37,47,163,57,52,26,158,225,171,153,156,12,216,145,+ 158,34,86,44,5,140,188,201,99,205,94,82,107,113,244,181,24,38,150,43,+ 81,37,33,106,202,145,216,158,215,249,223,163,223,16,191,21,53,197,242,198,+ 4,128,178,166,54,191,87,179,135,6,193,195,57,196,140,140,143,15,132,81,+ 8,40,3,103,47,223,246,210,148,38,245,119,56,146,156,68,35,204,159,86,+ 168,236,231,93,141,55,220,40,121,66,170,183,219,227,237,143,14,65,242,164,+ 225,197,66,152,173,193,160,212,148,214,132,155,208,223,65,108,4,19,31,42,+ 131,61,89,53,244,191,165,107,234,227,9,140,165,39,159,71,208,61,155,99,+ 96,5,237,112,196,147,136,92,98,78,10,194,84,72,55,177,118,114,228,144,+ 157,3,185,2,193,192,56,145,128,168,62,108,53,217,11,59,59,18,254,134,+ 182,234,13,70,36,228,224,196,130,241,84,222,221,135,189,123,226,36,21,202,+ 32,148,68,220,187,237,94,172,28,54,164,55,108,113,110,29,197,162,76,18,+ 96,42,128,89,39,109,192,255,0,126,113,48,136,115,69,150,158,154,252,255,+ 0,86,233,135,134,136,117,160,30,159,247,223,16,146,136,241,191,48,62,90,+ 141,110,193,253,233,237,137,73,73,201,136,158,108,52,58,70,148,21,125,251,+ 53,180,38,134,152,255,196,0,49,17,0,1,2,4,4,4,4,5,5,1,+ 0,0,0,0,0,0,1,2,17,3,4,18,33,0,5,34,49,6,50,65,+ 97,19,81,129,145,7,20,66,98,113,23,51,114,130,161,209,255,218,0,8,+ 1,2,1,1,63,1,151,137,243,19,169,73,217,252,152,210,111,125,175,73,+ 15,254,0,248,157,201,39,103,98,215,44,167,164,7,6,196,183,147,36,7,+ 54,220,223,233,1,177,30,110,26,115,120,50,157,86,133,170,255,0,106,145,+ 111,226,161,83,245,45,98,45,132,148,39,64,79,78,238,126,237,239,107,95,+ 167,71,196,156,99,47,61,13,38,224,169,143,94,99,238,24,108,223,128,116,+ 156,101,114,202,154,92,75,83,222,245,85,244,170,221,129,5,205,210,201,54,+ 36,99,142,81,11,46,248,130,149,175,246,233,42,63,219,155,187,223,74,88,+ 213,230,41,56,71,21,240,228,188,36,143,17,170,114,52,169,135,55,70,125,+ 195,6,246,166,233,135,155,39,44,155,133,57,31,145,17,18,166,190,166,80,+ 54,222,237,254,54,63,88,51,8,42,162,94,2,82,142,149,58,137,213,185,+ 98,144,5,59,132,130,106,5,148,30,217,239,21,76,113,38,100,137,185,144,+ 132,173,73,14,207,75,130,89,193,171,161,14,3,247,187,129,9,9,76,98,+ 143,102,184,5,246,27,212,236,195,165,197,241,155,152,235,152,20,15,52,139,+ 11,219,87,119,101,39,167,225,169,196,36,203,162,108,233,37,21,37,211,213,+ 216,186,124,58,202,136,26,147,85,85,38,196,148,168,182,2,160,196,41,115,+ 86,214,176,55,72,28,197,63,79,112,201,3,73,47,86,34,102,146,208,230,+ 153,113,92,173,37,87,185,164,2,72,118,103,1,37,153,137,13,96,77,35,+ 136,228,99,192,154,84,80,52,22,39,210,215,61,55,31,159,65,142,31,224,+ 196,102,242,34,106,109,101,148,5,59,5,88,53,201,171,75,134,13,184,191,+ 144,198,109,145,205,228,115,201,73,91,164,242,148,238,205,83,128,8,216,189,+ 223,113,81,1,195,207,65,154,156,49,32,161,4,181,52,247,244,99,81,189,+ 133,78,28,176,46,113,51,224,69,140,33,148,141,86,235,239,107,187,151,29,+ 253,113,47,45,224,67,68,4,167,96,44,157,173,255,0,60,183,111,54,199,+ 196,9,69,171,41,249,136,67,92,37,59,139,26,84,224,129,234,207,239,140,+ 158,103,40,144,205,32,46,105,122,18,160,165,144,21,112,157,84,139,131,169,+ 169,229,177,187,182,223,255,196,0,54,16,0,1,3,2,4,4,4,4,4,+ 6,3,0,0,0,0,0,1,2,3,17,4,33,0,18,19,49,5,34,65,+ 81,20,50,66,97,35,113,129,145,6,36,82,225,21,37,51,98,177,193,209,+ 240,241,255,218,0,8,1,1,0,6,63,2,28,64,240,210,254,155,201,67,+ 239,59,5,149,168,4,242,242,117,229,93,243,115,3,59,220,183,248,127,138,+ 112,71,232,244,152,81,226,205,56,233,68,161,41,73,79,41,25,147,42,203,+ 55,245,91,40,194,56,121,9,167,109,22,99,65,41,150,14,82,1,110,124,+ 170,78,233,34,226,49,76,162,167,42,22,206,116,84,166,158,178,62,17,153,+ 109,176,83,219,60,239,58,158,91,28,222,2,139,131,40,113,21,241,70,90,+ 166,101,72,248,104,81,43,248,74,212,59,230,203,101,126,147,56,52,41,98,+ 174,169,148,208,178,170,42,138,206,37,46,216,66,22,146,136,202,18,91,201,+ 148,250,83,108,178,146,27,101,47,178,173,118,25,90,194,39,152,243,194,147,+ 200,0,129,202,171,158,98,119,62,87,217,114,137,73,93,60,235,133,130,20,+ 223,48,77,199,206,223,92,83,86,185,225,91,105,42,45,151,27,35,84,0,+ 172,211,202,175,61,202,121,210,57,114,196,244,28,51,135,112,212,164,190,10,+ 29,210,206,84,103,210,102,230,47,247,192,79,165,80,76,28,58,164,48,130,+ 151,183,212,72,42,235,215,113,188,255,0,206,24,252,85,157,198,223,75,237,+ 41,213,164,5,12,169,80,157,193,139,64,181,224,123,153,165,227,148,20,105,+ 172,106,155,42,84,205,21,64,67,130,106,29,116,184,151,57,47,116,114,223,+ 206,187,130,51,224,84,112,254,13,73,70,234,233,238,132,83,182,219,145,108,+ 195,151,205,114,54,195,100,231,35,68,203,193,176,27,83,150,155,76,254,199,+ 20,212,181,180,250,69,198,193,94,112,57,150,173,239,214,252,184,21,108,52,+ 89,108,174,91,90,237,216,142,179,23,220,3,49,246,71,241,10,244,192,186,+ 146,203,55,6,14,202,39,223,183,249,192,85,74,178,32,83,160,13,37,115,+ 103,190,107,196,71,151,167,76,33,84,149,85,17,28,249,199,254,118,247,223,+ 26,172,169,215,210,214,193,149,202,237,17,189,230,221,251,206,9,90,74,164,+ 196,171,160,22,139,244,233,246,192,66,82,76,175,209,211,231,251,119,250,225,+ 202,206,34,83,225,216,203,153,10,87,153,74,60,162,61,87,27,119,140,38,+ 170,190,53,27,68,171,226,74,83,107,244,31,120,239,128,133,190,30,78,64,+ 166,234,66,243,107,36,204,42,66,66,126,216,113,212,113,86,25,212,91,158,+ 21,234,150,244,52,81,144,170,47,185,16,111,22,77,200,57,121,171,82,18,+ 127,48,226,93,78,155,203,106,84,18,145,5,73,84,250,6,192,8,232,111,+ 137,101,90,101,181,88,173,5,89,185,123,37,66,127,111,145,194,41,169,120,+ 106,198,131,33,33,229,59,58,129,48,47,36,255,0,108,18,169,60,211,176,+ 145,163,76,225,121,104,80,104,120,136,66,174,14,251,14,253,224,24,219,13,+ 210,58,54,74,105,223,66,58,71,145,125,226,63,223,108,4,211,56,222,125,+ 48,164,252,93,94,91,132,168,158,187,119,232,110,119,194,222,1,9,116,192,+ 65,205,25,251,2,98,194,73,29,119,193,72,105,41,5,102,36,239,108,23,+ 93,66,92,95,244,219,117,161,155,41,42,9,34,55,2,64,38,231,99,250,+ 70,37,12,50,160,172,201,121,53,39,33,41,76,222,8,230,25,163,218,21,+ 61,165,95,205,153,75,176,141,95,204,74,62,73,155,31,166,20,149,241,90,+ 102,178,168,149,168,172,37,50,76,252,189,66,253,76,253,41,42,43,148,183,+ 94,169,73,52,237,45,89,131,77,44,130,16,155,144,51,121,140,68,230,190,+ 195,8,171,110,160,101,242,186,134,215,33,10,49,25,132,127,217,247,195,134,+ 174,153,13,229,114,16,80,241,94,116,254,163,41,16,125,132,199,115,132,23,+ 92,203,115,230,94,88,247,191,188,15,168,194,154,91,45,212,50,245,178,1,+ 186,76,11,205,149,215,233,104,56,111,134,241,26,145,225,165,14,51,168,217,+ 200,147,223,107,145,19,212,143,174,8,119,56,153,3,38,227,223,253,227,192,+ 208,180,16,170,215,130,115,90,19,48,47,215,183,115,143,255,196,0,30,16,+ 1,1,1,1,1,1,1,0,3,1,0,0,0,0,0,0,1,17,33,49,+ 0,65,81,97,113,145,225,255,218,0,8,1,1,0,1,63,33,164,41,169,+ 99,156,138,105,103,157,94,15,86,47,86,5,81,189,43,96,243,108,130,140,+ 144,118,7,33,10,48,124,180,186,141,41,160,125,23,72,29,60,92,14,63,+ 60,131,20,1,90,85,218,22,100,114,176,5,230,37,165,47,124,154,114,160,+ 98,14,39,53,66,208,247,148,97,72,40,78,35,34,119,252,27,117,159,175,+ 59,180,179,32,126,204,230,177,96,228,212,64,187,46,51,195,86,182,132,250,+ 24,125,63,153,141,241,133,196,135,2,137,61,84,15,216,199,92,95,30,138,+ 97,244,81,156,2,120,117,21,160,109,200,76,255,0,212,160,196,226,33,164,+ 171,96,11,73,131,100,141,64,64,150,101,74,206,128,206,173,146,141,29,63,+ 228,68,56,142,228,236,42,122,39,85,52,49,135,16,162,2,190,54,224,243,+ 203,6,212,36,125,37,254,136,158,42,157,246,69,76,136,86,215,93,22,165,+ 137,184,232,222,122,36,225,135,153,173,26,138,189,40,87,244,138,210,111,157,+ 3,188,218,20,254,144,3,184,70,123,54,72,16,17,222,157,169,49,241,157,+ 241,130,227,108,36,100,195,15,218,235,140,68,36,75,17,134,47,29,14,240,+ 41,227,164,141,157,1,92,128,203,11,249,20,76,200,202,20,66,133,208,64,+ 41,188,236,0,203,140,164,31,93,4,250,210,14,234,42,107,140,105,227,245,+ 103,135,160,55,26,139,67,141,20,190,151,132,245,248,152,64,210,214,12,228,+ 17,74,46,9,5,231,10,173,7,149,201,131,196,206,164,118,105,123,77,235,+ 67,10,202,9,209,67,94,116,228,34,211,95,91,189,7,94,153,191,185,50,+ 6,96,30,168,161,56,206,188,62,49,17,83,69,54,4,82,116,37,24,95,+ 203,90,61,60,153,6,55,180,146,232,162,242,198,67,157,126,197,241,90,122,+ 5,63,64,158,43,126,240,28,152,40,37,160,10,200,44,6,98,203,251,128,+ 227,97,250,135,200,116,71,20,4,74,109,228,128,181,246,149,129,64,3,20,+ 34,149,244,223,218,251,1,43,30,6,2,134,183,156,4,234,94,15,192,138,+ 211,121,226,4,158,93,40,97,169,195,89,255,0,19,45,58,6,134,28,15,+ 232,96,92,15,216,30,255,218,0,12,3,1,0,2,0,3,0,0,0,16,+ 15,210,14,127,86,63,235,232,202,22,248,89,255,196,0,28,17,1,1,1,+ 1,1,1,1,1,1,0,0,0,0,0,0,0,1,17,33,49,65,0,81,+ 97,113,255,218,0,8,1,3,1,1,63,16,78,233,104,222,17,131,17,61,+ 110,225,57,104,28,188,71,0,17,228,137,186,54,112,71,233,11,11,225,230,+ 128,183,135,88,7,39,5,122,86,108,128,22,69,0,168,130,50,32,157,132,+ 3,182,70,12,220,160,189,107,119,10,92,25,44,200,114,134,54,40,76,75,+ 168,67,95,3,209,161,194,227,130,148,39,10,64,232,79,129,104,19,144,97,+ 136,49,72,172,46,173,245,39,160,167,80,129,81,48,141,213,74,77,145,82,+ 81,112,0,0,100,218,155,62,44,203,10,171,134,186,135,82,210,195,249,200,+ 128,233,170,192,1,132,176,165,216,33,69,134,150,150,80,95,22,170,125,33,+ 56,234,24,11,7,220,176,145,90,191,40,125,233,81,83,98,33,9,53,104,+ 88,199,181,131,104,204,51,9,168,176,36,95,144,65,81,0,170,170,43,166,+ 129,13,67,122,93,159,130,45,93,112,44,180,53,4,108,48,74,85,68,55,+ 10,25,107,10,168,57,220,193,5,186,12,58,211,122,254,113,159,22,191,172,+ 216,140,213,12,192,60,31,167,64,98,154,48,35,160,1,68,27,38,236,149,+ 208,58,144,232,174,191,193,40,133,177,255,196,0,26,17,1,1,1,1,1,+ 1,1,0,0,0,0,0,0,0,0,0,1,17,33,0,49,65,81,255,218,+ 0,8,1,2,1,1,63,16,120,74,217,22,84,77,80,147,8,18,161,64,+ 38,102,16,48,212,203,158,160,23,51,54,144,85,96,24,4,35,232,42,7,+ 50,150,208,43,130,63,5,98,135,197,193,229,6,9,13,20,109,10,33,16,+ 213,162,4,68,93,8,1,133,36,145,136,54,205,125,15,178,253,97,74,91,+ 100,66,177,110,252,53,196,230,171,171,233,2,250,87,8,119,176,48,17,152,+ 20,242,17,178,198,243,246,2,101,91,138,2,10,36,150,122,32,209,156,129,+ 86,137,104,63,138,228,241,5,112,224,4,0,0,32,170,89,147,175,58,227,+ 26,155,242,170,48,173,136,40,95,9,130,0,83,146,193,0,0,52,176,44,+ 128,244,60,121,65,13,10,130,215,8,212,172,2,138,180,131,0,3,157,80,+ 74,160,5,41,24,45,152,96,145,141,56,16,112,10,0,0,36,65,2,3,+ 48,217,224,52,89,128,210,151,96,49,14,54,181,16,51,239,98,86,8,103,+ 129,61,241,228,162,182,141,80,41,16,155,56,200,202,34,144,76,13,128,19,+ 74,130,58,113,88,72,168,32,163,42,168,95,5,66,245,119,63,13,102,95,+ 1,162,231,113,207,255,196,0,28,16,1,1,1,1,1,1,0,3,0,0,+ 0,0,0,0,0,0,1,17,0,33,49,65,81,113,145,255,218,0,8,1,+ 1,0,1,63,16,3,181,31,59,97,25,177,88,45,167,221,255,0,234,44,+ 54,195,4,7,25,147,200,48,115,120,209,36,97,116,139,208,233,73,240,228,+ 211,183,81,204,194,195,48,196,244,94,24,62,24,249,146,207,117,209,142,56,+ 192,149,149,55,60,156,38,227,161,68,53,101,80,133,138,168,81,40,103,23,+ 1,82,50,160,224,147,125,50,217,203,132,153,75,195,34,120,170,208,202,13,+ 78,230,205,20,226,20,51,222,203,1,138,12,177,40,199,228,152,167,117,160,+ 224,218,237,44,154,49,252,23,125,129,204,68,165,32,93,37,173,183,115,24,+ 15,2,108,96,149,146,99,201,67,222,150,11,51,136,184,207,197,130,39,91,+ 236,147,86,15,187,152,247,40,6,172,54,79,75,254,188,225,4,201,75,192,+ 168,35,9,86,164,94,247,58,224,225,211,100,182,16,254,96,1,20,80,42,+ 129,115,181,102,65,114,167,99,244,139,34,48,11,200,2,17,121,61,103,167,+ 189,1,32,101,181,118,61,156,98,99,176,43,117,105,12,4,40,73,11,116,+ 193,212,134,212,175,175,14,129,26,22,87,250,135,214,95,188,199,197,171,215,+ 34,93,189,245,177,84,78,66,169,12,51,50,157,73,180,109,21,181,156,135,+ 25,82,17,31,102,47,34,141,213,237,13,41,86,101,18,56,110,108,215,166,+ 168,138,2,175,153,231,125,237,7,138,244,20,83,74,80,80,16,239,28,104,+ 61,191,161,5,129,250,3,70,252,143,104,128,15,35,172,103,1,209,106,152,+ 59,116,209,65,143,217,185,25,4,103,150,64,201,141,210,125,209,172,116,8,+ 113,30,66,218,56,228,4,131,210,13,68,16,25,39,31,141,170,45,65,114,+ 70,227,60,121,82,150,146,5,37,97,89,96,23,38,14,100,88,130,248,80,+ 166,123,152,7,3,66,24,40,2,3,220,210,66,57,119,146,30,81,49,6,+ 128,179,92,77,98,194,159,80,101,48,196,138,251,211,233,182,64,64,212,125,+ 129,40,132,10,138,1,247,177,129,180,0,11,153,219,194,147,162,157,127,255,+ 217,+};+static const unsigned char pic6[] = {+ 255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,+ 255,219,0,67,0,6,4,5,6,5,4,6,6,5,6,7,7,6,8,10,+ 16,10,10,9,9,10,20,14,15,12,16,23,20,24,24,23,20,22,22,26,+ 29,37,31,26,27,35,28,22,22,32,44,32,35,38,39,41,42,41,25,31,+ 45,48,45,40,48,37,40,41,40,255,194,0,11,8,0,48,0,64,1,1,+ 17,0,255,196,0,27,0,0,2,3,1,1,1,0,0,0,0,0,0,0,+ 0,0,0,3,5,2,4,6,1,0,7,255,218,0,8,1,1,0,0,0,+ 1,249,171,45,222,25,115,116,213,220,106,219,175,175,43,70,68,225,196,122,+ 10,188,142,150,32,41,23,82,75,182,240,226,96,143,47,167,148,124,165,127,+ 63,255,196,0,33,16,0,2,2,1,4,3,1,1,0,0,0,0,0,0,+ 0,0,1,2,3,4,0,5,17,18,19,20,33,34,49,16,255,218,0,8,+ 1,1,0,1,5,2,111,89,74,14,203,111,26,50,189,41,99,17,161,103,+ 213,32,20,111,201,243,31,19,149,171,25,162,173,12,113,28,229,238,8,19,+ 205,213,43,246,92,142,56,212,251,202,241,116,87,138,180,146,103,137,203,60,+ 53,39,195,80,207,93,211,55,223,249,74,48,206,63,34,39,103,44,176,148,+ 198,223,36,136,156,49,58,182,133,111,226,50,10,185,227,156,246,197,127,100,+ 131,157,209,238,39,136,102,143,1,74,166,57,35,32,146,75,1,159,185,116,+ 179,74,51,172,187,127,255,196,0,43,16,0,1,3,3,3,3,3,3,5,+ 0,0,0,0,0,0,0,1,0,2,17,18,33,49,3,65,81,34,50,113,+ 16,97,240,19,35,145,98,114,129,161,193,255,218,0,8,1,1,0,6,63,+ 2,170,138,160,193,39,8,105,106,105,185,144,62,224,38,16,111,104,24,141,+ 188,38,101,196,88,210,237,184,31,55,84,183,79,238,253,64,26,54,241,117,+ 76,61,237,164,82,231,62,255,0,215,136,64,75,122,128,41,192,182,237,207,+ 178,99,141,32,98,70,126,121,84,49,153,177,143,71,88,95,148,221,123,135,+ 72,148,205,70,54,176,221,152,96,247,19,51,248,82,205,54,48,199,0,20,+ 51,140,197,165,48,56,68,133,80,176,217,10,221,248,11,170,194,54,229,10,+ 75,148,137,116,113,159,82,231,246,181,75,242,61,214,106,225,220,162,107,104,+ 153,164,187,166,22,167,234,189,140,124,194,181,161,0,214,30,145,153,202,176,+ 53,28,93,6,159,218,127,197,211,19,28,202,38,210,176,164,220,224,16,176,+ 61,234,178,239,108,239,123,35,214,208,152,95,37,206,192,224,20,12,249,132,+ 106,0,127,40,74,192,115,74,12,121,232,200,149,121,84,180,119,149,255,196,+ 0,36,16,1,0,2,2,2,2,1,5,1,1,0,0,0,0,0,0,1,+ 17,33,0,65,49,81,97,113,193,129,145,161,177,240,225,209,255,218,0,8,+ 1,1,0,1,63,33,82,56,133,179,96,170,244,220,216,224,205,170,177,32,+ 8,166,201,99,239,172,165,186,89,53,167,162,107,55,178,140,59,104,247,240,+ 238,127,134,180,162,205,189,163,158,156,124,0,127,165,210,144,28,29,26,164,+ 48,9,45,27,110,202,56,41,230,255,0,9,200,27,52,218,63,121,26,21,+ 124,75,51,79,55,20,226,34,112,152,134,118,37,157,63,219,197,74,211,141,+ 130,14,169,126,250,192,202,11,162,26,31,95,172,114,200,226,80,148,138,93,+ 211,203,239,15,49,241,60,193,92,235,32,186,60,5,79,174,28,173,145,152,+ 181,255,0,107,12,6,251,63,223,156,208,99,148,221,225,192,149,81,45,124,+ 167,233,198,176,81,229,78,9,33,118,105,243,141,179,44,213,227,50,1,203,+ 172,131,26,164,94,87,143,126,176,33,226,47,67,245,136,35,4,12,147,30,+ 232,15,182,4,166,160,41,137,139,235,215,29,197,203,14,193,146,240,28,143,+ 134,76,90,75,148,153,174,135,10,164,4,165,74,255,0,156,179,206,21,148,+ 12,42,63,31,56,93,32,25,168,229,253,231,47,110,65,211,165,119,198,24,+ 160,226,47,158,135,4,162,1,57,42,3,176,33,152,107,70,254,253,97,20,+ 179,32,208,79,139,39,247,131,170,171,183,232,154,192,195,29,181,64,239,231,+ 22,225,21,153,235,30,249,250,223,24,33,17,192,83,13,115,148,234,48,68,+ 164,239,131,237,141,73,30,216,254,255,0,113,18,141,22,60,124,239,52,171,+ 64,163,207,227,34,207,224,202,196,48,78,141,103,255,218,0,8,1,1,0,+ 0,0,16,128,99,218,231,181,30,255,196,0,29,16,1,1,0,2,3,1,+ 1,1,0,0,0,0,0,0,0,0,1,17,33,49,0,65,81,97,113,129,+ 255,218,0,8,1,1,0,1,63,16,164,182,97,6,95,53,90,209,179,13,+ 83,100,144,130,140,11,11,114,214,136,48,129,48,144,20,69,38,3,81,17,+ 7,141,74,213,160,60,133,173,90,230,176,98,188,44,13,147,184,115,165,65,+ 11,197,59,164,214,109,42,17,193,35,32,217,26,228,41,74,18,192,146,4,+ 104,45,37,171,121,78,232,193,112,25,14,162,13,119,202,181,193,8,23,98,+ 218,148,32,126,7,150,114,28,9,66,168,143,178,179,56,72,27,37,141,244,+ 215,167,223,120,67,54,38,0,86,72,185,43,15,237,228,112,210,130,43,133,+ 37,0,88,97,4,173,234,233,72,129,14,33,183,184,156,151,63,169,217,74,+ 43,69,210,234,254,80,180,152,145,180,18,180,116,89,28,182,114,121,193,74,+ 237,100,195,160,222,38,241,199,194,201,195,186,75,145,145,208,69,36,42,139,+ 215,90,10,66,52,88,102,106,165,247,139,192,77,39,54,70,3,19,162,197,+ 115,176,199,19,88,225,214,113,226,76,236,199,27,84,240,178,81,55,22,143,+ 123,107,200,144,85,72,104,4,12,245,0,62,7,92,186,6,5,234,111,53,+ 206,166,61,62,188,105,49,177,44,134,78,199,233,155,215,32,112,212,20,144,+ 177,129,214,208,219,161,225,163,18,2,139,19,81,1,158,191,40,147,209,135,+ 46,128,69,135,40,8,82,156,44,67,181,50,0,44,78,200,4,100,115,91,+ 82,97,22,69,104,11,189,62,107,79,4,129,21,145,133,106,251,10,45,180,+ 88,39,85,49,35,150,195,99,240,48,99,192,147,136,14,248,57,100,163,172,+ 252,28,22,14,18,220,165,140,147,44,185,18,178,240,128,66,56,25,114,17,+ 130,169,222,217,158,57,11,41,90,184,184,207,243,240,249,57,84,84,47,48,+ 148,102,128,178,234,156,56,205,26,184,166,107,20,224,23,82,133,160,226,57,+ 37,185,184,178,125,67,247,133,10,102,203,94,214,46,21,28,183,232,74,193,+ 138,160,67,168,8,185,42,33,68,61,227,138,134,162,102,243,140,247,244,226,+ 99,42,174,228,101,96,190,40,67,47,25,179,151,72,13,84,82,149,14,242,+ 61,230,21,144,33,148,4,69,131,110,177,0,93,146,6,175,111,170,67,40,+ 167,21,7,239,1,136,40,134,52,251,156,125,254,76,92,99,51,237,5,64,+ 94,211,1,114,135,176,57,255,217,+};+static const unsigned char pic7[] = {+ 255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,+ 255,219,0,67,0,5,3,4,4,4,3,5,4,4,4,5,5,5,6,7,+ 12,8,7,7,7,7,15,11,11,9,12,17,15,18,18,17,15,17,17,19,+ 22,28,23,19,20,26,21,17,17,24,33,24,26,29,29,31,31,31,19,23,+ 34,36,34,30,36,28,30,31,30,255,219,0,67,1,5,5,5,7,6,7,+ 14,8,8,14,30,20,17,20,30,30,30,30,30,30,30,30,30,30,30,30,+ 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,+ 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,255,192,+ 0,17,8,0,24,0,40,3,1,18,0,2,17,1,3,17,1,255,196,0,+ 26,0,0,3,0,3,1,0,0,0,0,0,0,0,0,0,0,0,0,4,+ 5,1,2,3,8,255,196,0,39,16,0,3,1,0,2,2,2,1,2,7,+ 0,0,0,0,0,0,1,2,3,4,5,17,18,33,0,19,34,6,50,20,+ 21,35,36,49,65,66,255,196,0,25,1,0,2,3,1,0,0,0,0,0,+ 0,0,0,0,0,0,0,1,2,0,3,5,4,255,196,0,42,17,0,1,+ 2,3,7,3,4,3,0,0,0,0,0,0,0,0,1,2,17,0,33,65,+ 3,18,49,81,97,177,240,34,145,161,113,129,193,209,4,19,241,255,221,0,+ 4,0,10,255,218,0,12,3,1,0,2,17,3,17,0,63,0,242,135,233,+ 252,107,208,211,29,154,22,182,205,162,43,56,104,156,168,106,64,66,157,121,+ 23,51,41,80,72,9,221,63,57,168,253,206,170,241,124,158,73,87,79,243,+ 94,34,28,162,106,117,52,118,171,202,210,252,187,102,147,169,241,86,32,145,+ 249,171,175,248,62,39,175,150,61,154,73,36,94,15,33,132,166,103,58,179,+ 12,137,7,0,98,18,26,99,156,246,104,114,179,124,121,113,243,45,133,87,+ 35,45,7,26,154,177,150,157,66,209,195,6,160,85,91,82,101,209,189,134,+ 82,58,86,232,1,51,207,146,28,190,217,102,228,121,123,104,88,114,214,95,+ 29,252,132,222,140,230,64,77,156,88,161,102,65,228,65,84,44,71,136,4,+ 18,171,242,130,130,160,234,50,203,182,53,216,28,38,198,7,82,212,111,1,+ 63,106,246,216,1,74,194,80,209,138,88,146,107,15,238,40,253,90,212,159,+ 216,81,58,101,254,152,36,47,228,31,216,101,236,25,169,87,29,144,24,195,+ 159,56,200,186,53,236,91,98,156,94,141,20,74,159,11,185,100,89,119,208,+ 85,161,8,41,229,217,95,21,255,0,166,83,63,140,84,128,83,120,60,245,+ 204,101,65,226,2,64,188,75,159,57,115,79,73,197,46,87,144,109,218,169,+ 203,242,124,102,29,209,72,71,37,124,43,24,148,243,86,105,52,211,63,128,+ 86,89,32,95,201,93,85,135,78,61,170,137,109,104,207,197,161,245,195,45,+ 41,167,232,149,224,151,178,35,168,64,93,204,212,56,255,0,65,135,181,101,+ 118,85,82,71,108,158,160,10,64,26,72,54,7,157,157,161,144,146,128,18,+ 144,0,2,84,12,228,229,57,191,174,17,137,227,219,40,211,144,199,133,140,+ 39,156,139,138,42,92,201,89,38,141,87,82,59,68,102,178,120,57,3,219,+ 40,86,44,189,252,219,6,173,37,111,183,101,134,156,205,52,195,117,174,146,+ 104,80,161,19,34,98,138,238,39,245,43,40,253,138,211,152,111,68,41,69,+ 186,154,92,241,161,175,185,156,19,214,64,35,106,86,156,169,198,56,108,217,+ 252,92,188,52,236,189,204,164,159,75,210,42,92,176,73,167,214,207,229,229,+ 245,170,167,75,236,128,20,116,171,228,122,62,88,84,240,0,3,1,21,70,+ 73,242,156,83,232,60,94,183,215,158,104,172,252,106,77,227,56,44,208,154,+ 213,16,121,7,10,174,88,177,252,217,201,37,12,152,57,242,36,155,69,245,+ 28,6,206,112,223,58,198,168,252,43,53,37,75,36,244,132,157,38,151,159,+ 60,206,39,94,57,227,29,179,197,201,155,50,0,148,250,215,235,141,230,40,+ 65,42,93,131,176,236,69,194,148,7,219,18,23,195,217,240,165,46,147,160,+ 249,17,141,105,106,164,41,9,77,100,117,145,63,3,77,30,112,231,5,179,+ 84,120,91,125,252,166,118,226,179,236,147,211,137,208,236,227,67,208,50,179,+ 164,129,3,176,136,65,160,100,101,5,64,96,72,248,124,1,9,107,228,57,+ 31,223,129,198,102,97,121,249,207,184,71,141,8,228,46,157,81,205,140,135,+ 163,77,217,217,43,73,76,148,86,68,62,64,185,62,1,189,0,104,222,192,+ 7,163,224,83,148,227,193,29,63,173,41,179,77,171,77,200,236,19,247,180,+ 127,255,217,+};+static const unsigned char pic8[] = {+ 255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,+ 255,219,0,67,0,6,4,5,6,5,4,6,6,5,6,7,7,6,8,10,+ 16,10,10,9,9,10,20,14,15,12,16,23,20,24,24,23,20,22,22,26,+ 29,37,31,26,27,35,28,22,22,32,44,32,35,38,39,41,42,41,25,31,+ 45,48,45,40,48,37,40,41,40,255,219,0,67,1,7,7,7,10,8,10,+ 19,10,10,19,40,26,22,26,40,40,40,40,40,40,40,40,40,40,40,40,+ 40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,+ 40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,255,194,+ 0,17,8,0,48,0,64,3,1,34,0,2,17,1,3,17,1,255,196,0,+ 26,0,0,2,3,1,1,0,0,0,0,0,0,0,0,0,0,0,5,6,+ 2,3,4,1,7,255,196,0,24,1,0,3,1,1,0,0,0,0,0,0,+ 0,0,0,0,0,0,1,4,5,2,3,255,221,0,4,0,4,255,218,0,+ 12,3,1,0,2,16,3,16,0,0,1,243,82,92,107,42,145,70,127,31,+ 77,95,63,46,195,40,245,16,243,250,117,221,199,255,208,10,98,108,179,229,+ 143,137,10,11,121,232,41,98,219,93,225,44,67,127,255,209,26,236,154,206,+ 130,250,235,170,47,247,182,236,130,83,6,235,88,228,145,255,196,0,33,16,+ 0,2,2,1,4,3,1,1,0,0,0,0,0,0,0,0,1,2,3,4,+ 0,5,17,18,19,20,33,34,49,16,255,221,0,4,0,8,255,218,0,8,+ 1,1,0,1,5,2,111,89,74,14,203,111,26,50,189,41,99,17,161,103,+ 213,32,20,111,201,243,31,19,159,255,208,173,88,205,21,104,99,136,231,47,+ 112,64,158,110,169,95,178,228,113,198,167,222,127,255,209,175,23,69,120,171,+ 73,38,120,156,179,195,82,124,53,12,245,221,51,125,255,0,159,255,210,165,+ 24,103,31,145,19,179,150,88,74,99,111,146,68,78,24,157,91,255,211,208,+ 173,252,70,65,87,60,115,158,216,175,236,144,115,186,61,196,241,12,255,212,+ 209,224,41,84,199,36,100,18,73,96,51,247,46,150,105,70,117,151,111,255,+ 196,0,33,17,0,2,1,3,4,3,1,0,0,0,0,0,0,0,0,0,+ 1,2,0,3,18,33,4,17,19,49,16,81,225,240,255,221,0,4,0,4,+ 255,218,0,8,1,3,1,1,63,1,175,168,85,192,253,246,104,93,179,22,+ 210,167,126,252,127,255,208,164,151,155,204,85,11,128,34,182,210,245,247,63,+ 255,209,20,237,234,4,0,78,29,240,176,232,57,15,102,127,255,196,0,32,+ 17,0,2,2,3,0,2,3,1,0,0,0,0,0,0,0,0,1,2,0,+ 3,4,17,18,34,49,19,20,33,82,255,218,0,8,1,2,1,1,63,1,+ 70,242,27,131,28,90,102,72,52,228,138,207,168,62,63,236,79,255,208,124,+ 134,173,129,95,115,239,229,111,93,203,47,123,91,167,59,157,40,51,255,209,+ 186,128,231,242,83,129,90,175,144,217,153,152,139,72,237,32,160,88,220,179,+ 104,25,255,196,0,43,16,0,1,3,3,3,3,3,3,5,0,0,0,0,+ 0,0,0,1,0,2,17,18,33,49,3,65,81,34,50,113,16,97,240,19,+ 35,145,98,114,129,161,193,255,221,0,4,0,8,255,218,0,8,1,1,0,+ 6,63,2,170,138,160,193,39,8,105,106,105,185,144,62,224,38,16,111,104,+ 24,141,188,38,101,196,88,210,237,184,31,55,84,183,79,238,253,64,26,54,+ 241,117,76,61,237,164,82,231,62,255,0,215,136,64,75,122,128,41,192,182,+ 237,207,178,255,208,99,141,32,98,70,126,121,84,49,153,177,143,71,88,95,+ 148,221,123,135,72,148,205,70,54,176,221,152,96,247,19,51,248,82,205,54,+ 48,199,0,20,51,140,197,165,127,255,209,96,112,137,10,161,97,178,21,187,+ 240,23,85,132,109,202,20,151,41,18,232,227,62,191,255,210,46,127,107,84,+ 191,35,221,102,174,29,202,38,182,137,154,75,186,97,106,126,171,216,199,204,+ 43,90,16,13,97,233,25,156,171,3,81,197,215,255,211,13,63,180,255,0,+ 139,166,38,57,148,77,165,97,73,185,192,33,96,123,213,101,222,217,222,246,+ 71,173,161,127,255,212,97,124,151,59,3,128,80,51,230,17,168,1,252,161,+ 43,1,205,40,49,231,163,34,85,229,82,209,222,87,255,196,0,36,16,1,+ 0,2,2,2,2,1,5,1,1,0,0,0,0,0,0,1,17,33,0,65,+ 49,81,97,113,193,129,145,161,177,240,225,209,255,218,0,8,1,1,0,1,+ 63,33,82,56,133,179,96,170,244,220,216,224,205,170,177,32,8,166,201,99,+ 239,172,165,186,89,53,167,162,107,55,178,140,59,104,247,240,238,127,134,180,+ 162,205,189,163,158,156,124,0,127,165,210,144,28,29,26,164,48,9,45,27,+ 110,202,56,41,230,255,0,9,200,27,52,218,63,121,255,208,141,10,190,37,+ 153,167,155,138,113,17,56,76,67,59,18,206,159,237,226,165,105,198,193,7,+ 84,191,125,96,101,5,209,13,15,175,214,57,100,113,40,74,69,46,233,229,+ 247,135,152,248,158,96,174,117,144,93,30,2,167,215,63,255,209,57,91,35,+ 49,107,254,214,24,13,246,127,191,57,160,199,41,187,195,129,42,162,90,249,+ 79,211,141,96,163,202,156,18,66,236,211,231,27,102,89,171,198,100,3,151,+ 89,255,210,131,26,164,94,87,143,126,176,33,226,47,67,245,136,35,4,12,+ 147,30,232,15,182,4,166,160,41,137,139,235,215,29,197,203,14,193,146,240,+ 28,143,134,76,90,75,148,153,174,135,10,164,4,165,74,255,0,156,179,206,+ 21,148,12,42,63,31,57,255,211,46,144,12,212,114,254,243,151,183,32,233,+ 210,187,227,12,80,113,23,207,67,130,81,0,156,149,1,216,16,204,53,163,+ 127,126,176,138,89,144,104,39,197,147,251,193,213,85,219,244,77,96,97,142,+ 218,160,119,243,159,255,212,91,132,86,103,172,123,231,235,124,96,132,71,1,+ 76,53,206,83,168,193,18,147,190,15,182,53,36,123,99,251,253,196,74,52,+ 88,241,243,188,210,173,2,143,63,140,139,63,131,43,16,193,58,53,159,255,+ 221,0,4,0,4,255,218,0,12,3,1,0,2,0,3,0,0,0,16,152,+ 128,15,255,208,245,137,47,255,209,135,71,91,255,196,0,33,17,0,2,2,+ 1,3,5,1,0,0,0,0,0,0,0,0,0,1,17,0,33,240,49,65,+ 129,97,113,145,161,177,241,255,218,0,8,1,3,1,1,63,16,6,92,73,+ 98,173,123,225,191,153,80,52,163,201,237,167,221,33,181,106,31,54,150,208,+ 159,255,208,56,142,81,36,141,239,247,42,233,48,245,11,112,115,51,104,15,+ 72,57,247,63,255,209,11,131,108,177,211,164,191,92,82,98,243,134,224,220,+ 17,217,172,214,127,255,196,0,36,17,0,1,3,3,3,5,1,1,0,0,+ 0,0,0,0,0,0,1,17,33,49,0,65,97,81,129,177,113,145,161,193,+ 209,225,240,255,218,0,8,1,2,1,1,63,16,9,22,190,126,241,181,18,+ 40,45,148,230,242,157,38,139,156,10,155,57,8,124,121,233,64,32,40,23,+ 35,238,61,45,127,255,208,128,1,85,144,161,226,249,208,26,6,137,102,135,+ 123,14,93,50,18,129,20,144,57,239,103,186,98,155,214,66,218,37,113,31,+ 147,95,255,209,61,38,132,246,38,128,2,192,10,190,135,246,194,132,4,80,+ 72,211,77,151,44,75,96,104,34,4,144,131,83,217,245,48,30,191,255,196,+ 0,29,16,1,1,0,2,3,1,1,1,0,0,0,0,0,0,0,0,1,+ 17,33,49,0,65,81,97,113,129,255,221,0,4,0,8,255,218,0,8,1,+ 1,0,1,63,16,164,182,97,6,95,53,90,209,179,13,83,100,144,130,140,+ 11,11,114,214,136,48,129,48,144,20,69,38,3,81,17,7,141,74,213,160,+ 60,133,173,90,230,176,98,188,44,13,147,184,115,165,65,11,197,59,164,214,+ 109,42,17,193,35,32,217,26,228,41,74,18,192,146,4,104,45,37,171,121,+ 78,232,193,112,25,14,162,13,119,207,255,208,171,92,16,129,118,45,169,66,+ 7,224,121,103,33,192,148,42,136,251,43,51,132,129,178,88,223,77,122,125,+ 247,132,51,98,96,5,100,139,146,176,254,222,71,13,40,34,184,82,80,5,+ 134,16,74,222,174,148,136,16,226,27,123,137,201,115,250,157,148,162,180,93,+ 46,175,229,11,73,137,27,65,43,71,69,145,203,103,63,255,209,158,112,82,+ 187,89,48,232,55,137,188,113,240,178,112,238,146,228,100,116,17,73,10,162,+ 245,214,130,144,141,22,25,154,169,125,226,240,19,73,205,145,128,196,232,177,+ 92,236,49,196,214,56,117,156,120,147,59,49,198,213,60,44,148,77,197,163,+ 222,218,242,36,21,82,26,1,3,61,64,15,129,215,46,129,129,122,155,205,+ 115,169,143,79,175,63,255,210,105,49,177,44,134,78,199,233,155,215,32,112,+ 212,20,144,177,129,214,208,219,161,225,163,18,2,139,19,81,1,158,191,40,+ 147,209,135,46,128,69,135,40,8,82,156,44,67,181,50,0,44,78,200,4,+ 100,115,91,82,97,22,69,104,11,189,62,107,79,4,129,21,145,133,106,251,+ 10,45,180,88,39,85,49,35,150,195,99,240,48,99,207,255,211,9,56,128,+ 239,131,150,74,58,207,193,193,96,225,45,202,88,201,50,203,145,43,47,8,+ 4,35,129,151,33,24,42,157,237,153,227,144,178,149,171,139,140,255,0,63,+ 15,147,149,69,66,243,9,70,104,11,46,169,195,140,209,171,138,102,177,78,+ 1,117,40,90,14,35,146,91,155,139,39,212,63,120,80,166,108,181,237,98,+ 225,81,203,126,135,255,212,149,131,21,64,135,80,17,114,84,66,136,123,199,+ 21,13,68,205,231,25,239,233,196,198,85,93,200,202,193,124,80,134,94,51,+ 103,46,144,26,168,165,42,29,228,123,204,43,32,67,40,8,139,6,221,98,+ 0,187,36,13,94,223,84,134,81,78,42,15,222,3,16,81,12,105,247,56,+ 251,252,152,184,198,103,218,10,128,189,166,2,229,15,96,115,255,217,+};+static const unsigned char pic9[] = {+ 255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,+ 255,219,0,67,0,16,11,12,14,12,10,16,14,13,14,18,17,16,19,24,+ 40,26,24,22,22,24,49,35,37,29,40,58,51,61,60,57,51,56,55,64,+ 72,92,78,64,68,87,69,55,56,80,109,81,87,95,98,103,104,103,62,77,+ 113,121,112,100,120,92,101,103,99,255,219,0,67,1,17,18,18,24,21,24,+ 47,26,26,47,99,66,56,66,99,99,99,99,99,99,99,99,99,99,99,99,+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,255,194,+ 0,17,8,0,5,0,7,3,1,17,0,2,17,1,3,17,1,255,196,0,+ 20,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,255,+ 196,0,21,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,+ 2,4,255,218,0,12,3,1,0,2,16,3,16,0,0,1,14,63,255,196,+ 0,22,16,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,2,+ 1,17,255,218,0,8,1,1,0,1,5,2,56,103,255,196,0,23,17,1,+ 0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,17,33,255,+ 218,0,8,1,3,1,1,63,1,119,138,127,255,196,0,23,17,0,3,1,+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,17,255,218,0,+ 8,1,2,1,1,63,1,151,176,127,255,196,0,24,16,0,2,3,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,1,2,0,17,33,255,218,0,8,+ 1,1,0,6,63,2,96,185,70,127,255,196,0,23,16,0,3,1,0,0,+ 0,0,0,0,0,0,0,0,0,0,0,0,1,17,113,255,218,0,8,1,+ 1,0,1,63,33,150,161,235,79,255,218,0,12,3,1,0,2,0,3,0,+ 0,0,16,255,0,255,196,0,24,17,1,1,0,3,0,0,0,0,0,0,+ 0,0,0,0,0,0,1,33,0,161,209,255,218,0,8,1,3,1,1,63,+ 16,69,22,186,135,115,255,196,0,22,17,1,1,1,0,0,0,0,0,0,+ 0,0,0,0,0,0,0,17,0,33,255,218,0,8,1,2,1,1,63,16,+ 88,203,255,196,0,25,16,1,0,2,3,0,0,0,0,0,0,0,0,0,+ 0,0,0,1,17,33,0,49,97,255,218,0,8,1,1,0,1,63,16,16,+ 172,148,110,219,123,25,255,217,+};++static const struct pic PICS[] = {+ {"h_c3_d4_i1_9x13.png", 9, 13, 0xfd31a6e59ad6a279ULL, 239, pic0},+ {"h_c0_d16_i1_9x13.png", 9, 13, 0xaa84bd7d6bab45d8ULL, 328, pic1},+ {"h_c6_d16_i0_9x13.png", 9, 13, 0xab2fd9de7f4fa975ULL, 1017, pic2},+ {"rgb8_7x5.png", 7, 5, 0x05f02a716f9ff53fULL, 131, pic3},+ {"q50_s2_64x48.jpg", 64, 48, 0x3d824e6215a1c6efULL, 1582, pic4},+ {"q95_s1p_64x48.jpg", 64, 48, 0x8f3480447c00c794ULL, 4221, pic5},+ {"grayp_64x48.jpg", 64, 48, 0x0f584b48a860eb55ULL, 1589, pic6},+ {"tiny_1x2_rst.jpg", 40, 24, 0x9f52c1b0209d9e41ULL, 1063, pic7},+ {"rstp_64x48.jpg", 64, 48, 0x38c04c87f2e79569ULL, 2278, pic8},+ {"q50_s0p_7x5.jpg", 7, 5, 0x273df216ac495029ULL, 568, pic9},+};diff --git a/tests/test_jns.c b/tests/test_jns.cindex dc386e0..cc216ec 100644--- a/tests/test_jns.c+++ b/tests/test_jns.c@@ -145,11 +145,21 @@ static void check_accepted(const struct janas_jns *j)for (uint32_t l = 0; l < j->h.n_layer; l++)for (uint32_t e = 0; e < j->h.n_expert; e++) {uint64_t off = janas_jns_expert_offset(j, l, e);+ if (off == 0 && j->expert_sum[(size_t)l * j->h.n_expert + e] == 0)+ continue; /* absent (Nemotron-H: fewer experts a layer) */CHECK(off >= j->h.experts_offset &&off + j->layers[l].slot_bytes <=j->h.experts_offset + j->h.experts_bytes,"accepted an expert outside the expert region");}+ for (uint64_t t = 0; t < j->h.n_tensor; t++) {+ const struct janas_jns_tensor *tn = &j->tensors[t];+ uint64_t n = 1;+ for (uint32_t d = 0; d < tn->n_dims; d++)+ n *= tn->dims[d];+ CHECK(tn->type != 0 || n * 4 == tn->bytes,+ "accepted a tensor whose shape is not its bytes");+ }}int main(void)@@ -213,6 +223,30 @@ int main(void)sizeof(err)) != 0,"wrong file size accepted");+ /* a tensor whose shape says more than its bytes: a reader trusting+ the dims would read past the resident region */+ {+ static const uint64_t shapes[][3] = {+ {1000, 1, 1}, {4, 2, 2}, {0, 1, 2}, {4, 0, 2}, {4, 1u << 31, 2}};+ for (size_t i = 0; i < sizeof(shapes) / sizeof(shapes[0]); i++) {+ memcpy(img, base, sizeof(img));+ struct janas_jns_header hh;+ struct janas_jns_tensor tt;+ memcpy(&hh, img, sizeof(hh));+ memcpy(&tt, img + hh.tensor_dir_offset, sizeof(tt));+ tt.dims[0] = shapes[i][0];+ tt.dims[1] = shapes[i][1];+ tt.n_dims = (uint32_t)shapes[i][2];+ memcpy(img + hh.tensor_dir_offset, &tt, sizeof(tt));+ seal(img);+ rc = janas_jns_parse(img, sizeof(img), file_bytes, &j, err,+ sizeof(err));+ CHECK(rc != 0, "tensor shape %zu accepted against its bytes", i);+ if (rc == 0)+ janas_jns_close(&j);+ }+ }+int accepted_flips = 0, accepted_fields = 0;for (int iter = 0; iter < 20000; iter++) {memcpy(img, base, sizeof(img));diff --git a/tests/test_sandbox.c b/tests/test_sandbox.cnew file mode 100644index 0000000..acf6fae--- /dev/null+++ b/tests/test_sandbox.c@@ -0,0 +1,256 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * test_sandbox.c - the cage of src/common/sandbox.c, tried as code that+ * wants out would: the user's files, writing outside /mnt/data, the+ * network, other processes, new processes, too much memory, files too+ * large, a /tmp filled, a loop that never ends; and what it must allow+ * (threads, the data directory, numpy where the system has it). Skipped+ * where the cage cannot be built (no user namespaces) or there is no+ * python3.+ */+#include "common/sandbox.h"++#ifdef _WIN32+#include <stdio.h>++int main(void)+{+ printf("test_sandbox: skipped (Linux only)\n");+ return 0;+}+#else++#include <errno.h>+#include <poll.h>+#include <signal.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include <sys/wait.h>+#include <time.h>+#include <unistd.h>++static int failures;++#define CHECK(c, ...) \+ do { \+ if (!(c)) { \+ fprintf(stderr, "test_sandbox: " __VA_ARGS__); \+ fputc('\n', stderr); \+ failures++; \+ } \+ } while (0)++static char data[256];++/* python's code run in the cage: its output (and error) into out, the exit+ status returned; -2 when it ran past secs and was killed. */+static int run(const char *code, char *out, size_t cap, int secs)+{+ char *argv[] = {"/usr/bin/python3", "-I", "-c", (char *)code, NULL};+ char *envp[] = {"PATH=/usr/bin:/bin", "HOME=/tmp", "LANG=C.UTF-8",+ "OPENBLAS_NUM_THREADS=2", NULL};+ const char *ro[] = {NULL};+ struct janas_sandbox_cfg c = {.data_dir = data,+ .ro = ro,+ .mem_bytes = (uint64_t)1 << 30,+ .file_bytes = (uint64_t)8 << 20,+ .tmp_bytes = (uint64_t)16 << 20};+ struct janas_sandbox sb;+ char why[256];+ if (janas_sandbox_spawn(&c, argv, envp, &sb, why, sizeof why) != 0) {+ snprintf(out, cap, "spawn: %s", why);+ return -1;+ }+ close(sb.in);+ sb.in = -1;+ size_t n = 0;+ out[0] = 0;+ time_t end = time(NULL) + secs;+ int open_fds = 2, killed = 0;+ while (open_fds > 0) {+ struct pollfd p[2] = {{sb.out, POLLIN, 0}, {sb.err, POLLIN, 0}};+ int left = (int)(end - time(NULL));+ if (left <= 0) {+ killed = 1;+ break;+ }+ if (poll(p, 2, left * 1000) <= 0)+ continue;+ for (int i = 0; i < 2; i++)+ if (p[i].revents & (POLLIN | POLLHUP)) {+ char buf[512];+ ssize_t k = read(i ? sb.err : sb.out, buf, sizeof buf);+ if (k <= 0) {+ p[i].fd = -1;+ if (i)+ sb.err = (close(sb.err), -1);+ else+ sb.out = (close(sb.out), -1);+ open_fds--;+ continue;+ }+ size_t m = (size_t)k < cap - 1 - n ? (size_t)k : cap - 1 - n;+ memcpy(out + n, buf, m);+ n += m;+ out[n] = 0;+ }+ }+ if (killed) {+ pid_t pid = sb.pid;+ janas_sandbox_kill(&sb);+ /* nothing left of it */+ CHECK(kill(pid, 0) != 0, "the cage outlived its kill");+ return -2;+ }+ int st = 0;+ while (waitpid(sb.pid, &st, 0) < 0 && errno == EINTR)+ ;+ sb.pid = 0;+ janas_sandbox_kill(&sb);+ return WIFEXITED(st) ? WEXITSTATUS(st) : 128 + WTERMSIG(st);+}++/* code must print "ok" and end well */+static void expect_ok(const char *name, const char *code)+{+ char out[4096];+ int rc = run(code, out, sizeof out, 30);+ CHECK(rc == 0 && strstr(out, "ok"), "%s: exit %d, output: %s", name, rc,+ out);+}++int main(int argc, char **argv)+{+ janas_sandbox_main(argc, argv);+ char why[256];+ if (!janas_sandbox_available(why, sizeof why)) {+ printf("test_sandbox: skipped (%s)\n", why);+ return 0;+ }+ if (access("/usr/bin/python3", X_OK) != 0) {+ printf("test_sandbox: skipped (no /usr/bin/python3)\n");+ return 0;+ }+ snprintf(data, sizeof data, "%s/tmp/janas-sandbox-test-XXXXXX",+ getenv("HOME") ? getenv("HOME") : "/tmp");+ char parent[256];+ snprintf(parent, sizeof parent, "%s/tmp",+ getenv("HOME") ? getenv("HOME") : "/tmp");+ mkdir(parent, 0700);+ if (!mkdtemp(data)) {+ perror("mkdtemp");+ return 1;+ }+ char code[2048];++ expect_ok("plain", "print('ok')");+ /* the user's files are not there */+ snprintf(code, sizeof code,+ "import os\n"+ "assert not os.path.exists('/home'), 'home'\n"+ "assert not os.path.exists('%s'), 'the data dir by its host "+ "path'\n"+ "assert os.listdir('/etc') and not os.path.exists('/etc/"+ "passwd'), 'etc'\n"+ "assert os.getcwd() == '/mnt/data'\n"+ "print('ok')",+ data);+ expect_ok("files", code);+ /* writing: only in /mnt/data, and it reaches the host */+ expect_ok("write",+ "import os\n"+ "for p in ('/usr/x', '/etc/x', '/x', '/proc/x'):\n"+ " try:\n"+ " open(p, 'w').write('x'); raise SystemExit('wrote '"+ " + p)\n"+ " except OSError: pass\n"+ "open('/mnt/data/hello.txt', 'w').write('hi')\n"+ "print('ok')");+ char hp[512];+ snprintf(hp, sizeof hp, "%s/hello.txt", data);+ FILE *f = fopen(hp, "r");+ char hb[8] = "";+ if (f) {+ if (!fgets(hb, sizeof hb, f))+ hb[0] = 0;+ fclose(f);+ }+ CHECK(!strcmp(hb, "hi"), "the file written in /mnt/data is not on the "+ "host");+ unlink(hp);+ /* no network */+ expect_ok("network",+ "import socket\n"+ "for host in (('1.1.1.1', 53), ('127.0.0.1', 22)):\n"+ " s = socket.socket(); s.settimeout(2)\n"+ " try:\n"+ " s.connect(host); raise SystemExit('connected')\n"+ " except OSError: pass\n"+ "print('ok')");+ /* no other process to see, no new process, no privilege */+ expect_ok("processes",+ "import os, subprocess\n"+ "pids = [p for p in os.listdir('/proc') if p.isdigit()]\n"+ "assert pids == ['1'], pids\n"+ "try:\n"+ " os.fork(); raise SystemExit('forked')\n"+ "except OSError: pass\n"+ "try:\n"+ " subprocess.run(['/usr/bin/true']); raise SystemExit('ran')\n"+ "except OSError: pass\n"+ "eff = [l for l in open('/proc/self/status') if "+ "l.startswith('CapEff')][0]\n"+ "assert int(eff.split()[1], 16) == 0, eff\n"+ "print('ok')");+ /* threads are fine */+ expect_ok("threads", "import threading\n"+ "r = []\n"+ "t = [threading.Thread(target=r.append, args=(i,)) "+ "for i in range(8)]\n"+ "[x.start() for x in t]; [x.join() for x in t]\n"+ "assert sorted(r) == list(range(8))\n"+ "print('ok')");+ /* memory, file size, /tmp: bounded */+ expect_ok("memory", "try:\n"+ " b = bytearray(3 << 30); raise SystemExit('got "+ "3 GiB')\n"+ "except MemoryError: pass\n"+ "print('ok')");+ expect_ok("file size", "try:\n"+ " open('/mnt/data/big', 'wb').write(b'x' * (16 "+ "<< 20)); raise SystemExit('wrote 16 MiB')\n"+ "except OSError: pass\n"+ "print('ok')");+ snprintf(hp, sizeof hp, "%s/big", data);+ unlink(hp);+ expect_ok("tmp", "try:\n"+ " f = open('/tmp/fill', 'wb')\n"+ " for i in range(32): f.write(b'x' * (1 << 20)); "+ "f.flush()\n"+ " raise SystemExit('filled /tmp')\n"+ "except OSError: pass\n"+ "print('ok')");+ /* a loop without end is killed, and the cage is gone */+ char out[256];+ CHECK(run("while True: pass", out, sizeof out, 2) == -2,+ "the endless loop ended by itself");+ /* numpy, where the system has it */+ char probe[64];+ if (run("import numpy; print('ok')", probe, sizeof probe, 30) == 0)+ expect_ok("numpy", "import numpy as np\n"+ "a = np.arange(1000000.0)\n"+ "assert abs(a.sum() - 499999500000.0) < 1\n"+ "print('ok')");+ rmdir(data);+ if (failures) {+ fprintf(stderr, "test_sandbox: %d failures\n", failures);+ return 1;+ }+ printf("test_sandbox: ok\n");+ return 0;+}++#endifdiff --git a/tests/test_server_files.c b/tests/test_server_files.cnew file mode 100644index 0000000..46f354d--- /dev/null+++ b/tests/test_server_files.c@@ -0,0 +1,272 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * test_server_files.c - the files janas-server keeps for its clients+ * (src/server/files_store.c) and the uploads they come in+ * (src/server/multipart.c): a body taken apart right, mangled ones neither+ * crashing nor leaking (under asan), files kept, listed in order, read+ * back after a restart, expired, deleted, and names that never become+ * paths.+ */+#include <fcntl.h>+#include <ftw.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include <unistd.h>++#include "common/md5.c"+#include "server/files_store.c"+#include "server/multipart.c"+#include "server/store_disk.c"++/* files_store.c names its files with it; it lives in http.c */+void srv_new_id(char *buf, size_t cap, const char *prefix)+{+ static int n;+ snprintf(buf, cap, "%s%04d", prefix, ++n);+}++static int failures;++#define CHECK(c, ...) \+ do { \+ if (!(c)) { \+ printf(__VA_ARGS__); \+ printf("\n"); \+ failures++; \+ } \+ } while (0)++static int rm_one(const char *p, const struct stat *s, int f, struct FTW *w)+{+ (void)s;+ (void)f;+ (void)w;+ return remove(p);+}++static const char BODY[] =+ "preamble\r\n"+ "--XyZ\r\n"+ "Content-Disposition: form-data; name=\"purpose\"\r\n\r\n"+ "batch\r\n"+ "--XyZ\r\n"+ "Content-Disposition: form-data; name=\"file\"; "+ "filename=\"../../etc/x \\\"q\\\".jsonl\"\r\n"+ "Content-Type: application/jsonl\r\n\r\n"+ "{\"a\":1}\r\n--X\r\n\0\x01\r\n"+ "--XyZ--\r\n";++static void multipart(void)+{+ struct srv_part p[8];+ char err[128];+ int n = srv_multipart_parse("multipart/form-data; boundary=XyZ", BODY,+ sizeof(BODY) - 1, p, 8, err, sizeof err);+ CHECK(n == 2, "parts: %d (%s)", n, n < 0 ? err : "");+ if (n != 2)+ return;+ const struct srv_part *f = srv_multipart_get(p, n, "file");+ const char want[] = "{\"a\":1}\r\n--X\r\n\0\x01";+ CHECK(f && f->has_filename &&+ !strcmp(f->filename, "../../etc/x \"q\".jsonl") &&+ f->n == sizeof(want) - 1 && !memcmp(f->data, want, f->n) &&+ !strcmp(f->type, "application/jsonl"),+ "the file part");+ const struct srv_part *pu = srv_multipart_get(p, n, "purpose");+ CHECK(pu && !pu->has_filename && pu->n == 5 &&+ !memcmp(pu->data, "batch", 5),+ "the purpose part");+ CHECK(srv_multipart_parse("multipart/form-data; boundary=\"XyZ\"", BODY,+ sizeof(BODY) - 1, p, 8, err, sizeof err) == 2,+ "a quoted boundary");+ CHECK(srv_multipart_parse("application/json", BODY, sizeof(BODY) - 1, p, 8,+ err, sizeof err) < 0,+ "not multipart, taken");+ CHECK(srv_multipart_parse("multipart/form-data; boundary=XyZ", BODY, 60, p,+ 8, err, sizeof err) < 0,+ "a cut body taken");+ /* mangled: bytes flipped, cut anywhere */+ char buf[sizeof(BODY)];+ uint64_t s = 12345;+ for (int it = 0; it < 20000; it++) {+ memcpy(buf, BODY, sizeof(BODY));+ size_t len = sizeof(BODY) - 1;+ s ^= s << 13, s ^= s >> 7, s ^= s << 17;+ int k = 1 + (int)(s % 4);+ for (int j = 0; j < k; j++) {+ s ^= s << 13, s ^= s >> 7, s ^= s << 17;+ buf[s % len] ^= (char)(1 << (s >> 32) % 8);+ }+ len = (size_t)(s >> 20) % (len + 1);+ int m = srv_multipart_parse("multipart/form-data; boundary=XyZ", buf,+ len, p, 8, err, sizeof err);+ for (int i = 0; i < m; i++)+ CHECK(p[i].data >= buf && p[i].data + p[i].n <= buf + len,+ "a part outside the body");+ }+}++static void store(void)+{+ const char *t = getenv("TMPDIR");+ char dir[4096];+ snprintf(dir, sizeof dir, "%s/janas-files-test-%d", t && *t ? t : "/tmp",+ (int)getpid());+ mkdir(dir, 0700);+ char err[200];+ struct srv_files *f = srv_files_open(dir, err, sizeof err);+ CHECK(f, "open: %s", err);+ if (!f)+ return;+ struct srv_file a, b, c, x;+ CHECK(srv_files_add(f, "C:\\docs\\a.jsonl", "batch", "abc", 3, 0, &a, err,+ sizeof err) == 0 &&+ !strcmp(a.filename, "a.jsonl"),+ "add a: %s", err);+ CHECK(srv_files_add(f, "b\n.txt", "user_data", "", 0, 3600, &b, err,+ sizeof err) == 0 &&+ !strcmp(b.filename, "b.txt") &&+ b.expires_at == b.created_at + 3600,+ "add b");+ CHECK(srv_files_add(f, "c", "assistants", "xyz", 3, 1, &c, err,+ sizeof err) == 0,+ "add c");+ struct srv_file *l;+ size_t n = srv_files_list(f, NULL, 0, NULL, &l);+ CHECK(n == 3 && !strcmp(l[0].id, c.id) && !strcmp(l[2].id, a.id),+ "newest first: %zu", n);+ free(l);+ n = srv_files_list(f, NULL, 1, a.id, &l);+ CHECK(n == 2 && !strcmp(l[0].id, b.id), "after the first, oldest first");+ free(l);+ n = srv_files_list(f, "batch", 0, NULL, &l);+ CHECK(n == 1 && !strcmp(l[0].id, a.id), "by purpose");+ free(l);+ sleep(2); /* c expires */+ CHECK(srv_files_get(f, c.id, &x) != 0, "an expired file still there");+ int fd = srv_files_open_content(f, a.id);+ char got[8] = {0};+ CHECK(fd >= 0 && read(fd, got, 8) == 3 && !memcmp(got, "abc", 3),+ "content");+ if (fd >= 0)+ close(fd);+ CHECK(srv_files_get(f, "../a", &x) != 0 &&+ srv_files_open_content(f, "../../x") < 0,+ "a path as an id");+ srv_files_close(f);++ /* read back */+ f = srv_files_open(dir, err, sizeof err);+ CHECK(f, "reopen: %s", err);+ if (!f)+ return;+ CHECK(srv_files_get(f, a.id, &x) == 0 && x.bytes == 3 &&+ !strcmp(x.filename, "a.jsonl") && !strcmp(x.purpose, "batch"),+ "a after reopening");+ CHECK(srv_files_get(f, b.id, &x) == 0 && x.expires_at == b.expires_at,+ "b after reopening");+ CHECK(srv_files_delete(f, a.id) == 0 && srv_files_get(f, a.id, &x) != 0 &&+ srv_files_delete(f, a.id) != 0,+ "delete");+ srv_files_close(f);+ nftw(dir, rm_one, 16, FTW_DEPTH | FTW_PHYS);++ /* no store: a directory of its own, gone at the end */+ f = srv_files_open(NULL, err, sizeof err);+ CHECK(f, "temporary: %s", err);+ if (f) {+ char tdir[4096];+ snprintf(tdir, sizeof tdir, "%s", f->dir);+ srv_files_add(f, "t", "evals", "1", 1, 0, &x, err, sizeof err);+ srv_files_close(f);+ CHECK(access(tdir, F_OK) != 0, "the temporary directory stayed");+ }+}++static void md5(void)+{+ static const char *const in[] = {"", "abc",+ "12345678901234567890123456789012345678"+ "901234567890123456789012345678901234567"+ "890"};+ static const char *const want[] = {"d41d8cd98f00b204e9800998ecf8427e",+ "900150983cd24fb0d6963f7d28e17f72",+ "57edf4a22be3c955ac49da2e2107b67a"};+ for (int i = 0; i < 3; i++) {+ struct janas_md5 h;+ char got[33];+ janas_md5_init(&h);+ /* in pieces of 7: the buffering between them */+ for (size_t k = 0; k < strlen(in[i]); k += 7)+ janas_md5_update(&h, in[i] + k,+ strlen(in[i]) - k < 7 ? strlen(in[i]) - k : 7);+ janas_md5_hex(&h, got);+ CHECK(!strcmp(got, want[i]), "md5 %d: %s", i, got);+ }+ struct janas_md5 h;+ char got[33], a[1000];+ memset(a, 'a', sizeof a);+ janas_md5_init(&h);+ for (int i = 0; i < 1000; i++)+ janas_md5_update(&h, a, sizeof a);+ janas_md5_hex(&h, got);+ CHECK(!strcmp(got, "7707d6ae4e027c70eea2a935c2296f21"), "md5 of a million");+}++/* An upload's parts joined into a file. */+static void parts(void)+{+ char err[200];+ struct srv_files *f = srv_files_open(NULL, err, sizeof err);+ CHECK(f, "parts: %s", err);+ if (!f)+ return;+ char p1[4200], p2[4200];+ snprintf(p1, sizeof p1, "%s/.p1", srv_files_dir(f));+ snprintf(p2, sizeof p2, "%s/.p2", srv_files_dir(f));+ FILE *a = fopen(p1, "wb"), *b = fopen(p2, "wb");+ if (a) {+ fputs("ab", a);+ fclose(a);+ }+ if (b) {+ fputs("c", b);+ fclose(b);+ }+ const char *const order[] = {p1, p2};+ struct srv_file x;+ CHECK(srv_files_add_parts(f, "j.txt", "batch", order, 2, 4, NULL, 0, &x,+ err, sizeof err) == -3,+ "parts of another size taken");+ CHECK(srv_files_add_parts(f, "j.txt", "batch", order, 2, 3,+ "00000000000000000000000000000000", 0, &x, err,+ sizeof err) == -3,+ "parts of another MD5 taken");+ CHECK(srv_files_add_parts(f, "j.txt", "batch", order, 2, 3,+ "900150983CD24FB0D6963F7D28E17F72", 0, &x, err,+ sizeof err) == 0,+ "parts refused: %s", err);+ int fd = srv_files_open_content(f, x.id);+ char got[8] = {0};+ CHECK(fd >= 0 && read(fd, got, 8) == 3 && !memcmp(got, "abc", 3),+ "the parts joined in order");+ if (fd >= 0)+ close(fd);+ unlink(p1);+ unlink(p2);+ srv_files_close(f);+}++int main(void)+{+ multipart();+ store();+ md5();+ parts();+ if (!failures)+ printf("test_server_files: ok\n");+ return failures != 0;+}diff --git a/tests/test_server_origin.c b/tests/test_server_origin.cnew file mode 100644index 0000000..65e157e--- /dev/null+++ b/tests/test_server_origin.c@@ -0,0 +1,159 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * test_server_origin.c - what janas-server lets in and lets out on someone+ * else's word: the web pages that may call it (src/server/origin.c: its+ * own, those named with --allow-origin, no other site even when that site+ * makes its name resolve to 127.0.0.1) and the addresses the MCP servers a+ * request names may have (src/common/mcp_policy.c: public ones, and those+ * named with --mcp-allow).+ */+#include <arpa/inet.h>+#include <netinet/in.h>+#include <stdio.h>+#include <string.h>+#include <sys/socket.h>++#include "common/mcp_policy.h"+#include "server/origin.c"++static int failures;++#define CHECK(c, ...) \+ do { \+ if (!(c)) { \+ printf(__VA_ARGS__); \+ printf("\n"); \+ failures++; \+ } \+ } while (0)++static void origins(void)+{+ struct srv_config c = {.host = "127.0.0.1"};+ static const struct {+ const char *origin, *host;+ int want;+ } cases[] = {+ {NULL, "127.0.0.1:8080", SRV_ORIGIN_NONE},+ {"http://127.0.0.1:8080", "127.0.0.1:8080", SRV_ORIGIN_SAME},+ {"http://localhost:8080", "localhost:8080", SRV_ORIGIN_SAME},+ {"http://[::1]:8080", "[::1]:8080", SRV_ORIGIN_SAME},+ {"http://127.0.0.1", "127.0.0.1", SRV_ORIGIN_SAME},+ {"http://127.0.0.1:80", "127.0.0.1", SRV_ORIGIN_SAME},+ /* another site's page */+ {"https://evil.example", "127.0.0.1:8080", SRV_ORIGIN_REFUSED},+ {"http://localhost:3000", "localhost:8080", SRV_ORIGIN_REFUSED},+ {"http://127.0.0.1:8080", "localhost:8080", SRV_ORIGIN_REFUSED},+ /* rebinding: the site's name resolved to this machine */+ {"http://evil.example:8080", "evil.example:8080", SRV_ORIGIN_REFUSED},+ {"null", "127.0.0.1:8080", SRV_ORIGIN_REFUSED},+ {"chrome-extension://abc", "127.0.0.1:8080", SRV_ORIGIN_REFUSED},+ {"http://127.0.0.1:8080", NULL, SRV_ORIGIN_REFUSED},+ {"http://[::1", "[::1]:8080", SRV_ORIGIN_REFUSED},+ };+ for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {+ int got = srv_origin_check(&c, cases[i].origin, cases[i].host);+ CHECK(got == cases[i].want, "origin %zu (%s, %s): %d, want %d", i,+ cases[i].origin ? cases[i].origin : "-",+ cases[i].host ? cases[i].host : "-", got, cases[i].want);+ }+ const char *allow[] = {"http://localhost:3000"};+ c.allow_origin = allow;+ c.n_allow_origin = 1;+ CHECK(srv_origin_check(&c, "http://localhost:3000", "localhost:8080") ==+ SRV_ORIGIN_LISTED,+ "an origin named with --allow-origin refused");+ CHECK(srv_origin_check(&c, "http://localhost:3001", "localhost:8080") ==+ SRV_ORIGIN_REFUSED,+ "an origin not named let in");+ const char *any[] = {"*"};+ c.allow_origin = any;+ CHECK(srv_origin_check(&c, "https://evil.example", "127.0.0.1:8080") ==+ SRV_ORIGIN_LISTED,+ "--allow-origin '*' did not let a page in");+}++static int public4(const char *a)+{+ struct sockaddr_in s = {.sin_family = AF_INET};+ inet_pton(AF_INET, a, &s.sin_addr);+ return janas_addr_public((struct sockaddr *)&s);+}++static int public6(const char *a)+{+ struct sockaddr_in6 s = {.sin6_family = AF_INET6};+ inet_pton(AF_INET6, a, &s.sin6_addr);+ return janas_addr_public((struct sockaddr *)&s);+}++static void addresses(void)+{+ static const char *const no4[] = {+ "127.0.0.1", "127.1.2.3", "10.0.0.1", "172.16.0.1",+ "172.31.255.1", "192.168.1.1", "169.254.169.254", "100.64.0.1",+ "100.127.255.255", "0.0.0.0", "224.0.0.1", "239.255.255.250",+ "255.255.255.255", "240.0.0.1", "192.0.2.1", "198.18.0.1",+ "198.51.100.7", "203.0.113.9", "192.0.0.8",+ };+ static const char *const yes4[] = {"8.8.8.8", "1.1.1.1", "172.32.0.1",+ "100.128.0.1", "93.184.216.34"};+ for (size_t i = 0; i < sizeof(no4) / sizeof(no4[0]); i++)+ CHECK(!public4(no4[i]), "%s taken for public", no4[i]);+ for (size_t i = 0; i < sizeof(yes4) / sizeof(yes4[0]); i++)+ CHECK(public4(yes4[i]), "%s taken for private", yes4[i]);+ static const char *const no6[] = {+ "::1",+ "::",+ "::ffff:127.0.0.1",+ "::ffff:10.1.2.3",+ "64:ff9b::a9fe:a9fe",+ "fc00::1",+ "fd12:3456::1",+ "fe80::1",+ "ff02::1",+ "2001:db8::1",+ "2002:7f00:1::1",+ "::127.0.0.1",+ "2001::1",+ "100::1",+ };+ static const char *const yes6[] = {"2606:4700:4700::1111",+ "2a00:1450:4001::200e", "::ffff:8.8.8.8",+ "2002:808:808::1"};+ for (size_t i = 0; i < sizeof(no6) / sizeof(no6[0]); i++)+ CHECK(!public6(no6[i]), "%s taken for public", no6[i]);+ for (size_t i = 0; i < sizeof(yes6) / sizeof(yes6[0]); i++)+ CHECK(public6(yes6[i]), "%s taken for private", yes6[i]);++ struct sockaddr_in lo = {.sin_family = AF_INET};+ inet_pton(AF_INET, "127.0.0.1", &lo.sin_addr);+ char *allow[] = {"localhost:9000", "[::1]", "Intranet.lan"};+ struct janas_net_policy p = {+ .public_only = 1, .n_allow = 3, .allow = allow};+ const struct sockaddr *a = (struct sockaddr *)&lo;+ CHECK(janas_net_allowed(NULL, "localhost", "9000", a),+ "no policy refused an address");+ CHECK(janas_net_allowed(&p, "localhost", "9000", a),+ "an allowed host:port refused");+ CHECK(!janas_net_allowed(&p, "localhost", "9001", a),+ "another port of an allowed host let through");+ CHECK(janas_net_allowed(&p, "::1", "8080", a), "an allowed [v6] refused");+ CHECK(janas_net_allowed(&p, "intranet.lan", "443", a),+ "an allowed host (any port, any case) refused");+ CHECK(!janas_net_allowed(&p, "evil.example", "80", a),+ "a name resolving to loopback let through");+ p.public_only = 0;+ CHECK(janas_net_allowed(&p, "evil.example", "80", a),+ "public_only 0 refused");+}++int main(void)+{+ origins();+ addresses();+ if (!failures)+ printf("test_server_origin: ok\n");+ return failures != 0;+}diff --git a/tests/test_server_request.c b/tests/test_server_request.cindex ac6866a..ca2ba27 100644--- a/tests/test_server_request.c+++ b/tests/test_server_request.c@@ -14,6 +14,20 @@#include "server/engine.c"#include "server/fim.c"+#include "server/images.c"++/* images.c reads files from the store: none here */+int srv_files_get(struct srv_files *f, const char *id, struct srv_file *out)+{+ (void)f, (void)id, (void)out;+ return -1;+}++int srv_files_open_content(struct srv_files *f, const char *id)+{+ (void)f, (void)id;+ return -1;+}#include "server/request.c"#include "server/request_opts.c"@@ -62,7 +76,8 @@ static void valid(void)"{\"role\":\"assistant\",\"content\":null},""{\"role\":\"developer\",\"content\":\"Italian.\"},""{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":\"Ci\"},"- "{\"type\":\"text\",\"text\":\"ao\"}]}],"+ "{\"type\":\"refusal\",\"refusal\":\"a\"},"+ "{\"type\":\"text\",\"text\":\"o\"}]}],""\"temperature\":0.2,\"top_p\":0.9,\"max_tokens\":50,""\"stop\":[\"END\",\"\"],\"seed\":7,\"stream\":true,""\"stream_options\":{\"include_usage\":true},"@@ -176,9 +191,27 @@ static void invalid(void){"{\"messages\":[{\"role\":\"user\",\"content\":\"a\"},""{\"role\":\"assistant\",\"content\":\"b\"}]}",1, "invalid_value"},+ /* a picture part without its picture */{"{\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":""\"image_url\"}]}]}",+ 1, "invalid_value"},+ /* a picture in the assistant's message */+ {"{\"messages\":[{\"role\":\"user\",\"content\":\"a\"},"+ "{\"role\":\"assistant\",\"content\":[{\"type\":\"image_url\","+ "\"image_url\":{\"url\":\"data:image/png;base64,AA\"}}]},"+ "{\"role\":\"user\",\"content\":\"b\"}]}",+ 1, "invalid_value"},+ /* sound: not read */+ {"{\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":"+ "\"input_audio\"}]}]}",1, "unsupported_content"},+ /* a part's text not a string: its length is not the bytes */+ {"{\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":"+ "\"text\",\"text\":[1,2,3,4,5,6,7,8]}]}]}",+ 1, "invalid_value"},+ {"{\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":"+ "\"text\",\"text\":{\"a\":1}}]}]}",+ 1, "invalid_value"},{"{\"messages\":[{\"role\":\"user\",\"content\":\"a\"}],""\"temperature\":3}",1, "invalid_value"},diff --git a/tests/test_server_store.c b/tests/test_server_store.cindex 1d38bb0..ed8d007 100644--- a/tests/test_server_store.c+++ b/tests/test_server_store.c@@ -152,6 +152,23 @@ int main(void)expect(st, "response", "resp_1", "{\"b\":22}", "items");srv_store_free(st);+ /* 6: a document past SRV_STORE_DOC_MAX is refused, put or grown */+ st = srv_store_new(0);+ char *big = malloc(SRV_STORE_DOC_MAX + 2);+ if (st && big) {+ memset(big, ' ', SRV_STORE_DOC_MAX + 1);+ big[SRV_STORE_DOC_MAX + 1] = 0;+ CHECK(srv_store_put(st, "response", "resp_big", 1, big, NULL) == -3,+ "a document past the limit was kept");+ CHECK(srv_store_put(st, "response", "resp_s", 1, "{}", NULL) == 0 &&+ srv_store_update(st, "response", "resp_s", NULL, big + 2) ==+ -3,+ "a document grew past the limit");+ expect(st, "response", "resp_s", "{}", NULL);+ }+ free(big);+ srv_store_free(st);+nftw(dir, rm_one, 16, FTW_DEPTH | FTW_PHYS);if (!failed)printf("test_server_store: ok\n");diff --git a/tools/gguf2jns.c b/tools/gguf2jns.cindex 4d2d6ca..5bb89cd 100644--- a/tools/gguf2jns.c+++ b/tools/gguf2jns.c@@ -131,6 +131,35 @@ static uint64_t need_uint(const struct janas_gguf *g, const char *key)return v;}+/* An integer, or the largest of an array of them (nemotron_h_moe's+ expert_used_count, one a layer); 0 if neither. */+static uint64_t uint_or_max(const struct janas_gguf *g, const char *key)+{+ uint64_t v;+ if (janas_gguf_uint(g, key, &v) == 0)+ return v;+ for (uint64_t i = 0; i < g->n_kv; i++) {+ const struct janas_gguf_kv *kv = &g->kv[i];+ if (kv->key.n != strlen(key) || memcmp(kv->key.p, key, kv->key.n) ||+ kv->type != 9)+ continue;+ uint32_t et;+ uint64_t n, best = 0;+ memcpy(&et, kv->val, 4);+ memcpy(&n, kv->val + 4, 8);+ if (et != 4 && et != 5)+ return 0;+ for (uint64_t k = 0; k < n; k++) {+ uint32_t x;+ memcpy(&x, kv->val + 12 + 4 * k, 4);+ if (x > best && !(et == 5 && (int32_t)x < 0))+ best = x;+ }+ return best;+ }+ return 0;+}+/* The tensor of this name in any part; the last part that has it wins, asthe dictionary of the Python script had it. */static struct tensor *find(const char *name)@@ -220,6 +249,10 @@ static uint32_t copy_rows(const struct tensor *t, uint64_t src, uint64_t rows,if (new_rb == rb) {crc = copy(t, src, rows * rb, dst, crc, with_crc);} else {+ /* a row at a time at the least: it must fit the buffers */+ if (new_rb < rb || new_rb > COPY_CHUNK)+ die("%s", "a row longer than 16 MiB: not one this converter "+ "rewrites");uint64_t per = COPY_CHUNK / new_rb ? COPY_CHUNK / new_rb : 1;int fd = parts[t->part].fd;for (uint64_t r0 = 0; r0 < rows; r0 += per) {@@ -358,11 +391,14 @@ int main(int argc, char **argv)n_expert = 1;else {snprintf(key, sizeof(key), "%s.expert_used_count", arch);- n_used = need_uint(g0, key);+ n_used = uint_or_max(g0, key);+ if (n_used == 0)+ die("metadata %s missing, or not a number", key);}snprintf(key, sizeof(key), "%s.embedding_length", arch);uint64_t d_model = need_uint(g0, key);- if (n_layer == 0 || n_layer > 4096 || n_expert == 0 || n_expert > 65536)+ if (n_layer == 0 || n_layer > JANAS_JNS_MAX_LAYERS || n_expert == 0 ||+ n_expert > 65536)die("%s: layers or experts out of range", src);/* the experts' width as it is (see the top of the file) */int keep_width = strcmp(arch, "qwen4exp") == 0;@@ -372,6 +408,38 @@ int main(int argc, char **argv)llama_kv = need_uint(g0, "llama.attention.head_count_kv");}+ /* nemotron_h_moe: a layer is a Mamba-2 mixer, an attention or a+ mixture, and only the mixtures have experts - each its own number of+ them (the router's rows) and no gate (relu^2 on up). Such a layer+ without experts has an empty slot (no matrices, size 0), a missing+ gate an empty matrix, and the table is as wide as the layer with the+ most experts: the others' slots past their own are absent (offset+ 0), never asked for. */+ int sparse = strcmp(arch, "nemotron_h_moe") == 0;+ uint64_t *layer_ne = calloc(n_layer, sizeof(*layer_ne));+ if (!layer_ne)+ die("%s", "out of memory");+ if (sparse) {+ n_expert = 0;+ for (uint64_t l = 0; l < n_layer; l++) {+ snprintf(key, sizeof(key), "blk.%llu.ffn_up_exps.weight",+ (unsigned long long)l);+ struct tensor *t = find(key);+ layer_ne[l] = t && t->t->n_dims > 2 ? t->t->dims[2] : 0;+ if (layer_ne[l] > n_expert)+ n_expert = layer_ne[l];+ }+ if (n_expert == 0 || n_expert > 65536)+ die("%s: no layer with experts", src);+ for (uint64_t l = 0; l < n_layer; l++)+ if (layer_ne[l] && layer_ne[l] < n_used)+ die("%s: a layer has fewer experts than are used", src);+ } else+ for (uint64_t l = 0; l < n_layer; l++)+ layer_ne[l] = n_expert;+ if (n_used > n_expert)+ die("%s: more experts used than there are", src);+/* expert geometry per layer */struct janas_jns_layer_v2 *layers = calloc(n_layer, sizeof(*layers));struct src_matrix(*mats)[3] = calloc(n_layer, sizeof(*mats));@@ -380,6 +448,8 @@ int main(int argc, char **argv)static const char *const part_name[3] = {"gate", "up", "down"};for (uint64_t l = 0; l < n_layer; l++) {uint64_t off = 0;+ if (layer_ne[l] == 0)+ continue; /* no experts: an empty slot */for (int p = 0; p < 3; p++) {snprintf(key, sizeof(key),dense ? "blk.%llu.ffn_%s.weight"@@ -393,16 +463,18 @@ int main(int argc, char **argv)t = find(key);half = 1;}+ if (!t && sparse && p == 0)+ continue; /* no gate: an empty matrix */if (!t)die("%s missing: only models with routed experts in every ""layer, or dense ones, are supported",key);uint64_t ne = t->t->n_dims > 2 ? t->t->dims[2] : 1;- if (ne != n_expert || t->t->bytes % n_expert ||+ if (ne != layer_ne[l] || t->t->bytes % ne ||t->t->dims[0] > UINT32_MAX || t->t->dims[1] > UINT32_MAX ||t->t->dims[3] != 1 || (half && t->t->dims[1] % 2))die("%s: unexpected shape", key);- uint64_t per = t->t->bytes / n_expert;+ uint64_t per = t->t->bytes / ne;uint64_t rows = t->t->dims[1] / (half ? 2 : 1);uint64_t cols = t->t->dims[0];if (per % t->t->dims[1])@@ -528,8 +600,10 @@ int main(int argc, char **argv)die("%s", "out of memory");for (uint64_t l = 0; l < n_layer; l++)for (uint64_t e = 0; e < n_expert; e++) {- slot_off[l * n_expert + e] = pos;- pos += layers[l].slot_bytes;+ /* an absent slot: offset 0 */+ slot_off[l * n_expert + e] = e < layer_ne[l] ? pos : 0;+ if (e < layer_ne[l])+ pos += layers[l].slot_bytes;}uint64_t experts_bytes = pos - experts_offset;uint64_t file_bytes = pos;@@ -571,8 +645,14 @@ int main(int argc, char **argv)uint64_t slot = slot_off[l * n_expert + e];uint32_t crc = 0;uint64_t end = 0;+ if (e >= layer_ne[l]) {+ sums[l * n_expert + e] = 0;+ continue;+ }for (int p = 0; p < 3; p++) {const struct janas_jns_matrix *m = &layers[l].m[p];+ if (m->type == 0)+ continue; /* an empty matrix */if (m->offset > end) /* padding inside the slot is zero */crc = crc_zeros(crc, m->offset - end);const struct src_matrix *s = &mats[l][p];@@ -621,6 +701,14 @@ int main(int argc, char **argv)d.bytes = resident[i]->new_bytes;memcpy(tbl + (tensor_dir - layer_table) + i * sizeof(d), &d, sizeof(d));}+ /* copied as it is, and the JNS reader takes no array of arrays */+ for (uint64_t i = 0; i < g0->n_kv; i++) {+ uint32_t et;+ if (g0->kv[i].type == 9 && (memcpy(&et, g0->kv[i].val, 4), et == 9))+ die("%s: an array of arrays in the metadata, which a JNS file "+ "cannot hold",+ src);+ }uint8_t *meta = malloc(metadata_bytes);if (!meta)die("%s", "out of memory");@@ -669,6 +757,7 @@ int main(int argc, char **argv)free(resident);free(mats);free(layers);+ free(layer_ne);free(arch);free(tensors);for (int i = 0; i < n_parts; i++) {diff --git a/tools/hf2gguf.c b/tools/hf2gguf.cindex 0ac02e1..579e6fb 100644--- a/tools/hf2gguf.c+++ b/tools/hf2gguf.c@@ -499,6 +499,8 @@ static int n_gts;static void add(const char *name, const char *hf, int src, uint32_t heads){+ if (n_gts == (int)(sizeof(gts) / sizeof(gts[0])))+ die("too many tensors (more than 1,024)", name);struct gt *g = >s[n_gts++];memset(g, 0, sizeof(*g));snprintf(g->name, sizeof(g->name), "%s", name);diff --git a/tools/hf2jns_mtp.c b/tools/hf2jns_mtp.cindex d48ceec..afdc7fd 100644--- a/tools/hf2jns_mtp.c+++ b/tools/hf2jns_mtp.c@@ -76,7 +76,7 @@ static _Noreturn void die(const char *msg, const char *arg)struct st_file {FILE *f;char *json;- uint64_t data_start;+ uint64_t data_start, size; /* size: of the file */};struct st_tensor {@@ -96,6 +96,9 @@ static void st_open(struct st_file *s, const char *path)die("cannot read the header of", path);s->json[n] = 0;s->data_start = 8 + n;+ if (fseeko(s->f, 0, SEEK_END) != 0 || ftello(s->f) < (off_t)s->data_start)+ die("cannot read", path);+ s->size = (uint64_t)ftello(s->f);}/* Parses the entry of one tensor: {"dtype":"BF16","shape":[..],@@ -118,22 +121,36 @@ static int st_find(const struct st_file *s, const char *name,if (!dt || !sh || !of || dt > end || sh > end || of > end ||!strstr(dt, "\"BF16\"") || strstr(dt, "\"BF16\"") > end)return -1;+ /* what the file says is input: every field inside the entry, a shape+ of one to four numbers, the data inside the file */sh = strchr(sh, '[');+ of = strchr(of, '[');+ if (!sh || !of || sh > end || of > end)+ return -1;t->dims = 0;- for (const char *q = sh + 1; *q && *q != ']' && t->dims < 4;) {+ const char *q = sh + 1;+ while (*q == ' ')+ q++;+ while (*q >= '0' && *q <= '9' && t->dims < 4) {t->shape[t->dims++] = strtoull(q, (char **)&q, 10);while (*q == ',' || *q == ' ')q++;}- of = strchr(of, '[');- char *q;- t->begin = strtoull(of + 1, &q, 10);- while (*q == ',' || *q == ' ')- q++;- t->end = strtoull(q, NULL, 10);+ if (*q != ']' || t->dims == 0)+ return -1;+ char *r;+ t->begin = strtoull(of + 1, &r, 10);+ while (*r == ',' || *r == ' ')+ r++;+ t->end = strtoull(r, NULL, 10);uint64_t count = 1;for (int i = 0; i < t->dims; i++)- count *= t->shape[i];+ if (t->shape[i] == 0 ||+ __builtin_mul_overflow(count, t->shape[i], &count))+ return -1;+ if (count > UINT64_MAX / 2 || t->end < t->begin ||+ t->end > s->size - s->data_start)+ return -1;return t->end - t->begin == 2 * count ? 0 : -1;}@@ -216,6 +233,10 @@ static void add(struct janas_pool *pool, const char *src, const char *dst,struct st_tensor t;uint64_t n;float *x = st_load(src, &n, &t);+ if (t.dims > 2)+ die("a tensor of more than two dimensions", src);+ if (n_outs == (int)(sizeof(outs) / sizeof(outs[0])))+ die("too many tensors in the block", src);struct out_tensor *o = &outs[n_outs++];snprintf(o->name, sizeof(o->name), "%s", dst);o->type = kind;@@ -228,6 +249,8 @@ static void add(struct janas_pool *pool, const char *src, const char *dst,o->data = to_q4k(pool, x, o->dims[1], o->dims[0], &o->bytes);} else if (kind == T_F16) {uint16_t *h = malloc(n * 2);+ if (!h)+ die("out of memory", NULL);for (uint64_t i = 0; i < n; i++)h[i] = janas_fp32_to_fp16(x[i]);o->data = (uint8_t *)h;@@ -237,6 +260,8 @@ static void add(struct janas_pool *pool, const char *src, const char *dst,for (uint64_t i = 0; i < n; i++)x[i] += 1.0f;o->data = malloc(n * 4);+ if (!o->data)+ die("out of memory", NULL);memcpy(o->data, x, n * 4);o->bytes = n * 4;}@@ -583,6 +608,8 @@ int main(int argc, char **argv)uint64_t tbl_len =tensor_dir + sizeof(struct janas_jns_tensor) * n_outs - layer_table;uint8_t *tbl = calloc(1, tbl_len);+ if (!tbl)+ die("out of memory", NULL);memcpy(tbl, &ly, sizeof(ly));memcpy(tbl + (expert_table - layer_table), slot_off, 8 * (size_t)n_exp);memcpy(tbl + (expert_sums - layer_table), sums, 8 * (size_t)n_exp);diff --git a/tools/hf2jns_vision.c b/tools/hf2jns_vision.cnew file mode 100644index 0000000..ff081e0--- /dev/null+++ b/tools/hf2jns_vision.c@@ -0,0 +1,566 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * hf2jns_vision.c - the vision encoder of a multimodal model, from its+ * Hugging Face checkpoint (safetensors) into a JNS file of resident+ * tensors (no layers, no experts) beside the model's: <model>.vision.jns.+ *+ * Qwen3.5 and Qwen3-VL (model.visual.*): a ViT - a 3D convolution over+ * patches of 16x16 pixels and two frames (a still picture is given twice),+ * learned positions on a 48x48 grid interpolated to the picture's, blocks+ * of LayerNorm, attention with a 2D rotary embedding, and a GELU MLP - and+ * a merger that joins 2x2 patches into one token of the language model's+ * width. The matrices are kept in bfloat16 as the checkpoint has them, the+ * norms and biases in float; the metadata holds the sizes, the processor's+ * mean, deviation and pixel bounds, and the ids of the image tokens.+ *+ * Gemma 4 (model.vision_tower.*): see gemma() below.+ *+ * Usage: hf2jns_vision <checkpoint directory> <out.vision.jns>+ * the directory holding config.json, preprocessor_config.json (or+ * processor_config.json) and the+ * safetensors (one file, or the shards of model.safetensors.index.json).+ */+#define _GNU_SOURCE+#include <dirent.h>+#include <float.h>+#include <stdint.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <unistd.h>++#include "common/hf_safetensors.h"+#include "llm/jns_write.h"+#include "llm/json.h"++#define GGML_F32 0+#define GGML_BF16 30++static _Noreturn void die(const char *msg, const char *arg)+{+ fprintf(stderr, "hf2jns_vision: %s%s%s\n", msg, arg ? ": " : "",+ arg ? arg : "");+ exit(1);+}++/* ---- the checkpoint ---- */++static struct janas_st shards[64];+static int n_shards;++static void open_shards(const char *dir)+{+ DIR *d = opendir(dir);+ if (!d)+ die("cannot open", dir);+ struct dirent *e;+ char p[4096], err[300];+ while ((e = readdir(d))) {+ size_t n = strlen(e->d_name);+ if (n < 12 || strcmp(e->d_name + n - 12, ".safetensors"))+ continue;+ if (n_shards == 64)+ die("more than 64 shards in", dir);+ snprintf(p, sizeof p, "%s/%s", dir, e->d_name);+ if (janas_st_open(&shards[n_shards], p, err, sizeof err) != 0)+ die(err, NULL);+ n_shards++;+ }+ closedir(d);+ if (!n_shards)+ die("no safetensors in", dir);+}++static const struct janas_st *find(const char *name, struct janas_st_tensor *t)+{+ for (int i = 0; i < n_shards; i++)+ if (janas_st_find(&shards[i], name, t) == 0)+ return &shards[i];+ return NULL;+}++static struct janas_json_doc *read_json_file(const char *path)+{+ FILE *f = fopen(path, "rb");+ if (!f)+ die("cannot read", path);+ fseek(f, 0, SEEK_END);+ long n = ftell(f);+ fseek(f, 0, SEEK_SET);+ char *t = malloc((size_t)n + 1);+ if (!t || fread(t, 1, (size_t)n, f) != (size_t)n)+ die("cannot read", path);+ fclose(f);+ char err[200];+ struct janas_json_doc *d = janas_json_parse(t, (size_t)n, err, sizeof err);+ free(t);+ if (!d)+ die(err, path);+ return d;+}++/* ---- the output ---- */++static struct janas_jw W;++static void put(const char *dst, uint32_t type, uint32_t n_dims,+ const uint64_t *dims, void *data, uint64_t bytes)+{+ if (!data)+ die("cannot read or out of memory", dst);+ if (janas_jw_tensor(&W, dst, type, n_dims, dims, data, bytes) != 0)+ die("out of memory, or a name too long", dst);+}++/* a tensor of the checkpoint as one of the file: a matrix (rows x cols,+ the rest of its dimensions flattened into the row; matrix 2: all but the+ last flattened into the rows) in bfloat16, anything else in float */+static void add(const char *src, const char *dst, int matrix)+{+ struct janas_st_tensor t;+ const struct janas_st *s = find(src, &t);+ if (!s)+ die("tensor missing or not F32/F16/BF16", src);+ if (matrix && t.dims < 2)+ die("not a matrix", src);+ if (matrix) {+ uint64_t cols =+ matrix == 2 ? t.shape[t.dims - 1] : t.count / t.shape[0];+ /* ggml order: the row length first */+ uint64_t dims[2] = {cols, t.count / cols};+ put(dst, GGML_BF16, 2, dims, janas_st_bf16(s, &t), 2 * t.count);+ } else {+ uint64_t dims[1] = {t.count};+ put(dst, GGML_F32, 1, dims, janas_st_floats(s, &t), 4 * t.count);+ }+}++/* the scalars of the checkpoint in names[] (each %u the layer) packed into+ one float tensor */+static void add_scalars(const char *const *names, int n, uint32_t l,+ const char *dst)+{+ float *x = malloc(sizeof(float) * (size_t)n);+ if (!x)+ die("out of memory", NULL);+ for (int i = 0; i < n; i++) {+ char s[160];+ snprintf(s, sizeof s, names[i], l);+ struct janas_st_tensor t;+ const struct janas_st *st = find(s, &t);+ float *v = st && t.count == 1 ? janas_st_floats(st, &t) : NULL;+ if (!v)+ die("scalar missing", s);+ x[i] = v[0];+ free(v);+ }+ uint64_t dims[1] = {(uint64_t)n};+ put(dst, GGML_F32, 1, dims, x, 4 * (uint64_t)n);+}++/* bounds that clamp nothing, for linears without them (Gemma 4 26B) */+static void add_bounds(const char *dst)+{+ float *x = malloc(sizeof(float) * 7 * 4);+ if (!x)+ die("out of memory", NULL);+ for (int i = 0; i < 7 * 4; i++)+ x[i] = i % 2 ? FLT_MAX : -FLT_MAX;+ uint64_t dims[1] = {7 * 4};+ put(dst, GGML_F32, 1, dims, x, 4 * 7 * 4);+}++static void m_u32(const char *k, uint32_t v)+{+ janas_jw_u32(&W, k, v);+}++static void m_f32(const char *k, float v)+{+ janas_jw_f32(&W, k, v);+}++static void m_str(const char *k, const char *v)+{+ janas_jw_str(&W, k, v);+}++static uint32_t cfg_u32(const struct janas_json *o, const char *k, int need,+ uint32_t def)+{+ const struct janas_json *v = janas_json_get(o, k);+ if (!v || v->type != JANAS_JSON_NUMBER) {+ if (need)+ die("config.json lacks", k);+ return def;+ }+ return (uint32_t)janas_json_num(v, 0);+}++/* Qwen3.5 and Qwen3-VL: the tensors and the metadata; the architecture,+ the width, a line of what was converted */+static const char *qwen(const struct janas_json *cfg,+ const struct janas_json *pre,+ const struct janas_json *vc, const char *mt,+ uint32_t *width, char *what, size_t what_len)+{+ const struct janas_json *ds =+ janas_json_get(vc, "deepstack_visual_indexes");+ if (ds && ds->n)+ die("deepstack layers are not converted yet", mt);+ uint32_t depth = cfg_u32(vc, "depth", 1, 0);+ uint32_t hidden = cfg_u32(vc, "hidden_size", 1, 0);+ uint32_t ff = cfg_u32(vc, "intermediate_size", 1, 0);+ uint32_t heads = cfg_u32(vc, "num_heads", 1, 0);+ uint32_t patch = cfg_u32(vc, "patch_size", 1, 0);+ uint32_t tpatch = cfg_u32(vc, "temporal_patch_size", 1, 0);+ uint32_t merge = cfg_u32(vc, "spatial_merge_size", 1, 0);+ uint32_t npos = cfg_u32(vc, "num_position_embeddings", 1, 0);+ uint32_t out_dim = cfg_u32(vc, "out_hidden_size", 1, 0);+ const char *act = janas_json_str(janas_json_get(vc, "hidden_act"));+ if (!act || strcmp(act, "gelu_pytorch_tanh"))+ die("an activation this does not run", act);+ if (!depth || !hidden || !heads || hidden % heads || !patch || !tpatch ||+ !merge || !npos || !out_dim)+ die("vision sizes out of range", NULL);++ /* tensors */+ add("model.visual.patch_embed.proj.weight", "v.patch_embd", 1);+ add("model.visual.patch_embed.proj.bias", "v.patch_embd.bias", 0);+ add("model.visual.pos_embed.weight", "v.pos_embd", 1);+ char s[160], d[64];+ static const char *const PARTS[][3] = {+ {"norm1.weight", "ln1.weight", "0"},+ {"norm1.bias", "ln1.bias", "0"},+ {"norm2.weight", "ln2.weight", "0"},+ {"norm2.bias", "ln2.bias", "0"},+ {"attn.qkv.weight", "attn_qkv", "1"},+ {"attn.qkv.bias", "attn_qkv.bias", "0"},+ {"attn.proj.weight", "attn_out", "1"},+ {"attn.proj.bias", "attn_out.bias", "0"},+ {"mlp.linear_fc1.weight", "ffn_up", "1"},+ {"mlp.linear_fc1.bias", "ffn_up.bias", "0"},+ {"mlp.linear_fc2.weight", "ffn_down", "1"},+ {"mlp.linear_fc2.bias", "ffn_down.bias", "0"}};+ for (uint32_t l = 0; l < depth; l++)+ for (size_t i = 0; i < sizeof PARTS / sizeof PARTS[0]; i++) {+ snprintf(s, sizeof s, "model.visual.blocks.%u.%s", l, PARTS[i][0]);+ snprintf(d, sizeof d, "v.blk.%u.%s", l, PARTS[i][1]);+ add(s, d, PARTS[i][2][0] == '1');+ }+ add("model.visual.merger.norm.weight", "v.merger.ln.weight", 0);+ add("model.visual.merger.norm.bias", "v.merger.ln.bias", 0);+ add("model.visual.merger.linear_fc1.weight", "v.merger.fc1", 1);+ add("model.visual.merger.linear_fc1.bias", "v.merger.fc1.bias", 0);+ add("model.visual.merger.linear_fc2.weight", "v.merger.fc2", 1);+ add("model.visual.merger.linear_fc2.bias", "v.merger.fc2.bias", 0);++ /* the shapes the encoder needs, checked here once */+ if (W.t[0].dims[1] != hidden ||+ W.t[0].dims[0] != 3ull * tpatch * patch * patch ||+ W.t[2].dims[1] != npos || W.t[2].dims[0] != hidden)+ die("the patch embedding or the positions are not of the sizes the "+ "config says",+ NULL);++ m_str("general.architecture", "qwen3vl_vision");+ m_str("vision.model_type", mt);+ m_u32("vision.block_count", depth);+ m_u32("vision.embedding_length", hidden);+ m_u32("vision.feed_forward_length", ff);+ m_u32("vision.attention.head_count", heads);+ m_f32("vision.attention.layer_norm_epsilon", 1e-6f);+ m_f32("vision.rope.freq_base", 10000.0f);+ m_u32("vision.patch_size", patch);+ m_u32("vision.temporal_patch_size", tpatch);+ m_u32("vision.spatial_merge_size", merge);+ m_u32("vision.position_count", npos);+ m_u32("vision.projection_dim", out_dim);+ m_str("vision.activation", "gelu_tanh");+ float mean[3] = {0.5f, 0.5f, 0.5f}, stdv[3] = {0.5f, 0.5f, 0.5f};+ const struct janas_json *a = janas_json_get(pre, "image_mean");+ for (int i = 0; a && a->type == JANAS_JSON_ARRAY && i < 3; i++) {+ const struct janas_json *v = a->child;+ for (int k = 0; v && k < i; k++)+ v = v->next;+ mean[i] = (float)janas_json_num(v, mean[i]);+ }+ a = janas_json_get(pre, "image_std");+ for (int i = 0; a && a->type == JANAS_JSON_ARRAY && i < 3; i++) {+ const struct janas_json *v = a->child;+ for (int k = 0; v && k < i; k++)+ v = v->next;+ stdv[i] = (float)janas_json_num(v, stdv[i]);+ }+ for (int i = 0; i < 3; i++) { /* per channel: r, g, b */+ char k[40];+ snprintf(k, sizeof k, "vision.image_mean.%d", i);+ m_f32(k, mean[i]);+ snprintf(k, sizeof k, "vision.image_std.%d", i);+ m_f32(k, stdv[i]);+ }+ const struct janas_json *size = janas_json_get(pre, "size");+ m_u32("vision.min_pixels", cfg_u32(size, "shortest_edge", 0, 56 * 56));+ m_u32("vision.max_pixels",+ cfg_u32(size, "longest_edge", 0, 28 * 28 * 1280));+ m_u32("vision.token.image", cfg_u32(cfg, "image_token_id", 1, 0));+ m_u32("vision.token.start", cfg_u32(cfg, "vision_start_token_id", 1, 0));+ m_u32("vision.token.end", cfg_u32(cfg, "vision_end_token_id", 1, 0));+ *width = hidden;+ snprintf(what, what_len,+ "%u blocks of %u, %u heads, patch %u, merge %u "+ "-> %u",+ depth, hidden, heads, patch, merge, out_dim);+ return "qwen3vl_vision";+}++/* Gemma 4 (model.vision_tower.*, model.embed_vision.*): a ViT of RMS+ norms, clamped linears and a 2D rotary embedding over learned positions+ of the patches' columns and rows, pooled 3 x 3 and projected; the+ clamps of a block's seven linears packed into one tensor of 28 floats (bounds+ that clamp nothing for a model without them) (each linear: input min, max,+ output min, max) */+static const char *gemma(const struct janas_json *cfg,+ const struct janas_json *pre,+ const struct janas_json *vc, uint32_t *width,+ char *what, size_t what_len)+{+ const struct janas_json *tc = janas_json_get(cfg, "text_config");+ uint32_t depth = cfg_u32(vc, "num_hidden_layers", 1, 0);+ uint32_t hidden = cfg_u32(vc, "hidden_size", 1, 0);+ uint32_t ff = cfg_u32(vc, "intermediate_size", 1, 0);+ uint32_t heads = cfg_u32(vc, "num_attention_heads", 1, 0);+ uint32_t kv = cfg_u32(vc, "num_key_value_heads", 0, heads);+ uint32_t hd = cfg_u32(vc, "head_dim", 0, heads ? hidden / heads : 0);+ uint32_t patch = cfg_u32(vc, "patch_size", 1, 0);+ uint32_t pool = cfg_u32(vc, "pooling_kernel_size", 1, 0);+ uint32_t npos = cfg_u32(vc, "position_embedding_size", 1, 0);+ uint32_t out_dim = cfg_u32(tc, "hidden_size", 1, 0);+ uint32_t soft = cfg_u32(pre, "max_soft_tokens", 0,+ cfg_u32(vc, "default_output_length", 1, 0));+ const char *act = janas_json_str(janas_json_get(vc, "hidden_activation"));+ const struct janas_json *rp = janas_json_get(vc, "rope_parameters");+ const struct janas_json *cl = janas_json_get(vc, "use_clipped_linears");+ const struct janas_json *sz = janas_json_get(vc, "standardize");+ int stdz = sz && sz->type == JANAS_JSON_TRUE;+ if (!act || strcmp(act, "gelu_pytorch_tanh"))+ die("an activation this does not run", act);+ int clipped = cl && cl->type == JANAS_JSON_TRUE;+ if (!depth || !hidden || !heads || hidden % heads || kv != heads ||+ hd * heads != hidden || !patch || !pool || !npos || !out_dim || !soft)+ die("vision sizes out of range", NULL);+ double eps = janas_json_num(janas_json_get(vc, "rms_norm_eps"), 1e-6);+ double theta = janas_json_num(janas_json_get(rp, "rope_theta"), 100.0);++ add("model.vision_tower.patch_embedder.input_proj.weight", "v.patch_embd",+ 1);+ add("model.vision_tower.patch_embedder.position_embedding_table",+ "v.pos_embd", 2);+ add("model.embed_vision.embedding_projection.weight", "v.proj", 1);+ if (W.t[0].dims[1] != hidden || W.t[0].dims[0] != 3ull * patch * patch ||+ W.t[1].dims[1] != 2ull * npos || W.t[1].dims[0] != hidden ||+ W.t[2].dims[1] != out_dim || W.t[2].dims[0] != hidden)+ die("the patch embedding, the positions or the projection are not "+ "of the sizes the config says",+ NULL);+ if (stdz) {+ add("model.vision_tower.std_bias", "v.std_bias", 0);+ add("model.vision_tower.std_scale", "v.std_scale", 0);+ }+ static const char *const NORMS[][2] = {+ {"input_layernorm", "in_norm"},+ {"post_attention_layernorm", "post_att_norm"},+ {"pre_feedforward_layernorm", "pre_ff_norm"},+ {"post_feedforward_layernorm", "post_ff_norm"},+ {"self_attn.q_norm", "q_norm"},+ {"self_attn.k_norm", "k_norm"}};+ /* in the order of vision_impl.h's GL_* */+ static const char *const LIN[][2] = {+ {"self_attn.q_proj", "q"}, {"self_attn.k_proj", "k"},+ {"self_attn.v_proj", "v"}, {"self_attn.o_proj", "o"},+ {"mlp.gate_proj", "gate"}, {"mlp.up_proj", "up"},+ {"mlp.down_proj", "down"}};+ static const char *const BOUND[] = {"input_min", "input_max", "output_min",+ "output_max"};+ char s[160], d[64];+ static char clip_names[7 * 4][128];+ const char *clips[7 * 4];+ for (uint32_t l = 0; l < depth; l++) {+ const char *pf = "model.vision_tower.encoder.layers";+ for (size_t i = 0; i < sizeof NORMS / sizeof NORMS[0]; i++) {+ snprintf(s, sizeof s, "%s.%u.%s.weight", pf, l, NORMS[i][0]);+ snprintf(d, sizeof d, "v.blk.%u.%s", l, NORMS[i][1]);+ add(s, d, 0);+ }+ for (size_t i = 0; i < 7; i++) {+ snprintf(s, sizeof s, "%s.%u.%s.linear.weight", pf, l, LIN[i][0]);+ snprintf(d, sizeof d, "v.blk.%u.%s", l, LIN[i][1]);+ add(s, d, 1);+ for (int b = 0; b < 4; b++) {+ snprintf(clip_names[i * 4 + b], sizeof clip_names[0],+ "%s.%%u.%s.%s", pf, LIN[i][0], BOUND[b]);+ clips[i * 4 + b] = clip_names[i * 4 + b];+ }+ }+ snprintf(d, sizeof d, "v.blk.%u.clip", l);+ if (clipped)+ add_scalars(clips, 7 * 4, l, d);+ else+ add_bounds(d);+ }++ m_str("general.architecture", "gemma4_vision");+ m_str("vision.model_type", "gemma4");+ m_u32("vision.block_count", depth);+ m_u32("vision.embedding_length", hidden);+ m_u32("vision.feed_forward_length", ff);+ m_u32("vision.attention.head_count", heads);+ m_f32("vision.attention.layer_norm_epsilon", (float)eps);+ m_f32("vision.rope.freq_base", (float)theta);+ m_u32("vision.patch_size", patch);+ m_u32("vision.pooling_kernel_size", pool);+ m_u32("vision.position_size", npos);+ m_u32("vision.max_soft_tokens", soft);+ m_u32("vision.projection_dim", out_dim);+ m_u32("vision.standardize", (uint32_t)stdz);+ /* the language model's attention over a picture's tokens: both ways+ (12B, 26B) or causal (E2B, E4B) */+ const char *bi =+ janas_json_str(janas_json_get(tc, "use_bidirectional_attention"));+ m_u32("vision.bidirectional", bi && !strcmp(bi, "vision"));+ m_str("vision.activation", "gelu_tanh");+ m_u32("vision.token.image", cfg_u32(cfg, "image_token_id", 1, 0));+ m_u32("vision.token.start", cfg_u32(cfg, "boi_token_id", 1, 0));+ m_u32("vision.token.end", cfg_u32(cfg, "eoi_token_id", 1, 0));+ *width = hidden;+ snprintf(what, what_len,+ "%u blocks of %u, %u heads, patch %u, pooling "+ "%u -> %u",+ depth, hidden, heads, patch, pool, out_dim);+ return "gemma4_vision";+}++/* Gemma 4 12B (gemma4_unified_vision, model.vision_embedder.*): no+ blocks, a 48x48 patch layer-normed, a dense layer, a norm, the positions+ and a norm; then the same projection */+static const char *gemma_unified(const struct janas_json *cfg,+ const struct janas_json *pre,+ const struct janas_json *vc, uint32_t *width,+ char *what, size_t what_len)+{+ const struct janas_json *tc = janas_json_get(cfg, "text_config");+ uint32_t patch = cfg_u32(vc, "patch_size", 1, 0);+ uint32_t pool = cfg_u32(vc, "pooling_kernel_size", 1, 0);+ uint32_t side = cfg_u32(vc, "model_patch_size", 0, patch * pool);+ uint32_t dim = cfg_u32(vc, "mm_embed_dim", 1, 0);+ uint32_t npos = cfg_u32(vc, "mm_posemb_size", 1, 0);+ uint32_t out_dim = cfg_u32(tc, "hidden_size", 1, 0);+ uint32_t soft = cfg_u32(pre, "max_soft_tokens", 0,+ cfg_u32(vc, "num_soft_tokens", 1, 0));+ if (!patch || !pool || side != patch * pool || !dim || !npos || !out_dim ||+ !soft)+ die("vision sizes out of range", NULL);+ double eps = janas_json_num(janas_json_get(vc, "rms_norm_eps"), 1e-6);+ const char *pf = "model.vision_embedder";+ char s[160], d[64];+ static const char *const LN[][2] = {{"patch_ln1", "v.u.ln1"},+ {"patch_ln2", "v.u.ln2"},+ {"pos_norm", "v.u.ln3"}};+ for (int i = 0; i < 3; i++)+ for (int b = 0; b < 2; b++) {+ snprintf(s, sizeof s, "%s.%s.%s", pf, LN[i][0],+ b ? "bias" : "weight");+ snprintf(d, sizeof d, "%s.%s", LN[i][1], b ? "bias" : "weight");+ add(s, d, 0);+ }+ add("model.vision_embedder.patch_dense.weight", "v.u.dense", 1);+ add("model.vision_embedder.patch_dense.bias", "v.u.dense.bias", 0);+ add("model.vision_embedder.pos_embedding", "v.pos_embd", 2);+ add("model.embed_vision.embedding_projection.weight", "v.proj", 1);+ const struct janas_jw_tensor *dn = &W.t[6], *pe = &W.t[8], *pj = &W.t[9];+ if (dn->dims[1] != dim || dn->dims[0] != 3ull * side * side ||+ pe->dims[1] != 2ull * npos || pe->dims[0] != dim ||+ pj->dims[1] != out_dim || pj->dims[0] != dim)+ die("the dense layer, the positions or the projection are not of "+ "the sizes the config says",+ NULL);+ m_str("general.architecture", "gemma4_vision");+ m_str("vision.model_type", "gemma4_unified");+ m_u32("vision.unified", 1);+ m_u32("vision.embedding_length", dim);+ m_u32("vision.patch_size", patch);+ m_u32("vision.pooling_kernel_size", pool);+ m_u32("vision.position_size", npos);+ m_u32("vision.max_soft_tokens", soft);+ m_u32("vision.projection_dim", out_dim);+ m_f32("vision.attention.layer_norm_epsilon", (float)eps);+ /* the embedder's norms are torch's LayerNorm, of its default epsilon */+ m_f32("vision.patch_norm_epsilon", 1e-5f);+ const char *bi =+ janas_json_str(janas_json_get(tc, "use_bidirectional_attention"));+ m_u32("vision.bidirectional", bi && !strcmp(bi, "vision"));+ m_u32("vision.token.image", cfg_u32(cfg, "image_token_id", 1, 0));+ m_u32("vision.token.start", cfg_u32(cfg, "boi_token_id", 1, 0));+ m_u32("vision.token.end", cfg_u32(cfg, "eoi_token_id", 1, 0));+ *width = dim;+ snprintf(what, what_len,+ "no blocks, patches of %u, %u wide, positions %u -> %u", side, dim,+ npos, out_dim);+ return "gemma4_vision";+}++int main(int argc, char **argv)+{+ if (argc != 3) {+ fprintf(stderr, "usage: hf2jns_vision <checkpoint directory> "+ "<out.vision.jns>\n");+ return 2;+ }+ const char *dir = argv[1], *out_path = argv[2];+ char p[4096];+ snprintf(p, sizeof p, "%s/config.json", dir);+ struct janas_json_doc *cd = read_json_file(p);+ const struct janas_json *cfg = janas_json_root(cd);+ const struct janas_json *vc = janas_json_get(cfg, "vision_config");+ const char *mt = janas_json_str(janas_json_get(cfg, "model_type"));+ int is_gemma =+ mt && (!strcmp(mt, "gemma4") || !strcmp(mt, "gemma4_unified"));+ if (!vc || !mt ||+ (!is_gemma && strcmp(mt, "qwen3_5") && strcmp(mt, "qwen3_vl") &&+ strcmp(mt, "qwen3_5_moe") && strcmp(mt, "qwen3_vl_moe")))+ die("not a model whose vision this converts (Qwen3.5, Qwen3-VL, "+ "Gemma 4)",+ mt);+ /* the image processor: preprocessor_config.json, or the part of+ processor_config.json */+ snprintf(p, sizeof p, "%s/preprocessor_config.json", dir);+ if (access(p, R_OK) != 0)+ snprintf(p, sizeof p, "%s/processor_config.json", dir);+ struct janas_json_doc *pd = read_json_file(p);+ const struct janas_json *pre = janas_json_root(pd);+ if (janas_json_get(pre, "image_processor"))+ pre = janas_json_get(pre, "image_processor");+ open_shards(dir);+ uint32_t hidden = 0;+ char what[160];+ const char *vt = janas_json_str(janas_json_get(vc, "model_type"));+ int unified = is_gemma && vt && !strcmp(vt, "gemma4_unified_vision");+ const char *arch =+ unified ? gemma_unified(cfg, pre, vc, &hidden, what, sizeof what)+ : is_gemma ? gemma(cfg, pre, vc, &hidden, what, sizeof what)+ : qwen(cfg, pre, vc, mt, &hidden, what, sizeof what);+ uint64_t file_bytes = 0;+ char err[300];+ if (janas_jw_write(&W, out_path, arch, hidden, &file_bytes, err,+ sizeof err) != 0)+ die(err, NULL);+ printf("written %s: %d tensors, %.1f MB (%s)\n", out_path, W.n,+ file_bytes / 1e6, what);+ janas_jw_free(&W);+ for (int i = 0; i < n_shards; i++)+ janas_st_close(&shards[i]);+ janas_json_free(cd);+ janas_json_free(pd);+ return 0;+}diff --git a/tools/hf2jns_whisper.c b/tools/hf2jns_whisper.cnew file mode 100644index 0000000..5857b1b--- /dev/null+++ b/tools/hf2jns_whisper.c@@ -0,0 +1,537 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * hf2jns_whisper.c - a Whisper model (speech to text), from its Hugging+ * Face checkpoint (openai/whisper-*: safetensors, config.json,+ * generation_config.json, preprocessor_config.json, tokenizer.json) into a+ * JNS file of resident tensors: <name>.jns, read by src/llm/whisper.c.+ *+ * The encoder - two convolutions over the log-mel spectrogram, sinusoidal+ * positions, blocks of LayerNorm, attention and a GELU MLP - and the+ * decoder - token and learned position embeddings, blocks of causal+ * attention, attention over the encoder's output and an MLP, the token+ * embeddings again for the logits. The matrices are kept in half+ * precision, as OpenAI trained and released them (the checkpoints' float32+ * are their exact copies: a value that is not is rounded and counted), and+ * transposed - a row per input, its outputs contiguous - as the engine's+ * tiled products read them, the token embeddings too; the norms, biases+ * and positions in float. The metadata holds the sizes, the+ * spectrogram's mel filters (as the processor has them), the special+ * tokens, the languages, the tokens never to be sampled, and the+ * vocabulary with its merges for the engine's tokenizer.+ *+ * Usage: hf2jns_whisper <checkpoint directory> <out.jns>+ */+#define _GNU_SOURCE+#include <dirent.h>+#include <stdint.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "common/hf_safetensors.h"+#include "llm/jns_write.h"+#include "llm/json.h"+#include "llm/quant.h"++#define GGML_F32 0+#define GGML_F16 1++static _Noreturn void die(const char *msg, const char *arg)+{+ fprintf(stderr, "hf2jns_whisper: %s%s%s\n", msg, arg ? ": " : "",+ arg ? arg : "");+ exit(1);+}++/* ---- the checkpoint ---- */++static struct janas_st shards[64];+static int n_shards;++static void open_shards(const char *dir)+{+ DIR *d = opendir(dir);+ if (!d)+ die("cannot open", dir);+ struct dirent *e;+ char p[4096], err[300];+ while ((e = readdir(d))) {+ size_t n = strlen(e->d_name);+ if (n < 12 || strcmp(e->d_name + n - 12, ".safetensors"))+ continue;+ if (n_shards == 64)+ die("more than 64 shards in", dir);+ snprintf(p, sizeof p, "%s/%s", dir, e->d_name);+ if (janas_st_open(&shards[n_shards], p, err, sizeof err) != 0)+ die(err, NULL);+ n_shards++;+ }+ closedir(d);+ if (!n_shards)+ die("no safetensors in", dir);+}++static const struct janas_st *find(const char *name, struct janas_st_tensor *t)+{+ for (int i = 0; i < n_shards; i++)+ if (janas_st_find(&shards[i], name, t) == 0)+ return &shards[i];+ return NULL;+}++static struct janas_json_doc *read_json_file(const char *dir, const char *name)+{+ char path[4096];+ snprintf(path, sizeof path, "%s/%s", dir, name);+ FILE *f = fopen(path, "rb");+ if (!f)+ die("cannot read", path);+ fseek(f, 0, SEEK_END);+ long n = ftell(f);+ fseek(f, 0, SEEK_SET);+ char *t = malloc((size_t)n + 1);+ if (!t || fread(t, 1, (size_t)n, f) != (size_t)n)+ die("cannot read", path);+ fclose(f);+ char err[200];+ struct janas_json_doc *d = janas_json_parse(t, (size_t)n, err, sizeof err);+ free(t);+ if (!d)+ die(err, path);+ return d;+}++/* ---- the output ---- */++static struct janas_jw W;+static uint64_t inexact; /* values half precision does not hold exactly */++static void put(const char *dst, uint32_t type, uint32_t n_dims,+ const uint64_t *dims, void *data, uint64_t bytes)+{+ if (!data)+ die("cannot read or out of memory", dst);+ if (janas_jw_tensor(&W, dst, type, n_dims, dims, data, bytes) != 0)+ die("out of memory, or a name too long", dst);+}++/* a tensor of the checkpoint: a matrix (rows the outermost dimension, the+ rest flattened into the row) in half precision, transposed, anything+ else in float; its elements must be count (0: any) */+static void add(const char *src, const char *dst, int matrix, uint64_t count)+{+ struct janas_st_tensor t;+ const struct janas_st *s = find(src, &t);+ if (!s)+ die("tensor missing or not F32/F16/BF16", src);+ if (count && t.count != count)+ die("tensor of another size", src);+ float *x = janas_st_floats(s, &t);+ if (!x)+ die("cannot read", src);+ if (!matrix) {+ uint64_t dims[1] = {t.count};+ put(dst, GGML_F32, 1, dims, x, 4 * t.count);+ return;+ }+ if (t.dims < 2)+ die("not a matrix", src);+ uint64_t rows = t.shape[0], cols = t.count / rows;+ uint16_t *h = malloc(2 * t.count);+ if (!h)+ die("out of memory", NULL);+ for (uint64_t r = 0; r < rows; r++)+ for (uint64_t c = 0; c < cols; c++) {+ float f = x[r * cols + c];+ uint16_t v = janas_fp32_to_fp16(f);+ if (janas_fp16_to_fp32(v) != f)+ inexact++;+ h[c * rows + r] = v;+ }+ free(x);+ /* ggml order: the length of a stored row (the outputs) first */+ uint64_t dims[2] = {rows, cols};+ put(dst, GGML_F16, 2, dims, h, 2 * t.count);+}++static uint32_t cfg_u32(const struct janas_json *o, const char *k)+{+ const struct janas_json *v = janas_json_get(o, k);+ if (!v || v->type != JANAS_JSON_NUMBER)+ die("missing", k);+ return (uint32_t)janas_json_num(v, 0);+}++/* the numbers of a JSON array as int32 (malloc'd), their count in *n */+static int32_t *int_array(const struct janas_json *a, uint64_t *n)+{+ *n = 0;+ if (!a || a->type != JANAS_JSON_ARRAY)+ return NULL;+ int32_t *v = malloc(sizeof(int32_t) * (a->n ? a->n : 1));+ if (!v)+ die("out of memory", NULL);+ for (const struct janas_json *e = a->child; e; e = e->next)+ if (e->type == JANAS_JSON_NUMBER)+ v[(*n)++] = (int32_t)janas_json_num(e, 0);+ return v;+}++/* ---- the vocabulary ---- */++static char **tok; /* by id */+static uint64_t *tok_n; /* their lengths */+static int32_t *tok_type;+static uint32_t n_vocab;++static void set_token(uint32_t id, const char *s, size_t n, int32_t type)+{+ if (id >= n_vocab)+ die("a token beyond the vocabulary's size", NULL);+ free(tok[id]);+ tok[id] = malloc(n + 1);+ if (!tok[id])+ die("out of memory", NULL);+ memcpy(tok[id], s, n);+ tok[id][n] = 0;+ tok_n[id] = n;+ tok_type[id] = type;+}++static int32_t token_id(const char *s)+{+ for (uint32_t i = 0; i < n_vocab; i++)+ if (tok[i] && tok_n[i] == strlen(s) && !memcmp(tok[i], s, tok_n[i]))+ return (int32_t)i;+ return -1;+}++static void vocabulary(const struct janas_json *tj)+{+ const struct janas_json *m = janas_json_get(tj, "model");+ const struct janas_json *v = janas_json_get(m, "vocab");+ const struct janas_json *mg = janas_json_get(m, "merges");+ if (!janas_json_is(janas_json_get(m, "type"), "BPE") || !v ||+ v->type != JANAS_JSON_OBJECT || !mg || mg->type != JANAS_JSON_ARRAY)+ die("tokenizer.json is not a BPE vocabulary", NULL);+ tok = calloc(n_vocab, sizeof *tok);+ tok_n = calloc(n_vocab, sizeof *tok_n);+ tok_type = calloc(n_vocab, sizeof *tok_type);+ if (!tok || !tok_n || !tok_type)+ die("out of memory", NULL);+ for (const struct janas_json *e = v->child; e; e = e->next)+ set_token((uint32_t)janas_json_num(e, -1), e->key, e->key_n, 1);+ const struct janas_json *add = janas_json_get(tj, "added_tokens");+ for (const struct janas_json *e = add ? add->child : NULL; e; e = e->next) {+ const struct janas_json *c = janas_json_get(e, "content");+ if (!c || c->type != JANAS_JSON_STRING)+ die("an added token without its text", NULL);+ /* the special tokens and the timestamps: never text */+ set_token((uint32_t)janas_json_num(janas_json_get(e, "id"), -1), c->s,+ c->n, 3);+ }+ for (uint32_t i = 0; i < n_vocab; i++)+ if (!tok[i])+ die("the vocabulary has a hole", NULL);+ janas_jw_str(&W, "tokenizer.ggml.model", "gpt2");+ janas_jw_str(&W, "tokenizer.ggml.pre", "whisper");+ janas_jw_strings(&W, "tokenizer.ggml.tokens", n_vocab,+ (const char *const *)tok, tok_n);+ janas_jw_ints(&W, "tokenizer.ggml.token_type", n_vocab, tok_type);+ /* merges: "a b", or [a, b] in the newer files */+ uint64_t nm = 0;+ char **ms = malloc(sizeof(char *) * (mg->n ? mg->n : 1));+ if (!ms)+ die("out of memory", NULL);+ for (const struct janas_json *e = mg->child; e; e = e->next) {+ if (e->type == JANAS_JSON_STRING) {+ ms[nm] = strndup(e->s, e->n);+ } else if (e->type == JANAS_JSON_ARRAY && e->n == 2 &&+ e->child->type == JANAS_JSON_STRING &&+ e->child->next->type == JANAS_JSON_STRING) {+ const struct janas_json *a = e->child, *b = a->next;+ ms[nm] = malloc(a->n + b->n + 2);+ if (ms[nm]) {+ memcpy(ms[nm], a->s, a->n);+ ms[nm][a->n] = ' ';+ memcpy(ms[nm] + a->n + 1, b->s, b->n);+ ms[nm][a->n + 1 + b->n] = 0;+ }+ } else {+ die("a merge of another form", NULL);+ }+ if (!ms[nm++])+ die("out of memory", NULL);+ }+ janas_jw_strings(&W, "tokenizer.ggml.merges", nm, (const char *const *)ms,+ NULL);+ for (uint64_t i = 0; i < nm; i++)+ free(ms[i]);+ free(ms);+}++static uint32_t need_token(const char *s)+{+ int32_t id = token_id(s);+ if (id < 0)+ die("the vocabulary lacks", s);+ return (uint32_t)id;+}++/* ---- the conversion ---- */++static void block(const char *src, const char *dst, uint32_t d, int cross)+{+ char s[160], o[64];+ static const char *const P[4] = {"q", "k", "v", "out"};+ static const char *const Q[4] = {"q", "k", "v", "o"};+ const char *att = cross ? "encoder_attn" : "self_attn";+ const char *x = cross ? "x" : "";+ for (int i = 0; i < 4; i++) {+ snprintf(s, sizeof s, "%s.%s.%s_proj.weight", src, att, P[i]);+ snprintf(o, sizeof o, "%s.%s%s", dst, x, Q[i]);+ add(s, o, 1, (uint64_t)d * d);+ if (i == 1)+ continue; /* the keys have no bias */+ snprintf(s, sizeof s, "%s.%s.%s_proj.bias", src, att, P[i]);+ snprintf(o, sizeof o, "%s.%s%s.b", dst, x, Q[i]);+ add(s, o, 0, d);+ }+ snprintf(s, sizeof s, "%s.%s_layer_norm.weight", src, att);+ snprintf(o, sizeof o, "%s.%sln.w", dst, x);+ add(s, o, 0, d);+ snprintf(s, sizeof s, "%s.%s_layer_norm.bias", src, att);+ snprintf(o, sizeof o, "%s.%sln.b", dst, x);+ add(s, o, 0, d);+}++static void mlp(const char *src, const char *dst, uint32_t d, uint32_t ffn)+{+ char s[160], o[64];+ snprintf(s, sizeof s, "%s.fc1.weight", src);+ snprintf(o, sizeof o, "%s.fc1", dst);+ add(s, o, 1, (uint64_t)d * ffn);+ snprintf(s, sizeof s, "%s.fc1.bias", src);+ snprintf(o, sizeof o, "%s.fc1.b", dst);+ add(s, o, 0, ffn);+ snprintf(s, sizeof s, "%s.fc2.weight", src);+ snprintf(o, sizeof o, "%s.fc2", dst);+ add(s, o, 1, (uint64_t)d * ffn);+ snprintf(s, sizeof s, "%s.fc2.bias", src);+ snprintf(o, sizeof o, "%s.fc2.b", dst);+ add(s, o, 0, d);+ snprintf(s, sizeof s, "%s.final_layer_norm.weight", src);+ snprintf(o, sizeof o, "%s.mln.w", dst);+ add(s, o, 0, d);+ snprintf(s, sizeof s, "%s.final_layer_norm.bias", src);+ snprintf(o, sizeof o, "%s.mln.b", dst);+ add(s, o, 0, d);+}++int main(int argc, char **argv)+{+ if (argc != 3) {+ fprintf(stderr, "usage: hf2jns_whisper <checkpoint directory> "+ "<out.jns>\n");+ return 2;+ }+ const char *dir = argv[1], *out_path = argv[2];+ struct janas_json_doc *cd = read_json_file(dir, "config.json");+ struct janas_json_doc *gd = read_json_file(dir, "generation_config.json");+ struct janas_json_doc *pd = read_json_file(dir, "preprocessor_config.json");+ struct janas_json_doc *td = read_json_file(dir, "tokenizer.json");+ const struct janas_json *cfg = janas_json_root(cd);+ const struct janas_json *gen = janas_json_root(gd);+ const struct janas_json *pre = janas_json_root(pd);+ if (!janas_json_is(janas_json_get(cfg, "model_type"), "whisper"))+ die("not a Whisper model (config.json's model_type)", NULL);+ uint32_t d = cfg_u32(cfg, "d_model");+ uint32_t el = cfg_u32(cfg, "encoder_layers");+ uint32_t eh = cfg_u32(cfg, "encoder_attention_heads");+ uint32_t ef = cfg_u32(cfg, "encoder_ffn_dim");+ uint32_t dl = cfg_u32(cfg, "decoder_layers");+ uint32_t dh = cfg_u32(cfg, "decoder_attention_heads");+ uint32_t df = cfg_u32(cfg, "decoder_ffn_dim");+ uint32_t mels = cfg_u32(cfg, "num_mel_bins");+ uint32_t actx = cfg_u32(cfg, "max_source_positions");+ uint32_t tctx = cfg_u32(cfg, "max_target_positions");+ n_vocab = cfg_u32(cfg, "vocab_size");+ uint32_t n_fft = cfg_u32(pre, "n_fft"), hop = cfg_u32(pre, "hop_length");+ uint32_t rate = cfg_u32(pre, "sampling_rate");+ uint32_t chunk = cfg_u32(pre, "chunk_length");+ if (!d || !eh || !dh || d % eh || d % dh || !el || !dl || !mels ||+ cfg_u32(pre, "feature_size") != mels || n_fft < 2 || !hop || !rate ||+ !chunk || actx * 2 * hop != chunk * rate)+ die("sizes out of range", NULL);+ open_shards(dir);++ janas_jw_u32(&W, "whisper.dim", d);+ janas_jw_u32(&W, "whisper.audio.layers", el);+ janas_jw_u32(&W, "whisper.audio.heads", eh);+ janas_jw_u32(&W, "whisper.audio.ffn", ef);+ janas_jw_u32(&W, "whisper.audio.ctx", actx);+ janas_jw_u32(&W, "whisper.text.layers", dl);+ janas_jw_u32(&W, "whisper.text.heads", dh);+ janas_jw_u32(&W, "whisper.text.ffn", df);+ janas_jw_u32(&W, "whisper.text.ctx", tctx);+ janas_jw_u32(&W, "whisper.vocab", n_vocab);+ janas_jw_u32(&W, "whisper.n_mels", mels);+ janas_jw_u32(&W, "whisper.n_fft", n_fft);+ janas_jw_u32(&W, "whisper.hop", hop);+ janas_jw_u32(&W, "whisper.sample_rate", rate);+ janas_jw_u32(&W, "whisper.chunk_seconds", chunk);++ /* the mel filters, n_mels rows of n_fft / 2 + 1 */+ const struct janas_json *mf = janas_json_get(pre, "mel_filters");+ uint32_t bins = n_fft / 2 + 1;+ if (!mf || mf->type != JANAS_JSON_ARRAY || mf->n != mels)+ die("preprocessor_config.json lacks the mel filters", NULL);+ float *filt = malloc(sizeof(float) * mels * bins);+ if (!filt)+ die("out of memory", NULL);+ uint32_t m = 0;+ for (const struct janas_json *r = mf->child; r; r = r->next, m++) {+ if (r->type != JANAS_JSON_ARRAY || r->n != bins)+ die("a mel filter of another length", NULL);+ uint32_t k = 0;+ for (const struct janas_json *e = r->child; e; e = e->next)+ filt[m * bins + k++] = (float)janas_json_num(e, 0);+ }+ uint64_t fd[2] = {bins, mels};+ put("mel_filters", GGML_F32, 2, fd, filt, 4ull * mels * bins);++ vocabulary(janas_json_root(td));+ struct {+ const char *key, *tok, *alt;+ } specials[] = {+ {"whisper.token.eot", "<|endoftext|>", NULL},+ {"whisper.token.sot", "<|startoftranscript|>", NULL},+ {"whisper.token.translate", "<|translate|>", NULL},+ {"whisper.token.transcribe", "<|transcribe|>", NULL},+ {"whisper.token.sot_lm", "<|startoflm|>", NULL},+ {"whisper.token.sot_prev", "<|startofprev|>", NULL},+ {"whisper.token.no_speech", "<|nospeech|>", "<|nocaptions|>"},+ {"whisper.token.no_timestamps", "<|notimestamps|>", NULL},+ {"whisper.token.timestamp_begin", "<|0.00|>", NULL},+ };+ for (size_t i = 0; i < sizeof specials / sizeof *specials; i++) {+ int32_t id = token_id(specials[i].tok);+ if (id < 0 && specials[i].alt)+ id = token_id(specials[i].alt);+ if (id < 0)+ die("the vocabulary lacks", specials[i].tok);+ janas_jw_u32(&W, specials[i].key, (uint32_t)id);+ }+ /* the languages, in the order of their tokens: the first follows the+ start of transcript */+ uint32_t sot = need_token("<|startoftranscript|>");+ const char *langs[200];+ uint32_t nl = 0;+ for (uint32_t id = sot + 1; id < n_vocab && nl < 200; id++) {+ const char *t = tok[id];+ size_t n = tok_n[id];+ if (n < 5 || n > 12 || strncmp(t, "<|", 2) || strcmp(t + n - 2, "|>"))+ break;+ if (!strcmp(t, "<|translate|>"))+ break;+ static char codes[200][12];+ memcpy(codes[nl], t + 2, n - 4);+ codes[nl][n - 4] = 0;+ langs[nl] = codes[nl];+ nl++;+ }+ if (!nl)+ die("no languages after <|startoftranscript|>", NULL);+ janas_jw_strings(&W, "whisper.languages", nl, langs, NULL);+ janas_jw_u32(&W, "whisper.multilingual",+ janas_json_get(gen, "is_multilingual") &&+ janas_json_get(gen, "is_multilingual")->type ==+ JANAS_JSON_TRUE);+ uint64_t n;+ int32_t *v = int_array(janas_json_get(gen, "suppress_tokens"), &n);+ if (!v)+ v = int_array(janas_json_get(cfg, "suppress_tokens"), &n);+ if (!v)+ die("no suppress_tokens", NULL);+ janas_jw_ints(&W, "whisper.suppress", n, v);+ free(v);+ v = int_array(janas_json_get(gen, "begin_suppress_tokens"), &n);+ if (v) {+ janas_jw_ints(&W, "whisper.begin_suppress", n, v);+ free(v);+ }+ const struct janas_json *mi =+ janas_json_get(gen, "max_initial_timestamp_index");+ janas_jw_u32(&W, "whisper.max_initial_timestamp",+ mi ? (uint32_t)janas_json_num(mi, 50) : 50);+ /* the heads that align words with time: pairs (layer, head) */+ const struct janas_json *ah = janas_json_get(gen, "alignment_heads");+ if (ah && ah->type == JANAS_JSON_ARRAY) {+ int32_t *p = malloc(sizeof(int32_t) * 2 * (ah->n ? ah->n : 1));+ uint64_t k = 0;+ if (!p)+ die("out of memory", NULL);+ for (const struct janas_json *e = ah->child; e; e = e->next)+ if (e->type == JANAS_JSON_ARRAY && e->n == 2) {+ p[k++] = (int32_t)janas_json_num(e->child, 0);+ p[k++] = (int32_t)janas_json_num(e->child->next, 0);+ }+ janas_jw_ints(&W, "whisper.alignment_heads", k, p);+ free(p);+ }++ /* the encoder */+ add("model.encoder.conv1.weight", "e.conv1", 1, (uint64_t)d * mels * 3);+ add("model.encoder.conv1.bias", "e.conv1.b", 0, d);+ add("model.encoder.conv2.weight", "e.conv2", 1, (uint64_t)d * d * 3);+ add("model.encoder.conv2.bias", "e.conv2.b", 0, d);+ add("model.encoder.embed_positions.weight", "e.pos", 0, (uint64_t)actx * d);+ for (uint32_t l = 0; l < el; l++) {+ char s[96], o[32];+ snprintf(s, sizeof s, "model.encoder.layers.%u", l);+ snprintf(o, sizeof o, "e.%u", l);+ block(s, o, d, 0);+ mlp(s, o, d, ef);+ }+ add("model.encoder.layer_norm.weight", "e.ln.w", 0, d);+ add("model.encoder.layer_norm.bias", "e.ln.b", 0, d);+ /* the decoder */+ add("model.decoder.embed_tokens.weight", "d.tok", 1, (uint64_t)n_vocab * d);+ add("model.decoder.embed_positions.weight", "d.pos", 0, (uint64_t)tctx * d);+ for (uint32_t l = 0; l < dl; l++) {+ char s[96], o[32];+ snprintf(s, sizeof s, "model.decoder.layers.%u", l);+ snprintf(o, sizeof o, "d.%u", l);+ block(s, o, d, 0);+ block(s, o, d, 1);+ mlp(s, o, d, df);+ }+ add("model.decoder.layer_norm.weight", "d.ln.w", 0, d);+ add("model.decoder.layer_norm.bias", "d.ln.b", 0, d);++ uint64_t file_bytes = 0;+ char err[300];+ if (janas_jw_write(&W, out_path, "whisper", d, &file_bytes, err,+ sizeof err) != 0)+ die(err, NULL);+ printf("written %s: %d tensors, %.1f MB (encoder %u x %u, decoder %u x "+ "%u, %u mels, %u languages, %u tokens)\n",+ out_path, W.n, file_bytes / 1e6, el, d, dl, d, mels, nl, n_vocab);+ if (inexact)+ printf("%llu weights rounded to half precision\n",+ (unsigned long long)inexact);+ janas_jw_free(&W);+ for (uint32_t i = 0; i < n_vocab; i++)+ free(tok[i]);+ free(tok);+ free(tok_n);+ free(tok_type);+ for (int i = 0; i < n_shards; i++)+ janas_st_close(&shards[i]);+ janas_json_free(cd);+ janas_json_free(gd);+ janas_json_free(pd);+ janas_json_free(td);+ return 0;+}diff --git a/tools/onnx_pb.h b/tools/onnx_pb.hnew file mode 100644index 0000000..8723953--- /dev/null+++ b/tools/onnx_pb.h@@ -0,0 +1,73 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * onnx_pb.h - the protobuf wire format, as the converters of ONNX models+ * (silero2jns, wespeaker2jns) read it: fields one after another, their+ * numbers, wire types and bytes.+ */+#ifndef JANAS_TOOLS_ONNX_PB_H+#define JANAS_TOOLS_ONNX_PB_H++#include <stdint.h>+#include <string.h>++struct pb {+ const uint8_t *p, *end;+};++static inline int varint(struct pb *b, uint64_t *v)+{+ *v = 0;+ for (int s = 0; s < 64 && b->p < b->end; s += 7) {+ uint8_t c = *b->p++;+ *v |= (uint64_t)(c & 0x7F) << s;+ if (!(c & 0x80))+ return 0;+ }+ return -1;+}++/* the next field: its number, wire type, and for length-delimited ones+ its bytes in sub; 0 at the end, -1 broken */+static inline int field(struct pb *b, int *num, int *wt, uint64_t *v,+ struct pb *sub)+{+ if (b->p >= b->end)+ return 0;+ uint64_t key;+ if (varint(b, &key) != 0)+ return -1;+ *num = (int)(key >> 3);+ *wt = (int)(key & 7);+ switch (*wt) {+ case 0:+ return varint(b, v) == 0 ? 1 : -1;+ case 1:+ if (b->end - b->p < 8)+ return -1;+ b->p += 8;+ return 1;+ case 5:+ if (b->end - b->p < 4)+ return -1;+ b->p += 4;+ return 1;+ case 2:+ if (varint(b, v) != 0 || *v > (uint64_t)(b->end - b->p))+ return -1;+ sub->p = b->p;+ sub->end = b->p + *v;+ b->p += *v;+ return 1;+ default:+ return -1;+ }+}++static inline int str_eq(const struct pb *s, const char *c)+{+ size_t n = strlen(c);+ return (size_t)(s->end - s->p) == n && !memcmp(s->p, c, n);+}++#endifdiff --git a/tools/openapi_routes.c b/tools/openapi_routes.cindex f54b13e..2894252 100644--- a/tools/openapi_routes.c+++ b/tools/openapi_routes.c@@ -46,7 +46,82 @@ static const char *const HANDLERS[][2] = {{"getConversationItem", "srv_conv_item_get"},{"deleteConversationItem", "srv_conv_item_delete"},{"createEmbedding", "srv_embeddings_create"},+ {"createTranscription", "srv_audio_transcriptions"},+ {"createTranslation", "srv_audio_translations"},{"createModeration", "srv_moderations_create"},+ {"createFile", "srv_files_create"},+ {"listFiles", "srv_files_list_req"},+ {"retrieveFile", "srv_files_retrieve"},+ {"deleteFile", "srv_files_delete_req"},+ {"downloadFile", "srv_files_content"},+ {"createUpload", "srv_uploads_create"},+ {"addUploadPart", "srv_uploads_part"},+ {"completeUpload", "srv_uploads_complete"},+ {"cancelUpload", "srv_uploads_cancel"},+ {"createBatch", "srv_batches_create"},+ {"retrieveBatch", "srv_batches_get"},+ {"cancelBatch", "srv_batches_cancel"},+ {"listBatches", "srv_batches_list"},+ {"createVectorStore", "srv_vs_create"},+ {"listVectorStores", "srv_vs_list"},+ {"getVectorStore", "srv_vs_get"},+ {"modifyVectorStore", "srv_vs_modify"},+ {"deleteVectorStore", "srv_vs_delete"},+ {"createVectorStoreFile", "srv_vs_file_create"},+ {"listVectorStoreFiles", "srv_vs_files_list"},+ {"getVectorStoreFile", "srv_vs_file_get"},+ {"deleteVectorStoreFile", "srv_vs_file_delete"},+ {"updateVectorStoreFileAttributes", "srv_vs_file_update"},+ {"retrieveVectorStoreFileContent", "srv_vs_file_content"},+ {"searchVectorStore", "srv_vs_search"},+ {"createVectorStoreFileBatch", "srv_vs_batch_create"},+ {"getVectorStoreFileBatch", "srv_vs_batch_get"},+ {"cancelVectorStoreFileBatch", "srv_vs_batch_cancel"},+ {"listFilesInVectorStoreBatch", "srv_vs_batch_files"},+ {"listEvals", "srv_evals_list"},+ {"createEval", "srv_evals_create"},+ {"getEval", "srv_evals_get"},+ {"updateEval", "srv_evals_update"},+ {"deleteEval", "srv_evals_delete"},+ {"getEvalRuns", "srv_eval_runs_list"},+ {"createEvalRun", "srv_eval_runs_create"},+ {"getEvalRun", "srv_eval_runs_get"},+ {"cancelEvalRun", "srv_eval_runs_cancel"},+ {"deleteEvalRun", "srv_eval_runs_delete"},+ {"getEvalRunOutputItems", "srv_eval_items_list"},+ {"getEvalRunOutputItem", "srv_eval_items_get"},+ {"listAssistants", "srv_assistants_list"},+ {"createAssistant", "srv_assistants_create"},+ {"getAssistant", "srv_assistants_get"},+ {"modifyAssistant", "srv_assistants_modify"},+ {"deleteAssistant", "srv_assistants_delete"},+ {"createThread", "srv_threads_create"},+ {"createThreadAndRun", "srv_runs_create_thread"},+ {"getThread", "srv_threads_get"},+ {"modifyThread", "srv_threads_modify"},+ {"deleteThread", "srv_threads_delete"},+ {"listMessages", "srv_messages_list"},+ {"createMessage", "srv_messages_create"},+ {"getMessage", "srv_messages_get"},+ {"modifyMessage", "srv_messages_modify"},+ {"deleteMessage", "srv_messages_delete"},+ {"listRuns", "srv_runs_list"},+ {"createRun", "srv_runs_create"},+ {"getRun", "srv_runs_get"},+ {"modifyRun", "srv_runs_modify"},+ {"cancelRun", "srv_runs_cancel"},+ {"listRunSteps", "srv_run_steps_list"},+ {"getRunStep", "srv_run_steps_get"},+ {"submitToolOuputsToRun", "srv_runs_submit"},+ {"ListContainers", "srv_containers_list"},+ {"CreateContainer", "srv_containers_create"},+ {"RetrieveContainer", "srv_containers_get"},+ {"DeleteContainer", "srv_containers_delete"},+ {"CreateContainerFile", "srv_container_files_create"},+ {"ListContainerFiles", "srv_container_files_list"},+ {"RetrieveContainerFile", "srv_container_files_get"},+ {"DeleteContainerFile", "srv_container_files_delete"},+ {"RetrieveContainerFileContent", "srv_container_files_content"},};/* What an operation that is not implemented is, by its tag: the text of thediff --git a/tools/silero2jns.c b/tools/silero2jns.cnew file mode 100644index 0000000..5f1a598--- /dev/null+++ b/tools/silero2jns.c@@ -0,0 +1,240 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * silero2jns.c - Silero VAD v5/v6's ONNX model (onnx-community/silero-vad,+ * MIT) into a JNS file: the weights of its 16 kHz branch in single+ * precision as the model holds them - the STFT's basis, the four+ * convolutions of the encoder, the LSTM's input and recurrent matrices and+ * biases (ONNX's gate order: input, output, forget, cell), the output+ * convolution. The ONNX file is read with a small protobuf reader of its+ * own: the graph of the "If" on the rate, its then branch, the LSTM node's+ * inputs naming its weights.+ *+ * silero2jns model.onnx out.jns+ */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "llm/jns_write.h"+#include "onnx_pb.h"++static void die(const char *msg, const char *arg)+{+ fprintf(stderr, "silero2jns: %s%s%s\n", msg, arg ? ": " : "",+ arg ? arg : "");+ exit(1);+}++/* ---- the tensors of a graph ---- */++struct tensor {+ char name[256];+ uint64_t dims[4];+ int n_dims;+ float *data;+ uint64_t count;+};++static struct tensor T[512];+static int n_t;++static void read_tensor(struct pb m)+{+ if (n_t == (int)(sizeof T / sizeof *T))+ die("too many tensors", NULL);+ struct tensor *t = &T[n_t];+ memset(t, 0, sizeof *t);+ int num, wt, type = 0;+ uint64_t v;+ struct pb s, raw = {0, 0}, fl = {0, 0};+ int r;+ while ((r = field(&m, &num, &wt, &v, &s)) == 1) {+ if (num == 1 && wt == 0 && t->n_dims < 4)+ t->dims[t->n_dims++] = v;+ else if (num == 1 && wt == 2)+ while (s.p < s.end && t->n_dims < 4 && varint(&s, &v) == 0)+ t->dims[t->n_dims++] = v;+ else if (num == 2 && wt == 0)+ type = (int)v;+ else if (num == 8 && wt == 2) {+ size_t k = (size_t)(s.end - s.p);+ if (k >= sizeof t->name)+ k = sizeof t->name - 1;+ memcpy(t->name, s.p, k);+ } else if (num == 9 && wt == 2)+ raw = s;+ else if (num == 4 && wt == 2)+ fl = s;+ }+ if (r < 0)+ die("a tensor that is not protobuf", NULL);+ if (type != 1) /* FLOAT only: the weights */+ return;+ uint64_t count = 1;+ for (int i = 0; i < t->n_dims; i++)+ count *= t->dims[i];+ struct pb src = raw.p ? raw : fl;+ if (!src.p || (uint64_t)(src.end - src.p) != 4 * count)+ return;+ t->data = malloc(4 * count);+ if (!t->data)+ die("out of memory", NULL);+ memcpy(t->data, src.p, 4 * count); /* little-endian, as here */+ t->count = count;+ n_t++;+}++static struct pb lstm_in[8];+static int n_lstm_in, found_lstm;++static void read_graph(struct pb g, int keep);++static void read_node(struct pb m, int keep)+{+ int num, wt;+ uint64_t v;+ struct pb s, ins[8];+ int n_ins = 0, is_lstm = 0;+ while (field(&m, &num, &wt, &v, &s) == 1) {+ if (num == 1 && wt == 2 && n_ins < 8)+ ins[n_ins++] = s;+ else if (num == 4 && wt == 2)+ is_lstm = str_eq(&s, "LSTM");+ else if (num == 5 && wt == 2) {+ /* an attribute: a then_branch holds the 16 kHz model */+ int an, aw;+ uint64_t av;+ struct pb as, name = {0, 0}, graph = {0, 0};+ while (field(&s, &an, &aw, &av, &as) == 1) {+ if (an == 1 && aw == 2)+ name = as;+ else if (an == 6 && aw == 2)+ graph = as;+ }+ if (graph.p)+ read_graph(graph,+ keep || (name.p && str_eq(&name, "then_branch")));+ }+ }+ if (is_lstm && keep && !found_lstm) {+ memcpy(lstm_in, ins, sizeof(struct pb) * (size_t)n_ins);+ n_lstm_in = n_ins;+ found_lstm = 1;+ }+}++static void read_graph(struct pb g, int keep)+{+ int num, wt;+ uint64_t v;+ struct pb s;+ while (field(&g, &num, &wt, &v, &s) == 1) {+ if (num == 1 && wt == 2)+ read_node(s, keep);+ else if (num == 5 && wt == 2 && keep)+ read_tensor(s);+ }+}++static struct tensor *find(const char *suffix, uint64_t count)+{+ size_t n = strlen(suffix);+ for (int i = 0; i < n_t; i++) {+ size_t k = strlen(T[i].name);+ if (k >= n && !strcmp(T[i].name + k - n, suffix) &&+ (!count || T[i].count == count))+ return &T[i];+ }+ return NULL;+}++static struct tensor *by_input(int i)+{+ if (i >= n_lstm_in)+ return NULL;+ for (int k = 0; k < n_t; k++)+ if (str_eq(&lstm_in[i], T[k].name))+ return &T[k];+ return NULL;+}++static struct janas_jw W;++static void put(const char *name, const struct tensor *t)+{+ if (!t)+ die("a tensor is missing", name);+ uint64_t dims[1] = {t->count};+ float *d = malloc(4 * t->count);+ if (!d)+ die("out of memory", NULL);+ memcpy(d, t->data, 4 * t->count);+ if (janas_jw_tensor(&W, name, 0, 1, dims, d, 4 * t->count) != 0)+ die("out of memory", NULL);+}++int main(int argc, char **argv)+{+ if (argc != 3) {+ fprintf(stderr, "usage: silero2jns model.onnx out.jns\n");+ return 2;+ }+ FILE *f = fopen(argv[1], "rb");+ if (!f)+ die("cannot open", argv[1]);+ fseek(f, 0, SEEK_END);+ long n = ftell(f);+ fseek(f, 0, SEEK_SET);+ uint8_t *buf = malloc(n > 0 ? (size_t)n : 1);+ if (!buf || fread(buf, 1, (size_t)n, f) != (size_t)n)+ die("cannot read", argv[1]);+ fclose(f);+ struct pb m = {buf, buf + n};+ int num, wt, r;+ uint64_t v;+ struct pb s;+ while ((r = field(&m, &num, &wt, &v, &s)) == 1)+ if (num == 7 && wt == 2) /* ModelProto.graph */+ read_graph(s, 0);+ if (r < 0)+ die("not an ONNX file", argv[1]);+ if (!found_lstm || n_lstm_in < 4)+ die("no LSTM in a then branch: not Silero VAD v5", argv[1]);+ /* the 16 kHz branch's weights by their names, the LSTM's by its node */+ put("vad.stft", find("stft.forward_basis_buffer", 258 * 256));+ put("vad.enc0.w", find("encoder.0.reparam_conv.weight", 128 * 129 * 3));+ put("vad.enc0.b", find("encoder.0.reparam_conv.bias", 128));+ put("vad.enc1.w", find("encoder.1.reparam_conv.weight", 64 * 128 * 3));+ put("vad.enc1.b", find("encoder.1.reparam_conv.bias", 64));+ put("vad.enc2.w", find("encoder.2.reparam_conv.weight", 64 * 64 * 3));+ put("vad.enc2.b", find("encoder.2.reparam_conv.bias", 64));+ put("vad.enc3.w", find("encoder.3.reparam_conv.weight", 128 * 64 * 3));+ put("vad.enc3.b", find("encoder.3.reparam_conv.bias", 128));+ struct tensor *lw = by_input(1), *lr = by_input(2), *lb = by_input(3);+ if (!lw || !lr || !lb || lw->count != 512 * 128 || lr->count != 512 * 128 ||+ lb->count != 1024)+ die("the LSTM's weights are not 512 x 128", NULL);+ put("vad.lstm.w", lw);+ put("vad.lstm.r", lr);+ put("vad.lstm.b", lb);+ put("vad.out.w", find("decoder.decoder.2.weight", 128));+ put("vad.out.b", find("decoder.decoder.2.bias", 1));+ janas_jw_u32(&W, "vad.window", 512);+ janas_jw_u32(&W, "vad.context", 64);+ janas_jw_u32(&W, "vad.rate", 16000);+ janas_jw_str(&W, "general.name", "Silero VAD");+ janas_jw_str(&W, "general.license", "MIT");+ char err[300];+ uint64_t bytes;+ if (janas_jw_write(&W, argv[2], "silero_vad", 128, &bytes, err,+ sizeof err) != 0)+ die(err, NULL);+ printf("silero2jns: %s, %d tensors, %llu bytes\n", argv[2], W.n,+ (unsigned long long)bytes);+ janas_jw_free(&W);+ for (int i = 0; i < n_t; i++)+ free(T[i].data);+ free(buf);+ return 0;+}diff --git a/tools/wespeaker2jns.c b/tools/wespeaker2jns.cnew file mode 100644index 0000000..72d10a9--- /dev/null+++ b/tools/wespeaker2jns.c@@ -0,0 +1,258 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * wespeaker2jns.c - WeSpeaker's ResNet34 speaker embedding model in ONNX+ * (Wespeaker/wespeaker-voxceleb-resnet34-LM, CC-BY-4.0: its batch norms+ * folded into the convolutions) into a JNS file: the 36 convolutions in+ * the graph's order, each weight in half precision laid out as the+ * engine's tiled products take it (a column per patch element: row, column+ * of the kernel, input channel; a row per output channel), its bias, its+ * kernel and stride; the pooled statistics' projection to 256 and the+ * embeddings' mean subtracted at the end.+ *+ * wespeaker2jns voxceleb_resnet34_LM.onnx out.jns+ */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "llm/quant.h"+#include "llm/jns_write.h"+#include "onnx_pb.h"++static void die(const char *msg, const char *arg)+{+ fprintf(stderr, "wespeaker2jns: %s%s%s\n", msg, arg ? ": " : "",+ arg ? arg : "");+ exit(1);+}++struct tensor {+ char name[128];+ uint64_t dims[4];+ int n_dims;+ const float *data;+ uint64_t count;+};++static struct tensor T[256];+static int n_t;++static void read_tensor(struct pb m)+{+ if (n_t == (int)(sizeof T / sizeof *T))+ die("too many tensors", NULL);+ struct tensor *t = &T[n_t];+ memset(t, 0, sizeof *t);+ int num, wt, type = 0, r;+ uint64_t v;+ struct pb s, raw = {0, 0};+ while ((r = field(&m, &num, &wt, &v, &s)) == 1) {+ if (num == 1 && wt == 0 && t->n_dims < 4)+ t->dims[t->n_dims++] = v;+ else if (num == 1 && wt == 2)+ while (s.p < s.end && t->n_dims < 4 && varint(&s, &v) == 0)+ t->dims[t->n_dims++] = v;+ else if (num == 2 && wt == 0)+ type = (int)v;+ else if (num == 8 && wt == 2) {+ size_t k = (size_t)(s.end - s.p);+ if (k >= sizeof t->name)+ k = sizeof t->name - 1;+ memcpy(t->name, s.p, k);+ } else if (num == 9 && wt == 2)+ raw = s;+ }+ if (r < 0)+ die("a tensor that is not protobuf", NULL);+ uint64_t count = 1;+ for (int i = 0; i < t->n_dims; i++)+ count *= t->dims[i];+ if (type != 1 || !raw.p || (uint64_t)(raw.end - raw.p) != 4 * count)+ return;+ t->data = (const float *)raw.p; /* little-endian floats, as here */+ t->count = count;+ n_t++;+}++static const struct tensor *find(const struct pb *name)+{+ for (int i = 0; i < n_t; i++)+ if (str_eq(name, T[i].name))+ return &T[i];+ return NULL;+}++static const struct tensor *find_name(const char *name)+{+ for (int i = 0; i < n_t; i++)+ if (!strcmp(name, T[i].name))+ return &T[i];+ return NULL;+}++struct conv {+ struct pb w, b;+ int kernel, stride, pad;+};++static struct conv C[64];+static int n_c;+static struct pb gemm_w, gemm_b;++static void read_node(struct pb m)+{+ int num, wt;+ uint64_t v;+ struct pb s, ins[4], op = {0, 0};+ int n_ins = 0, kernel = 0, stride = 1, pad = 0;+ while (field(&m, &num, &wt, &v, &s) == 1) {+ if (num == 1 && wt == 2 && n_ins < 4)+ ins[n_ins++] = s;+ else if (num == 4 && wt == 2)+ op = s;+ else if (num == 5 && wt == 2) {+ int an, aw;+ uint64_t av;+ struct pb as, name = {0, 0}, ints = {0, 0};+ int64_t first = -1;+ while (field(&s, &an, &aw, &av, &as) == 1) {+ if (an == 1 && aw == 2)+ name = as;+ else if (an == 8 && aw == 0 && first < 0)+ first = (int64_t)av;+ else if (an == 8 && aw == 2)+ ints = as;+ }+ if (ints.p && first < 0 && varint(&ints, &av) == 0)+ first = (int64_t)av;+ if (name.p && str_eq(&name, "kernel_shape"))+ kernel = (int)first;+ else if (name.p && str_eq(&name, "strides"))+ stride = (int)first;+ else if (name.p && str_eq(&name, "pads"))+ pad = (int)first;+ }+ }+ if (op.p && str_eq(&op, "Conv") && n_ins == 3) {+ if (n_c == (int)(sizeof C / sizeof *C))+ die("too many convolutions", NULL);+ C[n_c++] = (struct conv){ins[1], ins[2], kernel, stride, pad};+ } else if (op.p && str_eq(&op, "Gemm") && n_ins == 3) {+ gemm_w = ins[1];+ gemm_b = ins[2];+ }+}++static struct janas_jw W;++static void put_f32(const char *name, const float *d, uint64_t n)+{+ float *c = malloc(4 * n);+ if (!c)+ die("out of memory", NULL);+ memcpy(c, d, 4 * n);+ uint64_t dims[1] = {n};+ if (janas_jw_tensor(&W, name, 0, 1, dims, c, 4 * n) != 0)+ die("out of memory", NULL);+}++/* rows x cols in half precision, transposed: wt[col][row] */+static void put_f16t(const char *name, const float *w, uint64_t rows,+ uint64_t cols, int kernel, uint64_t cin)+{+ uint16_t *h = malloc(2 * rows * cols);+ if (!h)+ die("out of memory", NULL);+ for (uint64_t o = 0; o < rows; o++)+ for (uint64_t ci = 0; ci < cin; ci++)+ for (int ky = 0; ky < kernel; ky++)+ for (int kx = 0; kx < kernel; kx++) {+ /* ONNX: [out][in][ky][kx]; ours: column (ky, kx, in) */+ uint64_t col = ((uint64_t)ky * kernel + kx) * cin + ci;+ h[col * rows + o] = janas_fp32_to_fp16(+ w[((o * cin + ci) * kernel + ky) * kernel + kx]);+ }+ uint64_t dims[2] = {rows, cols};+ if (janas_jw_tensor(&W, name, 1, 2, dims, h, 2 * rows * cols) != 0)+ die("out of memory", NULL);+}++int main(int argc, char **argv)+{+ if (argc != 3) {+ fprintf(stderr, "usage: wespeaker2jns model.onnx out.jns\n");+ return 2;+ }+ FILE *f = fopen(argv[1], "rb");+ if (!f)+ die("cannot open", argv[1]);+ fseek(f, 0, SEEK_END);+ long n = ftell(f);+ fseek(f, 0, SEEK_SET);+ uint8_t *buf = malloc(n > 0 ? (size_t)n : 1);+ if (!buf || fread(buf, 1, (size_t)n, f) != (size_t)n)+ die("cannot read", argv[1]);+ fclose(f);+ struct pb m = {buf, buf + n}, s, g = {0, 0};+ int num, wt, r;+ uint64_t v;+ while ((r = field(&m, &num, &wt, &v, &s)) == 1)+ if (num == 7 && wt == 2)+ g = s;+ if (r < 0 || !g.p)+ die("not an ONNX file", argv[1]);+ /* initializers first: the nodes name them */+ struct pb gg = g;+ while (field(&gg, &num, &wt, &v, &s) == 1)+ if (num == 5 && wt == 2)+ read_tensor(s);+ gg = g;+ while (field(&gg, &num, &wt, &v, &s) == 1)+ if (num == 1 && wt == 2)+ read_node(s);+ if (n_c != 36 || !gemm_w.p)+ die("not WeSpeaker's ResNet34 (36 convolutions and a projection)",+ argv[1]);+ int32_t kernels[36], strides[36];+ for (int i = 0; i < n_c; i++) {+ const struct tensor *w = find(&C[i].w), *b = find(&C[i].b);+ if (!w || !b || w->n_dims != 4 || w->dims[2] != (uint64_t)C[i].kernel ||+ w->dims[3] != (uint64_t)C[i].kernel ||+ C[i].pad != (C[i].kernel - 1) / 2)+ die("a convolution's weights are not as expected", NULL);+ uint64_t cout = w->dims[0], cin = w->dims[1];+ char name[32];+ snprintf(name, sizeof name, "spk.conv%d.w", i);+ put_f16t(name, w->data, cout, cin * C[i].kernel * C[i].kernel,+ C[i].kernel, cin);+ snprintf(name, sizeof name, "spk.conv%d.b", i);+ put_f32(name, b->data, cout);+ kernels[i] = C[i].kernel;+ strides[i] = C[i].stride;+ }+ const struct tensor *pw = find(&gemm_w), *pb = find(&gemm_b);+ const struct tensor *mean = find_name("mean_vec");+ if (!pw || !pb || pw->n_dims != 2 || pw->dims[0] != 256 ||+ pw->dims[1] != 5120 || pb->count != 256 || !mean || mean->count != 256)+ die("the projection is not 5120 to 256", NULL);+ put_f16t("spk.proj.w", pw->data, 256, 5120, 1, 5120);+ put_f32("spk.proj.b", pb->data, 256);+ put_f32("spk.mean", mean->data, 256);+ janas_jw_ints(&W, "spk.kernels", 36, kernels);+ janas_jw_ints(&W, "spk.strides", 36, strides);+ janas_jw_u32(&W, "spk.mels", 80);+ janas_jw_u32(&W, "spk.dim", 256);+ janas_jw_str(&W, "general.name", "WeSpeaker ResNet34-LM (VoxCeleb2)");+ janas_jw_str(&W, "general.license", "CC-BY-4.0");+ char err[300];+ uint64_t bytes;+ if (janas_jw_write(&W, argv[2], "wespeaker_resnet34", 256, &bytes, err,+ sizeof err) != 0)+ die(err, NULL);+ printf("wespeaker2jns: %s, %d tensors, %llu bytes\n", argv[2], W.n,+ (unsigned long long)bytes);+ janas_jw_free(&W);+ free(buf);+ return 0;+}